From 08547bd2563739f556539cb1120135df411359ae Mon Sep 17 00:00:00 2001 From: Benjamin Prud'homme Date: Sun, 20 Oct 2024 21:50:38 -0400 Subject: [PATCH] Additional JMH test (legacy) --- RoaringBitmap/build.gradle.kts | 60 ++++++++++++++++--- .../buffer/TestMemoryMapping.java | 22 ++++++- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/RoaringBitmap/build.gradle.kts b/RoaringBitmap/build.gradle.kts index b9beaf5a3..ca4f7cefe 100644 --- a/RoaringBitmap/build.gradle.kts +++ b/RoaringBitmap/build.gradle.kts @@ -1,3 +1,8 @@ +plugins { + id("me.champeau.gradle.jmh") version "0.5.0" + id("com.github.johnrengelman.shadow") version "6.0.0" +} + buildscript { repositories { maven { @@ -57,15 +62,19 @@ tasks.compileTestJava { val deps: Map by extra dependencies { - testImplementation("org.junit.jupiter:junit-jupiter-api:${deps["jupiter"]}") - testImplementation("org.junit.jupiter:junit-jupiter-params:${deps["jupiter"]}") - testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${deps["jupiter"]}") - testImplementation("com.google.guava:guava:${deps["guava"]}") - testImplementation("org.apache.commons:commons-lang3:${deps["commons-lang"]}") - testImplementation("com.esotericsoftware:kryo:5.0.0-RC6") - testImplementation("com.fasterxml.jackson.core", "jackson-databind", "2.10.3") - testImplementation("org.assertj", "assertj-core", "3.23.1") - testImplementation("org.openjdk.jol", "jol-core", "0.16") + implementation("org.junit.jupiter:junit-jupiter-api:${deps["jupiter"]}") + implementation("org.junit.jupiter:junit-jupiter-params:${deps["jupiter"]}") + runtimeOnly("org.junit.jupiter:junit-jupiter-engine:${deps["jupiter"]}") + implementation("org.openjdk.jmh:jmh-core:1.35") + annotationProcessor("org.openjdk.jmh:jmh-generator-annprocess:1.35") + implementation("com.google.guava:guava:${deps["guava"]}") + implementation("org.apache.commons:commons-lang3:${deps["commons-lang"]}") + implementation("com.esotericsoftware:kryo:5.0.0-RC6") + implementation("com.fasterxml.jackson.core", "jackson-databind", "2.10.3") + implementation("org.assertj:assertj-core:3.23.1") + implementation("org.openjdk.jol:jol-core:0.16") + jmhImplementation("org.junit.jupiter:junit-jupiter-api:${deps["jupiter"]}") + jmhRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${deps["jupiter"]}") } sourceSets { @@ -115,3 +124,36 @@ tasks.test { // showStandardStreams = true } } + +jmh { + sourceSets { + getByName("main") { + java.srcDir("src/test/java") + } + } +} + +tasks.named("shadowJar") { + archiveClassifier.set("all") + + from(sourceSets["main"].output) + from(sourceSets["test"].output) + + manifest { + attributes( + "Main-Class" to "org.openjdk.jmh.Main" + ) + } + + configurations = listOf(project.configurations.getByName("runtimeClasspath")) +} + +tasks.register("runJmhShadow") { + group = "benchmark" + description = "Runs JMH benchmarks from the shadow JAR." + + classpath = files(tasks.named("shadowJar")) + + mainClass.set("org.openjdk.jmh.Main") + jvmArgs = listOf("-Xms2G", "-Xmx2G") +} diff --git a/RoaringBitmap/src/test/java/org/roaringbitmap/buffer/TestMemoryMapping.java b/RoaringBitmap/src/test/java/org/roaringbitmap/buffer/TestMemoryMapping.java index 1605cabfc..0d2258886 100644 --- a/RoaringBitmap/src/test/java/org/roaringbitmap/buffer/TestMemoryMapping.java +++ b/RoaringBitmap/src/test/java/org/roaringbitmap/buffer/TestMemoryMapping.java @@ -23,6 +23,20 @@ import static org.junit.jupiter.api.Assertions.*; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Fork; +import org.openjdk.jmh.annotations.Level; +import org.openjdk.jmh.annotations.Measurement; +import org.openjdk.jmh.annotations.Mode; +import org.openjdk.jmh.annotations.OutputTimeUnit; +import org.openjdk.jmh.annotations.Param; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.Setup; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.annotations.TearDown; +import org.openjdk.jmh.annotations.Warmup; + class ByteBufferBackedInputStream extends InputStream { @@ -99,8 +113,12 @@ public synchronized void write(int b) throws IOException { @SuppressWarnings({"static-method"}) +@State(Scope.Benchmark) public class TestMemoryMapping { + @Param({"10", "50", "100", "500", "1000", "5000"}) + int numThreads; + static ArrayList mappedbitmaps = new ArrayList(); static MappedByteBuffer out; @@ -110,6 +128,7 @@ public class TestMemoryMapping { static File tmpfile; @AfterAll + @TearDown(Level.Trial) public static void clearFiles() { System.out.println("[TestMemoryMapping] Cleaning memory-mapped file."); out = null; @@ -131,6 +150,7 @@ public static boolean equals(ByteBuffer bb1, ByteBuffer bb2) { } @BeforeAll + @Setup(Level.Trial) public static void initFiles() throws IOException { System.out.println("[TestMemoryMapping] Setting up memory-mapped file. (Can take some time.)"); final ArrayList offsets = new ArrayList(); @@ -358,11 +378,11 @@ public void intersections() { } @Test + @Benchmark public void multithreadingTest() throws InterruptedException, IOException { System.out.println("[TestMemoryMapping] multithreading test"); final MutableRoaringBitmap rr1 = new MutableRoaringBitmap(); - final int numThreads = Runtime.getRuntime().availableProcessors(); final Throwable[] errors = new Throwable[numThreads]; for (int i = 0; i < numThreads; i++) {