Repository navigation
Expand file tree
/
Copy pathEngineBenchmark.java
More file actions
67 lines (61 loc) · 2.22 KB
/
Copy pathEngineBenchmark.java
File metadata and controls
67 lines (61 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.flagsmith.flagengine;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
/**
* Benchmarks local flag evaluation against every engine test data case.
*/
@State(Scope.Benchmark)
public class EngineBenchmark {
private static final Path testCasesPath = Paths
.get("src/test/java/com/flagsmith/flagengine/enginetestdata/test_cases");
private static final JsonMapper mapper = JsonMapper.builder()
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS.mappedFeature())
.build();
private List<EvaluationContext> evaluationContexts;
/**
* Load the evaluation contexts from the engine test data.
*/
@Setup
public void loadEvaluationContexts() throws IOException {
try (Stream<Path> paths = Files.walk(testCasesPath)) {
evaluationContexts = paths
.filter(Files::isRegularFile)
.filter(p -> {
String n = p.getFileName().toString();
return n.endsWith(".json") || n.endsWith(".jsonc");
})
.sorted()
.map(EngineBenchmark::evaluationContextFromFile)
.collect(Collectors.toList());
}
}
private static EvaluationContext evaluationContextFromFile(Path path) {
try (BufferedReader reader = Files.newBufferedReader(path)) {
return mapper.treeToValue(mapper.readTree(reader).get("context"), EvaluationContext.class);
} catch (IOException e) {
throw new RuntimeException("Failed to read test data from file: " + path, e);
}
}
/**
* Evaluate flags for every engine test data case.
*/
@Benchmark
public void evaluateEngineTestCases(Blackhole blackhole) {
for (EvaluationContext evaluationContext : evaluationContexts) {
blackhole.consume(Engine.getEvaluationResult(evaluationContext));
}
}
}