官术网_书友最值得收藏!

Fixture methods

Since a state object can be kept during the whole lifetime of the benchmark, it would useful to have methods that do state housekeeping. This concept is implemented with the usual fixture methods that you might encounter in JUnit or TestNG. You can only write these methods inside a class that's declared with the @State annotation, and these methods can only be invoked by threads which are using the state. In this way, the thread-local context is created and you don't have to use thread synchronization.

The method for initializing must be marked with the @Setup annotation and the method that's declared with the @TearDown annotation will be invoked at the end of the object's lifetime. The following code example demonstrates how this concept works:

@State(Scope.Thread)
public class MyBenchmark {

double x;

@Setup
public void prepare() {
System.out.println("prepare");
x = Math.PI;
}

@TearDown
public void check() {
System.out.println("check");
assert x > Math.PI : "Nothing changed?";
}

@Benchmark
public void measureRight() {
x++;
}

@Benchmark
public void measureWrong() {
double x = 0;
x++;
}
}

The output will contain prepare and check messages, which talk about the start and the end of each scope. The measureRight() method obviously does the correct thing. Incrementing the x field in the state.check() benchmark will never fail this way because we're always guaranteed to have at least one benchmark call. However, the measureWrong() method will fail the check() method, because we deliberately have the typo, and it will only increment the local variable. This shouldn't pass the check, and the JMH will fail to run.

主站蜘蛛池模板: 清水河县| 江陵县| 闽侯县| 离岛区| 咸丰县| 澜沧| 弥勒县| 镇安县| 英吉沙县| 运城市| 平湖市| 灵石县| 象州县| 龙口市| 北海市| 寻乌县| 和政县| 汉寿县| 兴宁市| 手游| 民乐县| 阿拉善左旗| 中阳县| 会东县| 浠水县| 三台县| 西畴县| 盐城市| 贡觉县| 和顺县| 全州县| 寿光市| 乌海市| 海丰县| 凯里市| 荆州市| 信阳市| 丰县| 阿拉善右旗| 抚远县| 古交市|