bps

package module
v0.0.0-...-3398b4f Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2016 License: BSD-3-Clause Imports: 5 Imported by: 0

README

bps

GoDoc

bps is used to keep track of bandwidth rates

It is intended to be used as such:

// report on the rate over a 60 second interval, update every second
bw, _ := New(60*time.Second)
bw.Add(10)
b := &bytes.Buffer{}
b.Write([]byte("helloooooooooooooooooooooooooooooooooooooooooooooo"))
io.Copy(bw, b)
time.Sleep(1 * time.Second)
log.Printf("%+v", bw.HumanRate())

Documentation

Overview

Package bps keeps track of byte rates

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BPS

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

BPS calculates bandwidth rates.

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

func New

func New(interval time.Duration) (*BPS, error)

New Returns a populated and ready to use BPS.

bytes per second are reported for the last time interval (defined by interval), and is updated every (interval / 100) seconds.

func NewPrecise

func NewPrecise(interval time.Duration, resolution uint) (*BPS, error)

NewPrecise behaves like New, but allows the user to specify the temporal resolution.

If you need to query more or less frequently than interval / 100 you'll want to use this function.

func (*BPS) Add

func (b *BPS) Add(i int64)

Add adds i to the current time bucket.

func (*BPS) Close

func (b *BPS) Close()

Close cleans up and shuts down a BPS.

func (*BPS) Data

func (b *BPS) Data() []int

Data returns an slice of all the buckets, the most recent bucket first.

func (*BPS) HumanRate

func (b *BPS) HumanRate() string

HumanRate returns a human-friendly (e.g. 23.3MB/s) rate.

func (*BPS) Rate

func (b *BPS) Rate() float64

Rate returns the current rate (bytes / second).

func (*BPS) Write

func (b *BPS) Write(p []byte) (int, error)

Write implements io.Writer so that one can simply write bytes to the struct.

type Multi

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

Multi wraps multiple BPS

Example
// Create a Multi that records information for 1s, 10s, and 1m
bw, err := NewMulti(1*time.Second, 10*time.Second, 1*time.Minute)
if err != nil {
	log.Fatal(err)
}

// Add some data, then print out rates;w
bw.Add(1)
fmt.Printf("%v\n", len(bw.Rate()))
Output:

3

func NewMulti

func NewMulti(durs ...time.Duration) (*Multi, error)

NewMulti returns a populated Multi that has a BPS for each provided duration.

func (*Multi) Add

func (mw *Multi) Add(i int64)

Add adds i to each internal BPS.

func (*Multi) Rate

func (mw *Multi) Rate() []float64

Rate returns a slice of rates, one for each BPS.

Jump to

Keyboard shortcuts

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