Automatically compare Java analysis results of a single unit test

I want to run a single unit test and collect its "analysis" information: the frequency of each method call, how many instances of a specific class are created, how long it takes to execute a method / thread, and so on Then, I want to compare this information with some expected values Are there any profilers for Java that don't let me do this (all of which should be done automatically, of course, without any GUI or user interaction)?

That's 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)
    );
  }
}

Solution

Using multiple Java analyzers can automatically capture snapshots of analysis information. You must add VM parameters to the Java start command to load the analyzer. You must tell the analyzer to record data and save the snapshot when exiting

In jpprofiler, you can use "profile" ant task to perform this operation It gets the profile settings exported from the jpprofiler GUI from the profile You can use the controller API to start recording and save snapshots

Then you have two choices:

>You can use the jpcompare command line utility or the corresponding "compare" ant task to compare snapshots with known benchmarks There are several comparisons available You can export the results to HTML or machine-readable format (CSV / XML). > You can use the jpexport command line utility or the corresponding "export" ant task to extract data from the snapshot You can then write your own code to analyze the analysis data for a particular run

For a limited set of analysis data, you can actually write your own analyzer using the jpprofiler API, which can perform a profiler like operation in your example Gettotalcallsmadeto (foo. Class, "barmethod") operation See the API / samples / platform example in the jpofiler installation You will get information from the hotspot data

However, in order to assert whether to call a specific method, I recommend an AOP based method Using a profiler - based approach is suitable for detecting performance regression

Disclaimer: our company develops jpprofiler

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>