diff options
author | zpencer <spencerfang@google.com> | 2017-08-16 10:38:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-16 10:38:30 -0700 |
commit | 197f0b86680f2465e5be6be3dcb61d8e2d06ac14 (patch) | |
tree | 0198c2a4d2c173ebdbdc8577944ab30a070229ab /benchmarks | |
parent | 47ce000464695bc4b8855fb76cfc0ad15f7384b7 (diff) | |
download | grpc-grpc-java-197f0b86680f2465e5be6be3dcb61d8e2d06ac14.tar.gz |
benchmark: context benchmark (#3263)
* replace read benchmark with the one written by lukesandberg
* add a WriteBechmark
Diffstat (limited to 'benchmarks')
-rw-r--r-- | benchmarks/src/jmh/java/io/grpc/context/AttachDetachBenchmark.java (renamed from benchmarks/src/jmh/java/io/grpc/context/ContextBenchmark.java) | 2 | ||||
-rw-r--r-- | benchmarks/src/jmh/java/io/grpc/context/ReadBenchmark.java | 94 | ||||
-rw-r--r-- | benchmarks/src/jmh/java/io/grpc/context/WriteBenchmark.java | 65 | ||||
-rw-r--r-- | benchmarks/src/jmh/resources/foo.txt | 0 |
4 files changed, 160 insertions, 1 deletions
diff --git a/benchmarks/src/jmh/java/io/grpc/context/ContextBenchmark.java b/benchmarks/src/jmh/java/io/grpc/context/AttachDetachBenchmark.java index dac0eeba2..aa94b6eb8 100644 --- a/benchmarks/src/jmh/java/io/grpc/context/ContextBenchmark.java +++ b/benchmarks/src/jmh/java/io/grpc/context/AttachDetachBenchmark.java @@ -29,7 +29,7 @@ import org.openjdk.jmh.annotations.State; /** StatusBenchmark. */ @State(Scope.Benchmark) -public class ContextBenchmark { +public class AttachDetachBenchmark { private final Key<Integer> key = Context.keyWithDefault("key", 9999); private final Context cu = Context.current().withValue(key, 8888); diff --git a/benchmarks/src/jmh/java/io/grpc/context/ReadBenchmark.java b/benchmarks/src/jmh/java/io/grpc/context/ReadBenchmark.java new file mode 100644 index 000000000..ce2ef1888 --- /dev/null +++ b/benchmarks/src/jmh/java/io/grpc/context/ReadBenchmark.java @@ -0,0 +1,94 @@ +/* + * 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 new file mode 100644 index 000000000..3bf67e876 --- /dev/null +++ b/benchmarks/src/jmh/java/io/grpc/context/WriteBenchmark.java @@ -0,0 +1,65 @@ +/* + * 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/resources/foo.txt b/benchmarks/src/jmh/resources/foo.txt new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/benchmarks/src/jmh/resources/foo.txt |