diff --git a/src/jmh/java/dev/blaauwendraad/masker/json/JsonMaskMultipleTargetKeysBenchmark.java b/src/jmh/java/dev/blaauwendraad/masker/json/JsonMaskMultipleTargetKeysBenchmark.java deleted file mode 100644 index 33d87276..00000000 --- a/src/jmh/java/dev/blaauwendraad/masker/json/JsonMaskMultipleTargetKeysBenchmark.java +++ /dev/null @@ -1,88 +0,0 @@ -package dev.blaauwendraad.masker.json; - -import com.fasterxml.jackson.databind.ObjectMapper; -import dev.blaauwendraad.masker.json.config.JsonMaskerAlgorithmType; -import dev.blaauwendraad.masker.json.config.JsonMaskingConfig; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.infra.Blackhole; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.HashSet; -import java.util.Set; -import java.util.concurrent.TimeUnit; - -@Warmup(iterations = 1, time = 10) -@Fork(value = 1) -@Measurement(iterations = 1, time = 10) -@OutputTimeUnit(TimeUnit.NANOSECONDS) -@BenchmarkMode(Mode.AverageTime) -public class JsonMaskMultipleTargetKeysBenchmark { - @org.openjdk.jmh.annotations.State(Scope.Benchmark) - public static class State { - Set keysToBeMasked = getTargetKeys(100); - ObjectMapper mapper = new ObjectMapper(); - String smallJsonAsString = "{\"someSecret\": \"someValue\", \n\"someOtherKey\": {\"someSecret2\": \"value\"}}"; - byte[] simpleJsonAsBytes = smallJsonAsString.getBytes(StandardCharsets.UTF_8); - String largeJsonAsString = - ParseAndMaskUtil.readJsonFromFileAsString("large-input-benchmark.json", this.getClass()); - byte[] largeJsonAsBytes = largeJsonAsString.getBytes(StandardCharsets.UTF_8); - JsonMasker keyContainsJsonMasker = JsonMasker.getMasker(JsonMaskingConfig.custom(keysToBeMasked, JsonMaskingConfig.TargetKeyMode.MASK) - .algorithmTypeOverride(JsonMaskerAlgorithmType.KEYS_CONTAIN) - .build()); - JsonMasker loopJsonMasker = JsonMasker.getMasker(JsonMaskingConfig.custom(keysToBeMasked, - JsonMaskingConfig.TargetKeyMode.MASK - ) - .algorithmTypeOverride(JsonMaskerAlgorithmType.SINGLE_TARGET_LOOP) - .build()); - - private Set getTargetKeys(int numberOfKeys) { - Set targetKeys = new HashSet<>(); - for (int i = 0; i < numberOfKeys; i++) { - targetKeys.add("someSecret" + i); - } - return targetKeys; - } - } - - @Benchmark - public void loopMaskMultipleKeysSmallJson(State state, Blackhole blackhole) { - String maskedJsonOutput = state.loopJsonMasker.mask(state.smallJsonAsString); - blackhole.consume(maskedJsonOutput); - } - - @Benchmark - public void loopMaskMultipleKeysLargeJson(State state, Blackhole blackhole) { - String maskedJsonOutput = state.loopJsonMasker.mask(state.largeJsonAsString); - blackhole.consume(maskedJsonOutput); - } - - - @Benchmark - public void keyContainsMaskMultiKeysSmallJson(State state, Blackhole blackhole) { - String maskedJsonOutput = state.keyContainsJsonMasker.mask(state.smallJsonAsString); - blackhole.consume(maskedJsonOutput); - } - - @Benchmark - public void keyContainsMaskMultiKeysLargeJson(State state, Blackhole blackhole) { - String maskedJsonOutput = state.keyContainsJsonMasker.mask(state.largeJsonAsString); - blackhole.consume(maskedJsonOutput); - } - - @Benchmark - public void parseAndMaskMultiKeysSmallJson(State state, Blackhole blackhole) throws IOException { - String maskedJsonOutput = - ParseAndMaskUtil.mask(state.simpleJsonAsBytes, state.keysToBeMasked, JsonMaskingConfig.TargetKeyMode.MASK, state.mapper) - .toString(); - blackhole.consume(maskedJsonOutput); - } - - @Benchmark - public void parseAndMaskMultiKeysLargeJson(State state, Blackhole blackhole) throws IOException { - String maskedJsonOutput = - ParseAndMaskUtil.mask(state.largeJsonAsBytes, state.keysToBeMasked, JsonMaskingConfig.TargetKeyMode.MASK, state.mapper) - .toString(); - blackhole.consume(maskedJsonOutput); - } -} diff --git a/src/jmh/java/dev/blaauwendraad/masker/json/JsonMaskerBenchmark.java b/src/jmh/java/dev/blaauwendraad/masker/json/JsonMaskerBenchmark.java new file mode 100644 index 00000000..fd067459 --- /dev/null +++ b/src/jmh/java/dev/blaauwendraad/masker/json/JsonMaskerBenchmark.java @@ -0,0 +1,67 @@ +package dev.blaauwendraad.masker.json; + +import dev.blaauwendraad.masker.json.config.JsonMaskerAlgorithmType; +import dev.blaauwendraad.masker.json.config.JsonMaskingConfig; +import org.openjdk.jmh.annotations.*; + +import java.nio.charset.StandardCharsets; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.TimeUnit; + +@Warmup(iterations = 1, time = 3) +@Fork(value = 1) +@Measurement(iterations = 1, time = 3) +@OutputTimeUnit(TimeUnit.SECONDS) +@BenchmarkMode(Mode.Throughput) +public class JsonMaskerBenchmark { + + @org.openjdk.jmh.annotations.State(Scope.Thread) + public static class State { + + @Param({"200b", "4kb", "128kb", "2mb"}) + String jsonSize; + + @Param({"1", "100"}) + int numberOfKeys; + + @Param({"-1", "8"}) + int obfuscationLength; + private String jsonString; + private byte[] jsonBytes; + private JsonMasker jsonMasker; + + @Setup + public synchronized void setup() { + Set keysToBeMasked = getTargetKeys(numberOfKeys); + + jsonString = ParseAndMaskUtil.readJsonFromFileAsString("json-%s.json".formatted(jsonSize), this.getClass()); + jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8); + + jsonMasker = JsonMasker.getMasker( + JsonMaskingConfig.custom(keysToBeMasked, JsonMaskingConfig.TargetKeyMode.MASK) + .obfuscationLength(obfuscationLength) + .algorithmTypeOverride(JsonMaskerAlgorithmType.KEYS_CONTAIN) + .build() + ); + } + + private Set getTargetKeys(int numberOfKeys) { + Set targetKeys = new HashSet<>(); + for (int i = 0; i < numberOfKeys; i++) { + targetKeys.add("someSecret" + i); + } + return targetKeys; + } + } + + @Benchmark + public String maskJsonString(State state) { + return state.jsonMasker.mask(state.jsonString); + } + + @Benchmark + public byte[] maskJsonBytes(State state) { + return state.jsonMasker.mask(state.jsonBytes); + } +}