gpt4 book ai didi

java - JMH 基准迭代中的随机峰

转载 作者:行者123 更新时间:2023-11-30 12:05:10 24 4
gpt4 key购买 nike

我正在尝试一个非常快的方法(~20 us/op),它似乎工作得很好,除了一些随机很长的迭代:

Iteration  63: 14.319 us/op
Iteration 64: 13.128 us/op
Iteration 65: 15.198 us/op
Iteration 66: 20.822 us/op
Iteration 67: 21.669 us/op
Iteration 68: 21.439 us/op
Iteration 69: 15.946 us/op
Iteration 70: 18.793 us/op
Iteration 71: 19.212 us/op
Iteration 72: 816.129 us/op // oopsy
Iteration 73: 22.115 us/op
Iteration 74: 15.143 us/op
Iteration 75: 18.423 us/op
Iteration 76: 15.238 us/op

Result "benchmark.StuffBench.run_bench":
20.629 ±(99.9%) 9.164 us/op [Average]
(min, avg, max) = (12.689, 20.629, 816.129), stdev = 47.763
CI (99.9%): [11.464, 29.793] (assumes normal distribution)

它可能是 GC,但 shouldDoGc(false) 不会改变任何东西:

final Options options = new OptionsBuilder()
.include(StuffBench.class.getSimpleName())
.shouldDoGC(false)
.build();
Collection<RunResult> runResults = new Runner(options).run();

基准类:

@Fork(value = 2)
@Threads(1)
@Warmup(iterations = 1000, time = 50, timeUnit = TimeUnit.MICROSECONDS)
@Measurement(iterations = 150, time = 50, timeUnit = TimeUnit.MICROSECONDS)
@Timeout(time = 50, timeUnit = TimeUnit.MICROSECONDS)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@BenchmarkMode(Mode.AverageTime)
@State(Scope.Benchmark)
public class StuffBench {
private Stuff stuff;

@Setup
public void initialize() {
stuff = new Stuff();
}

@Benchmark
public void run_bench() {
stuff.run();
}
}

最佳答案

为了解决此类问题,我使用了所谓的抖动采样器。您有一个线程设置时间戳、运行代码、重置时间戳并暂停以防止 CPU 过载。第二个线程对时间戳进行采样,如果它处于 Activity 状态并且时间太长,例如20 us,你打印它正在做什么的堆栈跟踪。例如Thread.getStackTrace() 结合最常见的堆栈跟踪,你就有了一个可以指向问题的安全点(或问题之后的第一个安全点)它更像是艺术而不是科学;)

关于java - JMH 基准迭代中的随机峰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56563221/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com