流量回放工具之GoReplay input_file 源码分析

简介: 【6月更文挑战6天】流量回放工具之GoReplay input_file 源码分析

@[toc]

前言

GoReplay 对数据流的抽象出了两个概念,即用 输入(input ) 和 输出(output ) 来表示数据来源与去向,统称为 plugin,用介于输入和输出模块之间的中间件实现拓展机制。

input_file.go:实现文件读取的输入插件, 实现 io.Reader 接口,最后根据配置注册到 Plugin.inputs 队列里。

主要参数

  -input-file value   //从一个文件中读取请求
        Read requests from file: 
                gor --input-file ./requests.gor --output-http staging.com
  -input-file-dry-run  //模拟从数据源读取数据而不重新回放它
        Simulate reading from the data source without replaying it. You will get information about expected replay time, number of found records etc.
  -input-file-loop  //循环读取文件
        Loop input files, useful for performance testing.
  -input-file-max-wait duration  //设置请求之间的最大时间间隔
        Set the maximum time between requests. Can help in situations when you have too long periods between request, and you want to skip them. Example: --input-raw-max-wait 1s
  -input-file-read-depth int  //尝试提前读取和缓存多个记录。与此同时,如果请求没有按顺序出现,它还可以对请求进行排序。因为它需要在内存中保存这个缓冲区,较大的值会导致更差的性能(默认为100)
        GoReplay tries to read and cache multiple records, in advance. In parallel it also perform sorting of requests, if they came out of order. Since it needs hold this buffer in memory, bigger values can cause worse performance (default 100)

变速回放

使用说明

GoReplay 实现压力测试的核心特性就是满足流量变速回放功能。支持将录制的生产实际请求流量减少或者放大回放以用于压力测试

比如运行以下命令,将流量从文件回放到 237 服务器,并放大两倍:

[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'
  • requests.gor|1:最大不超过1QPS;
  • requests.gor|100%:超过原流量的100%。

源码解析

gor.go 为 main 方法,初始化所有插件,启动 emiter(核心处理器) 监听

初始化插件:
image.png

启动 emitter:
image.png

原始代码如下:

func main() {
   
   
    //获取环境变量
    if os.Getenv("GOMAXPROCS") == "" {
   
   
        //设置最大逻辑核
        runtime.GOMAXPROCS(runtime.NumCPU() * 2)
    }

    args := os.Args[1:]
    var plugins *InOutPlugins
    //文件服务
    if len(args) > 0 && args[0] == "file-server" {
   
   
        if len(args) != 2 {
   
   
            log.Fatal("You should specify port and IP (optional) for the file server. Example: `gor file-server :80`")
        }
        dir, _ := os.Getwd()

        Debug(0, "Started example file server for current directory on address ", args[1])

        log.Fatal(http.ListenAndServe(args[1], loggingMiddleware(args[1], http.FileServer(http.Dir(dir)))))
    } else {
   
   
        //解析命令行参数
        flag.Parse()
        //初始化全局的 Settings 变量
        checkSettings()
        //初始化可用插件
        plugins = NewPlugins()
    }

    log.Printf("[PPID %d and PID %d] Version:%s\n", os.Getppid(), os.Getpid(), VERSION)

    if len(plugins.Inputs) == 0 || len(plugins.Outputs) == 0 {
   
   
        log.Fatal("Required at least 1 input and 1 output")
    }

    if *memprofile != "" {
   
   
        profileMEM(*memprofile)
    }

    if *cpuprofile != "" {
   
   
        profileCPU(*cpuprofile)
    }

    if Settings.Pprof != "" {
   
   
        go func() {
   
   
            log.Println(http.ListenAndServe(Settings.Pprof, nil))
        }()
    }

    closeCh := make(chan int)
    //程序核心事件处理
    emitter := NewEmitter()
    //调用 Start 函数,启动 emitter
    go emitter.Start(plugins, Settings.Middleware)
    if Settings.ExitAfter > 0 {
   
   
        log.Printf("Running gor for a duration of %s\n", Settings.ExitAfter)

        time.AfterFunc(Settings.ExitAfter, func() {
   
   
            log.Printf("gor run timeout %s\n", Settings.ExitAfter)
            close(closeCh)
        })
    }
    c := make(chan os.Signal, 1)
    signal.Notify(c, os.Interrupt, syscall.SIGTERM)
    exit := 0
    select {
   
   
    case <-c:
        exit = 1
    case <-closeCh:
        exit = 0
    }
    //关闭所有协程
    emitter.Close()
    os.Exit(exit)
}

plugins.go 类中,通过 limiter 类执行各种变速操作:

image.png

源代码如下:

// Automatically detects type of plugin and initialize it
//
// See this article if curious about reflect stuff below: http://blog.burntsushi.net/type-parametric-functions-golang
func (plugins *InOutPlugins) registerPlugin(constructor interface{
   
   }, options ...interface{
   
   }) {
   
   
    var path, limit string
    vc := reflect.ValueOf(constructor)

    // Pre-processing options to make it work with reflect
    vo := []reflect.Value{
   
   }
    for _, oi := range options {
   
   
        vo = append(vo, reflect.ValueOf(oi))
    }

    if len(vo) > 0 {
   
   
        // Removing limit options from path
        path, limit = extractLimitOptions(vo[0].String())

        // Writing value back without limiter "|" options
        vo[0] = reflect.ValueOf(path)
    }

    // Calling our constructor with list of given options
    plugin := vc.Call(vo)[0].Interface()

    if limit != "" {
   
   
        plugin = NewLimiter(plugin, limit)
    }

    // Some of the output can be Readers as well because return responses
    if r, ok := plugin.(PluginReader); ok {
   
   
        plugins.Inputs = append(plugins.Inputs, r)
    }

    if w, ok := plugin.(PluginWriter); ok {
   
   
        plugins.Outputs = append(plugins.Outputs, w)
    }
    plugins.All = append(plugins.All, plugin)
}

plugins.go 类中,对类似 'request.gor|100%' 的参数进行解析:

image.png

源代码如下:

// extractLimitOptions detects if plugin get called with limiter support
// Returns address and limit
func extractLimitOptions(options string) (string, string) {
   
   
    split := strings.Split(options, "|")

    if len(split) > 1 {
   
   
        return split[0], split[1]
    }

    return split[0], ""
}

主要执行方法在 limiter.go 类,源代码如下:

package main

import (
    "fmt"
    "io"
    "math/rand"
    "strconv"
    "strings"
    "time"
)

// Limiter is a wrapper for input or output plugin which adds rate limiting
type Limiter struct {
   
   
    plugin    interface{
   
   }
    limit     int
    isPercent bool

    currentRPS  int
    currentTime int64
}

func parseLimitOptions(options string) (limit int, isPercent bool) {
   
   
    if n := strings.Index(options, "%"); n > 0 {
   
   
        limit, _ = strconv.Atoi(options[:n])
        isPercent = true
    } else {
   
   
        limit, _ = strconv.Atoi(options)
        isPercent = false
    }

    return
}

// NewLimiter constructor for Limiter, accepts plugin and options
// `options` allow to sprcify relatve or absolute limiting
func NewLimiter(plugin interface{
   
   }, options string) PluginReadWriter {
   
   
    l := new(Limiter)
    l.limit, l.isPercent = parseLimitOptions(options)
    l.plugin = plugin
    l.currentTime = time.Now().UnixNano()

    // FileInput have its own rate limiting. Unlike other inputs we not just dropping requests, we can slow down or speed up request emittion.
    if fi, ok := l.plugin.(*FileInput); ok && l.isPercent {
   
   
        fi.speedFactor = float64(l.limit) / float64(100)
    }

    return l
}

func (l *Limiter) isLimited() bool {
   
   
    // File input have its own limiting algorithm
    if _, ok := l.plugin.(*FileInput); ok && l.isPercent {
   
   
        return false
    }

    if l.isPercent {
   
   
        return l.limit <= rand.Intn(100)
    }

    if (time.Now().UnixNano() - l.currentTime) > time.Second.Nanoseconds() {
   
   
        l.currentTime = time.Now().UnixNano()
        l.currentRPS = 0
    }

    if l.currentRPS >= l.limit {
   
   
        return true
    }

    l.currentRPS++

    return false
}

// PluginWrite writes message to this plugin
func (l *Limiter) PluginWrite(msg *Message) (n int, err error) {
   
   
    if l.isLimited() {
   
   
        return 0, nil
    }
    if w, ok := l.plugin.(PluginWriter); ok {
   
   
        return w.PluginWrite(msg)
    }
    // avoid further writing
    return 0, io.ErrClosedPipe
}

// PluginRead reads message from this plugin
func (l *Limiter) PluginRead() (msg *Message, err error) {
   
   
    if r, ok := l.plugin.(PluginReader); ok {
   
   
        msg, err = r.PluginRead()
    } else {
   
   
        // avoid further reading
        return nil, io.ErrClosedPipe
    }

    if l.isLimited() {
   
   
        return nil, nil
    }

    return
}

func (l *Limiter) String() string {
   
   
    return fmt.Sprintf("Limiting %s to: %d (isPercent: %v)", l.plugin, l.limit, l.isPercent)
}

// Close closes the resources.
func (l *Limiter) Close() error {
   
   
    if fi, ok := l.plugin.(io.Closer); ok {
   
   
        fi.Close()
    }
    return nil
}

limiter.go 类同样实现文件限速功能:

image.png

input_file.go 类调用了文件限速功能:

image.png

文件循环读取

主要参数为:--input-file-loop

比如:

$ sudo ./gor --input-file 'request.gor|10000%' --input-file-loop --output-http 'http://10.96.136.36:8201'

在 plufins.go 类中应用文件循环使用:

image.png

源代码:

    for _, options := range Settings.InputFile {
   
   
        plugins.registerPlugin(NewFileInput, options, Settings.InputFileLoop, Settings.InputFileReadDepth, Settings.InputFileMaxWait, Settings.InputFileDryRun)
    }

input_file.go 类中实现文件读取功能:

image.png

源代码如下:

// NewFileInput constructor for FileInput. Accepts file path as argument.
func NewFileInput(path string, loop bool, readDepth int, maxWait time.Duration, dryRun bool) (i *FileInput) {
   
   
    i = new(FileInput)
    i.data = make(chan []byte, 1000)
    i.exit = make(chan bool)
    i.path = path
    i.speedFactor = 1
    i.loop = loop
    i.readDepth = readDepth
    i.stats = expvar.NewMap("file-" + path)
    i.dryRun = dryRun
    i.maxWait = maxWait

    if err := i.init(); err != nil {
   
   
        return
    }

    go i.emit()

    return
}

FileInput的构造函数,传入文件路径等参数。

image.png

源代码如下:

// FileInput can read requests generated by FileOutput
type FileInput struct {
   
   
    mu          sync.Mutex
    data        chan []byte
    exit        chan bool
    path        string
    readers     []*fileInputReader
    speedFactor float64
    loop        bool
    readDepth   int
    dryRun      bool
    maxWait     time.Duration

    stats *expvar.Map
}

判断是否循环读取:

image.png

源代码如下:

        if reader == nil {
   
   
            if i.loop {
   
   
                i.init()
                lastTime = -1
                continue
            } else {
   
   
                break
            }
        }

核心代码逻辑调用

image.png

image.png

目录
相关文章
hutool 验证是否为身份证号码(支持18位、15位和港澳台的10位)
hutool 验证是否为身份证号码(支持18位、15位和港澳台的10位)
|
人工智能 缓存 测试技术
Playwright进阶指南 (6) | 自动化测试实战
2025企业级测试解决方案全面解析:从单元测试到千级并发,构建高可用测试体系。结合Playwright智能工具,解决传统测试维护成本高、环境依赖强、执行效率低等痛点,提升测试成功率,内容从测试架构设计、电商系统实战框架、高级测试策略、Docker化部署、CI/CD集成及AI测试应用,助力测试工程师掌握前沿技术,打造高效稳定的测试流程。
Playwright进阶指南 (6) | 自动化测试实战
|
人工智能 供应链 搜索推荐
如何降低酒店运营成本?管理者不可不知的优化策略
在竞争激烈的酒店行业,提升管理效率、优化服务质量和客户满意度至关重要。本文探讨如何通过精准市场定位、高效运营管理、智能化管理系统及数字化工具,实现精细化管理,提供个性化服务,有效处理客户反馈,以提升酒店竞争力和客户体验。未来,智能化与个性化将成为行业发展的重要趋势。
1027 21
|
人工智能 自然语言处理 监控
17.1K star!两小时就能训练出专属与自己的个性化小模型,这个开源项目让AI触手可及!
🔥「只需一张消费级显卡,2小时完成26M参数GPT训练!」 🌟「从零构建中文大模型的最佳实践指南」 🚀「兼容OpenAI API,轻松接入各类AI应用平台」
1353 1
|
缓存 前端开发 JavaScript
前端性能优化:提升网页加载速度的10个技巧
【10月更文挑战第25天】在互联网时代,网页加载速度直接影响用户体验和搜索引擎排名。本文介绍了10个提升网页加载速度的技巧,包括减少HTTP请求、启用压缩、使用CDN、延迟加载非关键资源、优化图片、减少重定向、使用浏览器缓存、优化CSS和JavaScript、异步加载JavaScript以及代码分割。通过这些方法,可以显著提高网页性能,改善用户体验。
3820 5
|
数据采集 弹性计算 供应链
阿里云服务器付费模式:按量付费、包年包月和抢占式实例全解析
阿里云服务器提供包年包月、按量付费与抢占式实例三种付费模式。包年包月为预付费,适合长期稳定使用,价格更优惠并支持备案。按量付费则为后付费模式,按小时结算,适合短期或访问量波动大的场景,但不支持备案。抢占式实例基于按量付费,价格更低(最多节省90%),适用于无状态应用,如临时测试或可弹性伸缩的Web服务,但存在被系统释放的风险,同样不支持备案。根据具体需求选择合适的付费模式能够有效降低成本并提高效率。
1216 1
|
前端开发 数据安全/隐私保护
应用架构设计 - 智能物流柜
应用架构设计 - 智能物流柜
424 1
|
机器学习/深度学习 人工智能 自然语言处理
人工智能与艺术的融合:创意生成与审美判断的技术革新
【7月更文挑战第1天】人工智能与艺术融合,探索创意生成与审美判断的新边界。AI借助深度学习和GANs,生成图像、音乐和文本,革新艺术创作。同时,通过学习分析,AI在艺术鉴赏中提供客观审美判断,助力艺术家和观众理解作品。技术进步不断深化艺术与AI的合作,共创艺术未来。
YOLOv8打印模型结构配置信息并查看网络模型详细参数:参数量、计算量(GFLOPS)
YOLOv8打印模型结构配置信息并查看网络模型详细参数:参数量、计算量(GFLOPS)
|
存储 监控 测试技术
流量回放浅实践
实际案例
2479 0
流量回放浅实践

热门文章

最新文章