diff options
author | Eric Anderson <ejona@google.com> | 2017-08-26 17:10:18 -0700 |
---|---|---|
committer | Eric Anderson <ejona@google.com> | 2017-08-28 13:37:39 -0700 |
commit | 6164b7b2ee8066c5f047f6ea49c477b6f9e3aba6 (patch) | |
tree | 8e9b37119be250528049f41ac52eda80e4d5b56f /benchmarks | |
parent | 812e65a5ca109610422177b6e62eddd8465d01f9 (diff) | |
download | grpc-grpc-java-6164b7b2ee8066c5f047f6ea49c477b6f9e3aba6.tar.gz |
Move jmh benchmarks to their respective modules
The benchmarks should be close to the code they're benchmarking, like
we do with tests.
This includes a bugfix to SerializingExecutorBenchmark to let it run.
The io.grpc.benchmarks.netty benchmarks in benchmarks/ depend on
ByteBufOutputMarshaller from benchmarks's main, so they were not moved.
Diffstat (limited to 'benchmarks')
12 files changed, 3 insertions, 1038 deletions
diff --git a/benchmarks/build.gradle b/benchmarks/build.gradle index 7794375ad..221dd95ad 100644 --- a/benchmarks/build.gradle +++ b/benchmarks/build.gradle @@ -1,20 +1,12 @@ buildscript { repositories { mavenCentral() - maven { - url "https://plugins.gradle.org/m2/" - } } dependencies { - classpath libraries.math classpath libraries.protobuf_plugin } } -plugins { - id "me.champeau.gradle.jmh" version "0.4.4" -} - apply plugin: 'application' description = "grpc Benchmarks" @@ -23,11 +15,10 @@ startScripts.enabled = false run.enabled = false jmh { - jmhVersion = '1.19' - warmupIterations = 10 - iterations = 10 - fork = 1 jvmArgs = "-server -Xms2g -Xmx2g" + // Workaround + // https://github.com/melix/jmh-gradle-plugin/issues/97#issuecomment-316664026 + includeTests = true } dependencies { @@ -50,10 +41,6 @@ compileJava { options.compilerArgs += ["-Xep:JavaLangClash:OFF"] } -compileJmhJava { - options.compilerArgs = compileJava.options.compilerArgs -} - configureProtoCompilation() def vmArgs = [ diff --git a/benchmarks/src/jmh/java/io/grpc/DecompressorRegistryBenchmark.java b/benchmarks/src/jmh/java/io/grpc/DecompressorRegistryBenchmark.java deleted file mode 100644 index fdcad7542..000000000 --- a/benchmarks/src/jmh/java/io/grpc/DecompressorRegistryBenchmark.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2016, gRPC Authors All rights reserved. - * - * 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.grpc; - -import io.grpc.internal.GrpcUtil; -import io.grpc.internal.TransportFrameUtil; -import java.io.IOException; -import java.io.InputStream; -import java.util.UUID; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -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; - -/** - * Decomressor Registry encoding benchmark. - */ -@State(Scope.Benchmark) -public class DecompressorRegistryBenchmark { - - @Param({"0", "1", "2"}) - public int extraEncodings; - - private DecompressorRegistry reg = DecompressorRegistry.getDefaultInstance(); - - @Setup - public void setUp() throws Exception { - reg = DecompressorRegistry.getDefaultInstance(); - for (int i = 0; i < extraEncodings; i++) { - reg = reg.with(new Decompressor() { - - @Override - public String getMessageEncoding() { - return UUID.randomUUID().toString(); - - } - - @Override - public InputStream decompress(InputStream is) throws IOException { - return null; - - } - }, true); - } - } - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public byte[][] marshalOld() { - Metadata m = new Metadata(); - m.put( - GrpcUtil.MESSAGE_ACCEPT_ENCODING_KEY, - InternalDecompressorRegistry.getRawAdvertisedMessageEncodings(reg)); - return TransportFrameUtil.toHttp2Headers(m); - } -} - diff --git a/benchmarks/src/jmh/java/io/grpc/StatusBenchmark.java b/benchmarks/src/jmh/java/io/grpc/StatusBenchmark.java deleted file mode 100644 index c09ebad67..000000000 --- a/benchmarks/src/jmh/java/io/grpc/StatusBenchmark.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2016, gRPC Authors All rights reserved. - * - * 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.grpc; - -import java.nio.charset.Charset; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.State; - -/** StatusBenchmark. */ -@State(Scope.Benchmark) -public class StatusBenchmark { - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public byte[] messageEncodePlain() { - return Status.MESSAGE_KEY.toBytes("Unexpected RST in stream"); - } - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public byte[] messageEncodeEscape() { - return Status.MESSAGE_KEY.toBytes("Some Error\nWasabi and Horseradish are the same"); - } - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public String messageDecodePlain() { - return Status.MESSAGE_KEY.parseBytes( - "Unexpected RST in stream".getBytes(Charset.forName("US-ASCII"))); - } - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public String messageDecodeEscape() { - return Status.MESSAGE_KEY.parseBytes( - "Some Error%10Wasabi and Horseradish are the same".getBytes(Charset.forName("US-ASCII"))); - } - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public byte[] codeEncode() { - return Status.CODE_KEY.toBytes(Status.DATA_LOSS); - } - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public Status codeDecode() { - return Status.CODE_KEY.parseBytes("15".getBytes(Charset.forName("US-ASCII"))); - } -} - diff --git a/benchmarks/src/jmh/java/io/grpc/benchmarks/CallOptionsBenchmark.java b/benchmarks/src/jmh/java/io/grpc/benchmarks/CallOptionsBenchmark.java deleted file mode 100644 index c2fdac961..000000000 --- a/benchmarks/src/jmh/java/io/grpc/benchmarks/CallOptionsBenchmark.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright 2016, gRPC Authors All rights reserved. - * - * 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.grpc.benchmarks; - -import io.grpc.CallOptions; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Random; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -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; - -/** - * Call options benchmark. - */ -@State(Scope.Benchmark) -public class CallOptionsBenchmark { - - @Param({"1", "2", "4", "8"}) - public int customOptionsCount; - - private List<CallOptions.Key<String>> customOptions; - - private CallOptions allOpts; - private List<CallOptions.Key<String>> shuffledCustomOptions; - - /** - * Setup. - */ - @Setup - public void setUp() throws Exception { - customOptions = new ArrayList<CallOptions.Key<String>>(customOptionsCount); - for (int i = 0; i < customOptionsCount; i++) { - customOptions.add(CallOptions.Key.of("name " + i, "defaultvalue")); - } - - allOpts = CallOptions.DEFAULT; - for (int i = 0; i < customOptionsCount; i++) { - allOpts = allOpts.withOption(customOptions.get(i), "value"); - } - - shuffledCustomOptions = new ArrayList<CallOptions.Key<String>>(customOptions); - // Make the shuffling deterministic - Collections.shuffle(shuffledCustomOptions, new Random(1)); - } - - /** - * Adding custom call options without duplicate keys. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public CallOptions withOption() { - CallOptions opts = CallOptions.DEFAULT; - for (int i = 0; i < customOptions.size(); i++) { - opts = opts.withOption(customOptions.get(i), "value"); - } - return opts; - } - - /** - * Adding custom call options, overwritting existing keys. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public CallOptions withOptionDuplicates() { - CallOptions opts = allOpts; - for (int i = 1; i < shuffledCustomOptions.size(); i++) { - opts = opts.withOption(shuffledCustomOptions.get(i), "value2"); - } - return opts; - } -} diff --git a/benchmarks/src/jmh/java/io/grpc/context/AttachDetachBenchmark.java b/benchmarks/src/jmh/java/io/grpc/context/AttachDetachBenchmark.java deleted file mode 100644 index aa94b6eb8..000000000 --- a/benchmarks/src/jmh/java/io/grpc/context/AttachDetachBenchmark.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2016, gRPC Authors All rights reserved. - * - * 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.grpc.context; - -import io.grpc.Context; -import io.grpc.Context.Key; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.GroupThreads; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.State; - -/** StatusBenchmark. */ -@State(Scope.Benchmark) -public class AttachDetachBenchmark { - - private final Key<Integer> key = Context.keyWithDefault("key", 9999); - private final Context cu = Context.current().withValue(key, 8888); - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - @GroupThreads(6) - public int attachDetach() { - Context old = cu.attach(); - try { - return key.get(); - } finally { - Context.current().detach(old); - } - } -} diff --git a/benchmarks/src/jmh/java/io/grpc/context/ReadBenchmark.java b/benchmarks/src/jmh/java/io/grpc/context/ReadBenchmark.java deleted file mode 100644 index ce2ef1888..000000000 --- a/benchmarks/src/jmh/java/io/grpc/context/ReadBenchmark.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Copyright 2017, gRPC Authors All rights reserved. - * - * 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.grpc.context; - -import io.grpc.Context; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.Setup; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.infra.Blackhole; - -/** Read benchmark. */ -public class ReadBenchmark { - - @State(Scope.Benchmark) - public static class ContextState { - List<Context.Key<Object>> keys = new ArrayList<Context.Key<Object>>(); - List<Context> contexts = new ArrayList<Context>(); - - @Setup - public void setup() { - for (int i = 0; i < 8; i++) { - keys.add(Context.key("Key" + i)); - } - contexts.add(Context.ROOT.withValue(keys.get(0), new Object())); - contexts.add(Context.ROOT.withValues(keys.get(0), new Object(), keys.get(1), new Object())); - contexts.add( - Context.ROOT.withValues( - keys.get(0), new Object(), keys.get(1), new Object(), keys.get(2), new Object())); - contexts.add( - Context.ROOT.withValues( - keys.get(0), - new Object(), - keys.get(1), - new Object(), - keys.get(2), - new Object(), - keys.get(3), - new Object())); - contexts.add(contexts.get(0).withValue(keys.get(1), new Object())); - contexts.add( - contexts.get(1).withValues(keys.get(2), new Object(), keys.get(3), new Object())); - contexts.add( - contexts - .get(2) - .withValues( - keys.get(3), new Object(), keys.get(4), new Object(), keys.get(5), new Object())); - contexts.add( - contexts - .get(3) - .withValues( - keys.get(4), - new Object(), - keys.get(5), - new Object(), - keys.get(6), - new Object(), - keys.get(7), - new Object())); - } - } - - /** Perform the read operation. */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public void testContextLookup(ContextState state, Blackhole bh) { - for (Context.Key<?> key : state.keys) { - for (Context ctx : state.contexts) { - bh.consume(key.get(ctx)); - } - } - } -} diff --git a/benchmarks/src/jmh/java/io/grpc/context/WriteBenchmark.java b/benchmarks/src/jmh/java/io/grpc/context/WriteBenchmark.java deleted file mode 100644 index 3bf67e876..000000000 --- a/benchmarks/src/jmh/java/io/grpc/context/WriteBenchmark.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2017, gRPC Authors All rights reserved. - * - * 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.grpc.context; - -import io.grpc.Context; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -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.infra.Blackhole; - -/** Write benchmark. */ -public class WriteBenchmark { - @State(Scope.Thread) - public static class ContextState { - @Param({"0", "10", "25", "100"}) - public int preexistingKeys; - - Context.Key<Object> key1 = Context.key("key1"); - Context.Key<Object> key2 = Context.key("key2"); - Context.Key<Object> key3 = Context.key("key3"); - Context.Key<Object> key4 = Context.key("key4"); - Context context; - Object val = new Object(); - - @Setup - public void setup() { - context = Context.ROOT; - for (int i = 0; i < preexistingKeys; i++) { - context = context.withValue(Context.key("preexisting_key" + i), val); - } - } - } - - /** Perform the write operation. */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public Context doWrite(ContextState state, Blackhole bh) { - return state.context.withValues( - state.key1, state.val, - state.key2, state.val, - state.key3, state.val, - state.key4, state.val); - } -} diff --git a/benchmarks/src/jmh/java/io/grpc/internal/SerializingExecutorBenchmark.java b/benchmarks/src/jmh/java/io/grpc/internal/SerializingExecutorBenchmark.java deleted file mode 100644 index 1f7e5cbc3..000000000 --- a/benchmarks/src/jmh/java/io/grpc/internal/SerializingExecutorBenchmark.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright 2017, gRPC Authors All rights reserved. - * - * 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.grpc.internal; - -import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Phaser; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.annotations.TearDown; - -/** - * SerializingExecutor benchmark. - * - * <p>Since this is a microbenchmark, don't actually believe the numbers in a strict sense. Instead, - * it is a gauge that the code is behaving roughly as expected, to increase confidence that our - * understanding of the code is correct (and will behave as expected in other cases). Even more - * helpfully it pushes the implementation, which should weed out many multithreading bugs. - */ -@State(Scope.Thread) -public class SerializingExecutorBenchmark { - - private ExecutorService executorService; - private Executor executor = new SerializingExecutor(executorService); - - private static class IncrRunnable implements Runnable { - int val; - - @Override - public void run() { - val++; - } - } - - private final IncrRunnable incrRunnable = new IncrRunnable(); - - private final Phaser phaser = new Phaser(2); - private final Runnable phaserRunnable = new Runnable() { - @Override - public void run() { - phaser.arrive(); - } - }; - - @TearDown - public void tearDown() throws Exception { - executorService.shutdownNow(); - if (!executorService.awaitTermination(1, TimeUnit.SECONDS)) { - throw new RuntimeException("executor failed to shut down in a timely fashion"); - } - } - - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public void oneRunnableLatency() throws Exception { - executor.execute(phaserRunnable); - phaser.arriveAndAwaitAdvance(); - } - - /** - * Queue many runnables, to better see queuing/consumption cost instead of just context switch. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public void manyRunnables() throws Exception { - incrRunnable.val = 0; - for (int i = 0; i < 500; i++) { - executor.execute(incrRunnable); - } - executor.execute(phaserRunnable); - phaser.arriveAndAwaitAdvance(); - if (incrRunnable.val != 500) { - throw new AssertionError(); - } - } -}
\ No newline at end of file diff --git a/benchmarks/src/jmh/java/io/grpc/internal/StatsTraceContextBenchmark.java b/benchmarks/src/jmh/java/io/grpc/internal/StatsTraceContextBenchmark.java deleted file mode 100644 index 19e43b506..000000000 --- a/benchmarks/src/jmh/java/io/grpc/internal/StatsTraceContextBenchmark.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2017, gRPC Authors All rights reserved. - * - * 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.grpc.internal; - -import io.grpc.CallOptions; -import io.grpc.Metadata; -import io.grpc.MethodDescriptor; -import io.grpc.ServerStreamTracer; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.State; - -/** - * Benchmark for {@link StatsTraceContext}. - */ -@State(Scope.Benchmark) -public class StatsTraceContextBenchmark { - - private final String methodName = MethodDescriptor.generateFullMethodName("service", "method"); - - private final Metadata emptyMetadata = new Metadata(); - private final List<ServerStreamTracer.Factory> serverStreamTracerFactories = - Collections.<ServerStreamTracer.Factory>emptyList(); - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public StatsTraceContext newClientContext() { - return StatsTraceContext.newClientContext(CallOptions.DEFAULT, emptyMetadata); - } - - /** - * Javadoc comment. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public StatsTraceContext newServerContext_empty() { - return StatsTraceContext.newServerContext( - serverStreamTracerFactories, methodName, emptyMetadata); - } -} diff --git a/benchmarks/src/jmh/java/io/grpc/netty/InboundHeadersBenchmark.java b/benchmarks/src/jmh/java/io/grpc/netty/InboundHeadersBenchmark.java deleted file mode 100644 index 192a3d3d8..000000000 --- a/benchmarks/src/jmh/java/io/grpc/netty/InboundHeadersBenchmark.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright 2016, gRPC Authors All rights reserved. - * - * 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.grpc.netty; - -import static io.grpc.netty.Utils.CONTENT_TYPE_HEADER; -import static io.grpc.netty.Utils.TE_TRAILERS; -import static io.netty.util.AsciiString.of; - -import io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2RequestHeaders; -import io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2ResponseHeaders; -import io.netty.handler.codec.http2.DefaultHttp2Headers; -import io.netty.handler.codec.http2.Http2Headers; -import io.netty.util.AsciiString; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.CompilerControl; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.State; -import org.openjdk.jmh.infra.Blackhole; - -/** - * Benchmarks for {@link GrpcHttp2RequestHeaders} and {@link GrpcHttp2ResponseHeaders}. - */ -@State(Scope.Thread) -public class InboundHeadersBenchmark { - - private static AsciiString[] requestHeaders; - private static AsciiString[] responseHeaders; - - static { - setupRequestHeaders(); - setupResponseHeaders(); - } - - // Headers taken from the gRPC spec. - private static void setupRequestHeaders() { - requestHeaders = new AsciiString[18]; - int i = 0; - requestHeaders[i++] = of(":method"); - requestHeaders[i++] = of("POST"); - requestHeaders[i++] = of(":scheme"); - requestHeaders[i++] = of("http"); - requestHeaders[i++] = of(":path"); - requestHeaders[i++] = of("/google.pubsub.v2.PublisherService/CreateTopic"); - requestHeaders[i++] = of(":authority"); - requestHeaders[i++] = of("pubsub.googleapis.com"); - requestHeaders[i++] = of("te"); - requestHeaders[i++] = of("trailers"); - requestHeaders[i++] = of("grpc-timeout"); - requestHeaders[i++] = of("1S"); - requestHeaders[i++] = of("content-type"); - requestHeaders[i++] = of("application/grpc+proto"); - requestHeaders[i++] = of("grpc-encoding"); - requestHeaders[i++] = of("gzip"); - requestHeaders[i++] = of("authorization"); - requestHeaders[i] = of("Bearer y235.wef315yfh138vh31hv93hv8h3v"); - } - - private static void setupResponseHeaders() { - responseHeaders = new AsciiString[4]; - int i = 0; - responseHeaders[i++] = of(":status"); - responseHeaders[i++] = of("200"); - responseHeaders[i++] = of("grpc-encoding"); - responseHeaders[i] = of("gzip"); - } - - /** - * Checkstyle. - */ - @Benchmark - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public void grpcHeaders_serverHandler(Blackhole bh) { - serverHandler(bh, new GrpcHttp2RequestHeaders(4)); - } - - /** - * Checkstyle. - */ - @Benchmark - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public void defaultHeaders_serverHandler(Blackhole bh) { - serverHandler(bh, new DefaultHttp2Headers(true, 9)); - } - - /** - * Checkstyle. - */ - @Benchmark - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public void grpcHeaders_clientHandler(Blackhole bh) { - clientHandler(bh, new GrpcHttp2ResponseHeaders(2)); - } - - /** - * Checkstyle. - */ - @Benchmark - @BenchmarkMode(Mode.AverageTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public void defaultHeaders_clientHandler(Blackhole bh) { - clientHandler(bh, new DefaultHttp2Headers(true, 2)); - } - - @CompilerControl(CompilerControl.Mode.INLINE) - private static void serverHandler(Blackhole bh, Http2Headers headers) { - for (int i = 0; i < requestHeaders.length; i += 2) { - bh.consume(headers.add(requestHeaders[i], requestHeaders[i + 1])); - } - - // Sequence of headers accessed in NettyServerHandler - bh.consume(headers.get(TE_TRAILERS)); - bh.consume(headers.get(CONTENT_TYPE_HEADER)); - bh.consume(headers.method()); - bh.consume(headers.get(CONTENT_TYPE_HEADER)); - bh.consume(headers.path()); - - bh.consume(Utils.convertHeaders(headers)); - } - - @CompilerControl(CompilerControl.Mode.INLINE) - private static void clientHandler(Blackhole bh, Http2Headers headers) { - // NettyClientHandler does not directly access headers, but convert to Metadata immediately. - - bh.consume(headers.add(responseHeaders[0], responseHeaders[1])); - bh.consume(headers.add(responseHeaders[2], responseHeaders[3])); - - bh.consume(Utils.convertHeaders(headers)); - } - -// /** -// * Prints the size of the header objects in bytes. Needs JOL (Java Object Layout) as a -// * dependency. -// */ -// public static void main(String... args) { -// Http2Headers grpcRequestHeaders = new GrpcHttp2RequestHeaders(4); -// Http2Headers defaultRequestHeaders = new DefaultHttp2Headers(true, 9); -// for (int i = 0; i < requestHeaders.length; i += 2) { -// grpcRequestHeaders.add(requestHeaders[i], requestHeaders[i + 1]); -// defaultRequestHeaders.add(requestHeaders[i], requestHeaders[i + 1]); -// } -// long c = 10L; -// int m = ((int) c) / 20; -// -// long grpcRequestHeadersBytes = GraphLayout.parseInstance(grpcRequestHeaders).totalSize(); -// long defaultRequestHeadersBytes = -// GraphLayout.parseInstance(defaultRequestHeaders).totalSize(); -// -// System.out.printf("gRPC Request Headers: %d bytes%nNetty Request Headers: %d bytes%n", -// grpcRequestHeadersBytes, defaultRequestHeadersBytes); -// -// Http2Headers grpcResponseHeaders = new GrpcHttp2RequestHeaders(4); -// Http2Headers defaultResponseHeaders = new DefaultHttp2Headers(true, 9); -// for (int i = 0; i < responseHeaders.length; i += 2) { -// grpcResponseHeaders.add(responseHeaders[i], responseHeaders[i + 1]); -// defaultResponseHeaders.add(responseHeaders[i], responseHeaders[i + 1]); -// } -// -// long grpcResponseHeadersBytes = GraphLayout.parseInstance(grpcResponseHeaders).totalSize(); -// long defaultResponseHeadersBytes = -// GraphLayout.parseInstance(defaultResponseHeaders).totalSize(); -// -// System.out.printf("gRPC Response Headers: %d bytes%nNetty Response Headers: %d bytes%n", -// grpcResponseHeadersBytes, defaultResponseHeadersBytes); -// } -} diff --git a/benchmarks/src/jmh/java/io/grpc/netty/MethodDescriptorBenchmark.java b/benchmarks/src/jmh/java/io/grpc/netty/MethodDescriptorBenchmark.java deleted file mode 100644 index 9183cbc43..000000000 --- a/benchmarks/src/jmh/java/io/grpc/netty/MethodDescriptorBenchmark.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright 2016, gRPC Authors All rights reserved. - * - * 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.grpc.netty; - -import io.grpc.InternalKnownTransport; -import io.grpc.InternalMethodDescriptor; -import io.grpc.MethodDescriptor; -import io.netty.util.AsciiString; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -import org.openjdk.jmh.annotations.Mode; -import org.openjdk.jmh.annotations.OutputTimeUnit; -import org.openjdk.jmh.annotations.Scope; -import org.openjdk.jmh.annotations.State; - -/** - * Benchmark for Method Descriptors. - */ -@State(Scope.Benchmark) -public class MethodDescriptorBenchmark { - - private static final MethodDescriptor.Marshaller<Void> marshaller = - new MethodDescriptor.Marshaller<Void>() { - @Override - public InputStream stream(Void value) { - return new ByteArrayInputStream(new byte[]{}); - } - - @Override - public Void parse(InputStream stream) { - return null; - } - }; - - MethodDescriptor<Void, Void> method = MethodDescriptor.<Void, Void>newBuilder() - .setType(MethodDescriptor.MethodType.UNARY) - .setFullMethodName("Service/Method") - .setRequestMarshaller(marshaller) - .setResponseMarshaller(marshaller) - .build(); - - InternalMethodDescriptor imd = new InternalMethodDescriptor(InternalKnownTransport.NETTY); - - byte[] directBytes = new AsciiString("/" + method.getFullMethodName()).toByteArray(); - - /** Foo bar. */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public AsciiString old() { - return new AsciiString("/" + method.getFullMethodName()); - } - - /** Foo bar. */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public AsciiString transportSpecific() { - AsciiString path; - if ((path = (AsciiString) imd.geRawMethodName(method)) != null) { - path = new AsciiString("/" + method.getFullMethodName()); - imd.setRawMethodName(method, path); - } - return path; - } - - /** Foo bar. */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public AsciiString direct() { - return new AsciiString(directBytes, false); - } -} - diff --git a/benchmarks/src/jmh/java/io/grpc/netty/OutboundHeadersBenchmark.java b/benchmarks/src/jmh/java/io/grpc/netty/OutboundHeadersBenchmark.java deleted file mode 100644 index d06d004e5..000000000 --- a/benchmarks/src/jmh/java/io/grpc/netty/OutboundHeadersBenchmark.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright 2016, gRPC Authors All rights reserved. - * - * 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.grpc.netty; - -import io.grpc.Metadata; -import io.grpc.Metadata.AsciiMarshaller; -import io.netty.buffer.ByteBuf; -import io.netty.buffer.UnpooledByteBufAllocator; -import io.netty.handler.codec.http2.DefaultHttp2HeadersEncoder; -import io.netty.handler.codec.http2.Http2Headers; -import io.netty.handler.codec.http2.Http2HeadersEncoder; -import io.netty.util.AsciiString; -import java.util.UUID; -import java.util.concurrent.TimeUnit; -import org.openjdk.jmh.annotations.Benchmark; -import org.openjdk.jmh.annotations.BenchmarkMode; -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; - -/** - * Header encoding benchmark. - */ -@State(Scope.Benchmark) -public class OutboundHeadersBenchmark { - @Param({"1", "5", "10", "20"}) - public int headerCount; - - private final AsciiMarshaller<String> keyMarshaller = new AsciiMarshaller<String>() { - @Override - public String toAsciiString(String value) { - return value; - } - - @Override - public String parseAsciiString(String serialized) { - return serialized; - } - }; - - private final Metadata metadata = new Metadata(); - private final AsciiString scheme = new AsciiString("https"); - private final AsciiString defaultPath = new AsciiString("/Service.MethodMethodMethod"); - private final AsciiString authority = new AsciiString("authority.googleapis.bogus"); - private final AsciiString userAgent = new AsciiString("grpc-java-netty"); - private final Http2HeadersEncoder headersEncoder = new DefaultHttp2HeadersEncoder(); - private final ByteBuf scratchBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(4096); - - @Setup - public void setUp() throws Exception { - for (int i = 0; i < headerCount; i++) { - metadata.put(Metadata.Key.of("key-" + i, keyMarshaller), UUID.randomUUID().toString()); - } - } - - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public Http2Headers convertClientHeaders() { - return Utils.convertClientHeaders(metadata, scheme, defaultPath, authority, Utils.HTTP_METHOD, - userAgent); - } - - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public Http2Headers convertServerHeaders() { - return Utils.convertServerHeaders(metadata); - } - - /** - * This will encode the random metadata fields, and repeatedly lookup the default other headers. - */ - @Benchmark - @BenchmarkMode(Mode.SampleTime) - @OutputTimeUnit(TimeUnit.NANOSECONDS) - public ByteBuf encodeClientHeaders() throws Exception { - scratchBuffer.clear(); - Http2Headers headers = - Utils.convertClientHeaders(metadata, scheme, defaultPath, authority, Utils.HTTP_METHOD, - userAgent); - headersEncoder.encodeHeaders(1, headers, scratchBuffer); - return scratchBuffer; - } -} |