首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >gRPC 性能测试利器:ghz 工具详解

gRPC 性能测试利器:ghz 工具详解

原创
作者头像
不做虫子
发布2025-09-08 12:27:03
发布2025-09-08 12:27:03
1050
举报

背景

最近要深入研究下限流器,要研究限流首先要模拟大流量,那就得找一款比较合适好用的压测工具

组成测试服务

拉出我的祖传测试服务

代码语言:go
复制
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介绍

简介

ghz 是一个用 Go 语言编写的 gRPC 性能测试工具,类似于 HTTP 的 Apache Bench (ab) 或 wrk

主要特性

  • 高性能:基于 Go 语言,性能优异
  • 多协议支持:支持 unary、streaming 等多种 gRPC 调用模式
  • 灵活配置:丰富的参数配置,满足不同测试需求
  • 详细报告:提供详细的性能指标和统计信息(很有用)
  • 易于使用:命令行工具,使用简单

安装

我是mac上直接命令行安装

代码语言:bash
复制
brew install ghz

核心参数详解

-c 并发数(共享连接)

控制 goroutine 工作线程的数量,影响测试的并发度。

代码语言:bash
复制
# 使用 10 个并发线程
ghz -c 10 localhost:9988

使用场景:

  • 功能测试:-c 1-5
  • 性能测试:-c 10-50
  • 压力测试:-c 100+

--connections(连接数)

控制 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 of 10 and using 5 connections will result in 10 goroutine workers, each pair of 2 workers sharing 1 of the 5 connections. Each worker will get its share of the total number of requests specified using -n option.

意思是说,连接是连接,并发数是并发数,并发数均分链接。

例如-c=10 -connections=5,那么每两个并发协程共享一个grpc链接。

理论上设置成1比1是比较符合线上环境的,但是也要看网关侧是否配置了连接池,连接池能复用连接,减少开销。

连接配置建议:

  • 轻量级服务:-c 10 --connections 2-5
  • 中等负载服务:-c 20 --connections 5-10
  • 高负载服务:-c 50 --connections 15-25

-n (总请求数)

指定总共要发送的请求数量。

代码语言:bash
复制
ghz -n 1000 localhost:9988

-r (速率限制)

限制每秒发送的请求数量。默认是以最大速率请求

代码语言:bash
复制
# 每秒最多发送 100 个请求
ghz -r 100 localhost:9988

-z (持续时间)

指定测试的持续时间。

代码语言:bash
复制
# 持续测试 30 秒
ghz -z 30s localhost:9988

输出结果

总结

ghz 作为专业的 gRPC 性能测试工具,非常好用。

通过合理配置各种参数,可以评估 gRPC 服务的性能表现。

在实际应用中,建议根据具体的业务场景和测试目标,选择合适的参数配置,以获得准确的性能测试结果。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 背景
  • 组成测试服务
  • ghz介绍
    • 简介
      • 主要特性
    • 安装
    • 核心参数详解
      • -c 并发数(共享连接)
      • --connections(连接数)
      • -n (总请求数)
      • -r (速率限制)
      • -z (持续时间)
    • 输出结果
  • 总结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档