Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
CSV-284: Formalize PerformanceTest
  • Loading branch information
belugabehr committed Jul 15, 2021
commit 63e93022968bb6285328cdbeffbbd1de8f2b305d
12 changes: 12 additions & 0 deletions BENCHMARK.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ mvn test -Pbenchmark -Dbenchmark=<name>
# Example of running basic "read" benchmark
mvn test -Pbenchmark -Dbenchmark=read
```

Performance Test
-------------

Apache Commons CSV includes a stand-alone performance test which only covers commons-csv.

```shell
# Run the performance test
mvn test -Dtest=PerformanceTest
```

> :warning: This performance test does not use JMH; it uses simple timing metrics.
34 changes: 12 additions & 22 deletions src/test/java/org/apache/commons/csv/perf/PerformanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
Expand All @@ -46,6 +45,7 @@ public class PerformanceTest {

private final int max = 10;

private static final String TEST_RESRC = "org/apache/commons/csv/perf/worldcitiespop.txt.gz";
private static final File BIG_FILE = new File(System.getProperty("java.io.tmpdir"), "worldcitiespop.txt");

@BeforeAll
Expand All @@ -54,10 +54,10 @@ public static void setUpClass() throws FileNotFoundException, IOException {
System.out.println(String.format("Found test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
return;
}
System.out.println("Decompressing test fixture " + BIG_FILE + "...");
System.out.println("Decompressing test fixture to: " + BIG_FILE + "...");
try (
final InputStream input = new GZIPInputStream(
new FileInputStream("src/test/resources/perf/worldcitiespop.txt.gz"));
PerformanceTest.class.getClassLoader().getResourceAsStream(TEST_RESRC));
final OutputStream output = new FileOutputStream(BIG_FILE)) {
IOUtils.copy(input, output);
System.out.println(String.format("Decompressed test fixture %s: %,d bytes.", BIG_FILE, BIG_FILE.length()));
Expand Down Expand Up @@ -85,24 +85,12 @@ private long parse(final Reader reader, final boolean traverseColumns) throws IO
return recordCount;
}

private void println(final String s) {
System.out.println(s);
}

private long readAll(final BufferedReader in) throws IOException {
Comment thread
belugabehr marked this conversation as resolved.
long count = 0;
while (in.readLine() != null) {
count++;
}
return count;
}

public long testParseBigFile(final boolean traverseColumns) throws Exception {
private long testParseBigFile(final boolean traverseColumns) throws Exception {
final long startMillis = System.currentTimeMillis();
try (final BufferedReader reader = this.createBufferedReader()) {
final long count = this.parse(reader, traverseColumns);
final long totalMillis = System.currentTimeMillis() - startMillis;
this.println(
System.out.println(
String.format("File parsed in %,d milliseconds with Commons CSV: %,d lines.", totalMillis, count));
return totalMillis;
}
Expand All @@ -114,23 +102,25 @@ public void testParseBigFileRepeat() throws Exception {
for (int i = 0; i < this.max; i++) {
bestTime = Math.min(this.testParseBigFile(false), bestTime);
}
this.println(String.format("Best time out of %,d is %,d milliseconds.", this.max, bestTime));
System.out.println(String.format("Best time out of %,d is %,d milliseconds.", this.max, bestTime));
}

@Test
public void testReadBigFile() throws Exception {
long bestTime = Long.MAX_VALUE;
long count;
long count = 0L;
for (int i = 0; i < this.max; i++) {
final long startMillis;
try (final BufferedReader in = this.createBufferedReader()) {
startMillis = System.currentTimeMillis();
count = this.readAll(in);
while (in.readLine() != null) {
count++;
}
}
final long totalMillis = System.currentTimeMillis() - startMillis;
bestTime = Math.min(totalMillis, bestTime);
this.println(String.format("File read in %,d milliseconds: %,d lines.", totalMillis, count));
System.out.println(String.format("File read in %,d milliseconds: %,d lines.", totalMillis, count));
}
this.println(String.format("Best time out of %,d is %,d milliseconds.", this.max, bestTime));
System.out.println(String.format("Best time out of %,d is %,d milliseconds.", this.max, bestTime));
}
}