Skip to content

Commit 236163e

Browse files
committed
Add benchmark with generated text
1 parent 28029b2 commit 236163e

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@
3939
<version>4.12</version>
4040
<scope>test</scope>
4141
</dependency>
42+
<dependency>
43+
<groupId>org.openjdk.jmh</groupId>
44+
<artifactId>jmh-core</artifactId>
45+
<version>1.9.3</version>
46+
<scope>test</scope>
47+
</dependency>
48+
<dependency>
49+
<groupId>org.openjdk.jmh</groupId>
50+
<artifactId>jmh-generator-annprocess</artifactId>
51+
<version>1.9.3</version>
52+
<scope>test</scope>
53+
</dependency>
4254
</dependencies>
4355

4456
<developers>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.nibor.autolink;
2+
3+
import org.openjdk.jmh.Main;
4+
import org.openjdk.jmh.annotations.Benchmark;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.Random;
9+
10+
public class AutolinkBenchmark extends AutolinkTestCase {
11+
12+
private static final List<String> WORDS = Arrays.asList(
13+
"Lorem ", "ipsum ", "dolor ", "sit ", "amet ", "consectetur ", "adipiscing ", "elit ",
14+
".", ",", ":", "@", "(", ")", "http://example.com", "https://test.com/foo_(bar)",
15+
"foo.bar@example.com", "foo+bar@test.com"
16+
);
17+
18+
private static final String GENERATED_INPUT = generateText(WORDS, 10000);
19+
20+
public static void main(String[] args) throws Exception {
21+
System.out.println("input length: " + GENERATED_INPUT.length());
22+
System.out.println("number of links: " + LinkExtractor.builder().build().getLinks(GENERATED_INPUT).size());
23+
System.out.println();
24+
Main.main(args);
25+
}
26+
27+
@Benchmark
28+
public void generatedText() {
29+
link(GENERATED_INPUT, "|", null);
30+
}
31+
32+
@Override
33+
protected LinkExtractor getLinkExtractor() {
34+
return LinkExtractor.builder().build();
35+
}
36+
37+
private static String generateText(List<String> wordList, int words) {
38+
Random random = new Random(1);
39+
StringBuilder sb = new StringBuilder();
40+
for (int i = 0; i < words; i++) {
41+
String word = wordList.get(random.nextInt(wordList.size()));
42+
sb.append(word);
43+
}
44+
return sb.toString();
45+
}
46+
}

src/test/java/org/nibor/autolink/AutolinkTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ protected void assertNotLinked(String input) {
1616

1717
protected abstract LinkExtractor getLinkExtractor();
1818

19-
private String link(final String input, final String marker, final LinkType expectedLinkType) {
19+
protected String link(final String input, final String marker, final LinkType expectedLinkType) {
2020
return Autolink.renderLinks(input, getLinkExtractor(), new LinkRenderer() {
2121
@Override
2222
public void render(Link link, StringBuilder sb) {
23-
assertEquals(expectedLinkType, link.getType());
23+
if (expectedLinkType != null) {
24+
assertEquals(expectedLinkType, link.getType());
25+
}
2426
sb.append(marker);
2527
sb.append(input, link.getBeginIndex(), link.getEndIndex());
2628
sb.append(marker);

0 commit comments

Comments
 (0)