average

package module
v0.0.0-...-d26c465 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2020 License: BSD-3-Clause Imports: 3 Imported by: 7

README

average TravisCI Go Report Card GoDoc

This stupidly named Go package contains a single struct that is used to implement counters on a sliding time window.

Usage


import (
    "fmt"

    "github.com/prep/average"
)

func main() {
    // Create a SlidingWindow that has a window of 15 minutes, with a
    // granulity of 1 minute.
    sw := average.MustNew(15 * time.Minute, time.Minute)
    defer sw.Stop()

    // Do some work.
    sw.Add(15)
    // Do some more work.
    sw.Add(22)
    // Do even more work.
    sw.Add(22)

    fmt.Printf("Average of last  1m: %f\n", sw.Average(time.Minute)
    fmt.Printf("Average of last  5m: %f\n", sw.Average(5 * time.Minute)
    fmt.Printf("Average of last 15m: %f\n\n", sw.Average(15 * time.Minute)

    total, numSamples := sw.Total(15 * time.Minute)
    fmt.Printf("Counter has a total of %d over %d samples", total, numSamples)
}

License

This software is created for MessageBird B.V. and distributed under the BSD-style license found in the LICENSE file.

Documentation

Overview

Package average implements sliding time window.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SlidingWindow

type SlidingWindow struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

SlidingWindow provides a sliding time window with a custom size and granularity to store int64 counters. This can be used to determine the total or unweighted mean average of a subset of the window size.

func MustNew

func MustNew(window, granularity time.Duration) *SlidingWindow

MustNew returns a new SlidingWindow, but panics if an error occurs.

func New

func New(window, granularity time.Duration) (*SlidingWindow, error)

New returns a new SlidingWindow.

func (*SlidingWindow) Add

func (sw *SlidingWindow) Add(v int64)

Add increments the value of the current sample.

func (*SlidingWindow) Average

func (sw *SlidingWindow) Average(window time.Duration) float64

Average returns the unweighted mean of the specified window.

func (*SlidingWindow) Reset

func (sw *SlidingWindow) Reset()

Reset the samples in this sliding time window.

func (*SlidingWindow) Stop

func (sw *SlidingWindow) Stop()

Stop the shifter of this sliding time window. A stopped SlidingWindow cannot be started again.

func (*SlidingWindow) Total

func (sw *SlidingWindow) Total(window time.Duration) (int64, int)

Total returns the sum of all values over the specified window, as well as the number of samples.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL