项目作者: raspi

项目描述 :
Moving average window
高级语言: Go
项目地址: git://github.com/raspi/timeaverage.git
创建时间: 2019-12-20T16:44:33Z
项目社区:https://github.com/raspi/timeaverage

开源协议:Apache License 2.0

下载


timeaverage

Call a function which returns a float every X duration and keep history of N values and return average value of that window.

Install

  1. go get -u github.com/raspi/timeaverage

Example

Take measurement every 500 ms and keep history of 10 seconds.

  1. package main
  2. import (
  3. "github.com/raspi/timeaverage"
  4. "log"
  5. "time"
  6. )
  7. func exampleSampler() (float64, error) {
  8. return 1, nil
  9. }
  10. func main() {
  11. avg := timeaverage.New(time.Second*10, time.Millisecond*500, 0.0, exampleSampler)
  12. avg.Start()
  13. for {
  14. v := avg.Average()
  15. log.Printf(`%f`, v)
  16. time.Sleep(time.Second * 1)
  17. }
  18. }

See _examples directory for more examples.