aboutsummaryrefslogtreecommitdiff
path: root/context
diff options
context:
space:
mode:
authorEric Anderson <ejona@google.com>2017-08-26 17:10:18 -0700
committerEric Anderson <ejona@google.com>2017-08-28 13:37:39 -0700
commit6164b7b2ee8066c5f047f6ea49c477b6f9e3aba6 (patch)
tree8e9b37119be250528049f41ac52eda80e4d5b56f /context
parent812e65a5ca109610422177b6e62eddd8465d01f9 (diff)
downloadgrpc-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 'context')
-rw-r--r--context/src/jmh/java/io/grpc/AttachDetachBenchmark.java51
-rw-r--r--context/src/jmh/java/io/grpc/ReadBenchmark.java93
-rw-r--r--context/src/jmh/java/io/grpc/WriteBenchmark.java64
3 files changed, 208 insertions, 0 deletions
diff --git a/context/src/jmh/java/io/grpc/AttachDetachBenchmark.java b/context/src/jmh/java/io/grpc/AttachDetachBenchmark.java
new file mode 100644
index 000000000..52f0245c7
--- /dev/null
+++ b/context/src/jmh/java/io/grpc/AttachDetachBenchmark.java
@@ -0,0 +1,51 @@
+/*
+ * 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.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/context/src/jmh/java/io/grpc/ReadBenchmark.java b/context/src/jmh/java/io/grpc/ReadBenchmark.java
new file mode 100644
index 000000000..a789ed898
--- /dev/null
+++ b/context/src/jmh/java/io/grpc/ReadBenchmark.java
@@ -0,0 +1,93 @@
+/*
+ * 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;
+
+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/context/src/jmh/java/io/grpc/WriteBenchmark.java b/context/src/jmh/java/io/grpc/WriteBenchmark.java
new file mode 100644
index 000000000..f081900e9
--- /dev/null
+++ b/context/src/jmh/java/io/grpc/WriteBenchmark.java
@@ -0,0 +1,64 @@
+/*
+ * 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;
+
+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);
+ }
+}