2016-09-22 93 views
2

我正在運行jmh benchmark,但每個測試的調用都是以串行方式發生的。我怎樣才能使調用同時運行?jmh:同時運行基準測試

這裏是我的代碼摘要:

@State(Scope.Benchmark) 
public class FooBenchmark { 
    @Param({"123456"}) 
    public int barId; 

    @Setup 
    public void setup() { 
    } 

    @Benchmark 
    public void run(Blackhole hole) { 
     System.out.println("A"); // for proof that it's serial (see below) 
     ... 
     System.out.println("B"); // for proof that it's serial (see below) 
    } 
} 

這將打印A和B.然後永遠不會放棄兩個連續的A或B的。

回答

3

如果你想明確定義線程總量您的測試過程中使用,你需要@Threads(numberOfThreads)在明年來註釋測試方法(方法與@Benchmark註解):

@Threads(10) 
@Benchmark 
public void run(Blackhole hole) { 

在這個例子中, 10線程將同時執行測試方法。

作爲提醒,這裏是描述這個註釋的文檔的一部分:

@Threads:用於測試的線程數。默認值是 Runtime.getRuntime().availableProcessors()