Re: Automatic way to test performance optimizations
In my code, I must sometimes implement peformance optimizations. By
definition, the functional behavior does not change through this,
which can be tested through the public interface (regression tests).
But I would also like to test, that the optimization is used in the
correct places. Even using reflection to gain private access to
fields, I find this is difficult without introducing code (e.g. flags)
which only use is testing.
As an example consider the below code found in Arrays.mergeSort of the
Sun JDK. How would you go about testing, that the insertion sort is
used in the correct cases?
It depends on what I decide "correct" means. If I'm
satisfied that the INSERTIONSORT_THRESHOLD criterion is an
adequate definition of "correctness," I think I'd check it
by code inspection (perhaps even by formal proof methods)
rather than by testing. If the question is really more like
"Has INSERTIONSORT_THRESHOLD's value been chosen correctly?"
the job would be much more complicated, and probably wouldn't
lead to a hard-and-fast answer but to something with a lot
of "In typical cases" and "Most of the time" and so on.
Is there any specific tool available for JUnit (or any other) to test
performance automatically? Can we test that the code passes at certain
points or that certain methods have been called? If not, is there a
nice way to check in the log for a sign indicating the use of the
Take a look at the java.lang.instrument package (I haven't
used it myself, but the name looks promising).
With unit testing (JUnit or whatever) I think you're out
of luck from the outset. The underlying philosophy of unit
testing is to check that a unit behaves as advertised, not to
study the means by which the behavior is achieved. The unit
under test is a "black box," whose external manifestations are
the matter of interest and whose inner workings are a mystery.
Other testing schemes exist, but I don't think you'll find
them in a unit-testing framework.
PS: Does Sun remove all log statements from its JDK src before
distribution or are they developing without logs (and should thus be
I don't know. Ask the oracle.