栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Go语言

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

Go语言 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

性能工具之 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 8月  17 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: 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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/990782.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号