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

本文详细介绍了Goreplay,一款强大的HTTP实时流量复制工具,它支持流量操作、文件记录和回放分析。通过Golang环境安装、生产环境流量捕获、回放测试以及压力测试的实例,展示了Goreplay的易用性和灵活性。
摘要由CSDN通过智能技术生成

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

1.Goreplay 介绍

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

与 TCPCopy 相比它的架构更简单,只有一个 gor 组件,如下:
在这里插入图片描述
在这里插入图片描述
只需要在生产服务器上启动一个 gor 进程,它负责所有的工作包括监听、过滤和转发。它的设计遵循 Unix 设计哲学:一切都是由管道组成的,各种输入将数据复用为输出。

2.Golang环境安装

1.安装组件

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

https://golang.org/dl/
https://studygolang.com/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/go
export PATH=$PATH:$GOROOT/bin

# 编译生效
source /etc/profile
4.验证 go env
[root@vm ~]# go version
go version go1.15.5 linux/amd64
5.Goreplay 安装

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

在这里插入图片描述
下载二进制包:

[root@vm ~]# curl -L -O https://github.com/buger/goreplay/releases/download/v1.3.1/gor_1.3.1_x64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   626  100   626    0     0    741      0 --:--:-- --:--:-- --:--:--   741
100 10.5M  100 10.5M    0     0  3258k      0  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.gor
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))
2021/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 update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: 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 817 14:33 requests_0.gor
  • 请求文件的内容
[root@vm-1 ~]# cat requests_0.gor 
1 04481f92ac106afb42ce55ca 1629182282437724065 0
GET /person HTTP/1.1
User-Agent: PostmanRuntime/7.28.2
Accept: */*
Cache-Control: no-cache
Postman-Token: ab405d20-ef93-4ffb-b2f6-a16cf1205e7c
Host: 172.16.106.149:8082
Accept-Encoding: gzip, deflate, br
Connection: 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"
2021/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-stdout
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))
2021/08/17 14:55:45 [PPID 18060 and PID 18061] Version:1.3.0
1 22ee1f92ac106afb594d205a 1629183362175214036 0
POST /person/two HTTP/1.1
User-Agent: PostmanRuntime/7.28.2
Accept: */*
Cache-Control: no-cache
Postman-Token: 4922ce23-5706-42c5-8a54-ade5d5aee785
Host: 172.16.106.149:8082
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-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))
2021/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 0
POST /person/two HTTP/1.1
User-Agent: PostmanRuntime/7.28.2
Accept: */*
Cache-Control: no-cache
Postman-Token: a8600dd1-28c1-4825-b6a2-68c42659942f
Host: 172.16.106.149:8082
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Length: 0

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

[root@vm-1 ~]./gor --input-file "requests.gor|200%" --output-http="http://172.16.106.237:8082"
2021/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 update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: 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 的常见玩法,事实上远不止上述提到的这些,更多的玩法有时间再写。

文件源码:

https://github.com/zuozewei/blog-example/tree/master/Performance-testing/04-full-link/springboot-demo

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值