Skip to content

Commit

Permalink
Add micro benchmark by JMH (#1278)
Browse files Browse the repository at this point in the history
* Add benchmark module and test implicit cast

Signed-off-by: Chen Dai <[email protected]>

* Benchmark the second approach from Yury

Signed-off-by: Chen Dai <[email protected]>

* Refactor code to make benchmark module for future use

Signed-off-by: Chen Dai <[email protected]>

* Add readme

Signed-off-by: Chen Dai <[email protected]>

* Move benchmark class to right source location

Signed-off-by: Chen Dai <[email protected]>

* Change depedency accessiblity from api to impl

Signed-off-by: Chen Dai <[email protected]>

Signed-off-by: Chen Dai <[email protected]>
  • Loading branch information
dai-chen authored Jan 23, 2023
1 parent 13faca4 commit ff9ac16
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 0 deletions.
19 changes: 19 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# OpenSearch SQL/PPL Microbenchmark Suite

This directory contains the microbenchmark suite of OpenSearch SQL/PPL. It relies on [JMH](http://openjdk.java.net/projects/code-tools/jmh/).

## Purpose

Microbenchmarks are intended to spot performance regressions in performance-critical components.

The microbenchmark suite is also handy for ad-hoc microbenchmarks but please remove them again before merging your PR.

## Getting Started

Just run `./gradlew :benchmarks:jmh` from the project root directory or run specific benchmark via your IDE. It will build all microbenchmarks, execute them and print the result.

## Adding Microbenchmarks

Before adding a new microbenchmark, make yourself familiar with the JMH API. You can check our existing microbenchmarks and also the [JMH samples](http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/).

In contrast to tests, the actual name of the benchmark class is not relevant to JMH. However, stick to the naming convention and end the class name of a benchmark with `Benchmark`. To have JMH execute a benchmark, annotate the respective methods with `@Benchmark`.
23 changes: 23 additions & 0 deletions benchmarks/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

plugins {
id 'java-library'
id "me.champeau.jmh" version "0.6.8"
}

repositories {
mavenCentral()
}

dependencies {
implementation project(':core')

// Dependencies required by JMH micro benchmark
api group: 'org.openjdk.jmh', name: 'jmh-core', version: '1.36'
annotationProcessor group: 'org.openjdk.jmh', name: 'jmh-generator-annprocess', version: '1.36'
}

compileJava.options.compilerArgs.addAll(["-processor", "org.openjdk.jmh.generators.BenchmarkProcessor"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.sql.expression.operator.predicate;

import static org.opensearch.sql.data.model.ExprValueUtils.fromObjectValue;
import static org.opensearch.sql.data.model.ExprValueUtils.integerValue;
import static org.opensearch.sql.data.model.ExprValueUtils.stringValue;
import static org.opensearch.sql.data.type.ExprCoreType.DATE;
import static org.opensearch.sql.expression.DSL.literal;

import com.google.common.collect.ImmutableMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
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.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.FunctionExpression;

@Warmup(iterations = 1)
@Measurement(iterations = 3)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Fork(value = 1)
public class ComparisonOperatorBenchmark {

@Param(value = { "int", "string", "date" })
private String testDataType;

private final Map<String, ExprValue> params =
ImmutableMap.<String, ExprValue>builder()
.put("int", integerValue(1))
.put("string", stringValue("hello"))
.put("date", fromObjectValue("2022-01-12", DATE))
.build();

@Benchmark
public void testEqualOperator() {
run(DSL::equal);
}

@Benchmark
public void testLessOperator() {
run(DSL::less);
}

@Benchmark
public void testGreaterOperator() {
run(DSL::greater);
}

private void run(Function<Expression[], FunctionExpression> dsl) {
ExprValue param = params.get(testDataType);
FunctionExpression func = dsl.apply(new Expression[] {
literal(param), literal(param)
});
func.valueOf();
}
}
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ include 'doctest'
include 'legacy'
include 'sql'
include 'prometheus'
include 'benchmarks'

0 comments on commit ff9ac16

Please sign in to comment.