Skip to content

Commit 6d7a309

Browse files
committed
[benchmark] Add JMH Benchmark for Java String Join operations
1 parent 08b700e commit 6d7a309

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

exist-core-jmh/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
<header>${project.parent.relativePath}/LGPL-21-license.template.txt</header>
7979
<excludes>
8080
<exclude>src/main/java/org/exist/storage/lock/LockTableBenchmark.java</exclude>
81+
<exclude>src/main/java/org/exist/xquery/utils/StringJoinBenchmark.java</exclude>
8182
<exclude>src/main/java/org/exist/xquery/utils/URIUtilsBenchmark.java</exclude>
8283
</excludes>
8384
</licenseSet>
@@ -89,6 +90,7 @@
8990
<header>${project.parent.relativePath}/FDB-backport-LGPL-21-ONLY-license.template.txt</header>
9091
<includes>
9192
<include>src/main/java/org/exist/storage/lock/LockTableBenchmark.java</include>
93+
<include>src/main/java/org/exist/xquery/utils/StringJoinBenchmark.java</include>
9294
<include>src/main/java/org/exist/xquery/utils/URIUtilsBenchmark.java</include>
9395
</includes>
9496

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright (C) 2014, Evolved Binary Ltd
3+
*
4+
* This file was originally ported from FusionDB to eXist-db by
5+
* Evolved Binary, for the benefit of the eXist-db Open Source community.
6+
* Only the ported code as it appears in this file, at the time that
7+
* it was contributed to eXist-db, was re-licensed under The GNU
8+
* Lesser General Public License v2.1 only for use in eXist-db.
9+
*
10+
* This license grant applies only to a snapshot of the code as it
11+
* appeared when ported, it does not offer or infer any rights to either
12+
* updates of this source code or access to the original source code.
13+
*
14+
* The GNU Lesser General Public License v2.1 only license follows.
15+
*
16+
* ---------------------------------------------------------------------
17+
*
18+
* Copyright (C) 2014, Evolved Binary Ltd
19+
*
20+
* This library is free software; you can redistribute it and/or
21+
* modify it under the terms of the GNU Lesser General Public
22+
* License as published by the Free Software Foundation; version 2.1.
23+
*
24+
* This library is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27+
* Lesser General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU Lesser General Public
30+
* License along with this library; if not, write to the Free Software
31+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32+
*/
33+
package org.exist.xquery.utils;
34+
35+
import org.openjdk.jmh.annotations.*;
36+
37+
import java.nio.charset.StandardCharsets;
38+
import java.util.ArrayList;
39+
import java.util.List;
40+
import java.util.Random;
41+
42+
/**
43+
* Benchmarks on variations of Java String Join operations.
44+
*
45+
* @author <a href="mailto:[email protected]">Adam Retter</a>
46+
*/
47+
@State(Scope.Benchmark)
48+
public class StringJoinBenchmark {
49+
50+
private final int maxStringLength = 20;
51+
52+
@Param({ "1", "2", "5", "10", "100", "1000", "10000" })
53+
private int numOfStrings;
54+
55+
private final List<String> strings = new ArrayList();
56+
57+
// @State(Scope.Thread)
58+
// public static class BuilderState {
59+
// StringBuilder builder;
60+
//
61+
// @Setup(Level.Invocation) //TODO(AR) check that Level.Invocation is correct
62+
// public void setUp() {
63+
// builder = new StringBuilder();
64+
// }
65+
// }
66+
67+
@Setup(Level.Trial)
68+
public void setUp() {
69+
final byte[] strData = new byte[maxStringLength];
70+
final Random random = new Random();
71+
for (int i = 0; i < numOfStrings; i++) {
72+
final int strLen = random.nextInt(maxStringLength) + 1;
73+
random.nextBytes(strData);
74+
strings.add(new String(strData, 0, strLen, StandardCharsets.UTF_8));
75+
}
76+
}
77+
78+
@Benchmark
79+
public StringBuilder forApproach(/*final BuilderState builderState*/) {
80+
// final StringBuilder builder = builderState.builder;
81+
final StringBuilder builder = new StringBuilder();
82+
83+
for (int i = 0; i < strings.size(); i++) {
84+
if (i > 0) {
85+
builder.append(", ");
86+
}
87+
builder.append(strings.get(i));
88+
}
89+
return builder;
90+
}
91+
92+
@Benchmark
93+
public StringBuilder forEachApproach(/*final BuilderState builderState*/) {
94+
// final StringBuilder builder = builderState.builder;
95+
final StringBuilder builder = new StringBuilder();
96+
97+
boolean firstArgument = true;
98+
for (final String str : strings) {
99+
builder.append(str);
100+
if (firstArgument) {
101+
firstArgument = false;
102+
} else {
103+
builder.append(", ");
104+
}
105+
}
106+
return builder;
107+
}
108+
109+
@Benchmark
110+
public String jdkApproach() {
111+
return String.join(", ", strings);
112+
}
113+
114+
public static void main(final String args[]) {
115+
// NOTE: just for running with the java debugger
116+
final StringJoinBenchmark stringJoinBenchmark = new StringJoinBenchmark();
117+
// stringJoinBenchmark.forApproach();
118+
// stringJoinBenchmark.forEachApproach();
119+
stringJoinBenchmark.jdkApproach();
120+
}
121+
}

0 commit comments

Comments
 (0)