0%

TarsBenchmark 使用指南

TarsBenchmark 可以用来压测 tars 服务,方便测试服务的性能。TarsBenchmark 主要依赖 tars2case 命令和 tb 命令。

tars2case 命令用于生成测试用例,tb 命令用来进行压测。

构建环境

可以根据以下的 dockerfile 手动构建镜像,然后根据镜像生成容器。

1
2
3
FROM golang:latest
RUN apt update && apt install cmake bison flex zlib1g-dev -y && rm -rf /var/lib/apt/lists/*
RUN cd /root && git clone https://github.com/TarsCloud/TarsCpp.git --recursive && mkdir -p /root/TarsCpp/build && cd /root/TarsCpp/build && cmake .. && make install && cd /root && git clone https://github.com/TarsCloud/TarsBenchmark.git && mkdir -p /root/TarsBenchmark/build && cd /root/TarsBenchmark/build && cmake .. && make all && export PATH=$PATH:/usr/local/tars/cpp/tools:/root/TarsBenchmark/build/bin

或者直接利用现有镜像,

1
2
docker run -td --name golang golang:latest
docker exec -it golang bash

进入容器后执行:

1
2
apt update && apt install cmake bison flex zlib1g-dev && rm -rf /var/lib/apt/lists/*
cd /root && git clone https://github.com/TarsCloud/TarsCpp.git --recursive && mkdir -p /root/TarsCpp/build && cd /root/TarsCpp/build && cmake .. && make install && cd /root && git clone https://github.com/TarsCloud/TarsBenchmark.git && mkdir -p /root/TarsBenchmark/build && cd /root/TarsBenchmark/build && cmake .. && make all && export PATH=$PATH:/usr/local/tars/cpp/tools:/root/TarsBenchmark/build/bin

生成用例文件

例如你的 Tars 文件是这样的,名字为 test.tars

1
2
3
4
5
module Test {
interface Greet {
void Hello(string text, out string response);
};
};

进入 docker 容器,执行

1
tars2case --json test.tars --dir=.

得到如下文件

1
2
3
|-- Hello.case
|-- Hello.desc
`-- test.tars

每个方法会生成一个对应的 desc 和 case 文件,前者为描述文件,后者为测试用例。

Hello.desc

1
{ "1_text_string": "" }

Hello.case

1
{ "text": "string" }

修改用例内容

tb 对 tars 结构中 Number 和 string 支持生成随机内容,通过如下两种随机方式(Value 必须以 string)。

<范围随机值>用 [1-100] 表示,表示在 1-100 内随机出现, 必须是数字。
<限定随机值>用 [369,aaa,bbb] 表示,表示在 369,aaa,bbb 中随机出现。

例如修改 Hello.case,

1
{ "text": "[369, aaa, bbb]" }

执行用例

执行以下命令开始压测,压测会每隔指定时间展示该段时间内的请求结果,最终会生成汇总结果。

1
tb -c 5 -s 100 -I 60 -D 172.16.1.139 -P 10037 -p json -i 10 -I 60 -S QDINT.BookshelfService.BookshelfObj -M getAllItemList

执行压测时当前目录需要有对应的 .case 和 .desc 的用例文件,上边的命令需要有 getAllItemList.case 和 getAllItemList.desc 这 2 个文件。

参数说明

1
2
3
4
5
6
7
8
9
10
11
-h                   帮助信息
-c 连接数量
-D 压测服务器IP,多个IP用';'区隔
-P 网络传输端口
-p 接口通信协议(tars|http)
-t(可选) 单个请求超时时间,默认3秒
-T(可选) 网络层协议,默认tcp
-I(可选) 压测持续时间(单位秒),默认1H
-i(可选) 控制台打印周期时间(单位秒),默认10秒
-s(可选) 最大速率限制,为空或0表示自动适配服务最佳速率
-n(可选) 最大压测进程限制,默认跟CPU核心数