...

Source file src/google.golang.org/grpc/benchmark/server/main.go

Documentation: google.golang.org/grpc/benchmark/server

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

View as plain text