main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. *
  3. * Copyright 2017 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 provides a server used for benchmarking. It launches a server
  20. which is listening on port 50051. An example to start the server can be found
  21. at:
  22. go run benchmark/server/main.go -test_name=grpc_test
  23. After starting the server, the client can be run separately and used to test
  24. qps and latency.
  25. */
  26. package main
  27. import (
  28. "flag"
  29. "fmt"
  30. "net"
  31. _ "net/http/pprof"
  32. "os"
  33. "os/signal"
  34. "runtime"
  35. "runtime/pprof"
  36. "time"
  37. "google.golang.org/grpc/benchmark"
  38. "google.golang.org/grpc/grpclog"
  39. "google.golang.org/grpc/internal/syscall"
  40. )
  41. var (
  42. port = flag.String("port", "50051", "Localhost port to listen on.")
  43. testName = flag.String("test_name", "", "Name of the test used for creating profiles.")
  44. logger = grpclog.Component("benchmark")
  45. )
  46. func main() {
  47. flag.Parse()
  48. if *testName == "" {
  49. logger.Fatal("-test_name not set")
  50. }
  51. lis, err := net.Listen("tcp", ":"+*port)
  52. if err != nil {
  53. logger.Fatalf("Failed to listen: %v", err)
  54. }
  55. defer lis.Close()
  56. cf, err := os.Create("/tmp/" + *testName + ".cpu")
  57. if err != nil {
  58. logger.Fatalf("Failed to create file: %v", err)
  59. }
  60. defer cf.Close()
  61. pprof.StartCPUProfile(cf)
  62. cpuBeg := syscall.GetCPUTime()
  63. // Launch server in a separate goroutine.
  64. stop := benchmark.StartServer(benchmark.ServerInfo{Type: "protobuf", Listener: lis})
  65. // Wait on OS terminate signal.
  66. ch := make(chan os.Signal, 1)
  67. signal.Notify(ch, os.Interrupt)
  68. <-ch
  69. cpu := time.Duration(syscall.GetCPUTime() - cpuBeg)
  70. stop()
  71. pprof.StopCPUProfile()
  72. mf, err := os.Create("/tmp/" + *testName + ".mem")
  73. if err != nil {
  74. logger.Fatalf("Failed to create file: %v", err)
  75. }
  76. defer mf.Close()
  77. runtime.GC() // materialize all statistics
  78. if err := pprof.WriteHeapProfile(mf); err != nil {
  79. logger.Fatalf("Failed to write memory profile: %v", err)
  80. }
  81. fmt.Println("Server CPU utilization:", cpu)
  82. fmt.Println("Server CPU profile:", cf.Name())
  83. fmt.Println("Server Mem Profile:", mf.Name())
  84. }