You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

338 lines
10 KiB

  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. //go:generate protoc -I ../grpc_testing --go_out=plugins=grpc:../grpc_testing ../grpc_testing/metrics.proto
  19. // client starts an interop client to do stress test and a metrics server to report qps.
  20. package main
  21. import (
  22. "context"
  23. "flag"
  24. "fmt"
  25. "math/rand"
  26. "net"
  27. "strconv"
  28. "strings"
  29. "sync"
  30. "time"
  31. "google.golang.org/grpc"
  32. "google.golang.org/grpc/codes"
  33. "google.golang.org/grpc/credentials"
  34. "google.golang.org/grpc/grpclog"
  35. "google.golang.org/grpc/interop"
  36. testpb "google.golang.org/grpc/interop/grpc_testing"
  37. "google.golang.org/grpc/status"
  38. metricspb "google.golang.org/grpc/stress/grpc_testing"
  39. "google.golang.org/grpc/testdata"
  40. )
  41. var (
  42. serverAddresses = flag.String("server_addresses", "localhost:8080", "a list of server addresses")
  43. testCases = flag.String("test_cases", "", "a list of test cases along with the relative weights")
  44. testDurationSecs = flag.Int("test_duration_secs", -1, "test duration in seconds")
  45. numChannelsPerServer = flag.Int("num_channels_per_server", 1, "Number of channels (i.e connections) to each server")
  46. numStubsPerChannel = flag.Int("num_stubs_per_channel", 1, "Number of client stubs per each connection to server")
  47. metricsPort = flag.Int("metrics_port", 8081, "The port at which the stress client exposes QPS metrics")
  48. useTLS = flag.Bool("use_tls", false, "Connection uses TLS if true, else plain TCP")
  49. testCA = flag.Bool("use_test_ca", false, "Whether to replace platform root CAs with test CA as the CA root")
  50. tlsServerName = flag.String("server_host_override", "foo.test.google.fr", "The server name use to verify the hostname returned by TLS handshake if it is not empty. Otherwise, --server_host is used.")
  51. caFile = flag.String("ca_file", "", "The file containning the CA root cert file")
  52. )
  53. // testCaseWithWeight contains the test case type and its weight.
  54. type testCaseWithWeight struct {
  55. name string
  56. weight int
  57. }
  58. // parseTestCases converts test case string to a list of struct testCaseWithWeight.
  59. func parseTestCases(testCaseString string) []testCaseWithWeight {
  60. testCaseStrings := strings.Split(testCaseString, ",")
  61. testCases := make([]testCaseWithWeight, len(testCaseStrings))
  62. for i, str := range testCaseStrings {
  63. testCase := strings.Split(str, ":")
  64. if len(testCase) != 2 {
  65. panic(fmt.Sprintf("invalid test case with weight: %s", str))
  66. }
  67. // Check if test case is supported.
  68. switch testCase[0] {
  69. case
  70. "empty_unary",
  71. "large_unary",
  72. "client_streaming",
  73. "server_streaming",
  74. "ping_pong",
  75. "empty_stream",
  76. "timeout_on_sleeping_server",
  77. "cancel_after_begin",
  78. "cancel_after_first_response",
  79. "status_code_and_message",
  80. "custom_metadata":
  81. default:
  82. panic(fmt.Sprintf("unknown test type: %s", testCase[0]))
  83. }
  84. testCases[i].name = testCase[0]
  85. w, err := strconv.Atoi(testCase[1])
  86. if err != nil {
  87. panic(fmt.Sprintf("%v", err))
  88. }
  89. testCases[i].weight = w
  90. }
  91. return testCases
  92. }
  93. // weightedRandomTestSelector defines a weighted random selector for test case types.
  94. type weightedRandomTestSelector struct {
  95. tests []testCaseWithWeight
  96. totalWeight int
  97. }
  98. // newWeightedRandomTestSelector constructs a weightedRandomTestSelector with the given list of testCaseWithWeight.
  99. func newWeightedRandomTestSelector(tests []testCaseWithWeight) *weightedRandomTestSelector {
  100. var totalWeight int
  101. for _, t := range tests {
  102. totalWeight += t.weight
  103. }
  104. rand.Seed(time.Now().UnixNano())
  105. return &weightedRandomTestSelector{tests, totalWeight}
  106. }
  107. func (selector weightedRandomTestSelector) getNextTest() string {
  108. random := rand.Intn(selector.totalWeight)
  109. var weightSofar int
  110. for _, test := range selector.tests {
  111. weightSofar += test.weight
  112. if random < weightSofar {
  113. return test.name
  114. }
  115. }
  116. panic("no test case selected by weightedRandomTestSelector")
  117. }
  118. // gauge stores the qps of one interop client (one stub).
  119. type gauge struct {
  120. mutex sync.RWMutex
  121. val int64
  122. }
  123. func (g *gauge) set(v int64) {
  124. g.mutex.Lock()
  125. defer g.mutex.Unlock()
  126. g.val = v
  127. }
  128. func (g *gauge) get() int64 {
  129. g.mutex.RLock()
  130. defer g.mutex.RUnlock()
  131. return g.val
  132. }
  133. // server implements metrics server functions.
  134. type server struct {
  135. mutex sync.RWMutex
  136. // gauges is a map from /stress_test/server_<n>/channel_<n>/stub_<n>/qps to its qps gauge.
  137. gauges map[string]*gauge
  138. }
  139. // newMetricsServer returns a new metrics server.
  140. func newMetricsServer() *server {
  141. return &server{gauges: make(map[string]*gauge)}
  142. }
  143. // GetAllGauges returns all gauges.
  144. func (s *server) GetAllGauges(in *metricspb.EmptyMessage, stream metricspb.MetricsService_GetAllGaugesServer) error {
  145. s.mutex.RLock()
  146. defer s.mutex.RUnlock()
  147. for name, gauge := range s.gauges {
  148. if err := stream.Send(&metricspb.GaugeResponse{Name: name, Value: &metricspb.GaugeResponse_LongValue{LongValue: gauge.get()}}); err != nil {
  149. return err
  150. }
  151. }
  152. return nil
  153. }
  154. // GetGauge returns the gauge for the given name.
  155. func (s *server) GetGauge(ctx context.Context, in *metricspb.GaugeRequest) (*metricspb.GaugeResponse, error) {
  156. s.mutex.RLock()
  157. defer s.mutex.RUnlock()
  158. if g, ok := s.gauges[in.Name]; ok {
  159. return &metricspb.GaugeResponse{Name: in.Name, Value: &metricspb.GaugeResponse_LongValue{LongValue: g.get()}}, nil
  160. }
  161. return nil, status.Errorf(codes.InvalidArgument, "gauge with name %s not found", in.Name)
  162. }
  163. // createGauge creates a gauge using the given name in metrics server.
  164. func (s *server) createGauge(name string) *gauge {
  165. s.mutex.Lock()
  166. defer s.mutex.Unlock()
  167. if _, ok := s.gauges[name]; ok {
  168. // gauge already exists.
  169. panic(fmt.Sprintf("gauge %s already exists", name))
  170. }
  171. var g gauge
  172. s.gauges[name] = &g
  173. return &g
  174. }
  175. func startServer(server *server, port int) {
  176. lis, err := net.Listen("tcp", ":"+strconv.Itoa(port))
  177. if err != nil {
  178. grpclog.Fatalf("failed to listen: %v", err)
  179. }
  180. s := grpc.NewServer()
  181. metricspb.RegisterMetricsServiceServer(s, server)
  182. s.Serve(lis)
  183. }
  184. // performRPCs uses weightedRandomTestSelector to select test case and runs the tests.
  185. func performRPCs(gauge *gauge, conn *grpc.ClientConn, selector *weightedRandomTestSelector, stop <-chan bool) {
  186. client := testpb.NewTestServiceClient(conn)
  187. var numCalls int64
  188. startTime := time.Now()
  189. for {
  190. test := selector.getNextTest()
  191. switch test {
  192. case "empty_unary":
  193. interop.DoEmptyUnaryCall(client, grpc.WaitForReady(true))
  194. case "large_unary":
  195. interop.DoLargeUnaryCall(client, grpc.WaitForReady(true))
  196. case "client_streaming":
  197. interop.DoClientStreaming(client, grpc.WaitForReady(true))
  198. case "server_streaming":
  199. interop.DoServerStreaming(client, grpc.WaitForReady(true))
  200. case "ping_pong":
  201. interop.DoPingPong(client, grpc.WaitForReady(true))
  202. case "empty_stream":
  203. interop.DoEmptyStream(client, grpc.WaitForReady(true))
  204. case "timeout_on_sleeping_server":
  205. interop.DoTimeoutOnSleepingServer(client, grpc.WaitForReady(true))
  206. case "cancel_after_begin":
  207. interop.DoCancelAfterBegin(client, grpc.WaitForReady(true))
  208. case "cancel_after_first_response":
  209. interop.DoCancelAfterFirstResponse(client, grpc.WaitForReady(true))
  210. case "status_code_and_message":
  211. interop.DoStatusCodeAndMessage(client, grpc.WaitForReady(true))
  212. case "custom_metadata":
  213. interop.DoCustomMetadata(client, grpc.WaitForReady(true))
  214. }
  215. numCalls++
  216. gauge.set(int64(float64(numCalls) / time.Since(startTime).Seconds()))
  217. select {
  218. case <-stop:
  219. return
  220. default:
  221. }
  222. }
  223. }
  224. func logParameterInfo(addresses []string, tests []testCaseWithWeight) {
  225. grpclog.Infof("server_addresses: %s", *serverAddresses)
  226. grpclog.Infof("test_cases: %s", *testCases)
  227. grpclog.Infof("test_duration_secs: %d", *testDurationSecs)
  228. grpclog.Infof("num_channels_per_server: %d", *numChannelsPerServer)
  229. grpclog.Infof("num_stubs_per_channel: %d", *numStubsPerChannel)
  230. grpclog.Infof("metrics_port: %d", *metricsPort)
  231. grpclog.Infof("use_tls: %t", *useTLS)
  232. grpclog.Infof("use_test_ca: %t", *testCA)
  233. grpclog.Infof("server_host_override: %s", *tlsServerName)
  234. grpclog.Infoln("addresses:")
  235. for i, addr := range addresses {
  236. grpclog.Infof("%d. %s\n", i+1, addr)
  237. }
  238. grpclog.Infoln("tests:")
  239. for i, test := range tests {
  240. grpclog.Infof("%d. %v\n", i+1, test)
  241. }
  242. }
  243. func newConn(address string, useTLS, testCA bool, tlsServerName string) (*grpc.ClientConn, error) {
  244. var opts []grpc.DialOption
  245. if useTLS {
  246. var sn string
  247. if tlsServerName != "" {
  248. sn = tlsServerName
  249. }
  250. var creds credentials.TransportCredentials
  251. if testCA {
  252. var err error
  253. if *caFile == "" {
  254. *caFile = testdata.Path("ca.pem")
  255. }
  256. creds, err = credentials.NewClientTLSFromFile(*caFile, sn)
  257. if err != nil {
  258. grpclog.Fatalf("Failed to create TLS credentials %v", err)
  259. }
  260. } else {
  261. creds = credentials.NewClientTLSFromCert(nil, sn)
  262. }
  263. opts = append(opts, grpc.WithTransportCredentials(creds))
  264. } else {
  265. opts = append(opts, grpc.WithInsecure())
  266. }
  267. return grpc.Dial(address, opts...)
  268. }
  269. func main() {
  270. flag.Parse()
  271. addresses := strings.Split(*serverAddresses, ",")
  272. tests := parseTestCases(*testCases)
  273. logParameterInfo(addresses, tests)
  274. testSelector := newWeightedRandomTestSelector(tests)
  275. metricsServer := newMetricsServer()
  276. var wg sync.WaitGroup
  277. wg.Add(len(addresses) * *numChannelsPerServer * *numStubsPerChannel)
  278. stop := make(chan bool)
  279. for serverIndex, address := range addresses {
  280. for connIndex := 0; connIndex < *numChannelsPerServer; connIndex++ {
  281. conn, err := newConn(address, *useTLS, *testCA, *tlsServerName)
  282. if err != nil {
  283. grpclog.Fatalf("Fail to dial: %v", err)
  284. }
  285. defer conn.Close()
  286. for clientIndex := 0; clientIndex < *numStubsPerChannel; clientIndex++ {
  287. name := fmt.Sprintf("/stress_test/server_%d/channel_%d/stub_%d/qps", serverIndex+1, connIndex+1, clientIndex+1)
  288. go func() {
  289. defer wg.Done()
  290. g := metricsServer.createGauge(name)
  291. performRPCs(g, conn, testSelector, stop)
  292. }()
  293. }
  294. }
  295. }
  296. go startServer(metricsServer, *metricsPort)
  297. if *testDurationSecs > 0 {
  298. time.Sleep(time.Duration(*testDurationSecs) * time.Second)
  299. close(stop)
  300. }
  301. wg.Wait()
  302. grpclog.Infof(" ===== ALL DONE ===== ")
  303. }