|
| 1 | +package dev.blaauwendraad.masker.json; |
| 2 | + |
| 3 | +import dev.blaauwendraad.masker.json.config.JsonMaskerAlgorithmType; |
| 4 | +import dev.blaauwendraad.masker.json.config.JsonMaskingConfig; |
| 5 | +import org.openjdk.jmh.annotations.*; |
| 6 | + |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.util.HashSet; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | + |
| 12 | +@Warmup(iterations = 1, time = 3) |
| 13 | +@Fork(value = 1) |
| 14 | +@Measurement(iterations = 1, time = 3) |
| 15 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 16 | +@BenchmarkMode(Mode.Throughput) |
| 17 | +public class JsonMaskerBenchmark { |
| 18 | + |
| 19 | + @org.openjdk.jmh.annotations.State(Scope.Thread) |
| 20 | + public static class State { |
| 21 | + |
| 22 | + @Param({"200b", "4kb", "128kb", "2mb"}) |
| 23 | + String jsonSize; |
| 24 | + |
| 25 | + @Param({"1", "100"}) |
| 26 | + int numberOfKeys; |
| 27 | + |
| 28 | + @Param({"-1", "8"}) |
| 29 | + int obfuscationLength; |
| 30 | + private String jsonString; |
| 31 | + private byte[] jsonBytes; |
| 32 | + private JsonMasker jsonMasker; |
| 33 | + |
| 34 | + @Setup |
| 35 | + public synchronized void setup() { |
| 36 | + Set<String> keysToBeMasked = getTargetKeys(numberOfKeys); |
| 37 | + |
| 38 | + jsonString = ParseAndMaskUtil.readJsonFromFileAsString("json-%s.json".formatted(jsonSize), this.getClass()); |
| 39 | + jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8); |
| 40 | + |
| 41 | + jsonMasker = JsonMasker.getMasker( |
| 42 | + JsonMaskingConfig.custom(keysToBeMasked, JsonMaskingConfig.TargetKeyMode.MASK) |
| 43 | + .obfuscationLength(obfuscationLength) |
| 44 | + .algorithmTypeOverride(JsonMaskerAlgorithmType.KEYS_CONTAIN) |
| 45 | + .build() |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + private Set<String> getTargetKeys(int numberOfKeys) { |
| 50 | + Set<String> targetKeys = new HashSet<>(); |
| 51 | + for (int i = 0; i < numberOfKeys; i++) { |
| 52 | + targetKeys.add("someSecret" + i); |
| 53 | + } |
| 54 | + return targetKeys; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Benchmark |
| 59 | + public String maskJsonString(State state) { |
| 60 | + return state.jsonMasker.mask(state.jsonString); |
| 61 | + } |
| 62 | + |
| 63 | + @Benchmark |
| 64 | + public byte[] maskJsonBytes(State state) { |
| 65 | + return state.jsonMasker.mask(state.jsonBytes); |
| 66 | + } |
| 67 | +} |
0 commit comments