Added NewPrecise and changed New to be simpler
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
// Package bps is a simple tool for keeping track of the rate of bytes
|
||||
// transmitted
|
||||
// Package bps keeps track of byte rates
|
||||
package bps
|
||||
|
||||
import (
|
||||
@@ -13,11 +12,12 @@ const (
|
||||
minResolution = 3
|
||||
)
|
||||
|
||||
// BPS keeps track of state for byte counts
|
||||
// BPS calculates bandwith rates.
|
||||
//
|
||||
// Instantiate a BPS then feed bytes either via BPS.Add, or writing to it. When
|
||||
// the accumulated values are needed call BPS.Cur. When the BPS is no loner
|
||||
// needed call BPS.Close
|
||||
// Instantiate a BPS using one of the New* functions then feed bytes either via
|
||||
// BPS.Add, or writing to it (it implements io.Writer). When the accumulated
|
||||
// values are needed call BPS.Rate. When the BPS is no loner needed call
|
||||
// BPS.Close
|
||||
type BPS struct {
|
||||
sync.RWMutex
|
||||
|
||||
@@ -37,15 +37,29 @@ type BPS struct {
|
||||
timeI int
|
||||
}
|
||||
|
||||
// New Returns a populated and ready to use BPS. interval is the amount of time
|
||||
// (for example 60 seconds) over which to track byte flow (bytes/second for the
|
||||
// last interval), and resolution is used in the following calculation:
|
||||
// New Returns a populated and ready to use BPS.
|
||||
//
|
||||
// dt = interval / resolution (s)
|
||||
// bytes per second are reported for the last time interval (defined by
|
||||
// interval), and is updated every (interval / 100) seconds.
|
||||
func New(interval time.Duration) (*BPS, error) {
|
||||
return NewPrecise(interval, 100)
|
||||
}
|
||||
|
||||
// MustNew behaves like New, but panics on error.
|
||||
func MustNew(interval time.Duration) *BPS {
|
||||
b, err := New(interval)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
// NewPrecise behaves like New, but allows the user to specify the temporal
|
||||
// resolution.
|
||||
//
|
||||
// where the dt is the temporal resolution of the updates (add or remove
|
||||
// information every dt).
|
||||
func New(interval time.Duration, resolution uint) (*BPS, error) {
|
||||
// If you need to query more or less frequently than interval / 100 you'll want
|
||||
// to use this function.
|
||||
func NewPrecise(interval time.Duration, resolution uint) (*BPS, error) {
|
||||
if resolution < minResolution {
|
||||
return nil, fmt.Errorf("resolution must be larger than %d", minResolution)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user