Count metrics with JMH
Asked Answered
N

1

9

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?

Nealson answered 25/3, 2014 at 16:31 Comment(2)
Did you manage to get the metrics on memory usage? Adding the HotspotMemoryProfiler.class nor the GCProfiler.class didn't help. The only metric I was getting is the ops/secHydrometallurgy
Nevermind, I wasn't invoking my main class where I add the specific Profiler classes, but the default Benchmarks.jar's main class. Now I get some metrics like sun.gc.generation.0.space.0.usedHydrometallurgy
J
10

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.

Jews answered 25/3, 2014 at 18:0 Comment(3)
Thank you, before I did not notice profilers. But measuring the CPU is not there. I try to implement through @AuxCounter.Nealson
A benchmark consumes 100% or its thread. On a 8 core a single threaded benchmark will noch consume 100% of available CPU. And for me it makes a difference if I run 10 sec on 8 cores multithreaded or in 12 seconds on 1 core.... - I am also interested to measure CPU time and efficiency of multithreaded code.Theocritus
@oleg From the myriad of metrics reported by HS_GC, which one is actually reporting memory consumption? sun.gc.metaspace.used looks most promising by name, but it's just a guess. In the end the number should somehow reflect what runtime.totalMemory() - runtime.freeMemory()runtime.totalMemory() - runtime.freeMemory()` would provide.Fayalite

© 2022 - 2024 — McMap. All rights reserved.