-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegexBenchmark.java
69 lines (54 loc) · 2.67 KB
/
RegexBenchmark.java
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
package benchmark;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import utils.TestUtils;
public class RegexBenchmark {
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 1)
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void standartRegexWithCityDataBenchmark(MyState myState, Blackhole blackhole) {
myState.standartCitiesRegex.matcher(myState.cityInputTextWords).replaceAll("XXX");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 1)
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public static void re2JWithCityDataBenchmark(MyState myState, Blackhole blackhole) {
myState.re2jCitiesRegex.matcher(myState.cityInputTextWords).replaceAll("XXX");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 1)
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public void standartRegexWithEmailDataBenchmark(MyState myState, Blackhole blackhole) {
myState.standartEmailRegex.matcher(myState.emailInputTextWords).replaceAll("XXX");
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 1)
@Warmup(iterations = 2)
@Measurement(iterations = 5)
public static void re2JWithEmailDataBenchmark(MyState myState, Blackhole blackhole) {
myState.re2jEmailRegex.matcher(myState.emailInputTextWords).replaceAll("XXX");
}
@State(Scope.Benchmark)
public static class MyState {
private String injectedCity = "Richmond";
private String injectedEmail = "[email protected]";
public java.util.regex.Pattern standartCitiesRegex = java.util.regex.Pattern.compile(String.join("|", TestUtils.usCitiesAndTowns));
public com.google.re2j.Pattern re2jCitiesRegex = com.google.re2j.Pattern.compile(String.join("|", TestUtils.usCitiesAndTowns));
public java.util.regex.Pattern standartEmailRegex = java.util.regex.Pattern.compile(TestUtils.emailRegex);
public com.google.re2j.Pattern re2jEmailRegex = com.google.re2j.Pattern.compile(TestUtils.emailRegex);
public String cityInputTextWords = TestUtils.getRandomLongWord(injectedCity);
public String emailInputTextWords = TestUtils.getRandomLongWord(injectedEmail);
}
}
// Benchmark Mode Cnt Score Error Units
//RegexBenchmark.standartRegexWithCityDataBenchmark avgt 5 0.147 ± 0.018 s/op
//RegexBenchmark.re2JWithCityDataBenchmark avgt 5 0.625 ± 0.040 s/op
//RegexBenchmark.standartRegexWithEmailDataBenchmark avgt 5 0.004 ± 0.001 s/op
//RegexBenchmark.re2JWithEmailDataBenchmark avgt 5 0.002 ± 0.001 s/op