|
| 1 | +/* |
| 2 | + * Copyright 2002-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.util; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Random; |
| 24 | + |
| 25 | +import org.openjdk.jmh.annotations.Benchmark; |
| 26 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 27 | +import org.openjdk.jmh.annotations.Level; |
| 28 | +import org.openjdk.jmh.annotations.Mode; |
| 29 | +import org.openjdk.jmh.annotations.Param; |
| 30 | +import org.openjdk.jmh.annotations.Scope; |
| 31 | +import org.openjdk.jmh.annotations.Setup; |
| 32 | +import org.openjdk.jmh.annotations.State; |
| 33 | +import org.openjdk.jmh.infra.Blackhole; |
| 34 | + |
| 35 | +/** |
| 36 | + * Benchmarks for {@link StringUtils}. |
| 37 | + * |
| 38 | + * @author Brian Clozel |
| 39 | + */ |
| 40 | +@BenchmarkMode(Mode.Throughput) |
| 41 | +public class StringUtilsBenchmark { |
| 42 | + |
| 43 | + @Benchmark |
| 44 | + public void collectionToDelimitedString(DelimitedStringState state, Blackhole bh) { |
| 45 | + bh.consume(StringUtils.collectionToCommaDelimitedString(state.elements)); |
| 46 | + } |
| 47 | + |
| 48 | + @State(Scope.Benchmark) |
| 49 | + public static class DelimitedStringState { |
| 50 | + |
| 51 | + @Param("10") |
| 52 | + int elementMinSize; |
| 53 | + |
| 54 | + @Param("20") |
| 55 | + int elementMaxSize; |
| 56 | + |
| 57 | + @Param("10") |
| 58 | + int elementCount; |
| 59 | + |
| 60 | + Collection<String> elements; |
| 61 | + |
| 62 | + @Setup(Level.Iteration) |
| 63 | + public void setup() { |
| 64 | + Random random = new Random(); |
| 65 | + this.elements = new ArrayList<>(this.elementCount); |
| 66 | + int bound = this.elementMaxSize - this.elementMinSize; |
| 67 | + for (int i = 0; i < this.elementCount; i++) { |
| 68 | + this.elements.add(String.format("%0" + (random.nextInt(bound) + this.elementMinSize) + "d", 1)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Benchmark |
| 74 | + public void cleanPath(CleanPathState state, Blackhole bh) { |
| 75 | + for (String path : state.paths) { |
| 76 | + bh.consume(StringUtils.cleanPath(path)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @State(Scope.Benchmark) |
| 81 | + public static class CleanPathState { |
| 82 | + |
| 83 | + private static final List<String> SEGMENTS = Arrays.asList("some", "path", ".", "..", "springspring"); |
| 84 | + |
| 85 | + @Param("10") |
| 86 | + int segmentCount; |
| 87 | + |
| 88 | + @Param("20") |
| 89 | + int pathsCount; |
| 90 | + |
| 91 | + Collection<String> paths; |
| 92 | + |
| 93 | + @Setup(Level.Iteration) |
| 94 | + public void setup() { |
| 95 | + this.paths = new ArrayList<>(this.pathsCount); |
| 96 | + Random random = new Random(); |
| 97 | + for (int i = 0; i < this.pathsCount; i++) { |
| 98 | + this.paths.add(createSamplePath(random)); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private String createSamplePath(Random random) { |
| 103 | + String separator = random.nextBoolean() ? "/" : "\\"; |
| 104 | + StringBuilder sb = new StringBuilder(); |
| 105 | + sb.append("jar:file:///c:"); |
| 106 | + for (int i = 0; i < this.segmentCount; i++) { |
| 107 | + sb.append(separator); |
| 108 | + sb.append(SEGMENTS.get(random.nextInt(SEGMENTS.size()))); |
| 109 | + } |
| 110 | + sb.append(separator); |
| 111 | + sb.append("the%20file.txt"); |
| 112 | + return sb.toString(); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | +} |
0 commit comments