Skip to content

Commit

Permalink
Add option --auto-flush to enable benchmarking of ion-java writer wit…
Browse files Browse the repository at this point in the history
…h auto-flush configured. (#59)
  • Loading branch information
linlin-s authored Dec 6, 2023
1 parent 2e995de commit 36bfda9
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/com/amazon/ion/benchmark/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Constants {
static final String JSON_USE_BIG_DECIMALS_NAME = "g";
static final String AUTO_VALUE = "auto";
static final String NONE_VALUE = "none";
static final String AUTO_FLUSH_ENABLED = "m";

private Constants() {
// Do not instantiate.
Expand Down
5 changes: 5 additions & 0 deletions src/com/amazon/ion/benchmark/IonUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ static IonWriterSupplier newBinaryWriterSupplier(OptionsCombinationBase options)
} else {
builder.withFloatBinary32Disabled();
}
if(options.autoFlush) {
builder.withAutoFlushEnabled();
} else {
builder.withAutoFlushDisabled();
}
if (options instanceof WriteOptionsCombination) {
// When this method is used by the read benchmark for converting the input file, 'options' will be a
// ReadOptionsCombination, which does not have the 'ionWriterUserBufferSize' value, because this value
Expand Down
6 changes: 5 additions & 1 deletion src/com/amazon/ion/benchmark/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Main {
+ "[--results-file <file>] [--io-type <type>]... [--io-buffer-size <int>]... [--format <type>]... "
+ "[--api <api>]... [--ion-imports-for-input <file>] [--ion-imports-for-benchmark <file>]... "
+ "[--ion-flush-period <int>]... [--ion-length-preallocation <int>]... [--ion-float-width <int>]... "
+ "[--ion-use-symbol-tokens <bool>]... [--ion-writer-block-size <int>]... "
+ "[--ion-use-symbol-tokens <bool>]... [--ion-writer-block-size <int>]... [--auto-flush <bool>]..."
+ "[--json-use-big-decimals <bool>]... <input_file>\n"

+ " ion-java-benchmark read [--profile] [--limit <int>] [--mode <mode>] [--time-unit <unit>] "
Expand Down Expand Up @@ -200,6 +200,10 @@ public class Main {
+ "specified multiple times to compare different values. Ignored unless the format is ion_binary. "
+ "[default: auto]\n"

+ " -m --auto-flush <bool> If this option is enabled, then the flush operation will be executed "
+ "automatically when the size of the value exceeds the writer's block size. This option may be specified multiple times to compare different values."
+ "[default: false]\n"

// 'read' options:

+ " -s --paths <file> A file containing a sequence of Ion s-expressions representing "
Expand Down
5 changes: 4 additions & 1 deletion src/com/amazon/ion/benchmark/OptionsCombinationBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
import java.nio.file.Path;
import java.util.function.Function;

import static com.amazon.ion.benchmark.Constants.API_NAME;
import static com.amazon.ion.benchmark.Constants.AUTO_FLUSH_ENABLED;
import static com.amazon.ion.benchmark.Constants.FLUSH_PERIOD_NAME;
import static com.amazon.ion.benchmark.Constants.FORMAT_NAME;
import static com.amazon.ion.benchmark.Constants.API_NAME;
import static com.amazon.ion.benchmark.Constants.ION_FLOAT_WIDTH_NAME;
import static com.amazon.ion.benchmark.Constants.ION_IMPORTS_FOR_BENCHMARK_NAME;
import static com.amazon.ion.benchmark.Constants.ION_IMPORTS_FOR_INPUT_NAME;
Expand Down Expand Up @@ -49,6 +50,7 @@ abstract class OptionsCombinationBase {
final boolean useSymbolTokens;
final int limit;
final boolean jsonUseBigDecimals;
final boolean autoFlush;

/**
* Retrieves and translates a value from the struct, if the field is present and is not the 'auto' value. Otherwise,
Expand Down Expand Up @@ -89,6 +91,7 @@ static <T> T getOrDefault(IonStruct options, String fieldName, Function<IonValue
useSymbolTokens = getOrDefault(optionsCombinationStruct, ION_USE_SYMBOL_TOKENS_NAME, val -> ((IonBool) val).booleanValue(), false);
limit = getOrDefault(optionsCombinationStruct, LIMIT_NAME, val -> ((IonInt) val).intValue(), Integer.MAX_VALUE);
jsonUseBigDecimals = getOrDefault(optionsCombinationStruct, JSON_USE_BIG_DECIMALS_NAME, val -> ((IonBool) val).booleanValue(), true);
autoFlush = getOrDefault(optionsCombinationStruct, AUTO_FLUSH_ENABLED, val -> ((IonBool) val).booleanValue(), false);
}

/**
Expand Down
12 changes: 11 additions & 1 deletion src/com/amazon/ion/benchmark/OptionsMatrixBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import static com.amazon.ion.benchmark.Constants.API_NAME;
import static com.amazon.ion.benchmark.Constants.AUTO_FLUSH_ENABLED;
import static com.amazon.ion.benchmark.Constants.FLUSH_PERIOD_NAME;
import static com.amazon.ion.benchmark.Constants.FORMAT_NAME;
import static com.amazon.ion.benchmark.Constants.API_NAME;
import static com.amazon.ion.benchmark.Constants.ION_FLOAT_WIDTH_NAME;
import static com.amazon.ion.benchmark.Constants.ION_IMPORTS_FOR_BENCHMARK_NAME;
import static com.amazon.ion.benchmark.Constants.ION_IMPORTS_FOR_INPUT_NAME;
Expand Down Expand Up @@ -403,6 +404,15 @@ private static String getFileOrDefault(String fileNameOrNone, String defaultFile
() -> ION_SYSTEM.newBool(true),
OPTION_ONLY_APPLIES_TO_JSON
);
parseAndCombine(
optionsMatrix.get("--auto-flush"),
AUTO_FLUSH_ENABLED,
OptionsMatrixBase::getTrueOrNull,
ION_SYSTEM::newBool,
optionsCombinationStructs,
() -> ION_SYSTEM.newBool(false),
OPTION_ONLY_APPLIES_TO_ION_STREAMING
);
parseCommandSpecificOptions(optionsMatrix, optionsCombinationStructs);
serializedOptionsCombinations = serializeOptionsCombinations(optionsCombinationStructs);
if (profile) {
Expand Down
33 changes: 33 additions & 0 deletions tst/com/amazon/ion/benchmark/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ private static class ExpectedWriteOptionsCombination
extends ExpectedOptionsCombinationBase<ExpectedWriteOptionsCombination, WriteOptionsCombination> {

Integer ionWriterBlockSize = null;
Boolean autoFlush = null;

static ExpectedWriteOptionsCombination defaultOptions() {
return new ExpectedWriteOptionsCombination();
Expand All @@ -363,6 +364,11 @@ final ExpectedWriteOptionsCombination ionWriterBlockSize(Integer ionWriterBlockS
return this;
}

final ExpectedWriteOptionsCombination autoFlush(Boolean autoFlush) {
this.autoFlush = autoFlush;
return this;
}

@Override
void assertOptionsEqual(WriteOptionsCombination that) {
super.assertOptionsEqual(that);
Expand Down Expand Up @@ -1224,6 +1230,33 @@ public void ionWriterBlockSize() throws Exception {
assertTrue(expectedCombinations.isEmpty());
}

@Test
public void autoFlush() throws Exception {
List<WriteOptionsCombination> optionsCombinations = parseOptionsCombinations(
"write",
"--auto-flush",
"true",
"--auto-flush",
"false",
"--io-type",
"buffer",
"binaryStructs.10n"
);
assertEquals(2, optionsCombinations.size());
List<ExpectedWriteOptionsCombination> expectedCombinations = new ArrayList<>(2);

expectedCombinations.add(ExpectedWriteOptionsCombination.defaultOptions().autoFlush(true));
expectedCombinations.add(ExpectedWriteOptionsCombination.defaultOptions().autoFlush(false));

for (WriteOptionsCombination optionsCombination : optionsCombinations) {
expectedCombinations.removeIf(candidate -> nullSafeEquals(candidate.autoFlush, optionsCombination.autoFlush));

assertWriteTaskExecutesCorrectly("binaryStructs.10n", optionsCombination, Format.ION_BINARY, IoType.BUFFER);
assertWriteTaskExecutesCorrectly("textStructs.ion", optionsCombination, Format.ION_BINARY, IoType.BUFFER);
}
assertTrue(expectedCombinations.isEmpty());
}

@Test
public void writeAllTypes() throws Exception {
List<WriteOptionsCombination> optionsCombinations = parseOptionsCombinations(
Expand Down

0 comments on commit 36bfda9

Please sign in to comment.