Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add picocsv benchmark #4

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions picocsv/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dependencies {
jmh(rootProject)
implementation("com.github.nbbrd.picocsv:picocsv:2.4.0")
}
66 changes: 66 additions & 0 deletions picocsv/src/jmh/java/picocsv/PicocsvBenchmark.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package picocsv;

import java.io.IOException;
import java.util.Collection;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.TearDown;
import org.openjdk.jmh.infra.Blackhole;

import de.siegmar.csvbenchmark.CsvConstants;
import de.siegmar.csvbenchmark.ICsvReader;
import de.siegmar.csvbenchmark.ICsvWriter;
import de.siegmar.csvbenchmark.util.NullWriter;
import de.siegmar.csvbenchmark.util.RowSupplier;

public class PicocsvBenchmark {

@State(Scope.Benchmark)
public static class WriteState {

private final RowSupplier rowSupplier = new RowSupplier(CsvConstants.RECORDS);
private ICsvWriter writer;

@Setup
public void setup(final Blackhole bh) throws IOException {
writer = Factory.writer(new NullWriter(bh));
}

@TearDown
public void teardown() throws IOException {
writer.close();
}

}

@Benchmark
public void write(final WriteState state) throws Exception {
state.writer.writeRecord(state.rowSupplier.get());
}

@State(Scope.Benchmark)
public static class ReadState {

private ICsvReader reader;

@Setup
public void setup() throws IOException {
reader = Factory.reader();
}

@TearDown
public void teardown() throws IOException {
reader.close();
}

}

@Benchmark
public Collection<String> read(final ReadState state) throws Exception {
return state.reader.readRecord();
}

}
76 changes: 76 additions & 0 deletions picocsv/src/main/java/picocsv/Factory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package picocsv;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import de.siegmar.csvbenchmark.CsvConstants;
import de.siegmar.csvbenchmark.ICsvReader;
import de.siegmar.csvbenchmark.ICsvWriter;
import de.siegmar.csvbenchmark.util.InfiniteDataReader;
import nbbrd.picocsv.Csv;

public final class Factory {

private static final Csv.Format FORMAT = Csv.Format.DEFAULT
.toBuilder()
.delimiter(CsvConstants.SEPARATOR)
.separator(Csv.Format.UNIX_SEPARATOR)
.quote(CsvConstants.DELIMITER)
.build();

// Read performances seems highly related to the buffer size
// Slower: the default buf size (8192) is too big for the benchmark data
private static final int BUF_SIZE_SLOWER = Csv.DEFAULT_CHAR_BUFFER_SIZE;
// Equivalent: a random low buf size seems efficient
private static final int BUF_SIZE_EQUIVALENT = 200;
// Faster: a perfect buf size (163) aligns the stars and performs very well
private static final int BUF_SIZE_FASTER = CsvConstants.DATA.length();

private Factory() {
}

public static ICsvReader reader() throws IOException {
return new ICsvReader() {
private final Csv.Reader csvReader = Csv.Reader.of(FORMAT, Csv.ReaderOptions.DEFAULT,
new InfiniteDataReader(CsvConstants.DATA), BUF_SIZE_SLOWER);

@Override
public List<String> readRecord() throws IOException {
final List<String> result = new ArrayList<>();
if (csvReader.readLine()) {
while (csvReader.readField()) {
result.add(csvReader.toString());
}
}
return result;
}

@Override
public void close() throws IOException {
csvReader.close();
}
};
}

public static ICsvWriter writer(final Writer writer) throws IOException {
return new ICsvWriter() {
private final Csv.Writer csvWriter = Csv.Writer.of(FORMAT, Csv.WriterOptions.DEFAULT,
writer, Csv.DEFAULT_CHAR_BUFFER_SIZE);

@Override
public void writeRecord(final List<String> fields) throws IOException {
for (final String field : fields) {
csvWriter.writeField(field);
}
csvWriter.writeEndOfLine();
}

@Override
public void close() throws IOException {
csvWriter.close();
}
};
}
}
37 changes: 37 additions & 0 deletions picocsv/src/test/java/picocsv/FormatTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package picocsv;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.StringWriter;
import java.util.List;

import org.junit.jupiter.api.Test;

import de.siegmar.csvbenchmark.CsvConstants;
import de.siegmar.csvbenchmark.ICsvReader;
import de.siegmar.csvbenchmark.ICsvWriter;

public class FormatTest {

@Test
public void reader() throws Exception {
try (ICsvReader reader = Factory.reader()) {
for (final List<String> row : CsvConstants.RECORDS) {
assertEquals(row, reader.readRecord());
}
}
}

@Test
public void writer() throws Exception {
final StringWriter sw = new StringWriter();
try (ICsvWriter writer = Factory.writer(sw)) {
for (final List<String> row : CsvConstants.RECORDS) {
writer.writeRecord(row);
}
}

assertEquals(CsvConstants.DATA, sw.toString());
}

}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include("fastcsv")
include("jackson")
include("javacsv")
include("opencsv")
include("picocsv")
include("simpleflatmapper")
include("supercsv")
include("univocity")
Loading