3 * Copyright 2017 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
30 "golang.org/x/net/context"
31 "google.golang.org/grpc"
32 "google.golang.org/grpc/benchmark"
33 testpb "google.golang.org/grpc/benchmark/grpc_testing"
34 "google.golang.org/grpc/benchmark/stats"
35 "google.golang.org/grpc/grpclog"
39 server = flag.String("server", "", "The server address")
40 maxConcurrentRPCs = flag.Int("max_concurrent_rpcs", 1, "The max number of concurrent RPCs")
41 duration = flag.Int("duration", math.MaxInt32, "The duration in seconds to run the benchmark client")
42 trace = flag.Bool("trace", true, "Whether tracing is on")
43 rpcType = flag.Int("rpc_type", 0,
44 `Configure different client rpc type. Valid options are:
49 func unaryCaller(client testpb.BenchmarkServiceClient) {
50 benchmark.DoUnaryCall(client, 1, 1)
53 func streamCaller(stream testpb.BenchmarkService_StreamingCallClient) {
54 benchmark.DoStreamingRoundTrip(stream, 1, 1)
57 func buildConnection() (s *stats.Stats, conn *grpc.ClientConn, tc testpb.BenchmarkServiceClient) {
58 s = stats.NewStats(256)
59 conn = benchmark.NewClientConn(*server)
60 tc = testpb.NewBenchmarkServiceClient(conn)
64 func closeLoopUnary() {
65 s, conn, tc := buildConnection()
67 for i := 0; i < 100; i++ {
70 ch := make(chan int, *maxConcurrentRPCs*4)
75 wg.Add(*maxConcurrentRPCs)
77 for i := 0; i < *maxConcurrentRPCs; i++ {
82 elapse := time.Since(start)
90 // Stop the client when time is up.
91 done := make(chan struct{})
93 <-time.After(time.Duration(*duration) * time.Second)
107 grpclog.Println(s.String())
111 func closeLoopStream() {
112 s, conn, tc := buildConnection()
113 ch := make(chan int, *maxConcurrentRPCs*4)
118 wg.Add(*maxConcurrentRPCs)
119 // Distribute RPCs over maxConcurrentCalls workers.
120 for i := 0; i < *maxConcurrentRPCs; i++ {
122 stream, err := tc.StreamingCall(context.Background())
124 grpclog.Fatalf("%v.StreamingCall(_) = _, %v", tc, err)
127 for i := 0; i < 100; i++ {
133 elapse := time.Since(start)
141 // Stop the client when time is up.
142 done := make(chan struct{})
144 <-time.After(time.Duration(*duration) * time.Second)
158 grpclog.Println(s.String())
163 grpc.EnableTracing = *trace
165 lis, err := net.Listen("tcp", ":0")
167 grpclog.Fatalf("Failed to listen: %v", err)
169 grpclog.Println("Client profiling address: ", lis.Addr().String())
170 if err := http.Serve(lis, nil); err != nil {
171 grpclog.Fatalf("Failed to serve: %v", err)