1、以下内容如需转载请标注出处:
http://www.code4fs.xyz/article/47/
2、同时该内容,部分参考以下链接:
https://github.com/buger/goreplay/wiki https://www.jianshu.com/p/858b5099a193
一、简介
随着应用程序的增长,测试它所需的工作也呈指数增长。 GoReplay提供了一个简单的想法,可以重用现有流量进行测试,从而使其功能异常强大。先进的技术可让您分析和记录您的应用程序流量,而不会对其造成影响。这消除了将第三方组件置于关键路径中所带来的风险。
GoReplay是一个用于捕获和回放实时HTTP流量的开源工具,可以通过真实的数据不断在测试环境测试你的系统。
GoReplay提供了独特的屏蔽处理方法。后台GoReplay不再是代理,而是侦听网络接口上的流量,无需更改生产基础结构,而是在与服务相同的计算机上运行GoReplay守护程序。
GoReplay的使用增强了您对代码部署,配置和基础架构更改后的信心。
二、准备
2.1 准备工作
Mac: Mojave 10.14.6 Go: https://dl.google.com/go/go1.13.5.darwin-amd64.pkg python:3.7.5 安装这里就不再阐述,如有需要,请www.google.com
确认GO环境安装成功
$go version
go version go1.13.5 darwin/amd64
三、本地Demo演练
3.1 编写一个基于python的http接口服务
3.1.1 创建一个python3的虚拟环境
kenwu@KenMBP-2 ~/Documents/pythonsyscode/gortest python3 -m venv venv kenwu@KenMBP-2 ~/Documents/pythonsyscode/gortest ll total 0 drwxr-xr-x 6 kenwu staff 192B 12 18 20:56 venv kenwu@KenMBP-2 ~/Documents/pythonsyscode/gortest source venv/bin/activate (venv) kenwu@KenMBP-2 ~/Documents/pythonsyscode/gortest ll total 0 drwxr-xr-x 6 kenwu staff 192B 12 18 20:56 venv
3.1.2 创建一个基于flask的工程
(venv) kenwu@KenMBP-2 ~/Documents/pythonsyscode/gortest pip install flask (venv) kenwu@KenMBP-2 ~/Documents/pythonsyscode/gortest pip install flask-restful
app.py:
# -*- coding: utf-8 -*- # by kenwu 2019/12/18 from flask import Flask from flask_restful import Api, Resource app = Flask(__name__) api = Api(app) TODOS = { 'todo1': {'task': 'build an API'}, 'todo2': {'task': '呵呵!'}, } class TodoList(Resource): def get(self): return TODOS api.add_resource(TodoList, '/todos') if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=8000)
启动运行flask服务:
3.2 MAC下安装Goreplay
从https://github.com/buger/gor/releases下载最新的 Gor 二进制文件(我们为 Windows、Linux x64 和 Mac OS 提供预编译的二进制文件),或者您可以自行编译编译https://github.com/buger/goreplay/wiki/Compilation。
这里我们下载Mac版本:https://github.com/buger/goreplay/releases/download/v1.0.0/gor_1.0.0_mac.tar.gz
3.2.1 解压并捕获流量通过终端输出
sudo ./gor --input-raw :8000 --output-stdout 此命令表示侦听端口 8000 上发生的所有网络活动并将其记录到粗壮。如果您熟悉 tcpdump,我们将实现类似的功能。
您可能会注意到,它使用 sudo 并请求密码:为了分析网络,Gor 需要仅对超级用户可用的权限。但是,可以配置为非根用户运行 Gor,查看一下链接:https://github.com/buger/goreplay/wiki/Running-as-non-root-user。
通过在浏览器中打开http://localhost:8000/todos,或者只需在终端调用 curl http://localhost:8000/todos,发送一些请求。您应该看到gor将所有 HTTP请求直接输出到运行它的终端窗口。请注意,默认情况下GoReplay不跟踪响应,您可以通过使用--output-http-track-response 选项启用。
kenwu@KenMBP-2 ~/Documents/tooltest pwd /Users/kenwu/Documents/tooltest kenwu@KenMBP-2 ~/Documents/tooltest tar xvf gor_1.0.0_mac.tar-2.gz x gor kenwu@KenMBP-2 ~/Documents/tooltest ll -a total 48728 drwxr-xr-x 4 kenwu staff 128B 12 18 21:34 . drwx------+ 22 kenwu staff 704B 12 18 21:32 .. -rwxr-xr-x@ 1 kenwu staff 16M 3 30 2019 gor -rw-r--r--@ 1 kenwu staff 7.1M 12 18 20:59 gor_1.0.0_mac.tar-2.gz kenwu@KenMBP-2 ~/Documents/tooltest sudo ./gor --input-raw :8000 --output-stdout Password: Version: 1.0.0 1 dad10dcf728fd1f9cc857a8f5e0d44c91b4db455 1576676146990796000 GET /todos HTTP/1.1 Host: localhost:8000 Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 Sec-Fetch-User: ?1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 Sec-Fetch-Site: none Sec-Fetch-Mode: navigate Accept-Encoding: gzip, deflate, br Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
连续两次请求:http://localhost:8000/todos
服务端表现:
3.2.2 回放到其他服务器
现在是时候将原始流量重播到另一个环境了。让我们启动同一个工程服务,但在不同的端口:http://localhost:8001/todos。代替 -outpue-stdout 我们将使用 --output-http 并提供第二个服务器的 URL:sudo ./gor --intput-raw :8000 --output-http="http://localhost:8001"。 向第一个服务器发出很少的请求。你应该看到他们复制到第二个。
✘ kenwu@KenMBP-2 ~/Documents/tooltest sudo ./gor --input-raw :8000 --output-http="http://localhost:8001/todos" Password: Version: 1.0.0
8000端口服务器:
8001端口服务器:
3.2.3 保存请求到文件,使用文件回放
有的时候,不可能实时回放请求,Gor允许我们保存请求到文件,并且使用它进行回放。
首先使用:sudo ./gor --input-raw :8000 --output-file=requests.gor
这将创建新文件requests_0.gor,并持续写入所有捕获的请求。
✘ kenwu@KenMBP-2 ~/Documents/tooltest sudo ./gor --input-raw :8000 --output-file=requests.gor Version: 1.0.0
8000端口服务器,请求三次:
关闭侦听:
使用请求文件,请求8001服务器:
✘ kenwu@KenMBP-2 ~/Documents/tooltest ./gor --input-file requests_0.gor --output-http="http://localhost:8001" Version: 1.0.0 2019/12/18 22:23:53 FileInput: end of file 'requests_0.gor'