Added NewPrecise and changed New to be simpler

This commit is contained in:
Stephen McQuay 2015-10-04 15:47:24 -07:00
parent fd030fd6b2
commit d8a8452961
3 changed files with 34 additions and 20 deletions

40
bps.go
View File

@ -1,5 +1,4 @@
// Package bps is a simple tool for keeping track of the rate of bytes // Package bps keeps track of byte rates
// transmitted
package bps package bps
import ( import (
@ -13,11 +12,12 @@ const (
minResolution = 3 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 // Instantiate a BPS using one of the New* functions then feed bytes either via
// the accumulated values are needed call BPS.Cur. When the BPS is no loner // BPS.Add, or writing to it (it implements io.Writer). When the accumulated
// needed call BPS.Close // values are needed call BPS.Rate. When the BPS is no loner needed call
// BPS.Close
type BPS struct { type BPS struct {
sync.RWMutex sync.RWMutex
@ -37,15 +37,29 @@ type BPS struct {
timeI int timeI int
} }
// New Returns a populated and ready to use BPS. interval is the amount of time // New Returns a populated and ready to use BPS.
// (for example 60 seconds) over which to track byte flow (bytes/second for the
// last interval), and resolution is used in the following calculation:
// //
// 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 // If you need to query more or less frequently than interval / 100 you'll want
// information every dt). // to use this function.
func New(interval time.Duration, resolution uint) (*BPS, error) { func NewPrecise(interval time.Duration, resolution uint) (*BPS, error) {
if resolution < minResolution { if resolution < minResolution {
return nil, fmt.Errorf("resolution must be larger than %d", minResolution) return nil, fmt.Errorf("resolution must be larger than %d", minResolution)
} }

View File

@ -13,7 +13,7 @@ func init() {
} }
func TestSimple(t *testing.T) { func TestSimple(t *testing.T) {
bw, err := New(1*time.Second, 100) bw, err := New(1 * time.Second)
if err != nil { if err != nil {
t.Fatalf("failure to make reasonable BPS: %v", err) t.Fatalf("failure to make reasonable BPS: %v", err)
} }
@ -21,7 +21,7 @@ func TestSimple(t *testing.T) {
} }
func TestBadResolution(t *testing.T) { func TestBadResolution(t *testing.T) {
_, err := New(1*time.Hour, 1) _, err := New(1 * time.Hour)
if err == nil { if err == nil {
t.Fatal("expected an error, got nil") t.Fatal("expected an error, got nil")
} }
@ -29,7 +29,7 @@ func TestBadResolution(t *testing.T) {
func TestWriter(t *testing.T) { func TestWriter(t *testing.T) {
t.Parallel() t.Parallel()
bw, err := New(1*time.Second, 100) bw, err := New(1 * time.Second)
if err != nil { if err != nil {
t.Fatalf("failure to make reasonable BPS: %v", err) t.Fatalf("failure to make reasonable BPS: %v", err)
} }
@ -55,7 +55,7 @@ func TestWriter(t *testing.T) {
func TestAdd(t *testing.T) { func TestAdd(t *testing.T) {
t.Parallel() t.Parallel()
bw, err := New(1*time.Second, 100) bw, err := New(1 * time.Second)
if err != nil { if err != nil {
t.Fatalf("failure to make reasonable BPS: %v", err) t.Fatalf("failure to make reasonable BPS: %v", err)
} }
@ -79,7 +79,7 @@ func TestAdd(t *testing.T) {
func TestHumanBytes(t *testing.T) { func TestHumanBytes(t *testing.T) {
t.Parallel() t.Parallel()
bw, err := New(100*time.Second, 10) bw, err := NewPrecise(100*time.Second, 10)
if err != nil { if err != nil {
t.Fatalf("failure to make reasonable BPS: %v", err) t.Fatalf("failure to make reasonable BPS: %v", err)
} }

View File

@ -1,11 +1,11 @@
# bps # bps
`bps` is used to keep track of rates `bps` is used to keep track of bandwidth rates
It is intended to be used as such: It is intended to be used as such:
// report on the rate over a 60 second interval, update every second // report on the rate over a 60 second interval, update every second
bw, _ := New(60*time.Second, 60) bw, _ := New(60*time.Second)
bw.Add(10) bw.Add(10)
b := &bytes.Buffer{} b := &bytes.Buffer{}
b.Write([]byte("helloooooooooooooooooooooooooooooooooooooooooooooo")) b.Write([]byte("helloooooooooooooooooooooooooooooooooooooooooooooo"))