Using Java Profiler in Junit

Is there a Java profiler that can be integrated as part of JUnit testing.

For example, I would like to have the condition that if a certain method in the test uses more than X% of the CPU, the test must fail / pass.

Or, if a particular method works for more than X minutes, it should fail.

I looked at YourKit, the JVM monitor, and other suggested tools here, as well java -Xrunhprof:cpu=samples,file=myprogram.hprof

but all are not intended to be used as part of the test.

I would also be fine with calling the profiler from the test, saving the results (automatically) to csv / txt / any other file, and writing code to extract the information.

An open source / free tool would be better if one exists

+4
source share
1 answer

JProfiler has an API that can be used for this purpose. In the JProfiler installation, review api/samples/platform directoryand verifysrc-profiler/TestProfiler.java

As a rough outline, the code might look like this:

Connection connection = ConnectionFactory.getLocalConnection();

connection.recordCpuData(true);
// TODO execute unit test
connection.recordCpuData(false);

CpuHotspots cpuHotspots = connection.getCpuHotspots(null, Aggregation.METHOD,
        ThreadStatus.RUNNING, true);

List<ProfilingValue> hotspots = cpuHotspots.getHotspots();
Collections.sort(hotspots);
// TODO analyze hot spots

connection.close();

Disclaimer: My company is developing JProfiler.

+1
source

Source: https://habr.com/ru/post/1612282/


All Articles