gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/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  
    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  	"os"
    36  	"os/signal"
    37  	"runtime"
    38  	"runtime/pprof"
    39  	"time"
    40  
    41  	_ "gitee.com/ks-custle/core-gm/gmhttp/pprof"
    42  
    43  	"gitee.com/ks-custle/core-gm/grpc/benchmark"
    44  	"gitee.com/ks-custle/core-gm/grpc/grpclog"
    45  	"gitee.com/ks-custle/core-gm/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.Fatalf("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  	// Wait on OS terminate signal.
    76  	ch := make(chan os.Signal, 1)
    77  	signal.Notify(ch, os.Interrupt)
    78  	<-ch
    79  	cpu := time.Duration(syscall.GetCPUTime() - cpuBeg)
    80  	stop()
    81  	pprof.StopCPUProfile()
    82  	mf, err := os.Create("/tmp/" + *testName + ".mem")
    83  	if err != nil {
    84  		logger.Fatalf("Failed to create file: %v", err)
    85  	}
    86  	defer mf.Close()
    87  	runtime.GC() // materialize all statistics
    88  	if err := pprof.WriteHeapProfile(mf); err != nil {
    89  		logger.Fatalf("Failed to write memory profile: %v", err)
    90  	}
    91  	fmt.Println("Server CPU utilization:", cpu)
    92  	fmt.Println("Server CPU profile:", cf.Name())
    93  	fmt.Println("Server Mem Profile:", mf.Name())
    94  }