Skip to content

Commit e6768c5

Browse files
committed
gotta go fast
1 parent 773fb3e commit e6768c5

5 files changed

Lines changed: 190 additions & 1 deletion

File tree

‎.github/workflows/codspeed.yml‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: CodSpeed
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
workflow_dispatch:
9+
10+
permissions:
11+
contents: read # Check out the repository and its submodules
12+
id-token: write # Authenticate result uploads to CodSpeed via OIDC
13+
14+
jobs:
15+
benchmarks:
16+
name: Run benchmarks
17+
runs-on: codspeed-macro
18+
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
22+
with:
23+
submodules: recursive
24+
25+
- name: Set up JDK
26+
uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
27+
with:
28+
distribution: temurin
29+
java-version: 21
30+
cache: maven
31+
32+
- name: Publish CodSpeed JMH fork to local Maven repository
33+
working-directory: third-party/codspeed-jvm
34+
run: ./gradlew -p jmh-fork publishToMavenLocal
35+
36+
- name: Build benchmarks
37+
run: mvn --batch-mode --no-transfer-progress -P benchmarks -DskipTests package
38+
39+
- name: Run benchmarks
40+
uses: CodSpeedHQ/action@78102a468f96d8e0ccb92d0ec49425753fc42062 # v5.6.0
41+
with:
42+
mode: walltime
43+
run: java -jar target/benchmarks.jar

‎.gitmodules‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
[submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"]
22
path = src/test/java/com/flagsmith/flagengine/enginetestdata
33
url = git@github.com:Flagsmith/engine-test-data.git
4-
branch = v3.7.0
4+
branch = v3.7.0
5+
[submodule "third-party/codspeed-jvm"]
6+
path = third-party/codspeed-jvm
7+
url = https://github.com/CodSpeedHQ/codspeed-jvm.git

‎pom.xml‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,5 +388,80 @@
388388
<okhttp.mockwebserver.version>4.12.0</okhttp.mockwebserver.version>
389389
</properties>
390390
</profile>
391+
<profile>
392+
<!-- CodSpeed JMH benchmarks (JDK 21+), see https://codspeed.io/docs/benchmarks/java -->
393+
<id>benchmarks</id>
394+
<properties>
395+
<codspeed.jmh.version>0.2.1</codspeed.jmh.version>
396+
</properties>
397+
<dependencies>
398+
<dependency>
399+
<groupId>io.codspeed.jmh</groupId>
400+
<artifactId>jmh-core</artifactId>
401+
<version>${codspeed.jmh.version}</version>
402+
</dependency>
403+
</dependencies>
404+
<build>
405+
<plugins>
406+
<plugin>
407+
<groupId>org.codehaus.mojo</groupId>
408+
<artifactId>build-helper-maven-plugin</artifactId>
409+
<version>3.6.2</version>
410+
<executions>
411+
<execution>
412+
<id>add-jmh-sources</id>
413+
<phase>generate-sources</phase>
414+
<goals>
415+
<goal>add-source</goal>
416+
</goals>
417+
<configuration>
418+
<sources>
419+
<source>${project.basedir}/src/jmh/java</source>
420+
</sources>
421+
</configuration>
422+
</execution>
423+
</executions>
424+
</plugin>
425+
<plugin>
426+
<groupId>org.apache.maven.plugins</groupId>
427+
<artifactId>maven-compiler-plugin</artifactId>
428+
<configuration>
429+
<source>21</source>
430+
<target>21</target>
431+
<annotationProcessorPaths combine.children="append">
432+
<path>
433+
<groupId>io.codspeed.jmh</groupId>
434+
<artifactId>jmh-generator-annprocess</artifactId>
435+
<version>${codspeed.jmh.version}</version>
436+
</path>
437+
</annotationProcessorPaths>
438+
</configuration>
439+
</plugin>
440+
<plugin>
441+
<groupId>org.apache.maven.plugins</groupId>
442+
<artifactId>maven-shade-plugin</artifactId>
443+
<version>3.6.2</version>
444+
<executions>
445+
<execution>
446+
<phase>package</phase>
447+
<goals>
448+
<goal>shade</goal>
449+
</goals>
450+
<configuration>
451+
<outputFile>${project.build.directory}/benchmarks.jar</outputFile>
452+
<createDependencyReducedPom>false</createDependencyReducedPom>
453+
<transformers>
454+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
455+
<mainClass>org.openjdk.jmh.Main</mainClass>
456+
</transformer>
457+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
458+
</transformers>
459+
</configuration>
460+
</execution>
461+
</executions>
462+
</plugin>
463+
</plugins>
464+
</build>
465+
</profile>
391466
</profiles>
392467
</project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.flagsmith.flagengine;
2+
3+
import com.fasterxml.jackson.core.json.JsonReadFeature;
4+
import com.fasterxml.jackson.databind.json.JsonMapper;
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.List;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
13+
import org.openjdk.jmh.annotations.Benchmark;
14+
import org.openjdk.jmh.annotations.Scope;
15+
import org.openjdk.jmh.annotations.Setup;
16+
import org.openjdk.jmh.annotations.State;
17+
import org.openjdk.jmh.infra.Blackhole;
18+
19+
/**
20+
* Benchmarks local flag evaluation against every engine test data case.
21+
*/
22+
@State(Scope.Benchmark)
23+
public class EngineBenchmark {
24+
private static final Path testCasesPath = Paths
25+
.get("src/test/java/com/flagsmith/flagengine/enginetestdata/test_cases");
26+
private static final JsonMapper mapper = JsonMapper.builder()
27+
.enable(JsonReadFeature.ALLOW_JAVA_COMMENTS.mappedFeature())
28+
.build();
29+
30+
private List<EvaluationContext> evaluationContexts;
31+
32+
/**
33+
* Load the evaluation contexts from the engine test data.
34+
*/
35+
@Setup
36+
public void loadEvaluationContexts() throws IOException {
37+
try (Stream<Path> paths = Files.walk(testCasesPath)) {
38+
evaluationContexts = paths
39+
.filter(Files::isRegularFile)
40+
.filter(p -> {
41+
String n = p.getFileName().toString();
42+
return n.endsWith(".json") || n.endsWith(".jsonc");
43+
})
44+
.sorted()
45+
.map(EngineBenchmark::evaluationContextFromFile)
46+
.collect(Collectors.toList());
47+
}
48+
}
49+
50+
private static EvaluationContext evaluationContextFromFile(Path path) {
51+
try (BufferedReader reader = Files.newBufferedReader(path)) {
52+
return mapper.treeToValue(mapper.readTree(reader).get("context"), EvaluationContext.class);
53+
} catch (IOException e) {
54+
throw new RuntimeException("Failed to read test data from file: " + path, e);
55+
}
56+
}
57+
58+
/**
59+
* Evaluate flags for every engine test data case.
60+
*/
61+
@Benchmark
62+
public void evaluateEngineTestCases(Blackhole blackhole) {
63+
for (EvaluationContext evaluationContext : evaluationContexts) {
64+
blackhole.consume(Engine.getEvaluationResult(evaluationContext));
65+
}
66+
}
67+
}

‎third-party/codspeed-jvm‎

Submodule codspeed-jvm added at af05f6f

0 commit comments

Comments
 (0)