faststats

package
v3.2.2 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: Apache-2.0 Imports: 11 Imported by: 3

Documentation

Overview

Package faststats contains helpers to calculate circuit statistics quickly (usually atomically).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicBoolean

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

AtomicBoolean is a helper struct to simulate atomic operations on a boolean

func (*AtomicBoolean) Get

func (a *AtomicBoolean) Get() bool

Get the current atomic value

func (*AtomicBoolean) MarshalJSON

func (a *AtomicBoolean) MarshalJSON() ([]byte, error)

MarshalJSON encodes this value in a thread safe way as a json bool

func (*AtomicBoolean) Set

func (a *AtomicBoolean) Set(value bool)

Set the atomic boolean value

func (*AtomicBoolean) String

func (a *AtomicBoolean) String() string

String returns "true" or "false"

func (*AtomicBoolean) UnmarshalJSON

func (a *AtomicBoolean) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes this value in a thread safe way as a json bool

type AtomicInt64

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

AtomicInt64 is a helper struct to simulate atomic operations on an int64 Note that I could have used `type AtomicInt642 int64`, but I did not want to make it easy to do + and - operations so easily without using atomic functions.

func (*AtomicInt64) Add

func (a *AtomicInt64) Add(value int64) int64

Add a value to the current store

func (*AtomicInt64) CompareAndSwap

func (a *AtomicInt64) CompareAndSwap(expected int64, newVal int64) bool

CompareAndSwap acts like a CompareAndSwap operation on the value

func (*AtomicInt64) Duration

func (a *AtomicInt64) Duration() time.Duration

Duration returns the currently stored value as a time.Duration

func (*AtomicInt64) Get

func (a *AtomicInt64) Get() int64

Get the current int64

func (*AtomicInt64) MarshalJSON

func (a *AtomicInt64) MarshalJSON() ([]byte, error)

MarshalJSON encodes this value as an int in a thread safe way

func (*AtomicInt64) Set

func (a *AtomicInt64) Set(value int64)

Set the current store to a value

func (*AtomicInt64) String

func (a *AtomicInt64) String() string

String returns the integer as a string in a thread safe way

func (*AtomicInt64) Swap

func (a *AtomicInt64) Swap(newValue int64) int64

Swap the current value with a new one

func (*AtomicInt64) UnmarshalJSON

func (a *AtomicInt64) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes this value as an int in a thread safe way

type RollingBuckets

type RollingBuckets struct {
	NumBuckets   int
	StartTime    time.Time
	BucketWidth  time.Duration
	LastAbsIndex AtomicInt64
}

RollingBuckets simulates a time rolling list of buckets of items. It is safe to use JSON to encode this object in a thread safe way.

This implementation cheats in order to not take a lock. It is correct, but only if the total size of the buckets (NumBuckets * BucketWidth) is less than any duration of how long Advance will take to execute. In anything but super small bucket sizes this should be fine. The common case, where (NumBuckets * BucketWidth >= 1sec) should always work.

func (*RollingBuckets) Advance

func (r *RollingBuckets) Advance(now time.Time, clearBucket func(int)) int

Advance to now, clearing buckets as needed

func (*RollingBuckets) String

func (r *RollingBuckets) String() string

type RollingCounter

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

RollingCounter uses a slice of buckets to keep track of counts of an event over time with a sliding window

func NewRollingCounter

func NewRollingCounter(bucketWidth time.Duration, numBuckets int, now time.Time) RollingCounter

NewRollingCounter initializes a rolling counter with a bucket width and # of buckets

func (*RollingCounter) GetBuckets

func (r *RollingCounter) GetBuckets(now time.Time) []int64

GetBuckets returns a copy of the buckets in order backwards in time

func (*RollingCounter) Inc

func (r *RollingCounter) Inc(now time.Time)

Inc adds a single event to the current bucket

func (*RollingCounter) MarshalJSON

func (r *RollingCounter) MarshalJSON() ([]byte, error)

MarshalJSON JSON encodes a counter. It is thread safe.

func (*RollingCounter) Reset

func (r *RollingCounter) Reset(now time.Time)

Reset the counter to all zero values.

func (*RollingCounter) RollingSum

func (r *RollingCounter) RollingSum() int64

RollingSum returns the total number of events in the rolling time window (With time time.Now())

func (*RollingCounter) RollingSumAt

func (r *RollingCounter) RollingSumAt(now time.Time) int64

RollingSumAt returns the total number of events in the rolling time window

func (*RollingCounter) String

func (r *RollingCounter) String() string

String for debugging

func (*RollingCounter) StringAt

func (r *RollingCounter) StringAt(now time.Time) string

StringAt converts the counter to a string at a given time.

func (*RollingCounter) TotalSum

func (r *RollingCounter) TotalSum() int64

TotalSum returns the total number of events of all time

func (*RollingCounter) UnmarshalJSON

func (r *RollingCounter) UnmarshalJSON(b []byte) error

UnmarshalJSON stores the previous JSON encoding. Note, this is *NOT* thread safe.

type RollingPercentile

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

RollingPercentile is a bucketed array of time.Duration that cycles over time

func NewRollingPercentile

func NewRollingPercentile(bucketWidth time.Duration, numBuckets int, bucketSize int, now time.Time) RollingPercentile

NewRollingPercentile creates a new rolling percentile bucketer

func (*RollingPercentile) AddDuration

func (r *RollingPercentile) AddDuration(d time.Duration, now time.Time)

AddDuration adds a duration to the rolling buckets

func (*RollingPercentile) Reset

func (r *RollingPercentile) Reset(now time.Time)

Reset the counter to all zero values.

func (*RollingPercentile) Snapshot

func (r *RollingPercentile) Snapshot() SortedDurations

Snapshot the current rolling buckets, allowing easy p99 calculations

func (*RollingPercentile) SnapshotAt

func (r *RollingPercentile) SnapshotAt(now time.Time) SortedDurations

SnapshotAt is an optimization on Snapshot that takes the current time

func (*RollingPercentile) SortedDurations

func (r *RollingPercentile) SortedDurations(now time.Time) []time.Duration

SortedDurations creates a raw []time.Duration in sorted order that is stored in these buckets

func (*RollingPercentile) Var

func (r *RollingPercentile) Var() expvar.Var

Var allows exposing a rolling percentile snapshot on expvar

type SortedDurations

type SortedDurations []time.Duration

SortedDurations is a sorted list of time.Duration that allows fast Percentile operations

func (SortedDurations) Max

func (s SortedDurations) Max() time.Duration

Max returns the last (largest) item, or -1 if the list is empty

func (SortedDurations) Mean

func (s SortedDurations) Mean() time.Duration

Mean (average) of the current list

func (SortedDurations) Min

func (s SortedDurations) Min() time.Duration

Min returns the first (smallest) item, or -1 if the list is empty

func (SortedDurations) Percentile

func (s SortedDurations) Percentile(p float64) time.Duration

Percentile returns a p [0.0 - 1.0] percentile of the list

func (SortedDurations) String

func (s SortedDurations) String() string

func (SortedDurations) Var

func (s SortedDurations) Var() expvar.Var

Var allows exposing the durations on expvar

type TimedCheck

type TimedCheck struct {
	TimeAfterFunc func(time.Duration, func()) *time.Timer
	// contains filtered or unexported fields
}

TimedCheck lets X events happen every sleepDuration units of time. For optimizations, it uses TimeAfterFunc to reset an internal atomic boolean for when events are allowed. This timer could run a little bit behind real time since it depends on when the OS decides to trigger the timer.

func (*TimedCheck) Check

func (c *TimedCheck) Check(now time.Time) bool

Check returns true if a check is allowed at this time

func (*TimedCheck) MarshalJSON

func (c *TimedCheck) MarshalJSON() ([]byte, error)

MarshalJSON writes the object as JSON. It is thread safe.

func (*TimedCheck) SetEventCountToAllow

func (c *TimedCheck) SetEventCountToAllow(newCount int64)

SetEventCountToAllow configures how many times Check() can return true before moving time to the next interval

func (*TimedCheck) SetSleepDuration

func (c *TimedCheck) SetSleepDuration(newDuration time.Duration)

SetSleepDuration modifies how long time timed check will sleep. It will not change alredy sleeping checks, but will change during the next check.

func (*TimedCheck) SleepStart

func (c *TimedCheck) SleepStart(now time.Time)

SleepStart resets the checker to trigger after now + sleepDuration

func (*TimedCheck) String

func (c *TimedCheck) String() string

func (*TimedCheck) UnmarshalJSON

func (c *TimedCheck) UnmarshalJSON(b []byte) error

UnmarshalJSON changes the object from JSON. It is *NOT* thread safe.

Jump to

Keyboard shortcuts

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