Skip to content

Commit

Permalink
align jmh benchmark classes with benchmark suite
Browse files Browse the repository at this point in the history
  • Loading branch information
osiegmar committed Jan 5, 2024
1 parent 8d2da14 commit 191a1d8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 38 deletions.
28 changes: 21 additions & 7 deletions lib/src/jmh/java/de/siegmar/fastcsv/CsvConstants.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> RECORD_2 = List.of(
"Example row with only",
"two columns"
);

/**
* Data to write CSV.
*/
public static final List<List<String>> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 7 additions & 30 deletions lib/src/jmh/java/de/siegmar/fastcsv/NullWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
34 changes: 34 additions & 0 deletions lib/src/jmh/java/de/siegmar/fastcsv/RowSupplier.java
Original file line number Diff line number Diff line change
@@ -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<List<String>> {

private final List<List<String>> 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<List<String>> rows) {
this.rows = List.copyOf(rows);
pos = rows.size();
}

@Override
public List<String> get() {
final List<String> d = rows.get(--pos);
if (pos == 0) {
pos = rows.size();
}
return d;
}

}

0 comments on commit 191a1d8

Please sign in to comment.