Search in sources :

Example 1 with WritingMode

use of com.electronwill.nightconfig.core.io.WritingMode in project MinecraftForge by MinecraftForge.

the class ForgeConfigSpecTest method executeSpeedTest.

private <T> void executeSpeedTest(final String configKey, final T defaultKeyValue, final String testName) throws IOException {
    final ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
    final ForgeConfigSpec.ConfigValue<T> simpleValue = builder.define(configKey, defaultKeyValue);
    final ForgeConfigSpec spec = builder.build();
    final String configPath = String.format(Locale.ROOT, TEST_CONFIG_PATH_TEMPLATE, testName);
    final File configFile = new File(configPath);
    configFile.getParentFile().mkdirs();
    configFile.createNewFile();
    final CommentedFileConfig configData = CommentedFileConfig.builder(configPath).sync().preserveInsertionOrder().onFileNotFound((newfile, configFormat) -> {
        Files.createFile(newfile);
        return true;
    }).writingMode(WritingMode.REPLACE).build();
    spec.setConfig(configData);
    final List<TestResult> results = runTestHarness(defaultKeyValue, testName, simpleValue, spec, 3, 20);
    final double averageEnabled = results.stream().mapToLong(value -> value.enabledWatch.elapsed(TimeUnit.NANOSECONDS)).average().getAsDouble();
    final double averageDisabled = results.stream().mapToLong(value -> value.disabledWatch.elapsed(TimeUnit.NANOSECONDS)).average().getAsDouble();
    final double maxEnabledDeviation = results.stream().mapToLong(value -> value.enabledWatch.elapsed(TimeUnit.NANOSECONDS)).mapToDouble(e -> e - averageEnabled).map(Math::abs).max().getAsDouble();
    final double maxDisabledDeviation = results.stream().mapToLong(value -> value.disabledWatch.elapsed(TimeUnit.NANOSECONDS)).mapToDouble(e -> e - averageDisabled).map(Math::abs).max().getAsDouble();
    System.out.printf("Computed test results for: %s:%n", testName);
    System.out.printf("  > Enabled:  ~%10.2f ns.   +- %10.2f ns.%n", averageEnabled, maxEnabledDeviation);
    System.out.printf("  > Disabled: ~%10.2f ns.   +- %10.2f ns.%n", averageDisabled, maxDisabledDeviation);
    configFile.delete();
}
Also used : Files(java.nio.file.Files) Stopwatch(com.google.common.base.Stopwatch) WritingMode(com.electronwill.nightconfig.core.io.WritingMode) ForgeConfigSpec(net.minecraftforge.common.ForgeConfigSpec) Test(org.junit.Test) IOException(java.io.IOException) CommentedFileConfig(com.electronwill.nightconfig.core.file.CommentedFileConfig) Field(java.lang.reflect.Field) File(java.io.File) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Lists(com.google.common.collect.Lists) Locale(java.util.Locale) Assert(org.junit.Assert) ForgeConfigSpec(net.minecraftforge.common.ForgeConfigSpec) CommentedFileConfig(com.electronwill.nightconfig.core.file.CommentedFileConfig) File(java.io.File)

Aggregations

CommentedFileConfig (com.electronwill.nightconfig.core.file.CommentedFileConfig)1 WritingMode (com.electronwill.nightconfig.core.io.WritingMode)1 Stopwatch (com.google.common.base.Stopwatch)1 Lists (com.google.common.collect.Lists)1 File (java.io.File)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Files (java.nio.file.Files)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 TimeUnit (java.util.concurrent.TimeUnit)1 ForgeConfigSpec (net.minecraftforge.common.ForgeConfigSpec)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1