ratecounter

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2017 License: MIT Imports: 3 Imported by: 212

README

ratecounter

CircleCI Go Report Card GoDoc codecov

A Thread-Safe RateCounter implementation in Golang

Usage

import "github.com/paulbellamy/ratecounter"

Package ratecounter provides a thread-safe rate-counter, for tracking counts in an interval

Useful for implementing counters and stats of 'requests-per-second' (for example):

// We're recording marks-per-1second
counter := ratecounter.NewRateCounter(1 * time.Second)
// Record an event happening
counter.Incr(1)
// get the current requests-per-second
counter.Rate()

To record an average over a longer period, you can:

// Record requests-per-minute
counter := ratecounter.NewRateCounter(60 * time.Second)
// Calculate the average requests-per-second for the last minute
counter.Rate() / 60

Also you can track average value of some metric in an interval.

Useful for implementing counters and stats of 'average-execution-time' (for example):

// We're recording average execution time of some heavy operation in the last minute.
counter := ratecounter.NewAvgRateCounter(60 * time.Second)
// Start timer.
startTime := time.Now()
// Execute heavy operation.
heavyOperation()
// Record elapsed time.
counter.Incr(time.Since(startTime).Nanoseconds())
// Get the current average execution time.
counter.Rate()

Documentation

Check latest documentation on go doc.

Documentation

Overview

Package ratecounter provides a thread-safe rate-counter, for tracking counts in an interval

Useful for implementing counters and stats of 'requests-per-second' (for example).

// We're recording marks-per-1second
counter := ratecounter.NewRateCounter(1 * time.Second)

// Record an event happening
counter.Mark()

// get the current requests-per-second
counter.Rate()

To record an average over a longer period, you can:

// Record requests-per-minute
counter := ratecounter.NewRateCounter(60 * time.Second)

// Calculate the average requests-per-second for the last minute
counter.Rate() / 60

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AvgRateCounter added in v0.2.0

type AvgRateCounter struct {
	// contains filtered or unexported fields
}

An AvgRateCounter is a thread-safe counter which returns the ratio between the number of calls 'Incr' and the counter value in the last interval

func NewAvgRateCounter added in v0.2.0

func NewAvgRateCounter(intrvl time.Duration) *AvgRateCounter

NewAvgRateCounter constructs a new AvgRateCounter, for the interval provided

func (*AvgRateCounter) Hits added in v0.2.0

func (a *AvgRateCounter) Hits() int64

Hits returns the number of calling method Incr during specified interval

func (*AvgRateCounter) Incr added in v0.2.0

func (a *AvgRateCounter) Incr(val int64)

Incr Adds an event into the AvgRateCounter

func (*AvgRateCounter) Rate added in v0.2.0

func (a *AvgRateCounter) Rate() float64

Rate Returns the current ratio between the events count and its values during the last interval

func (*AvgRateCounter) String added in v0.2.0

func (a *AvgRateCounter) String() string

String returns counter's rate formatted to string

func (*AvgRateCounter) WithResolution added in v0.2.0

func (a *AvgRateCounter) WithResolution(resolution int) *AvgRateCounter

WithResolution determines the minimum resolution of this counter

type Counter

type Counter int64

A Counter is a thread-safe counter implementation

func (*Counter) Incr

func (c *Counter) Incr(val int64)

Incr method increments the counter by some value

func (*Counter) Reset added in v0.2.0

func (c *Counter) Reset()

Reset method resets the counter's value to zero

func (*Counter) Value

func (c *Counter) Value() int64

Value method returns the counter's current value

type RateCounter

type RateCounter struct {
	// contains filtered or unexported fields
}

A RateCounter is a thread-safe counter which returns the number of times 'Incr' has been called in the last interval

func NewRateCounter

func NewRateCounter(intrvl time.Duration) *RateCounter

NewRateCounter Constructs a new RateCounter, for the interval provided

func (*RateCounter) Incr

func (r *RateCounter) Incr(val int64)

Incr Add an event into the RateCounter

func (*RateCounter) Rate

func (r *RateCounter) Rate() int64

Rate Return the current number of events in the last interval

func (*RateCounter) String

func (r *RateCounter) String() string

func (*RateCounter) WithResolution added in v0.2.0

func (r *RateCounter) WithResolution(resolution int) *RateCounter

WithResolution determines the minimum resolution of this counter, default is 20

Jump to

Keyboard shortcuts

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