|
17 | 17 | package org.apache.commons.mail2.jakarta; |
18 | 18 |
|
19 | 19 | 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; |
23 | 20 | import org.openjdk.jmh.infra.Blackhole; |
24 | 21 |
|
25 | 22 | import java.util.regex.Matcher; |
26 | 23 | import java.util.regex.Pattern; |
27 | 24 |
|
28 | 25 | public class ImageHtmlEmailBenchmark { |
29 | 26 |
|
| 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 | + |
30 | 63 | @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); |
35 | 88 | } |
36 | | - context.matcherImg.reset(); |
37 | | - context.matcherScript.reset(); |
38 | 89 | } |
39 | 90 |
|
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 + "\">"); |
52 | 100 | 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); |
63 | 101 | } |
| 102 | + html.append("</pre></body></html>"); |
| 103 | + return html.toString(); |
64 | 104 | } |
65 | 105 |
|
66 | 106 | public static void main(String[] args) throws Exception { |
|
0 commit comments