Skip to content

Commit d6d521a

Browse files
committed
Fix benchmark
Add license header
1 parent e680120 commit d6d521a

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@
135135
<version>${jmh.version}</version>
136136
<scope>test</scope>
137137
</dependency>
138+
<dependency>
139+
<groupId>org.apache.commons</groupId>
140+
<artifactId>commons-csv</artifactId>
141+
<version>1.14.1</version>
142+
<scope>test</scope>
143+
</dependency>
138144
</dependencies>
139145
<build>
140146
<defaultGoal>clean verify apache-rat:check japicmp:cmp checkstyle:check pmd:check spotbugs:check javadoc:javadoc</defaultGoal>
@@ -149,7 +155,6 @@
149155
<inputExclude>src/site/resources/download_text.cgi</inputExclude>
150156
<inputExclude>src/site/resources/release-notes/RELEASE-NOTES-*.txt</inputExclude>
151157
<inputExclude>src/test/resources/org/apache/commons/text/stringEscapeUtilsTestData.txt</inputExclude>
152-
<inputExclude>src/test/resources/org/apache/commons/text/lcs-perf-analysis-inputs.csv</inputExclude>
153158
<inputExclude>src/test/resources/org/apache/commons/text/oss-fuzz/**</inputExclude>
154159
</inputExcludes>
155160
</configuration>

src/test/java/org/apache/commons/text/jmh/LongestCommonSubsequencePerformance.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
*/
1717
package org.apache.commons.text.jmh;
1818

19-
import java.io.BufferedReader;
2019
import java.io.IOException;
21-
import java.io.InputStream;
2220
import java.io.InputStreamReader;
23-
import java.io.UncheckedIOException;
2421
import java.util.ArrayList;
2522
import java.util.List;
2623
import java.util.Objects;
2724
import java.util.concurrent.TimeUnit;
2825

26+
import org.apache.commons.csv.CSVFormat;
2927
import org.apache.commons.lang3.tuple.ImmutablePair;
3028
import org.apache.commons.lang3.tuple.Pair;
3129
import org.apache.commons.text.similarity.LongestCommonSubsequence;
@@ -114,22 +112,21 @@ public static class InputData {
114112
final List<Pair<CharSequence, CharSequence>> inputs = new ArrayList<>();
115113

116114
@Setup(Level.Trial)
117-
public void setup() {
115+
public void setup() throws IOException {
118116
final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
119-
try (InputStream is = classLoader.getResourceAsStream("org/apache/commons/text/lcs-perf-analysis-inputs.csv");
120-
InputStreamReader isr = new InputStreamReader(Objects.requireNonNull(is));
121-
BufferedReader br = new BufferedReader(isr)) {
122-
String line;
123-
while ((line = br.readLine()) != null && !line.trim().isEmpty()) {
124-
line = line.trim();
125-
final int indexOfComma = line.indexOf(',');
126-
final String inputA = line.substring(0, indexOfComma);
127-
final String inputB = line.substring(1 + indexOfComma);
128-
this.inputs.add(ImmutablePair.of(inputA, inputB));
129-
}
130-
} catch (final IOException exception) {
131-
throw new UncheckedIOException(exception.getMessage(), exception);
132-
}
117+
CSVFormat.DEFAULT.builder().setCommentMarker('#').setTrim(true).get()
118+
.parse(new InputStreamReader(
119+
Objects.requireNonNull(classLoader.getResourceAsStream("org/apache/commons/text/lcs-perf-analysis-inputs.csv"))))
120+
.forEach(record -> {
121+
final String line = record.get(0);
122+
final int indexOfComma = line.indexOf(',');
123+
if (indexOfComma < 0) {
124+
throw new IllegalStateException("Invalid input line: " + line);
125+
}
126+
final String inputA = line.substring(0, indexOfComma);
127+
final String inputB = line.substring(1 + indexOfComma);
128+
this.inputs.add(ImmutablePair.of(inputA, inputB));
129+
});
133130
}
134131
}
135132

src/test/resources/org/apache/commons/text/lcs-perf-analysis-inputs.csv

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
"This is test data for a JMH (the Java Microbenchmark Harness) which is used from the class org.apache.commons.text.jmh.LongestCommonSubsequencePerformance."
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. 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+
"This is test data for a JMH (the Java Microbenchmark Harness), which is used from the class org.apache.commons.text.jmh.LongestCommonSubsequencePerformance."
216
"The quick brown fox jumps over the lazy dog. A man, a plan, a canal, Panama. Was it a car or a cat I saw? Step on no pets. Rats live on no evil star."
317
"Here, the field iterations will be populated with appropriate values from the @Param annotation by the JMH when it is passed to the benchmark method. The @Setup annotated method is invoked before each invocation of the benchmark and creates a new Hasher ensuring isolation. When the execution is finished, we'll get a result similar to the one below: When running microbenchmarks, it's very important to be aware of optimizations. Otherwise, they may affect the","benchmark results in a very misleading way. To make matters a bit more concrete, let's consider an example: We expect object allocation costs more than doing nothing at all. However, if we run the benchmarks: Apparently finding a place in the TLAB, creating and initializing an object is almost free! Just by looking at these numbers, we should know that something does not quite add up here."
418
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. This code is free software; you can redistribute it and/or modify it published by the Free Software Foundation. Oracle designates this This code is distributed in the hope that it will be useful, but WITHOUT FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License You should have received a copy of the GNU General Public License version","Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? under the terms of the GNU General Public License version 2 only, as particular file as subject to the *Classpath* exception as provided ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or version 2 for more details (a copy is included in the LICENSE file that 2 along with this work; if not, write to the Free Software Foundation,"

0 commit comments

Comments
 (0)