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.
 
 
 

91 lines
2.4 KiB

  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. )
  45. func main() {
  46. flag.Parse()
  47. if *testName == "" {
  48. grpclog.Fatalf("test name not set")
  49. }
  50. lis, err := net.Listen("tcp", ":"+*port)
  51. if err != nil {
  52. grpclog.Fatalf("Failed to listen: %v", err)
  53. }
  54. defer lis.Close()
  55. cf, err := os.Create("/tmp/" + *testName + ".cpu")
  56. if err != nil {
  57. grpclog.Fatalf("Failed to create file: %v", err)
  58. }
  59. defer cf.Close()
  60. pprof.StartCPUProfile(cf)
  61. cpuBeg := syscall.GetCPUTime()
  62. // Launch server in a separate goroutine.
  63. stop := benchmark.StartServer(benchmark.ServerInfo{Type: "protobuf", Listener: lis})
  64. // Wait on OS terminate signal.
  65. ch := make(chan os.Signal, 1)
  66. signal.Notify(ch, os.Interrupt)
  67. <-ch
  68. cpu := time.Duration(syscall.GetCPUTime() - cpuBeg)
  69. stop()
  70. pprof.StopCPUProfile()
  71. mf, err := os.Create("/tmp/" + *testName + ".mem")
  72. if err != nil {
  73. grpclog.Fatalf("Failed to create file: %v", err)
  74. }
  75. defer mf.Close()
  76. runtime.GC() // materialize all statistics
  77. if err := pprof.WriteHeapProfile(mf); err != nil {
  78. grpclog.Fatalf("Failed to write memory profile: %v", err)
  79. }
  80. fmt.Println("Server CPU utilization:", cpu)
  81. fmt.Println("Server CPU profile:", cf.Name())
  82. fmt.Println("Server Mem Profile:", mf.Name())
  83. }