Skip to content

Commit

Permalink
Switch benchmarks module to multi-release one
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta committed Dec 8, 2023
1 parent 323c8c7 commit 2969070
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 21 deletions.
40 changes: 35 additions & 5 deletions benchmarks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ compileJava.options.compilerArgs.addAll(["-processor", "org.openjdk.jmh.generato

run.executable = "${BuildParams.runtimeJavaHome}/bin/java"

// Add support for incubator modules on supported Java versions.
if (BuildParams.runtimeJavaVersion >= JavaVersion.VERSION_20) {
run.jvmArgs += ['--add-modules=jdk.incubator.vector']
}

// classes generated by JMH can use all sorts of forbidden APIs but we have no influence at all and cannot exclude these classes
disableTasks('forbiddenApisMain')

Expand All @@ -89,3 +84,38 @@ spotless {
targetExclude 'src/main/generated/**/*.java'
}
}

if (BuildParams.runtimeJavaVersion >= JavaVersion.VERSION_20) {
// Add support for incubator modules on supported Java versions.
run.jvmArgs += ['--add-modules=jdk.incubator.vector']
evaluationDependsOn(':libs:opensearch-common')

sourceSets {
java20 {
java {
srcDirs = ['src/main/java20']
}
}
}

configurations {
java20Implementation.extendsFrom(implementation)
}

dependencies {
java20Implementation sourceSets.main.output
java20Implementation project(':libs:opensearch-common').sourceSets.java20.output
}

compileJava20Java {
targetCompatibility = JavaVersion.VERSION_20
}

jar {
metaInf {
into 'versions/20'
from sourceSets.java20.output
}
manifest.attributes('Multi-Release': 'true')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.lang.invoke.MethodHandles;
import java.util.Random;
import java.util.function.Supplier;

Expand Down Expand Up @@ -84,7 +83,7 @@ public static class Options {
"256" })
public Integer size;

@Param({ "linear", "binary", "btree" })
@Param({ "binary", "linear" })
public String type;

@Param({ "uniform", "skewed_edge", "skewed_center" })
Expand All @@ -94,7 +93,7 @@ public static class Options {
public Supplier<Roundable> supplier;

@Setup
public void setup() throws ClassNotFoundException, IllegalAccessException {
public void setup() {
Random random = new Random(size);
long[] values = new long[size];
for (int i = 1; i < values.length; i++) {
Expand Down Expand Up @@ -136,19 +135,6 @@ public void setup() throws ClassNotFoundException, IllegalAccessException {
case "linear":
supplier = () -> new BidirectionalLinearSearcher(values, size);
break;
case "btree":
// Not supported below Java 20.
// Using MethodHandles to reflectively look up the class (if available) in order to
// avoid setting up separate Java 20 sources with mostly duplicate code.
Class<?> clz = MethodHandles.lookup().findClass("org.opensearch.common.round.BtreeSearcher");
supplier = () -> {
try {
return (Roundable) clz.getDeclaredConstructor(long[].class, int.class).newInstance(values, size);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
break;
default:
throw new IllegalArgumentException("invalid type: " + type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.round;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
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.Warmup;
import org.openjdk.jmh.infra.Blackhole;

import java.util.Random;
import java.util.function.Supplier;

@Fork(value = 3)
@Warmup(iterations = 3, time = 1)
@Measurement(iterations = 1, time = 1)
@BenchmarkMode(Mode.Throughput)
public class RoundableBenchmark {

@Benchmark
public void floor(Blackhole bh, Options opts) {
Roundable roundable = opts.supplier.get();
for (long key : opts.queries) {
bh.consume(roundable.floor(key));
}
}

@State(Scope.Benchmark)
public static class Options {
@Param({
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"12",
"14",
"16",
"18",
"20",
"22",
"24",
"26",
"29",
"32",
"37",
"41",
"45",
"49",
"54",
"60",
"64",
"74",
"83",
"90",
"98",
"108",
"118",
"128",
"144",
"159",
"171",
"187",
"204",
"229",
"256" })
public Integer size;

@Param({ "linear", "binary", "btree" })
public String type;

@Param({ "uniform", "skewed_edge", "skewed_center" })
public String distribution;

public long[] queries;
public Supplier<Roundable> supplier;

@Setup
public void setup() throws ClassNotFoundException, IllegalAccessException {
Random random = new Random(size);
long[] values = new long[size];
for (int i = 1; i < values.length; i++) {
values[i] = values[i - 1] + 100;
}

long range = values[values.length - 1] - values[0] + 100;
long mean, stddev;
queries = new long[1000000];

switch (distribution) {
case "uniform": // all values equally likely.
for (int i = 0; i < queries.length; i++) {
queries[i] = values[0] + (nextPositiveLong(random) % range);
}
break;
case "skewed_edge": // distribution centered at p90 with ± 5% stddev.
mean = values[0] + (long) (range * 0.9);
stddev = (long) (range * 0.05);
for (int i = 0; i < queries.length; i++) {
queries[i] = Math.max(values[0], mean + (long) (random.nextGaussian() * stddev));
}
break;
case "skewed_center": // distribution centered at p50 with ± 5% stddev.
mean = values[0] + (long) (range * 0.5);
stddev = (long) (range * 0.05);
for (int i = 0; i < queries.length; i++) {
queries[i] = Math.max(values[0], mean + (long) (random.nextGaussian() * stddev));
}
break;
default:
throw new IllegalArgumentException("invalid distribution: " + distribution);
}

switch (type) {
case "binary":
supplier = () -> new BinarySearcher(values, size);
break;
case "linear":
supplier = () -> new BidirectionalLinearSearcher(values, size);
break;
case "btree":
supplier = () -> new BtreeSearcher(values, size);
break;
default:
throw new IllegalArgumentException("invalid type: " + type);
}
}

private static long nextPositiveLong(Random random) {
return random.nextLong() & Long.MAX_VALUE;
}
}
}

0 comments on commit 2969070

Please sign in to comment.