Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: CodSpeed

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

permissions:
contents: read # Check out the repository and its submodules
id-token: write # Authenticate result uploads to CodSpeed via OIDC

jobs:
benchmarks:
name: Run benchmarks
runs-on: codspeed-macro

steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
submodules: recursive
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
persist-credentials: false

- name: Set up JDK
uses: actions/setup-java@b843ebf9b913146b38da2eaa7aeddf76074ae1ad # v6.1.0
with:
distribution: temurin
java-version: 21
cache: maven

- name: Set up Maven
uses: stCarolas/setup-maven@12eb41b233df95d49b0c11fc1b5bc8312e5d4ce0 # v5.1
with:
maven-version: 3.10.0

- name: Run benchmarks
uses: CodSpeedHQ/action@78102a468f96d8e0ccb92d0ec49425753fc42062 # v5.6.0
with:
mode: walltime
run: make benchmark
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[submodule "src/test/java/com/flagsmith/flagengine/enginetestdata"]
path = src/test/java/com/flagsmith/flagengine/enginetestdata
url = git@github.com:Flagsmith/engine-test-data.git
branch = v3.7.0
branch = v3.7.0
[submodule "third-party/codspeed-jvm"]
path = third-party/codspeed-jvm
url = https://github.com/CodSpeedHQ/codspeed-jvm.git
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BENCHMARKS_JAR ?= target/benchmarks.jar

$(BENCHMARKS_JAR): pom.xml $(shell find src/main src/jmh -type f)
cd third-party/codspeed-jvm && ./gradlew -p jmh-fork publishToMavenLocal
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
mvn --batch-mode --no-transfer-progress -P benchmarks -DskipTests package

.PHONY: benchmark
benchmark: $(BENCHMARKS_JAR)
java -jar $(BENCHMARKS_JAR)
75 changes: 75 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -388,5 +388,80 @@
<okhttp.mockwebserver.version>4.12.0</okhttp.mockwebserver.version>
</properties>
</profile>
<profile>
<!-- CodSpeed JMH benchmarks (JDK 21+), see https://codspeed.io/docs/benchmarks/java -->
<id>benchmarks</id>
<properties>
<codspeed.jmh.version>0.2.1</codspeed.jmh.version>
</properties>
<dependencies>
<dependency>
<groupId>io.codspeed.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${codspeed.jmh.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.2</version>
<executions>
<execution>
<id>add-jmh-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.basedir}/src/jmh/java</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>21</source>
<target>21</target>
<annotationProcessorPaths combine.children="append">
<path>
<groupId>io.codspeed.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${codspeed.jmh.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/benchmarks.jar</outputFile>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
67 changes: 67 additions & 0 deletions src/jmh/java/com/flagsmith/flagengine/EngineBenchmark.java
Comment thread
khvn26 marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,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));
}
}
Comment thread
khvn26 marked this conversation as resolved.
Resolved
}
1 change: 1 addition & 0 deletions third-party/codspeed-jvm
Comment thread
khvn26 marked this conversation as resolved.
Resolved
Submodule codspeed-jvm added at af05f6
Loading