|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in all |
| 12 | + * copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | +package com.github.packageurl; |
| 23 | + |
| 24 | +import java.nio.charset.StandardCharsets; |
| 25 | +import java.util.Random; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import org.openjdk.jmh.annotations.Benchmark; |
| 28 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 29 | +import org.openjdk.jmh.annotations.Mode; |
| 30 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 31 | +import org.openjdk.jmh.annotations.Param; |
| 32 | +import org.openjdk.jmh.annotations.Scope; |
| 33 | +import org.openjdk.jmh.annotations.Setup; |
| 34 | +import org.openjdk.jmh.annotations.State; |
| 35 | +import org.openjdk.jmh.infra.Blackhole; |
| 36 | + |
| 37 | +/** |
| 38 | + * Measures the performance of performance decoding and encoding. |
| 39 | + * <p> |
| 40 | + * Run the benchmark with: |
| 41 | + * </p> |
| 42 | + * <pre> |
| 43 | + * mvn -Pbenchmark |
| 44 | + * </pre> |
| 45 | + */ |
| 46 | +@BenchmarkMode(Mode.AverageTime) |
| 47 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 48 | +@State(Scope.Benchmark) |
| 49 | +public class PercentEncodingBenchmark { |
| 50 | + |
| 51 | + private static final int DATA_COUNT = 1000; |
| 52 | + private static final int DECODED_LENGTH = 256; |
| 53 | + private static final byte[] UNRESERVED = |
| 54 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~".getBytes(StandardCharsets.US_ASCII); |
| 55 | + |
| 56 | + @Param({"0", "0.1", "0.5"}) |
| 57 | + private double nonAsciiProb; |
| 58 | + |
| 59 | + private String[] decodedData = createDecodedData(); |
| 60 | + private String[] encodedData = encodeData(decodedData); |
| 61 | + |
| 62 | + @Setup |
| 63 | + public void setup() { |
| 64 | + decodedData = createDecodedData(); |
| 65 | + encodedData = encodeData(encodedData); |
| 66 | + } |
| 67 | + |
| 68 | + private String[] createDecodedData() { |
| 69 | + Random random = new Random(); |
| 70 | + String[] decodedData = new String[DATA_COUNT]; |
| 71 | + for (int i = 0; i < DATA_COUNT; i++) { |
| 72 | + char[] chars = new char[DECODED_LENGTH]; |
| 73 | + for (int j = 0; j < DECODED_LENGTH; j++) { |
| 74 | + if (random.nextDouble() < nonAsciiProb) { |
| 75 | + chars[j] = (char) (Byte.MAX_VALUE + 1 + random.nextInt(Short.MAX_VALUE - Byte.MAX_VALUE - 1)); |
| 76 | + } else { |
| 77 | + chars[j] = (char) UNRESERVED[random.nextInt(UNRESERVED.length)]; |
| 78 | + } |
| 79 | + } |
| 80 | + decodedData[i] = new String(chars); |
| 81 | + } |
| 82 | + return decodedData; |
| 83 | + } |
| 84 | + |
| 85 | + private static String[] encodeData(String[] decodedData) { |
| 86 | + String[] encodedData = new String[decodedData.length]; |
| 87 | + for (int i = 0; i < decodedData.length; i++) { |
| 88 | + encodedData[i] = PackageURL.percentEncode(decodedData[i]); |
| 89 | + } |
| 90 | + return encodedData; |
| 91 | + } |
| 92 | + |
| 93 | + @Benchmark |
| 94 | + public void baseline(Blackhole blackhole) { |
| 95 | + for (int i = 0; i < DATA_COUNT; i++) { |
| 96 | + byte[] buffer = decodedData[i].getBytes(StandardCharsets.UTF_8); |
| 97 | + // Change the String a little bit |
| 98 | + for (int idx = 0; idx < buffer.length; idx++) { |
| 99 | + byte b = buffer[idx]; |
| 100 | + if ('a' <= b && b <= 'z') { |
| 101 | + buffer[idx] = (byte) (b & 0x20); |
| 102 | + } |
| 103 | + } |
| 104 | + blackhole.consume(new String(buffer, StandardCharsets.UTF_8)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Benchmark |
| 109 | + public void percentDecode(final Blackhole blackhole) { |
| 110 | + for (int i = 0; i < DATA_COUNT; i++) { |
| 111 | + blackhole.consume(PackageURL.percentDecode(encodedData[i])); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Benchmark |
| 116 | + public void percentEncode(final Blackhole blackhole) { |
| 117 | + for (int i = 0; i < DATA_COUNT; i++) { |
| 118 | + blackhole.consume(PackageURL.percentEncode(decodedData[i])); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments