Skip to content

gor 流量回放

霍格沃兹测试开发学社

ceshiren.com


GoReplay 流量回放

霍格沃兹测试开发学社

ceshiren.com


goreplay 介绍

GoReplay is an open-source tool for capturing and replaying live HTTP traffic into a test environment in order to continuously test your system with real data. It can be used to increase confidence in code deployments, configuration changes and infrastructure changes.
GoReplay is an open-source network monitoring tool which can record your live traffic, and use it for shadowing, load testing, monitoring and detailed analysis.

GoReplay 是一个开源工具,用于捕获实时 HTTP 流量并将其重播到测试环境中,以便使用真实数据持续测试您的系统。 它可用于增加对代码部署、配置更改和基础架构更改的信心。
GoReplay 是一个开源的网络监控工具,可以记录您的实时流量,并将其用于跟踪、负载测试、监控和详细分析。


goreplay 价值

Tired from debugging on production? Make your deploys and infrastructure changes predictable by using GoReplay to accurately emulate production environment. GoReplay is an innovative open-source solution which allows you to capture your existing users activity and re-use it for testing your application. With GoReplay you can perform shadowing, load testing, or detailed analysis and monitoring.

厌倦了生产调试?使用 GoReplay 准确模拟生产环境,使您的部署和基础架构更改可预测

GoReplay 是一种创新的开源解决方案,它允许您捕获现有用户的活动并将其重新用于测试您的应用程序。使用 GoReplay,您可以执行跟踪、负载测试或详细分析和监控


技术架构 技术架构


下载

  • 预编译版本 https://github.com/buger/goreplay/releases
  • 编译代码 https://github.com/buger/goreplay
  • 容器版本比较老旧 https://hub.docker.com/search?q=goreplay&type=image

使用方法


演练环境

可以利用 gor python nginx 等各种工具搭建多套环境
python3 -m http.server 8001
./gor file-server :8000

  • dev 环境 http://127.0.0.1:8001
  • stage 环境 http://127.0.0.1:8002
  • prod 环境 http://127.0.0.1:8000

快速开始 Quick Start

  • 监听模式
    • sudo ./gor --input-raw :8000 --output-stdout
  • 转发模式
    • sudo ./gor --input-raw :8000 --output-http="http://localhost:8001"
  • 重放模式
    • gor --input-file requests.gor --output-http="http://localhost:8001"
  • 性能模式
    • gor --input-file "requests.gor|200%" --output-http "staging.com"

使用流程

uml diagram


核心参数

input 参数 - --input-raw - --input-file - --input-http - --input-tcp - --input-raw-track-response
output 参数 - --output-http - --output-file - --output-tcp - --output-stdout

请求过滤

gor --input-raw :8080 --output-http staging.com \
    --http-allow-url /api

gor --input-raw :8080 --output-http staging.com \
    --http-disallow-url /api

gor --input-raw :8080 --output-http staging.com \
    --http-allow-header api-version:^1\.0\d

gor --input-raw :80 --output-http "http://staging.server" \
    --http-allow-method GET \
    --http-allow-method OPTIONS

请求重写

gor --input-raw :8080 --output-http staging.com \
    --http-rewrite-url /v1/user/([^\\/]+)/ping:/v2/user/$1/ping

gor --input-raw :8080 --output-http staging.com \
    --http-set-param api_key=1

gor --input-raw :80 --output-http "http://staging.server" \
    --http-header "User-Agent: Replayed by Gor" \
    --http-header "Enable-Feature-X: true"

经典案例


tcpdump 监听模式

sudo ./gor --input-raw :8000 --output-stdout
sudo ./gor --input-raw :8000 --output-file=requests.gor

线上流量同步

sudo ./gor --input-raw :80 --output-http="http://dev.ceshiren.com:8001"

压测

#录制并保存
sudo ./gor --input-raw :8000 --output-file=requests.gor
#加速回放
./gor --input-file "requests_0.gor|1000%" --http-allow-url json  --output-http "http://127.0.0.1:8003"
#持续加压
./gor --input-file "requests_0.gor|1000%" --http-allow-url json  --output-http "http://127.0.0.1:8003" -input-file-loop

总结

  • 利用流量同步实现预发布测试
  • 利用流量回放进行回归测试
  • 利用流量放大进行压力测试