Skip to content

Commit b142089

Browse files
committed
feat: Improve benchmark (package-url#222)
Fixes a bug in the benchmark initialization and adds a `toLowerCase` benchmark.
1 parent d5f136c commit b142089

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

src/test/java/com/github/packageurl/utils/StringUtilBenchmark.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
*/
2222
package com.github.packageurl.utils;
2323

24+
import com.github.packageurl.PackageURL;
2425
import java.nio.charset.StandardCharsets;
26+
import java.util.Locale;
2527
import java.util.Random;
2628
import java.util.concurrent.TimeUnit;
2729
import org.openjdk.jmh.annotations.Benchmark;
@@ -30,7 +32,6 @@
3032
import org.openjdk.jmh.annotations.OutputTimeUnit;
3133
import org.openjdk.jmh.annotations.Param;
3234
import org.openjdk.jmh.annotations.Scope;
33-
import org.openjdk.jmh.annotations.Setup;
3435
import org.openjdk.jmh.annotations.State;
3536
import org.openjdk.jmh.infra.Blackhole;
3637

@@ -62,14 +63,8 @@ public class StringUtilBenchmark {
6263
@Param({"0", "0.1", "0.5"})
6364
private double nonAsciiProb;
6465

65-
private String[] decodedData = createDecodedData();
66-
private String[] encodedData = encodeData(decodedData);
67-
68-
@Setup
69-
public void setup() {
70-
decodedData = createDecodedData();
71-
encodedData = encodeData(encodedData);
72-
}
66+
private final String[] decodedData = createDecodedData();
67+
private final String[] encodedData = encodeData(decodedData);
7368

7469
private String[] createDecodedData() {
7570
Random random = new Random();
@@ -92,6 +87,9 @@ private static String[] encodeData(String[] decodedData) {
9287
String[] encodedData = new String[decodedData.length];
9388
for (int i = 0; i < decodedData.length; i++) {
9489
encodedData[i] = StringUtil.percentEncode(decodedData[i]);
90+
if (!StringUtil.percentDecode(encodedData[i]).equals(decodedData[i])) {
91+
throw new RuntimeException("Invalid implementation of `percentEncode` and `percentDecode`.");
92+
}
9593
}
9694
return encodedData;
9795
}
@@ -100,17 +98,28 @@ private static String[] encodeData(String[] decodedData) {
10098
public void baseline(Blackhole blackhole) {
10199
for (int i = 0; i < DATA_COUNT; i++) {
102100
byte[] buffer = decodedData[i].getBytes(StandardCharsets.UTF_8);
103-
// Change the String a little bit
101+
// Prevent JIT compiler from assuming the buffer was not modified
104102
for (int idx = 0; idx < buffer.length; idx++) {
105-
byte b = buffer[idx];
106-
if ('a' <= b && b <= 'z') {
107-
buffer[idx] = (byte) (b & 0x20);
108-
}
103+
buffer[idx] ^= 0x20;
109104
}
110105
blackhole.consume(new String(buffer, StandardCharsets.UTF_8));
111106
}
112107
}
113108

109+
@Benchmark
110+
public void toLowerCaseJre(Blackhole blackhole) {
111+
for (int i = 0; i < DATA_COUNT; i++) {
112+
blackhole.consume(decodedData[i].toLowerCase(Locale.ROOT));
113+
}
114+
}
115+
116+
@Benchmark
117+
public void toLowerCase(Blackhole blackhole) {
118+
for (int i = 0; i < DATA_COUNT; i++) {
119+
blackhole.consume(StringUtil.toLowerCase(decodedData[i]));
120+
}
121+
}
122+
114123
@Benchmark
115124
public void percentDecode(final Blackhole blackhole) {
116125
for (int i = 0; i < DATA_COUNT; i++) {

0 commit comments

Comments
 (0)