Code analysis
Plan for today:
- Follow up on last homework exercise
- Static analysis tools and code review
- Performance measurement and profiling
Learning outcomes
After this module you should be able to:
- Measure performance in your code, and in particular do so in multitier architectures.
- Design a code-review strategy
- Be able to evaluate code quality on at least 10 differnt parameters
Relevance
It goes without saying that the quality of your code is an important aspect of your professional work. Besides fulfilling the functional requirements, you should be able to write code that is up to professional standards.
Resources
Static tools
Performance testing
Example code
Sample code full of bad style.
Exercises
Short exercises
Performance testing
We are going to find the bottleneck in the mergesort algorithms in this small suite of implementation attemps by Kasper.
- Run the program a few times to see if you can get consistent results
- Try to close all programs, and run it from a console - do you get more consistent results?
- Comparing with the person next to you - who has the most consistent results?
Looking for the bottleneck…
- The QuickSort algorithm seems to run quite a lot faster than the mergesort (around 5-8 times).
-
What hypothesis could we have?
- Try to use the Netbeans profiler on each of the two algorithms.
Static analysis
- Take a look at the
CircularBuffer.java
in the sample code. In groups of two or three, try to find as many style problems as you can - and you can agree upon.
Next, lets take a look at the PMD tool from above.
- download it to anywhere on your machine
- try it out on the code in the the basic algorithm project.The following java checks can be relevant to try:
-R rulesets/java/optimizations.xml
-R rulesets/java/naming.xml
-R rulesets/java/coupling.xml
-R rulesets/java/controversial.xml
For me to get the tool running, I just needed to download it and unzip it. I was then able to run it in command line. I cd to the directory where I unzipped it, and down into its bin directory:
/Users/kasper/Downloads/pmd-bin-5.8.1/bin
From there I issued commands like:
./run.sh pmd -d ~/nearlyEmpty/MergeSortTrouble/src/main/java/dk/cphbusiness/soft/mergesorttrouble -R rulesets/java/controversial.xml -f html > ~/pmdresult.html
which gives me a html file I can load into the browser.
For next week
You should try to use one of the tools above on a larger example. If you have a project lying around from earlier projects, feel free to use that.
If not, this project on github, GraphQL for JPA has a suited level of complexity, and it is not quite clear if it well done, or just functional.
Exercise 1.
Pick one of the tools from the above list, and run it on your chosen project.
As there are 100s of different metrics, you have to argue for which 10 you pick. In particular what situation would make those you pick particular relevant.
Exercise 2
You can again pick something from a project you have done yourself, or just pick something from the GraphQL sample.
The exercise is to locate a library call which takes up most of the overall execution time for a user story.
If you do not have something of your own, I suggest you look into the the “find droid by name” test.
Notice, it calls `executor.execute(query).data - which is the thing we should see where its bottlenecks are hidden.