client.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. *
  3. * Copyright 2014 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. // Binary client is an interop client.
  19. package main
  20. import (
  21. "flag"
  22. "strings"
  23. "sync"
  24. "time"
  25. "google.golang.org/grpc"
  26. "google.golang.org/grpc/credentials/google"
  27. "google.golang.org/grpc/credentials/insecure"
  28. "google.golang.org/grpc/grpclog"
  29. "google.golang.org/grpc/interop"
  30. _ "google.golang.org/grpc/balancer/grpclb" // Register the grpclb load balancing policy.
  31. _ "google.golang.org/grpc/balancer/rls" // Register the RLS load balancing policy.
  32. _ "google.golang.org/grpc/xds/googledirectpath" // Register xDS resolver required for c2p directpath.
  33. testgrpc "google.golang.org/grpc/interop/grpc_testing"
  34. )
  35. const (
  36. computeEngineCredsName = "compute_engine_channel_creds"
  37. insecureCredsName = "INSECURE_CREDENTIALS"
  38. )
  39. var (
  40. serverURIs = flag.String("server_uris", "", "Comma-separated list of sever URIs to make RPCs to")
  41. credentialsTypes = flag.String("credentials_types", "", "Comma-separated list of credentials, each entry is used for the server of the corresponding index in server_uris. Supported values: compute_engine_channel_creds, INSECURE_CREDENTIALS")
  42. soakIterations = flag.Int("soak_iterations", 10, "The number of iterations to use for the two soak tests: rpc_soak and channel_soak")
  43. soakMaxFailures = flag.Int("soak_max_failures", 0, "The number of iterations in soak tests that are allowed to fail (either due to non-OK status code or exceeding the per-iteration max acceptable latency).")
  44. soakPerIterationMaxAcceptableLatencyMs = flag.Int("soak_per_iteration_max_acceptable_latency_ms", 1000, "The number of milliseconds a single iteration in the two soak tests (rpc_soak and channel_soak) should take.")
  45. soakOverallTimeoutSeconds = flag.Int("soak_overall_timeout_seconds", 10, "The overall number of seconds after which a soak test should stop and fail, if the desired number of iterations have not yet completed.")
  46. soakMinTimeMsBetweenRPCs = flag.Int("soak_min_time_ms_between_rpcs", 0, "The minimum time in milliseconds between consecutive RPCs in a soak test (rpc_soak or channel_soak), useful for limiting QPS")
  47. testCase = flag.String("test_case", "rpc_soak",
  48. `Configure different test cases. Valid options are:
  49. rpc_soak: sends --soak_iterations large_unary RPCs;
  50. channel_soak: sends --soak_iterations RPCs, rebuilding the channel each time`)
  51. logger = grpclog.Component("interop")
  52. )
  53. type clientConfig struct {
  54. tc testgrpc.TestServiceClient
  55. opts []grpc.DialOption
  56. uri string
  57. }
  58. func main() {
  59. flag.Parse()
  60. // validate flags
  61. uris := strings.Split(*serverURIs, ",")
  62. creds := strings.Split(*credentialsTypes, ",")
  63. if len(uris) != len(creds) {
  64. logger.Fatalf("Number of entries in --server_uris (%d) != number of entries in --credentials_types (%d)", len(uris), len(creds))
  65. }
  66. for _, c := range creds {
  67. if c != computeEngineCredsName && c != insecureCredsName {
  68. logger.Fatalf("Unsupported credentials type: %v", c)
  69. }
  70. }
  71. var resetChannel bool
  72. switch *testCase {
  73. case "rpc_soak":
  74. resetChannel = false
  75. case "channel_soak":
  76. resetChannel = true
  77. default:
  78. logger.Fatal("Unsupported test case: ", *testCase)
  79. }
  80. // create clients as specified in flags
  81. var clients []clientConfig
  82. for i := range uris {
  83. var opts []grpc.DialOption
  84. switch creds[i] {
  85. case computeEngineCredsName:
  86. opts = append(opts, grpc.WithCredentialsBundle(google.NewComputeEngineCredentials()))
  87. case insecureCredsName:
  88. opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
  89. }
  90. cc, err := grpc.Dial(uris[i], opts...)
  91. if err != nil {
  92. logger.Fatalf("Fail to dial %v: %v", uris[i], err)
  93. }
  94. defer cc.Close()
  95. clients = append(clients, clientConfig{
  96. tc: testgrpc.NewTestServiceClient(cc),
  97. opts: opts,
  98. uri: uris[i],
  99. })
  100. }
  101. // run soak tests with the different clients
  102. logger.Infof("Clients running with test case %q", *testCase)
  103. var wg sync.WaitGroup
  104. for i := range clients {
  105. wg.Add(1)
  106. go func(c clientConfig) {
  107. interop.DoSoakTest(c.tc, c.uri, c.opts, resetChannel, *soakIterations, *soakMaxFailures, time.Duration(*soakPerIterationMaxAcceptableLatencyMs)*time.Millisecond, time.Duration(*soakMinTimeMsBetweenRPCs)*time.Millisecond, time.Now().Add(time.Duration(*soakOverallTimeoutSeconds)*time.Second))
  108. logger.Infof("%s test done for server: %s", *testCase, c.uri)
  109. wg.Done()
  110. }(clients[i])
  111. }
  112. wg.Wait()
  113. logger.Infoln("All clients done!")
  114. }