1 /* 2 * Copyright 2015 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.benchmarks.netty; 18 19 import io.grpc.CallOptions; 20 import io.grpc.stub.ClientCalls; 21 import io.netty.buffer.Unpooled; 22 import org.openjdk.jmh.annotations.Benchmark; 23 import org.openjdk.jmh.annotations.Fork; 24 import org.openjdk.jmh.annotations.Level; 25 import org.openjdk.jmh.annotations.Scope; 26 import org.openjdk.jmh.annotations.Setup; 27 import org.openjdk.jmh.annotations.State; 28 import org.openjdk.jmh.annotations.TearDown; 29 30 /** 31 * Benchmark showing performance of a linear sequence of blocking calls in a single thread which 32 * is the worst case for throughput. The benchmark permutes response payload size and 33 * client inbound flow-control window size. 34 */ 35 @State(Scope.Benchmark) 36 @Fork(1) 37 public class SingleThreadBlockingQpsBenchmark extends AbstractBenchmark { 38 39 /** 40 * Setup with direct executors, small payloads and the default flow control window. 41 */ 42 @Setup(Level.Trial) setup()43 public void setup() throws Exception { 44 super.setup(ExecutorType.DIRECT, 45 ExecutorType.DIRECT, 46 MessageSize.SMALL, 47 MessageSize.SMALL, 48 FlowWindowSize.MEDIUM, 49 ChannelType.NIO, 50 1, 51 1); 52 } 53 54 /** 55 * Stop the server and client channels. 56 */ 57 @Override 58 @TearDown(Level.Trial) teardown()59 public void teardown() throws Exception { 60 Thread.sleep(5000); 61 super.teardown(); 62 } 63 64 /** 65 * Issue a unary call and wait for the response. 66 */ 67 @Benchmark blockingUnary()68 public Object blockingUnary() throws Exception { 69 return ClientCalls.blockingUnaryCall( 70 channels[0].newCall(unaryMethod, CallOptions.DEFAULT), Unpooled.EMPTY_BUFFER); 71 } 72 73 /** 74 * Useful for triggering a subset of the benchmark in a profiler. 75 */ main(String[] argv)76 public static void main(String[] argv) throws Exception { 77 SingleThreadBlockingQpsBenchmark bench = new SingleThreadBlockingQpsBenchmark(); 78 bench.setup(); 79 for (int i = 0; i < 10000; i++) { 80 bench.blockingUnary(); 81 } 82 Thread.sleep(30000); 83 bench.teardown(); 84 System.exit(0); 85 } 86 } 87