对 @Group @GroupThreads 的介绍
_/*
在之前的测试中, 每个Benchmark都是同样的一套代码, 然后所有线程都去执行一样的代码
—-
现在我们有个新的需求, 就是两套不同的代码, 要在一个Benchmark中一起执行
然后再看执行效率
*/
/*
比如下面这个例子:_
复制官方的代码到本地
我们猜猜看看在多线程竞争状态下, AtomicInteger 的执行效率如何
—-
3个线程执行自增的代码, 1个线程执行获取数据的代码
一共4个线程, 他们在Benchmark中一起执行, 可以模拟 AtomicInteger在多线程竞争的情况下执行效率
*/
Benchmark Mode Cnt Score Error UnitsSample_15_Asymmetric.benchmark_get_no_competition thrpt 651.724 ops/usSample_15_Asymmetric.benchmark_inc_no_competition thrpt 160.327 ops/usSample_15_Asymmetric.benchmark_thread_competition thrpt 104.397 ops/usSample_15_Asymmetric.benchmark_thread_competition:get thrpt 56.061 ops/usSample_15_Asymmetric.benchmark_thread_competition:inc thrpt 48.337 ops/us
private AtomicInteger counter;@Setuppublic void up() {counter = new AtomicInteger();}@Benchmark@Group("benchmark_thread_competition")@GroupThreads(3)public int inc() {return counter.incrementAndGet();}@Benchmark@Group("benchmark_thread_competition")@GroupThreads(1)public int get() {return counter.get();}
@Benchmark@Group("benchmark_inc_no_competition")@GroupThreads(1)public int inc3() {// 3个线程只执行自增操作, 应该跟上面差不多// 因为即使只有自增操作, 但由3个线程, 所有操作还是会在竞争状态return counter.incrementAndGet();}
@Benchmark@Group("benchmark_get_no_competition")public int get1() {// 这个benchmark只有一个线程在执行get操作// 效率直接拉满return counter.get();}
