diff --git a/lib/src/jmh/java/de/siegmar/fastcsv/CsvConstants.java b/lib/src/jmh/java/de/siegmar/fastcsv/CsvConstants.java index 9123c279..badf11b0 100644 --- a/lib/src/jmh/java/de/siegmar/fastcsv/CsvConstants.java +++ b/lib/src/jmh/java/de/siegmar/fastcsv/CsvConstants.java @@ -1,26 +1,40 @@ package de.siegmar.fastcsv; +import java.util.List; + final class CsvConstants { - /** - * Data to write CSV. - */ - static final String[] RECORD = { + public static final List RECORD_1 = List.of( "Simple field", + "", "Example with separator ,", "Example with delimiter \"", "Example with\nnewline", - "Example with , and \" and \nnewline", - }; + "Example with , and \" and \nnewline" + ); + + public static final List RECORD_2 = List.of( + "Example row with only", + "two columns" + ); + + /** + * Data to write CSV. + */ + public static final List> RECORDS = List.of(RECORD_1, RECORD_2); /** * Data to read CSV. */ - static final String DATA = "Simple field," + public static final String DATA = "Simple field," + + "," + "\"Example with separator ,\"," + "\"Example with delimiter \"\"\"," + "\"Example with\nnewline\"," + "\"Example with , and \"\" and \nnewline\"" + + "\n" + + "Example row with only," + + "two columns" + "\n"; private CsvConstants() { diff --git a/lib/src/jmh/java/de/siegmar/fastcsv/FastCsvWriteBenchmark.java b/lib/src/jmh/java/de/siegmar/fastcsv/FastCsvWriteBenchmark.java index 105de04b..27b7da26 100644 --- a/lib/src/jmh/java/de/siegmar/fastcsv/FastCsvWriteBenchmark.java +++ b/lib/src/jmh/java/de/siegmar/fastcsv/FastCsvWriteBenchmark.java @@ -16,12 +16,13 @@ public class FastCsvWriteBenchmark { @Benchmark public void write(final WriteState state) { - state.writer.writeRecord(CsvConstants.RECORD); + state.writer.writeRecord(state.rowSupplier.get()); } @State(Scope.Benchmark) public static class WriteState { + private final RowSupplier rowSupplier = new RowSupplier(CsvConstants.RECORDS); private CsvWriter writer; @Setup diff --git a/lib/src/jmh/java/de/siegmar/fastcsv/NullWriter.java b/lib/src/jmh/java/de/siegmar/fastcsv/NullWriter.java index c999e4d4..05b6bd9f 100644 --- a/lib/src/jmh/java/de/siegmar/fastcsv/NullWriter.java +++ b/lib/src/jmh/java/de/siegmar/fastcsv/NullWriter.java @@ -10,51 +10,28 @@ */ public class NullWriter extends Writer { - private static final int BUFFER_SIZE = 8192; - private final Blackhole bh; - private final char[] buf = new char[BUFFER_SIZE]; - private int pos; - NullWriter(final Blackhole bh) { + /** + * Initializes a new instance of the {@link NullWriter} class. + * + * @param bh the black hole to send all data to + */ + public NullWriter(final Blackhole bh) { this.bh = bh; } - @Override - public void write(final int c) { - if (pos == buf.length) { - flush(); - } - buf[pos++] = (char) c; - } - @Override public void write(final char[] cbuf, final int off, final int len) { - if (len + pos > buf.length) { - flush(); - } - System.arraycopy(cbuf, off, buf, pos, len); - pos += len; - } - - @Override - public void write(final String str, final int off, final int len) { - if (len + pos > buf.length) { - flush(); - } - str.getChars(off, off + len, buf, pos); - pos += len; + bh.consume(Arrays.copyOfRange(cbuf, off, off + len)); } @Override public void flush() { - bh.consume(Arrays.copyOf(buf, pos)); - pos = 0; } @Override public void close() { - flush(); } } diff --git a/lib/src/jmh/java/de/siegmar/fastcsv/RowSupplier.java b/lib/src/jmh/java/de/siegmar/fastcsv/RowSupplier.java new file mode 100644 index 00000000..dd7aac0e --- /dev/null +++ b/lib/src/jmh/java/de/siegmar/fastcsv/RowSupplier.java @@ -0,0 +1,34 @@ +package de.siegmar.fastcsv; + +import java.util.List; +import java.util.function.Supplier; + +/** + * This class is used to supply rows for the JMH benchmarks. + */ +public class RowSupplier implements Supplier> { + + private final List> rows; + private int pos; + + /** + * Initializes a new instance of the {@link RowSupplier} class + * with the specified rows. + * + * @param rows the rows to supply + */ + public RowSupplier(final List> rows) { + this.rows = List.copyOf(rows); + pos = rows.size(); + } + + @Override + public List get() { + final List d = rows.get(--pos); + if (pos == 0) { + pos = rows.size(); + } + return d; + } + +}