I want to run a single unit test and collect its “profiling” information: how often every method was called, how many instances of certain class were created, how much time did it take to execute certain method/thread, etc. Then, I want to compare this information with some expected values. Are there any Profilers for Java than let me do this (all this should be done automatically, without any GUI or user interaction, of course)?
This is how I want it to work:
public class MyTest {
@Test
public void justTwoCallsToFoo() {
Profiler.start(Foo.class);
Foo foo = new Foo();
foo.someMethodToProfile(); // profiler should collect data here
assertThat(
Profiler.getTotalCallsMadeTo(Foo.class, "barMethod"),
equalTo(3)
);
}
}
I found out that this could be done with a J2SE built-in HPROF profiling tool.
It produces a text file formatted like this:
Then it’s easy to parse the file and extract the methods you’re interested in. The solution is completely free and JSE built-in, which is a great benefit comparing to other tools.