首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JMH超时不会中断

JMH超时不会中断
EN

Stack Overflow用户
提问于 2018-04-24 14:15:51
回答 1查看 925关注 0票数 4

在尝试使用JMH中的超时时,我发现没有一个超时实际上会导致任何中断。我可以将问题减少到以下几行:

代码语言:javascript
复制
package main.java;

import org.openjdk.jmh.Main;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.RunnerException;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class TimeoutBenchmark {
    public static void main(String... args) throws IOException, RunnerException {
        Main.main(args);
    }

    @Benchmark
    @BenchmarkMode(Mode.AverageTime)
    @Warmup(iterations = 0)
    @Timeout(time = 10, timeUnit = TimeUnit.SECONDS)
    public long benchmark() throws InterruptedException {
        Thread.sleep(20000);
        return 0;
    }
}

由于Thread.sleep处理中断,所以我希望每次迭代运行10秒,而不是20秒。然而,情况并非如此:

代码语言:javascript
复制
# JMH version: 1.20
# VM version: JDK 1.8.0_144, VM 25.144-b01
# VM invoker: C:\Program Files\Java\jdk1.8.0_144\jre\bin\java.exe
# VM options: -Dfile.encoding=UTF-8
# Warmup: <none>
# Measurement: 20 iterations, 1 s each
# Timeout: 10 s per iteration
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Benchmark: main.java.TimeoutBenchmark.benchmark

# Run progress: 0,00% complete, ETA 00:03:20
# Fork: 1 of 10
Iteration   1: 20,004 s/op
Iteration   2: 20,009 s/op
Iteration   3: 20,009 s/op
Iteration   4: 20,014 s/op
Iteration   5: 20,003 s/op
Iteration   6: 20,003 s/op
Iteration   7: 20,003 s/op

为什么会这样呢?如何更改此代码,使每次迭代在10秒后实际中断?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-24 15:16:58

BenchmarkHandler中,在处理超时时,代码中有注释

// now we communicate all worker threads should stop control.announceDone(); // wait for all workers to transit to teardown control.awaitWarmdownReady(); // Wait for the result, handling timeouts while (completed.size() < numThreads) {

因此,这个超时基本上只适用于teardown阶段。

但是,我认为这个注释的javadoc应该用这些信息进行修改。

下面描述的是:http://java-performance.info/jmh/

另外,这封邮件可能很有用:http://mail.openjdk.java.net/pipermail/jmh-dev/2015-May/001912.html

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50003967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档