Skip to content

Commit 59a9c16

Browse files
sean.fabriseanfabs
authored andcommitted
allow benchmarking regex variants
1 parent 427b34e commit 59a9c16

File tree

1 file changed

+71
-31
lines changed

1 file changed

+71
-31
lines changed

commons-email2-jakarta/src/test/java/org/apache/commons/mail2/jakarta/ImageHtmlEmailBenchmark.java

Lines changed: 71 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,50 +17,90 @@
1717
package org.apache.commons.mail2.jakarta;
1818

1919
import org.openjdk.jmh.annotations.Benchmark;
20-
import org.openjdk.jmh.annotations.Scope;
21-
import org.openjdk.jmh.annotations.Setup;
22-
import org.openjdk.jmh.annotations.State;
2320
import org.openjdk.jmh.infra.Blackhole;
2421

2522
import java.util.regex.Matcher;
2623
import java.util.regex.Pattern;
2724

2825
public class ImageHtmlEmailBenchmark {
2926

27+
private final static int MATCHES_TO_FIND = 50;
28+
29+
private final static String TEST_HTML = testHtml();
30+
31+
public final static TestHarness testHarnessCurrentRegex = new TestHarness(
32+
ImageHtmlEmail.REGEX_IMG_SRC,
33+
ImageHtmlEmail.REGEX_SCRIPT_SRC
34+
);
35+
36+
/**
37+
* The original regex pre EMAIL-198
38+
*/
39+
public final static TestHarness testHarnessOriginalRegex = new TestHarness(
40+
"(<[Ii][Mm][Gg]\\s*[^>]*?\\s+[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])",
41+
"(<[Ss][Cc][Rr][Ii][Pp][Tt]\\s*.*?\\s+[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])"
42+
);
43+
44+
/**
45+
* Test an alternative regex with a non-greedy url: note the {@code ([^"']+?)} capture.
46+
* This should be slower than the current regex test case.
47+
*/
48+
public final static TestHarness testHarnessCurrentRegexNonGreedyUrl = new TestHarness(
49+
"(<[Ii][Mm][Gg](?=\\s)[^>]*?\\s[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])",
50+
"(<[Ss][Cc][Rr][Ii][Pp][Tt](?=\\s)[^>]*?\\s[Ss][Rr][Cc]\\s*=\\s*[\"'])([^\"']+?)([\"'])"
51+
);
52+
53+
@Benchmark
54+
public void testCaseCurrentRegex(Blackhole blackhole) {
55+
testHarnessCurrentRegex.runTest(blackhole);
56+
}
57+
58+
@Benchmark
59+
public void testCaseOriginalRegex(Blackhole blackhole) {
60+
testHarnessOriginalRegex.runTest(blackhole);
61+
}
62+
3063
@Benchmark
31-
public void testRegularExpressions(BenchmarkContext context, Blackhole blackhole) {
32-
for (int i = 0; i < context.matchesToFind; i++) {
33-
blackhole.consume(context.matcherImg.find());
34-
blackhole.consume(context.matcherScript.find());
64+
public void testCaseCurrentRegexNonGreedyUrl(Blackhole blackhole) {
65+
testHarnessCurrentRegexNonGreedyUrl.runTest(blackhole);
66+
}
67+
68+
public static class TestHarness {
69+
private Matcher matcherImg;
70+
private Matcher matcherScript;
71+
72+
public TestHarness(String patternImg, String patternScript){
73+
matcherImg = prepareRegex(patternImg);
74+
matcherScript = prepareRegex(patternScript);
75+
}
76+
77+
public void runTest(Blackhole blackhole) {
78+
for (int i = 0; i < MATCHES_TO_FIND; i++) {
79+
blackhole.consume(matcherImg.find());
80+
blackhole.consume(matcherScript.find());
81+
}
82+
matcherImg.reset();
83+
matcherScript.reset();
84+
}
85+
86+
public Matcher prepareRegex(String pattern) {
87+
return Pattern.compile(pattern).matcher(TEST_HTML);
3588
}
36-
context.matcherImg.reset();
37-
context.matcherScript.reset();
3889
}
3990

40-
@State(Scope.Thread)
41-
public static class BenchmarkContext {
42-
Matcher matcherImg;
43-
Matcher matcherScript;
44-
int matchesToFind = 50;
45-
46-
@Setup
47-
public void prepare() {
48-
String longUrl = new String(new char[200]).replace("\0", "a");
49-
String longSpace = new String(new char[100]).replace("\0", " ");
50-
StringBuilder html = new StringBuilder();
51-
html.append("<html><body><pre>");
91+
private static String testHtml() {
92+
String longUrl = new String(new char[200]).replace("\0", "a");
93+
String longSpace = new String(new char[100]).replace("\0", " ");
94+
StringBuilder html = new StringBuilder();
95+
html.append("<html><body><pre>");
96+
html.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
97+
for (int i = 0; i < MATCHES_TO_FIND; i++) {
98+
html.append("<img" + longSpace + "xxx=\"no-src-attribute\">");
99+
html.append("<script some=\"true\" other=\"1\" attributes=\"yes\" src = \"" + longUrl + "\">");
52100
html.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
53-
for (int i = 0; i < matchesToFind; i++) {
54-
html.append("<img" + longSpace + "xxx=\"no-src-attribute\">");
55-
html.append("<script some=\"true\" other=\"1\" attributes=\"yes\" src = \"" + longUrl + "\">");
56-
html.append("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
57-
}
58-
html.append("</pre></body></html>");
59-
Pattern patternImg = Pattern.compile(ImageHtmlEmail.REGEX_IMG_SRC);
60-
matcherImg = patternImg.matcher(html);
61-
Pattern patternScript = Pattern.compile(ImageHtmlEmail.REGEX_SCRIPT_SRC);
62-
matcherScript = patternScript.matcher(html);
63101
}
102+
html.append("</pre></body></html>");
103+
return html.toString();
64104
}
65105

66106
public static void main(String[] args) throws Exception {

0 commit comments

Comments
 (0)