main.go 2.2 KB

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