Home > OS >  How to put a threshold on "runtime complexity" of a Maven module?
How to put a threshold on "runtime complexity" of a Maven module?

Time:12-20

I'm looking for a Maven plugin, which would measure runtime complexity of all unit tests in the module and fail the build if it's higher than the threshold. I need this to prevent accidental increases of complexity, which doesn't break the functionality but makes the code much slower. Does it exist?

CodePudding user response:

Complexity is about the change in performance measures as a particular parameter or parameters increase. The time to run a suite of unit tests doesn't have a meaningful complexity measure ... because:

  • there is (typically) no set of parameters to vary for any individual test, and
  • the overall performance of the test suite is an aggregate of the individual tests that typically do completely different things.

Methods have complexity. But you would need deep semantic knowledge of which methods matter, and how they are intended to be used, and what the parameters for the complexity measure should be in order to generate some meaningful "empirical" complexity approximations for them. I don't think there is much chance that anyone has tried to implement a Maven or IDE plugin to do do this kind of thing. Or that it would be reliable if they ever did.


My advice would be:

  • Work out the methods, and parameters that are critical to the performance of your application.

  • Write some performance tests to run the methods for a range of parameters.

  • Write some "harness" code to run the tests, capture the measures and compare them with the "predictions" of the complexity class you are aiming for ... for each measure.

  • Integrate these tests into your testing regime.

But beware of the traps of writing Java micro-benchmarks; see How do I write a correct micro-benchmark in Java?.


Actually ... my better advice would be to not attempt to measure complexity at all. Instead, just create some regular benchmarks for your application, and decide on some the performance thresholds to trigger your attention. After all, your real goal should be performance, not complexity.

CodePudding user response:

There are checkstyle Metrics for the complexity of source codes as bellow. The Cyclomatic Complexity is the tool may best fit your requirements.

  • BooleanExpressionComplexity - Restricts the number of boolean operators (&&, ||, &, | and ^) in an expression.
  • ClassDataAbstractionCoupling - Measures the number of instantiations of other classes within the given class or record.
  • ClassFanOutComplexity - Checks the number of other types a given class/record/interface/enum/annotation relies on.
  • CyclomaticComplexity - Checks cyclomatic complexity against a specified limit. It is a measure of the minimum number of possible paths through the source and therefore the number of required tests.
  • JavaNCSS - Determines complexity of methods, classes and files by counting the Non Commenting Source Statements (NCSS).
  • NPathComplexity - Checks the NPATH complexity against a specified limit. The NPATH metric computes the number of possible execution paths through a function(method).

We may enable them the maven pom.xml file

  • Related