goreplay 是一个开源工具,抓取生产环境流量,并使用真实流量持续测试系统。

image.png


它使用raw-socket 抓取系统流量,并根据指定的url ,将流量进行转发。


githup地址:https://github.com/buger/goreplay/wiki

下载安装包:https://github.com/buger/goreplay/releases

可以直接下载二进制文件gor_0.16.1_x64.tar.gz, 解压缩后是一个可执行的二进制文件


使用方法:

启动两个文件服务器的实例

./goreplay file-server :8000

./goreplay file-server :9000


image.png


image.png


访问这两个页面,会将抓到的包输出到终端

image.png



重放数据包

./goreplay  --input-raw :8000 --output-http="http://localhost:9000"

将本机8000端口的流量,复制并转发到http://localhost:9000 这个URL



保存数据包并重放

./goreplay --input-raw :8000 --output-file=requests.gor --output-file-append

将本机8000端口流量保存到当前路径下的request.gor文件中,--output-file-append的作用是加入当前路径下有requests.gor文件存在,则将数据包追加到该文件,如果不加该参数

则会将抓取的数据包以另外的文件保存



从文件中重放流量

./goreplay --input-file requests.gor --output-http="http://localhost:9000"

注意:可以有多个 --input-file request.gor,例如

./goreplay --input-file requests.gor --input-file requests.gor --output-http="http://localhost:9000"

这样就将两倍的流量进行重放了。



转发流量到多个地址

gor --input-tcp :28020 --output-http "http://staging.com"  --output-http "http://dev.com"



将流量均衡的分割到多个环境

gor --input-raw :80 --output-http "http://staging.com"  --output-http "http://dev.com" --split-output true



流量缩放:

流量放大:将流量放大两倍,转发到stagin.com,测试时不生效

./goreplay –input-file "requests.gor|200%" –output-http "staging.com"



流量缩小:将流量只转发10%到staging.com

./goreplay –input-file "requests.gor" –output-http "staging.com|10%"



将流量全部转到到staging.com,但限制速率为每秒不超过10个请求,这样会防止测试环境流量过载造成的性能瓶颈,这个很有用

./goreplay –input-file "requests.gor" –output-http "staging.com|10"


将流量以时间变量的名称保存

gor ... --output-file %Y%m%d.log --output-file-append



流量过滤:

例如:只转发发送到/api的请求

# only forward requests being sent to the /api endpoint
gor --input-raw :8080 --output-http staging.com --http-allow-url /api


只转发除了发送到/api 之外的请求,与上一个相反

# only forward requests NOT being sent to the /api... endpoint
gor --input-raw :8080 --output-http staging.com --http-disallow-url /api




HTTP 流量过滤复制

gor –input-raw :8080 –output-http staging.com –output-http-url-regexp ^www.