This commit is contained in:
Gary Gregory 2021-07-13 10:50:23 -04:00
commit 3627c5ae83
1 changed files with 82 additions and 66 deletions

View File

@ -18,17 +18,20 @@
package org.apache.commons.csv; package org.apache.commons.csv;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.Iterator;
import java.util.Scanner;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.zip.GZIPInputStream; import java.util.zip.GZIPInputStream;
import com.generationjava.io.CsvReader; import com.generationjava.io.CsvReader;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReaderBuilder;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.Benchmark;
@ -62,124 +65,140 @@ public class CSVBenchmark {
*/ */
@Setup @Setup
public void init() throws IOException { public void init() throws IOException {
final File file = new File("src/test/resources/perf/worldcitiespop.txt.gz"); InputStream in = this.getClass().getClassLoader().getResourceAsStream(
final InputStream in = new GZIPInputStream(new FileInputStream(file)); "org/apache/commons/csv/perf/worldcitiespop.txt.gz");
this.data = IOUtils.toString(in, StandardCharsets.ISO_8859_1); try (final InputStream gzin = new GZIPInputStream(in, 8192)) {
in.close(); this.data = IOUtils.toString(gzin, StandardCharsets.ISO_8859_1);
}
} }
private BufferedReader getReader() { private Reader getReader() {
return new BufferedReader(new StringReader(data)); return new StringReader(data);
} }
@Benchmark @Benchmark
public int read(final Blackhole bh) throws Exception { public int read(final Blackhole bh) throws Exception {
final BufferedReader in = getReader();
int count = 0; int count = 0;
String line;
while ((line = in.readLine()) != null) { try (BufferedReader reader = new BufferedReader(getReader())) {
count++; while (reader.readLine() != null) {
count++;
}
}
bh.consume(count);
return count;
}
@Benchmark
public int scan(final Blackhole bh) throws Exception {
int count = 0;
try (Scanner scanner = new Scanner(getReader())) {
while (scanner.hasNextLine()) {
scanner.nextLine();
count++;
}
} }
bh.consume(count); bh.consume(count);
in.close();
return count; return count;
} }
@Benchmark @Benchmark
public int split(final Blackhole bh) throws Exception { public int split(final Blackhole bh) throws Exception {
final BufferedReader in = getReader(); int count = 0;
int count = 0;
String line; try (BufferedReader reader = new BufferedReader(getReader())) {
while ((line = in.readLine()) != null) { String line;
while ((line = reader.readLine()) != null) {
final String[] values = StringUtils.split(line, ','); final String[] values = StringUtils.split(line, ',');
count += values.length; count += values.length;
} }
}
bh.consume(count); bh.consume(count);
in.close(); return count;
return count;
} }
@Benchmark @Benchmark
public int parseCommonsCSV(final Blackhole bh) throws Exception { public int parseCommonsCSV(final Blackhole bh) throws Exception {
final BufferedReader in = getReader();
final CSVFormat format = CSVFormat.DEFAULT.withHeader();
int count = 0; int count = 0;
for (final CSVRecord record : format.parse(in)) {
count++; try (final Reader in = getReader()) {
final CSVFormat format = CSVFormat.Builder.create().setSkipHeaderRecord(true).build();
Iterator<CSVRecord> iter = format.parse(in).iterator();
while (iter.hasNext()) {
count++;
iter.next();
}
} }
bh.consume(count); bh.consume(count);
in.close();
return count; return count;
} }
@Benchmark @Benchmark
public int parseGenJavaCSV(final Blackhole bh) throws Exception { public int parseGenJavaCSV(final Blackhole bh) throws Exception {
final BufferedReader in = getReader();
final CsvReader reader = new CsvReader(in);
reader.setFieldDelimiter(',');
int count = 0; int count = 0;
String[] record = null;
while ((record = reader.readLine()) != null) { try (final Reader in = getReader()) {
count++; final CsvReader reader = new CsvReader(in);
reader.setFieldDelimiter(',');
while (reader.readLine() != null) {
count++;
}
} }
bh.consume(count); bh.consume(count);
in.close();
return count; return count;
} }
@Benchmark @Benchmark
public int parseJavaCSV(final Blackhole bh) throws Exception { public int parseJavaCSV(final Blackhole bh) throws Exception {
final BufferedReader in = getReader();
final com.csvreader.CsvReader reader = new com.csvreader.CsvReader(in, ',');
reader.setRecordDelimiter('\n');
int count = 0; int count = 0;
while (reader.readRecord()) {
count++; try (final Reader in = getReader()) {
final com.csvreader.CsvReader reader = new com.csvreader.CsvReader(in, ',');
reader.setRecordDelimiter('\n');
while (reader.readRecord()) {
count++;
}
} }
bh.consume(count); bh.consume(count);
in.close();
return count; return count;
} }
@Benchmark @Benchmark
public int parseOpenCSV(final Blackhole bh) throws Exception { public int parseOpenCSV(final Blackhole bh) throws Exception {
final BufferedReader in = getReader();
final com.opencsv.CSVReader reader = new com.opencsv.CSVReader(in, ',');
int count = 0; int count = 0;
while (reader.readNext() != null) {
count++; final com.opencsv.CSVParser parser = new CSVParserBuilder()
.withSeparator(',').withIgnoreQuotations(true).build();
try (final Reader in = getReader()) {
final com.opencsv.CSVReader reader = new CSVReaderBuilder(in).withSkipLines(1).withCSVParser(parser).build();
while (reader.readNext() != null) {
count++;
}
} }
bh.consume(count); bh.consume(count);
in.close();
return count; return count;
} }
@Benchmark @Benchmark
public int parseSkifeCSV(final Blackhole bh) throws Exception { public int parseSkifeCSV(final Blackhole bh) throws Exception {
final BufferedReader in = getReader();
final org.skife.csv.CSVReader reader = new org.skife.csv.SimpleReader(); final org.skife.csv.CSVReader reader = new org.skife.csv.SimpleReader();
reader.setSeperator(','); reader.setSeperator(',');
final CountingReaderCallback callback = new CountingReaderCallback(); final CountingReaderCallback callback = new CountingReaderCallback();
reader.parse(in, callback);
try (final Reader in = getReader()) {
reader.parse(in, callback);
}
bh.consume(callback); bh.consume(callback);
in.close();
return callback.count; return callback.count;
} }
@ -194,18 +213,15 @@ public class CSVBenchmark {
@Benchmark @Benchmark
public int parseSuperCSV(final Blackhole bh) throws Exception { public int parseSuperCSV(final Blackhole bh) throws Exception {
final BufferedReader in = getReader();
final CsvListReader reader = new CsvListReader(in, CsvPreference.STANDARD_PREFERENCE);
int count = 0; int count = 0;
List<String> record = null;
while ((record = reader.read()) != null) { try (final CsvListReader reader = new CsvListReader(getReader(), CsvPreference.STANDARD_PREFERENCE)) {
count++; while (reader.read() != null) {
count++;
}
} }
bh.consume(count); bh.consume(count);
in.close();
return count; return count;
} }
} }