控制每次迭代JMH的操作次数

时间:2016-10-17 08:16:04

标签: java microbenchmark jmh

我目前的设置:

    public void launchBenchmark() throws Exception {
    Options opt = new OptionsBuilder()
            .include(this.getClass().getName())
            .mode(Mode.Throughput) //Calculate number of operations in a time unit.
            .mode(Mode.AverageTime) //Calculate an average running time per operation
            .timeUnit(TimeUnit.MILLISECONDS)
            .warmupIterations(1)
            .measurementIterations(30)
            .threads(Runtime.getRuntime().availableProcessors())
            .forks(1)
            .shouldFailOnError(true)
            .shouldDoGC(true)
            .build();

    new Runner(opt).run();
}

我如何知道/控制(如果可能)每个基准测试的操作次数?

设置warmUp时间和measurementIteration时间是否很重要?

谢谢。

1 个答案:

答案 0 :(得分:7)

您无法控制每次迭代的操作次数。 JMH的重点是正确衡量该数字

您可以使用注释配置预热:

[RoutePrefix("api/v2/dummy")]
public class DummyController : ApiController
{
    // GET api/v2/dummy
    [HttpGet]
    [Route("get")]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2", "value3" };
    }
}

测量依据:

@Warmup(iterations = 10, time = 500, timeUnit = MILLISECONDS)

只需为您的用例设置适当的值

相关问题