-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAssertBenchmark.java
More file actions
90 lines (77 loc) · 2.76 KB
/
AssertBenchmark.java
File metadata and controls
90 lines (77 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package benchmark;
import graphql.Assert;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.Random;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 2, time = 5, batchSize = 50)
@Measurement(iterations = 3, batchSize = 50)
@Fork(2)
public class AssertBenchmark {
private static final int LOOPS = 100;
private static final boolean BOOL = new Random().nextBoolean();
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void benchMarkAssertWithString() {
for (int i = 0; i < LOOPS; i++) {
Assert.assertTrue(jitTrue(), "This string is constant");
}
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void benchMarkAssertWithStringSupplier() {
for (int i = 0; i < LOOPS; i++) {
Assert.assertTrue(jitTrue(), () -> "This string is constant");
}
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void benchMarkAssertWithStringSupplierFormatted() {
for (int i = 0; i < LOOPS; i++) {
final int captured = i;
Assert.assertTrue(jitTrue(), () -> String.format("This string is not constant %d", captured));
}
}
@Benchmark
@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public void benchMarkAssertWithStringFormatted() {
for (int i = 0; i < LOOPS; i++) {
Assert.assertTrue(jitTrue(), "This string is not constant %d", i);
}
}
private boolean jitTrue() {
// can you jit this away, Mr JIT??
//noinspection ConstantValue,SimplifiableConditionalExpression
return BOOL ? BOOL : !BOOL;
}
public static void main(String[] args) throws RunnerException {
runAtStartup();
Options opt = new OptionsBuilder()
.include("benchmark.AssertBenchmark")
.build();
new Runner(opt).run();
}
private static void runAtStartup() {
AssertBenchmark benchMark = new AssertBenchmark();
BenchmarkUtils.runInToolingForSomeTimeThenExit(
() -> {
},
benchMark::benchMarkAssertWithStringSupplier,
() -> {
}
);
}
}