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