Java @Group和@GroupThreads在jmh中指的是什么?
我一直在研究jmh实现“多线程”基准的方式。据我所知,注释Java @Group和@GroupThreads在jmh中指的是什么?,java,jmh,Java,Jmh,我一直在研究jmh实现“多线程”基准的方式。据我所知,注释@Group(“Identifier”)和@GroupThreads(thread\u number)允许并行运行同一组中的基准测试 @Benchmark @Group("parallel") @GroupThreads(1) public void benchmark_1() { while(true) {} } @Benchmark @Group("parallel") @GroupThreads(1) public voi
@Group(“Identifier”)
和@GroupThreads(thread\u number)
允许并行运行同一组中的基准测试
@Benchmark
@Group("parallel")
@GroupThreads(1)
public void benchmark_1() {
while(true) {}
}
@Benchmark
@Group("parallel")
@GroupThreads(1)
public void benchmark_2() {
while(true) {}
}
使用CPU监视器,我发现两个CPU都被充分利用了。
我想知道跑步者是如何解释这些注释的。你试过阅读Javadocs吗?这些都没有回答问题吗 例如,
@Group
表示:
* <p>Multiple {@link Benchmark} methods can be bound in the execution group
* to produce the asymmetric benchmark. Each execution group contains of one
* or more threads. Each thread within a particular execution group executes
* one of {@link Group}-annotated {@link Benchmark} methods. The number of
* threads executing a particular {@link Benchmark} defaults to a single thread,
* and can be overridden by {@link GroupThreads}.</p>
*
* <p>Multiple copies of an execution group may participate in the run, and
* the number of groups depends on the number of worker threads requested.
* JMH will take the requested number of worker threads, round it up to execution
* group size, and then distribute the threads among the (multiple) groups.
* Among other things, this guarantees fully-populated execution groups.</p>
* <p>For example, running {@link Group} with two {@link Benchmark} methods,
* each having {@link GroupThreads}(4), will run 8*N threads, where N is an
* integer.</p>
执行组中可以绑定多个{@link Benchmark}方法
*生成非对称基准。每个执行组包含一个
*一个或多个线程。特定执行组中的每个线程都执行
*{@link Group}注释的{@link Benchmark}方法之一。人数
*执行特定{@link Benchmark}的线程默认为单个线程,
*并且可以被{@link GroupThreads}覆盖
*
*一个执行组的多个副本可以参与运行,并且
*组的数量取决于请求的工作线程的数量。
*JMH将获取请求的工作线程数,将其四舍五入直至执行
*组大小,然后在(多个)组之间分配线程。
*除其他事项外,这保证了完全填充执行组
*例如,使用两个{@link Benchmark}方法运行{@link Group},
*每个具有{@linkgroupthreads}(4)的线程将运行8*N个线程,其中N是一个
*整数
因此,
@Group
生成了一个非对称基准,@GroupThreads
控制组内线程的分布(-tg…
在CLI中对其进行复制)@Threads
说明总共要运行多少个线程(-t…
在CLI中进行配音)。我仍然不理解现在执行组的线程是如何工作的,例如在jmh的示例15中,三个线程将调用counter.incrementAndGet(),一个线程将调用counter.get()。这四个线程是按顺序运行(意味着最后一个线程将等待三个线程完成更新,然后获取值)还是并行运行。非对称基准与对称基准的运行方式相同,对@Group
Javadoc中概述的差异进行模化。现在,对称基准测试并行运行所有线程。它说Javadocs中的任何地方非对称基准都不会并行运行线程吗?等等,什么?当然,在访问基准状态、控制线程等方面需要一定程度的同步,但这对于常规对称基准也是必需的。当然,在实际处理器上调度线程时会涉及一些操作系统仲裁,特别是当请求的线程数超过CPU时。BenchmarkHandler
和BenchmarkHandler.BenchmarkTask
,这将调用../target/generated sources/
中的某个位置生成的存根。但我还是不明白你在那里会发现什么。非对称基准在选择要调用的@Benchmark
方法的方式上有所不同,没有其他选择。好了,开始吧。但有一句话值得警告:依赖实施细节是危险的。您可以窥视实现,但不要错误地认为实现将保持不变。