使用JMH计数指标 [英] Count metrics with JMH

查看:226
本文介绍了使用JMH计数指标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用JMH计算CPU时间和内存量? 例如,我有: 代码:

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();
    }
}

结果:

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

我可以看到请求的次数,测试的平均时间,但是看不到平均的CPU使用量和内存使用量.这可以通过JMH来完成吗?

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?

推荐答案

对于内存使用,您可以在构建运行器选项时使用addProfiler方法添加GCHS_GC分析器.

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

关于CPU使用率,基准通常自然会消耗100%的可用CPU.但是,您应该检查其他可用的配置文件,以查看它们是否会产生对您有用的信息.

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.

这篇关于使用JMH计数指标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆