使用 GoReplay 对业务流量进行录制与回放

流量录制与回放是一个非常有用的功能,在国内也慢慢开始流行起来,主要用于在测试环境更好模拟真实的流量,而不是简单的使用压测工具进行压测,这样的压测是不真实的,因为真实的流量是非常复杂的,而且很多时候压测工具也无法模拟出来,所以流量录制与回放就是为了解决这个问题。另外还有正式环境中,如果出现了一些问题,可以将问题流量录制下来,然后在测试环境进行回放,这样就可以很好的复现问题,从而更好的解决问题。

我们先安装下 GoReplay,因为涉及到底层的监听,我并没有使用 Docker 方式,而是直接在服务器上安装,这样可以更好的控制。

1
2
3
4
5
$ wget https://github.com/buger/goreplay/releases/download/1.3.3/gor_1.3.3_x64.tar.gz -O /tmp/gor.tar.gz
$ mkdir -p /usr/local/gor
$ tar xzvf /tmp/gor.tar.gz -C /usr/local/gor
$ rm -rf /tmp/gor.tar.gz
$ ln -s /usr/local/gor/gor /usr/local/bin/gor

安装完成后我们启动两个 WEB 服务。

1
2
$ docker run -d --name web-80 -p 80:80 ghcr.io/hongfs/env:web-1
$ docker run -d --name web-88 -p 88:80 ghcr.io/hongfs/env:web-1

现在启动一个流量录制,将 80 端口的流量录制下来。

1
$ gor --input-raw :80 --output-file=/tmp/web80.gor -output-stdout

接着我们用 curl 访问一下 80 端口的服务。

1
$ curl http://local.hongfs.cn/output

可以看见录制是有输出内容的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ gor --input-raw :80 --output-file=/tmp/web80.gor -output-stdout
Interface: eth0 . BPF Filter: ((tcp dst port 80) and (dst host 10.0.16.15 or dst host fe80::5054:ff:fed2:e474))
Interface: docker0 . BPF Filter: ((tcp dst port 80) and (dst host 172.17.0.1 or dst host fe80::42:21ff:fe4a:7640))
Interface: veth51ea98c . BPF Filter: ((tcp dst port 80) and (dst host fe80::3841:e1ff:feec:a5b8))
Interface: veth16d83cd . BPF Filter: ((tcp dst port 80) and (dst host fe80::906a:35ff:fec0:433))
Interface: lo . BPF Filter: ((tcp dst port 80) and (dst host 127.0.0.1 or dst host ::1))
2023/06/19 09:57:23 [PPID 9736 and PID 15280] Version:1.3.0
1 9c7000507f0000013b0b05c9 1687139845180728562 0
GET /output HTTP/1.1
User-Agent: curl/7.29.0
Host: local.hongfs.cn
Accept: */*


🐵🙈🙉

现在我们来试一下把流量回放到 88 端口的服务上。

1
$ gor --input-file "/tmp/web80_0.gor" --output-http http://local.hongfs.cn:88/output

测试下流量一直在回放(实现压测)。

1
$ gor --input-file "/tmp/web80_0.gor" -input-file-loop --output-http http://local.hongfs.cn:88/output

另外文档提到可以以倍数进行回放,这边测试是没有成功的。

1
2
# 2 倍速回放(无效)
$ gor --input-file "/tmp/web80_0.gor|200%" --output-http http://local.hongfs.cn:88/output
往上