What are Klocwork Metrics Reports?
In this article, we will go through the steps necessary to use Klocwork’s powerful built-in metrics analysis to produce interactive reports that will let you quickly spot code that needs refactoring.
Here is an example of a report based on methods exceeding a cyclomatic complexity value of 20. If you want to know how to create reports like these, then read on!
What information do you need?
The custom reports that we will generate will depend on the metrics.xml file located in the directory
In our example, we will configure four new report types based on the following metrics:
- The lines of code per file
- The number of times a global variable is used for all routines per function
- The cyclomatic complexity per method over 20
- The number of writes to global variables per method over 2
The corresponding Klocwork metrics respectively are
Each metric has an ID. This is what you will add to the <kids></kids> element in the metrics.xml file. They respectively are
Finally, we need to know the entity to which these metrics apply. For example does the report apply the metric for each file, class, method, function, or interface? This is what you will add to the <entityKids></entityKids> element in the metrics.xml file.
In our case, the ID of the relevant entities respectively are:
- 1 // FILE
- 5 // FUNCTION
- 48 // CLASS-METHOD
- 48 // CLASS-METHOD
We now have all the data we need to edit the metrics.xml file.
Setting up the metrics.xml file
- Back up <projects_root>/config/metrics.xml
- Open the original metrics.xml for editing
- By default the file should look similar to this:
Notice how there are two categories: TotalMetricValue and EntityCountOverThreshold
TotalMetricValue is a category for metrics where you want it to report only the metric value e.g. Lines of Code.
EntityCountOverThreshold is slightly different because there is a threshold. This means Klocwork will report the metric only if the value is over a certain threshold. For example if you wanted Klocwork to report lines of code per method that exceeded 200 lines of code, then you would define the metric in this category.
Here is how the TotalMetricValue category should like
And this is how the EntityCountOverThreshold category should look like
Save the metrics.xml file.
Creating the report
Once you have saved your metrics.xml file, go ahead and restart the Klocwork servers by running the command:
kwservice -r <projects_root> restart
Once all services have restarted, go to the Klocwork web portal and click on the Reports tab, then click Create a new report.
Scroll to the bottom and click on show more
You should see the metrics defined in metrics.xml here
Now you can click and see the reports. For example, click on Writes to Global Variables per Method.
And if you click on the bar charts, you can get a better breakdown of where the metric is being reported
And that’s it! For any questions or if you need further clarification, please send us a message using the contact form below.