JMH:从@State 的@Setup 方法访问BenchmarkParams class

JMH: accessing BenchmarkParams from @Setup method of @State class

是否可以在 @State(Scope.Benchmark) class 的 @Setup 中访问 BenchmarkParams,如果那个 class 被传入a @Benchmark 作为参数?

最小代码示例(实际使用更复杂,但这重现了我的问题):

@State( Scope.Benchmark )
public class Test
{
    @Setup
    public void setUp( BenchmarkParams params ){}

    @Benchmark
    public void nothing( Test test ){}
}

有一个基本的 JMH sample,但没有将 @State 传递到 @Benchmark 方法的地方

我想访问 @Setup 中的 BenchmarkParams 到 retrieve/log 我根据 JMH @Param

创建的每个基准配置数据

上面的示例代码:

  1. 定义 @State(Scope.Benchmark) class 命名 Test <--- 有效
  2. 定义名为 nothing()@Benchmark 方法 <--- 有效
  3. @State 实例传递给 @Benchmark 方法<--- 失败!

这里是错误:

[ERROR]
/Users/.../jmh-benchmarks/target/generated-sources/annotations/test/generated/Test_nothing_jmhTest.java:[390,16]
method setUp in class test.Test cannot be applied to given
types;
  required: org.openjdk.jmh.infra.BenchmarkParams
  found:
org.openjdk.jmh.infra.generated.BenchmarkParams_jmhType,org.openjdk.jmh.infra.generated.BenchmarkParams_jmhType
  reason: actual and formal argument lists differ in length

欢迎任何帮助!

[编辑 1]

仅供参考,在我的实际代码中还有一个 @State(Scope.Thread) class,更像是:

@State( Scope.Benchmark )
public abstract class TestBase
{
    @Setup
    public void setUp( BenchmarkParams params ){}
}

@State( Scope.Benchmark )
public class TestImpl extends TestBase
{
    @State( Scope.Thread )
    public static class ThreadState 
    {
            @Setup
            public void setUp( TestImpl state ){}
    }

    @Benchmark
    public void nothing( ThreadState state ){}
}

[编辑 2]

从 JMH 1.3 开始,这不再是问题

如问题所述,将 BenchmarkParams 传递到 TestBase@Setup 会导致 JMH 构建失败

好像跟有DAGs of @State classes

有关

BenchmarkParams 传递到 @State(Scope.Benchmark) class 中 而不是 "main" DAG 的一部分(例如,@State(Scope.Benchmark)->@State(Scope.Thread)->@Benchmark) 分支似乎解决了那个问题

例如,

@State( Scope.Benchmark )
public abstract class TestBase
{
    @Setup
    public void setUp( BenchmarkParamsState state )
    {
        // do something with state.someParam
    }

    @State( Scope.Benchmark )
    public static class BenchmarkParamsState
    {
        String someParam;

        @Setup
        public void setUp( BenchmarkParams params )
        {
            // set someParam based on contents of params
        }
    }
}

@State( Scope.Benchmark )
public class TestImpl extends TestBase
{
    @State( Scope.Thread )
    public static class ThreadState 
    {
        @Setup
        public void setUp( TestImpl state ){}
    }

    @Benchmark
    public void nothing( ThreadState state ){}
}

此外,因为 BenchmarkParamsState 是更大 DAG 的一部分(由于被传递到 TestBase@Setup),它的 @Setup 仍然会在每个 @Benchmark

[编辑]

从 JMH 1.3 开始,这不再是问题