goreplay~http输出统计分析

HTTPOutputConfig 统计信息收集

是否收集统计信息,统计输出间隔是多少

if o.config.Stats {
    o.queueStats = NewGorStat("output_http", o.config.StatsMs)
}

统计信息收集

func (o *HTTPOutput) PluginWrite(msg *Message) (n int, err error) {
    if !isRequestPayload(msg.Meta) {
        return len(msg.Data), nil
    }

    select {
    case <-o.stop:
        return 0, ErrorStopped
    case o.queue <- msg:
    }

    if o.config.Stats {
        o.queueStats.Write(len(o.queue))
    }
    if len(o.queue) > 0 {
        // try to start a new worker to serve
        if atomic.LoadInt32(&o.activeWorkers) < int32(o.config.WorkersMax) {
            go o.startWorker()
            atomic.AddInt32(&o.activeWorkers, 1)
        }
    }
    return len(msg.Data) + len(msg.Meta), nil
}

NewGorStat统计类

func NewGorStat(statName string, rateMs int) (s *GorStat) {
    s = new(GorStat)
    s.statName = statName
    s.rateMs = rateMs
    s.latest = 0
    s.mean = 0
    s.max = 0
    s.count = 0

    if Settings.Stats {
        go s.reportStats()
    }
    return
}

写入时做统计

func (s *GorStat) Write(latest int) {
    if Settings.Stats {
        if latest > s.max {
            s.max = latest
        }
        if latest != 0 {
            s.mean = ((s.mean * s.count) + latest) / (s.count + 1)
        }
        s.latest = latest
        s.count = s.count + 1
    }
}

打印输出,简单的sleep

func (s *GorStat) reportStats() {
    Debug(0, "
", s.statName+":latest,mean,max,count,count/second,gcount")
    for {
        Debug(0, "
", s)
        s.Reset()
        time.Sleep(time.Duration(s.rateMs) * time.Millisecond)
    }
}
原文地址:https://www.cnblogs.com/it-worker365/p/15113743.html