最近要深入研究下限流器,要研究限流首先要模拟大流量,那就得找一款比较合适好用的压测工具
拉出我的祖传测试服务
package main
import (
"context"
"fmt"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"grpc_test/hellopb"
)
type helloServerImpl struct {
hellopb.UnimplementedHelloServiceServer
}
func main() {
startGrpcServer()
}
func startGrpcServer() {
server := zrpc.MustNewServer(zrpc.RpcServerConf{
ListenOn: "0.0.0.0:9988",
}, func(grpcServer *grpc.Server) {
hellopb.RegisterHelloServiceServer(grpcServer, &helloServerImpl{})
reflection.Register(grpcServer)
})
fmt.Println("Starting rpc server at 0.0.0.0:9988...")
server.Start()
}
func (s *helloServerImpl) SayHello(ctx context.Context, req *hellopb.HelloRequest) (*hellopb.HelloReply, error) {
return &hellopb.HelloReply{
Message: "Hello " + req.Name,
}, nil
}
ghz 是一个用 Go 语言编写的 gRPC 性能测试工具,类似于 HTTP 的 Apache Bench (ab) 或 wrk
我是mac上直接命令行安装
brew install ghz
控制 goroutine 工作线程的数量,影响测试的并发度。
# 使用 10 个并发线程
ghz -c 10 localhost:9988
使用场景:
控制 gRPC 连接的数量,连接数不能超过并发数。
这个参数和-c有点区别,官方文档是这么解释的
By default we use a single gRPC connection for the whole test run, and the concurrency (
-c
) is achieved using goroutine workers sharing this single connection. The number of gRPC connections used can be controlled using this parameter. This parameter cannot exceed concurrency option. The specified number of connections will be distributed evenly to be shared among the concurrency goroutine workers. So for example a concurrency of10
and using5
connections will result in10
goroutine workers, each pair of2
workers sharing1
of the5
connections. Each worker will get its share of the total number of requests specified using-n
option.
意思是说,连接是连接,并发数是并发数,并发数均分链接。
例如-c=10 -connections=5,那么每两个并发协程共享一个grpc链接。
理论上设置成1比1是比较符合线上环境的,但是也要看网关侧是否配置了连接池,连接池能复用连接,减少开销。
连接配置建议:
指定总共要发送的请求数量。
ghz -n 1000 localhost:9988
限制每秒发送的请求数量。默认是以最大速率请求
# 每秒最多发送 100 个请求
ghz -r 100 localhost:9988
指定测试的持续时间。
# 持续测试 30 秒
ghz -z 30s localhost:9988
ghz 作为专业的 gRPC 性能测试工具,非常好用。
通过合理配置各种参数,可以评估 gRPC 服务的性能表现。
在实际应用中,建议根据具体的业务场景和测试目标,选择合适的参数配置,以获得准确的性能测试结果。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。