Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JMH support and improve MultiSpanExporter performance #678

Merged
merged 7 commits into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ subprojects {
apply plugin: 'com.github.sherter.google-java-format'
apply plugin: 'net.ltgt.errorprone'
apply plugin: 'com.jfrog.artifactory'
apply from: "$rootDir/jmh.gradle"

group = "io.opentelemetry"
version = "0.3.0-SNAPSHOT" // CURRENT_VERSION
Expand Down
142 changes: 142 additions & 0 deletions jmh.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright 2020, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*
* This file should be imported in the top-level Gradle project.
* To add JMH microbenchmarks to any sub-project simply create 'src/jmh/java'
* and if required 'src/jmh/resource'. This JMH Gradle script will check for
* this sourceset, and will add tasks if it exists, and build any benchmarks.
*
* Running benchmarks can be done either through gradle using the task runJMH,
* or by downloading the created artifact and required dependencies.
*
* To use Gradle to download the benchmark for a specific version you can use
* a basic Gradle build file like the following.
*
* apply plugin: 'java'
*
* repositories {
* ...
* }
*
* dependencies {
* compile(group: 'GROUP', name: 'NAME', version: 'VERSION')
* compile(group: 'GROUP', name: 'NAME', version: 'VERSION', classifier: 'jmh')
* }
*
* task copyDependencies(type: Copy) {
* into 'execute'
* }
*
* task setup {
* dependsOn copyDependencies
* }
*
* $ gradle setup
* $ java -jar execute/group-name-version-jmh.jar
*/

sourceSets {
jmh {
java.srcDir 'src/jmh/java'
resources.srcDir 'src/jmh/resources'
compileClasspath += main.output
runtimeClasspath += main.output
}
}

def benchmarksAvailable = sourceSets.jmh.java.getFiles().any { f -> f.exists() }

dependencies {
jmhCompile 'org.openjdk.jmh:jmh-core:1.23'
jmhAnnotationProcessor 'org.openjdk.jmh:jmh-generator-annprocess:1.23'
}

configurations {
jmhCompile {
extendsFrom compile
}

jmhImplementation {
extendsFrom implementation
extendsFrom jmhCompile
}

jmh {
extendsFrom runtime
extendsFrom jmhCompile
}
}

compileJmhJava {
enabled benchmarksAvailable
options.compilerArgs = compileJava.options.compilerArgs
options.fork = compileJava.options.fork
options.forkOptions = compileJava.options.forkOptions
}

task jmhRun(type: JavaExec) {
enabled benchmarksAvailable
description 'Run JMH benchmark(s). Use the property jmhFilter to set benchmark filtering regex.'
group 'Verification'
classpath = sourceSets.jmh.runtimeClasspath
main = 'org.openjdk.jmh.Main'
// Allows users to filter JMH benchmarks using a regex
args project.findProperty("jmhFilter") ?: ''
}

task jmhJar(type: Jar) {
dependsOn = ['compileJmhJava']

enabled benchmarksAvailable
description 'Generate executable JMH benchmark JAR, if any benchmarks exists.'
group 'Build'
classifier "jmh"
doFirst {
manifest {
attributes('Main-Class': 'org.openjdk.jmh.Main', 'Class-Path': ([jar.archiveName] + configurations.runtimeClasspath.files.collect { it.getName() }).join(' '))
}
}
from {
// Only include JMH benchmarks and JMH in Jar file
(configurations.jmhRuntimeClasspath.files - configurations.runtimeClasspath.files).collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude 'META-INF/*.SF'
exclude 'META-INF/*.DSA'
exclude 'META-INF/*.RSA'
exclude 'log4j.properties'
}
from sourceSets.jmh.output
}

artifacts {
if (benchmarksAvailable) {
archives jmhJar
}
}

publishing {
publications {
mavenJava(MavenPublication) {
if (benchmarksAvailable) {
artifact jmhJar {
classifier "jmh"
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2019, OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.opentelemetry.sdk.trace.export;

import io.opentelemetry.sdk.trace.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter.ResultCode;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.SpanId;
import io.opentelemetry.trace.Status;
import io.opentelemetry.trace.TraceId;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Measurement;
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.Warmup;

@State(Scope.Benchmark)
public class MultiSpanExporterBenchmark {

private static class NoopSpanExporter implements SpanExporter {

@Override
public ResultCode export(List<SpanData> spans) {
return ResultCode.SUCCESS;
}

@Override
public void shutdown() {}
}

@Param({"1", "3"})
private int exporterCount;

private SpanExporter exporter;

@Param({"1000"})
private int spanCount;

private List<SpanData> spans;

@Setup(Level.Trial)
public final void setup() {
SpanExporter[] exporter = new SpanExporter[exporterCount];
Arrays.fill(exporter, new NoopSpanExporter());
this.exporter = MultiSpanExporter.create(Arrays.asList(exporter));

SpanData[] spans = new SpanData[spanCount];
for (int i = 0; i < spans.length; i++) {
spans[i] =
SpanData.newBuilder()
.setTraceId(new TraceId(1, 1))
.setSpanId(new SpanId(1))
.setName("noop")
.setKind(Span.Kind.CLIENT)
.setStartEpochNanos(1)
.setStatus(Status.OK)
.setEndEpochNanos(2)
.setHasEnded(true)
.build();
}
this.spans = Arrays.asList(spans);
}

@Benchmark
@Fork(1)
@Warmup(iterations = 5, time = 1)
@Measurement(iterations = 10, time = 1)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
public ResultCode export() {
return exporter.export(spans);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
import static io.opentelemetry.sdk.trace.export.SpanExporter.ResultCode.SUCCESS;

import io.opentelemetry.sdk.trace.SpanData;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -36,7 +34,7 @@
*/
public final class MultiSpanExporter implements SpanExporter {
private static final Logger logger = Logger.getLogger(MultiSpanExporter.class.getName());
private final List<SpanExporter> spanExporters;
private final SpanExporter[] spanExporters;

/**
* Constructs and returns an instance of this class.
Expand All @@ -45,7 +43,7 @@ public final class MultiSpanExporter implements SpanExporter {
* @return the aggregate span exporter
*/
public static SpanExporter create(List<SpanExporter> spanExporters) {
return new MultiSpanExporter(Collections.unmodifiableList(new ArrayList<>(spanExporters)));
return new MultiSpanExporter(spanExporters);
}

@Override
Expand Down Expand Up @@ -89,6 +87,6 @@ private static ResultCode mergeResultCode(
}

private MultiSpanExporter(List<SpanExporter> spanExporters) {
this.spanExporters = spanExporters;
this.spanExporters = spanExporters.toArray(new SpanExporter[spanExporters.size()]);
}
}