gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/stress/metrics_client/main.go (about)

     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  // Binary metrics_client is a client to retrieve metrics from the server.
    20  package main
    21  
    22  import (
    23  	"context"
    24  	"flag"
    25  	"fmt"
    26  	"io"
    27  
    28  	grpc "gitee.com/ks-custle/core-gm/grpc"
    29  	"gitee.com/ks-custle/core-gm/grpc/grpclog"
    30  	metricspb "gitee.com/ks-custle/core-gm/grpc/stress/grpc_testing"
    31  )
    32  
    33  var (
    34  	metricsServerAddress = flag.String("metrics_server_address", "", "The metrics server addresses in the format <hostname>:<port>")
    35  	totalOnly            = flag.Bool("total_only", false, "If true, this prints only the total value of all gauges")
    36  
    37  	logger = grpclog.Component("stress")
    38  )
    39  
    40  func printMetrics(client metricspb.MetricsServiceClient, totalOnly bool) {
    41  	stream, err := client.GetAllGauges(context.Background(), &metricspb.EmptyMessage{})
    42  	if err != nil {
    43  		logger.Fatalf("failed to call GetAllGauges: %v", err)
    44  	}
    45  
    46  	var (
    47  		overallQPS int64
    48  		rpcStatus  error
    49  	)
    50  	for {
    51  		gaugeResponse, err := stream.Recv()
    52  		if err != nil {
    53  			rpcStatus = err
    54  			break
    55  		}
    56  		if _, ok := gaugeResponse.GetValue().(*metricspb.GaugeResponse_LongValue); !ok {
    57  			panic(fmt.Sprintf("gauge %s is not a long value", gaugeResponse.Name))
    58  		}
    59  		v := gaugeResponse.GetLongValue()
    60  		if !totalOnly {
    61  			logger.Infof("%s: %d", gaugeResponse.Name, v)
    62  		}
    63  		overallQPS += v
    64  	}
    65  	if rpcStatus != io.EOF {
    66  		logger.Fatalf("failed to finish server streaming: %v", rpcStatus)
    67  	}
    68  	logger.Infof("overall qps: %d", overallQPS)
    69  }
    70  
    71  func main() {
    72  	flag.Parse()
    73  	if *metricsServerAddress == "" {
    74  		logger.Fatalf("Metrics server address is empty.")
    75  	}
    76  
    77  	conn, err := grpc.Dial(*metricsServerAddress, grpc.WithInsecure())
    78  	if err != nil {
    79  		logger.Fatalf("cannot connect to metrics server: %v", err)
    80  	}
    81  	defer conn.Close()
    82  
    83  	c := metricspb.NewMetricsServiceClient(conn)
    84  	printMetrics(c, *totalOnly)
    85  }