700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 性能工具之 Goreplay 安装及入门使用

性能工具之 Goreplay 安装及入门使用

时间:2022-12-27 16:12:06

相关推荐

性能工具之 Goreplay 安装及入门使用

性能工具之 Goreplay 安装及入门使用

1.Goreplay 介绍

Goreplay 是用 Golang 写的一个 HTTP 实时流量复制工具。功能更强大,支持流量的放大、缩小,频率限制,还支持把请求记录到文件,方便回放和分析,也支持和 ElasticSearch 集成,将流量存入 ES 进行实时分析。GoReplay 不是代理,而是监听网络接口上的流量,不需要更改生产基础架构,而是在与服务相同的计算机上运行 GoReplay 守护程序。特点:简单易用地址:/buger/goreplaystars:14.1k

与 TCPCopy 相比它的架构更简单,只有一个 gor 组件,如下:

只需要在生产服务器上启动一个 gor 进程,它负责所有的工作包括监听、过滤和转发。它的设计遵循 Unix 设计哲学:一切都是由管道组成的,各种输入将数据复用为输出。

2.Golang环境安装

1.安装组件

首先安装Golang及相关依赖环境 在官网上下载安装包或者go的一些中文网站上下载安装包

/dl/

/dl

我下载的是: go1.15.5.linux-amd64.tar.gz

2.解压到/usr/local目录下

tar -C /usr/local -zxvf go1.14.4.linux-amd64.tar.gz

3.配置环境变量

# 打开vim /etc/profile# 添加export GOROOT=/usr/local/goexport PATH=$PATH:$GOROOT/bin# 编译生效source /etc/profile

4.验证 go env

[root@vm ~]# go versiongo version go1.15.5 linux/amd64

5.Goreplay 安装

从/buger/gor/releases 下载最新的 Gor 二进制文件(提供 Windows、Linux x64 和 Mac OS 的预编译二进制文件),也可以自己编译 。

下载二进制包:

[root@vm ~]# curl -L -O /buger/goreplay/releases/download/v1.3.1/gor_1.3.1_x64.tar.gz% Total % Received % Xferd Average Speed Time TimeTime CurrentDload Upload Total Spent Left Speed100 626 100 626 00 7410 --:--:-- --:--:-- --:--:-- 741100 10.5M 100 10.5M 00 3258k0 0:00:03 0:00:03 --:--:-- 5951k[root@vm-jmeter ~]# tar xvzf gor_1.3.1_x64.tar.gz gor

解压缩包后,您可以从当前目录运行 Gor,或者您可能希望将二进制文件复制到您的 PATH(对于 Linux 和 Mac OS,它可以是 /usr/local/bin)

3.Goreplay 使用示例

3.1.接口服务环境部署

准备一个RESTful API 接口部署到两个地址服务器上,模拟生产环境和测试环境。

3.2.捕获生产环境流量保存到本地文件

在终端运行这个命令:

#将端口 8082 流量保存到本地的文件sudo ./gor --input-raw :8082 --output-file=requests.gor

[root@vm-1 ~]# sudo ./gor --input-raw :8082 --output-file=requests.gorInterface: eth0 . BPF Filter: ((tcp dst port 8082) and (dst host 172.16.106.149 or dst host fe80::f599:6e65:3dc9:b84a))Interface: lo . BPF Filter: ((tcp dst port 8082) and (dst host 127.0.0.1 or dst host ::1))Interface: virbr0 . BPF Filter: ((tcp dst port 8082) and (dst host 192.168.124.1))/08/17 14:26:41 [PPID 17654 and PID 17655] Version:1.3.0

注意:使用–output-file-append 选项可以将捕获流量保存为一个单独的文件 使用sudo并要求输入密码:为了分析网络,Gor 需要只有超级用户才能使用的权限。但是,可以为非 root 用户配置运行Gor 。

发送请求

通过接口请求工具向服务器接口发送请求,模拟用户操作行为。 查看生产服务日志

看到生产环境产生了日志,gor运行正常。

Hibernate: select person0_.id as id1_0_, person0_.age as age2_0_, person0_.name as name3_0_ from person person0_Hibernate: select next_val as id_val from hibernate_sequence for updateHibernate: update hibernate_sequence set next_val= ? where next_val=?Hibernate: select next_val as id_val from hibernate_sequence for updateHibernate: update hibernate_sequence set next_val= ? where next_val=?Hibernate: insert into person (age, name, id) values (?, ?, ?)Hibernate: insert into person (age, name, id) values (?, ?, ?)

停掉gor

我们停掉在服务器上运行的 Gor,可以看到已经生成了录制的请求文件

[root@vm-1 ~]# ls -l | grep "requests"-rw-r----- 1 root root 300 8月 17 14:33 requests_0.gor

请求文件的内容

[root@vm-1 ~]# cat requests_0.gor 1 04481f92ac106afb42ce55ca 1629182282437724065 0GET /person HTTP/1.1User-Agent: PostmanRuntime/7.28.2Accept: */*Cache-Control: no-cachePostman-Token: ab405d20-ef93-4ffb-b2f6-a16cf1205e7cHost: 172.16.106.149:8082Accept-Encoding: gzip, deflate, brConnection: keep-alive

3.3.将流量从文件回放到测试服务

现在是时候将原始流量回放到测试服务器中了。如果是多个请求并且它们将按照与记录的顺序和时间完全相同的顺序重放。

//--output-http 提供测试服务的 URL./gor --input-file requests_0.gor --output-http="http://172.16.106.237:8082"

[root@vm-1 ~]./gor --input-file requests_0.gor --output-http="http://172.16.106.237:8082"/08/17 14:42:55 [PPID 12356 and PID 17914] Version:1.3.0[DEBUG][elapsed 985.792µs]: [INPUT-FILE] FileInput: end of file 'requests_0.gor'

3.4.将 http 的请求打印到终端

sudo ./gor --input-raw :8082 --output-stdout

此命令其记录到标准控制台输出,请注意,默认情况下 GoReplay 不跟踪响应,您可以使用 --output-http-track-response 选项启用它们。

[root@vm-1 ~]sudo ./gor --input-raw :8082 --output-stdoutInterface: eth0 . BPF Filter: ((tcp dst port 8082) and (dst host 172.16.106.149 or dst host fe80::f599:6e65:3dc9:b84a))Interface: lo . BPF Filter: ((tcp dst port 8082) and (dst host 127.0.0.1 or dst host ::1))Interface: virbr0 . BPF Filter: ((tcp dst port 8082) and (dst host 192.168.124.1))/08/17 14:55:45 [PPID 18060 and PID 18061] Version:1.3.01 22ee1f92ac106afb594d205a 1629183362175214036 0POST /person/two HTTP/1.1User-Agent: PostmanRuntime/7.28.2Accept: */*Cache-Control: no-cachePostman-Token: 4922ce23-5706-42c5-8a54-ade5d5aee785Host: 172.16.106.149:8082Accept-Encoding: gzip, deflate, brConnection: keep-aliveContent-Length: 0

3.5.将 http 的请求实时转发到测试服务

sudo ./gor --input-raw :8082 --output-http="http://172.16.106.237:8082"

[root@vm-1 ~]# sudo ./gor --input-raw :8082 --output-http="http://172.16.106.237:8082"Interface: eth0 . BPF Filter: ((tcp dst port 8082) and (dst host 172.16.106.149 or dst host fe80::f599:6e65:3dc9:b84a))Interface: lo . BPF Filter: ((tcp dst port 8082) and (dst host 127.0.0.1 or dst host ::1))Interface: virbr0 . BPF Filter: ((tcp dst port 8082) and (dst host 192.168.124.1))/08/17 14:59:49 [PPID 18118 and PID 18119] Version:1.3.0

3.6.压力测试

GOR 支持将捕获到的生产实际请求流量减少或者放大回放以用于各种环境的压力测试。

录制的请求文件:

[root@vm-1 ~]# cat requests.gor 1 040f1f92ac106afb7e8d5679 1629183084044026882 0POST /person/two HTTP/1.1User-Agent: PostmanRuntime/7.28.2Accept: */*Cache-Control: no-cachePostman-Token: a8600dd1-28c1-4825-b6a2-68c42659942fHost: 172.16.106.149:8082Accept-Encoding: gzip, deflate, brConnection: keep-aliveContent-Length: 0

运行以下命令,将流量从文件回放到测试服务,并放大两倍:

[root@vm-1 ~]./gor --input-file "requests.gor|200%" --output-http="http://172.16.106.237:8082"/08/17 15:03:58 [PPID 12356 and PID 18187] Version:1.3.0[DEBUG][elapsed 1.361742ms]: [INPUT-FILE] FileInput: end of file 'requests.gor'

查看下测试服务日志的处理情况

Hibernate: select next_val as id_val from hibernate_sequence for updateHibernate: update hibernate_sequence set next_val= ? where next_val=?Hibernate: select next_val as id_val from hibernate_sequence for updateHibernate: update hibernate_sequence set next_val= ? where next_val=?Hibernate: insert into person (age, name, id) values (?, ?, ?)Hibernate: insert into person (age, name, id) values (?, ?, ?)

注意,当然也也支持请求流量 10%,20% 等缩小。

4.总结

今天简单介绍 Gor 的常见玩法,事实上远不止上述提到的这些,更多的玩法有时间再写。

文件源码:

/zuozewei/blog-example/tree/master/Performance-testing/04-full-link/springboot-demo

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。