سؤال

How can I use to calculate the amount of CPU time and memory in JMH? For example, I have: Code:

@State(Scope.Thread)
@BenchmarkMode(Mode.All)
public class JMHSample_My {

    int x = 1;
    int y = 2;

    @GenerateMicroBenchmark
    public int measureAdd() {
        return (x + y);
    }

    @GenerateMicroBenchmark
    public int measureMul() {
        return (x * y);
    }

    public static void main(String[] args) throws RunnerException {
        Options opt = new OptionsBuilder()
                .include(".*" + JMHSample_My.class.getSimpleName() + ".*")
                .warmupIterations(5)
                .measurementIterations(5)
                .forks(1)
                .build();

        new Runner(opt).run();
    }
}

Result:

Benchmark                  Mode   Samples         Mean   Mean error    Units
JMHSample_My.measureAdd    thrpt         5  1060579.757    39506.950   ops/ms

JMHSample_My.measureMul    thrpt         5  1046872.684    79805.116   ops/ms

JMHSample_My.measureAdd     avgt         5        0.000        0.000    ms/op

JMHSample_My.measureMul     avgt         5        0.000        0.000    ms/op

JMHSample_My.measureAdd   sample   9549793        0.000        0.000    ms/op

JMHSample_My.measureMul   sample   9287002        0.000        0.000    ms/op

JMHSample_My.measureAdd       ss         5        0.001        0.000       ms

JMHSample_My.measureMul       ss         5        0.001        0.000       ms

I can see the number of requests for time, the average time of the test, but do not see the average amount of CPU usage and memory usage. This can be done by means of JMH?

هل كانت مفيدة؟

المحلول

For memory usage you can add GC or HS_GC profiler using the addProfiler method when building runner options.

As for CPU usage, benchmarks usually and naturally consume 100% of available CPU. However you should check other available profiles to see whether they will produce information useful to you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top