OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / stress / metrics_client / main.go
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18
19 package main
20
21 import (
22         "flag"
23         "fmt"
24         "io"
25
26         "golang.org/x/net/context"
27         "google.golang.org/grpc"
28         "google.golang.org/grpc/grpclog"
29         metricspb "google.golang.org/grpc/stress/grpc_testing"
30 )
31
32 var (
33         metricsServerAddress = flag.String("metrics_server_address", "", "The metrics server addresses in the fomrat <hostname>:<port>")
34         totalOnly            = flag.Bool("total_only", false, "If true, this prints only the total value of all gauges")
35 )
36
37 func printMetrics(client metricspb.MetricsServiceClient, totalOnly bool) {
38         stream, err := client.GetAllGauges(context.Background(), &metricspb.EmptyMessage{})
39         if err != nil {
40                 grpclog.Fatalf("failed to call GetAllGuages: %v", err)
41         }
42
43         var (
44                 overallQPS int64
45                 rpcStatus  error
46         )
47         for {
48                 gaugeResponse, err := stream.Recv()
49                 if err != nil {
50                         rpcStatus = err
51                         break
52                 }
53                 if _, ok := gaugeResponse.GetValue().(*metricspb.GaugeResponse_LongValue); !ok {
54                         panic(fmt.Sprintf("gauge %s is not a long value", gaugeResponse.Name))
55                 }
56                 v := gaugeResponse.GetLongValue()
57                 if !totalOnly {
58                         grpclog.Printf("%s: %d", gaugeResponse.Name, v)
59                 }
60                 overallQPS += v
61         }
62         if rpcStatus != io.EOF {
63                 grpclog.Fatalf("failed to finish server streaming: %v", rpcStatus)
64         }
65         grpclog.Printf("overall qps: %d", overallQPS)
66 }
67
68 func main() {
69         flag.Parse()
70         if *metricsServerAddress == "" {
71                 grpclog.Fatalf("Metrics server address is empty.")
72         }
73
74         conn, err := grpc.Dial(*metricsServerAddress, grpc.WithInsecure())
75         if err != nil {
76                 grpclog.Fatalf("cannot connect to metrics server: %v", err)
77         }
78         defer conn.Close()
79
80         c := metricspb.NewMetricsServiceClient(conn)
81         printMetrics(c, *totalOnly)
82 }