throughput

package
v0.5.10 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package throughput provides an interface and implementations of that interface to read the throughput of network devices.

Synopsis

A brief example of using the package to print throughput stats:

package main

import (
  "fmt"
  "github.com/robkingsbury/bndstat/throughput"
  "time"
)

func main() {
  // Throwing away errors is bad practice but is done here for brevity.
  reporter, _ := throughput.NewReporter()
  table := throughput.NewTable()

  for {
    stats, _ := reporter.Report()

    // Directly accessing device stats
    for _, device := range stats.Devices() {
      in, out, _ := stats.Avg(device, throughput.Kbps)
      fmt.Printf("%s: in = %.2f, out = %.2f\n", device, in, out)
    }

    // Using the builtin, aligned table output
    table.Write(throughput.TableWriteOpts{
      Stats:    stats,
      Devices:  stats.Devices(),
      Unit:     throughput.Kbps,
      ShowUnit: false,
    })

    time.Sleep(time.Second)
  }
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Linux

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

Linux implements the Reporter interface for linux systems.

func NewLinux

func NewLinux() *Linux

NewLinux returns a pointer to an initialized Linux Reporter.

func (*Linux) Report

func (l *Linux) Report() (*Stats, error)

Report reads /proc/net/dev, updates its internal state with the latest counters, and returns a slice of Stats for all network devices.

type NilReporter

type NilReporter struct{}

NilReporter implements the Reporter interface in a most trivial fashion.

func (*NilReporter) Report

func (*NilReporter) Report() (*Stats, error)

Report always returns an empty Stat slice and an error.

type Reporter

type Reporter interface {
	// Report should return a pointer to a Stats struct containing data for all
	// discovered network devices.
	Report() (*Stats, error)
}

Reporter is the interface implemented by types that report basic network device throughput stats.

func NewReporter added in v0.3.0

func NewReporter() (Reporter, error)

NewReporter returns an initialized Reporter that's compatible with the current OS. An error is returned if the OS is not supported.

type Stats

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

Stats contains throughput data for each network device.

func (*Stats) Avg

func (s *Stats) Avg(device string, unit Unit) (in float64, out float64, err error)

Avg returns the average throughput for the device, in the units specified. If the device does not exist, zeros are returned.

func (*Stats) Devices

func (s *Stats) Devices() []string

Devices returns a slice of device names that Stats has information on.

type Table

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

Table is used to print device Stats in a nicely formatted output.

func NewTable

func NewTable() *Table

NewTable() returns a pointer to a Table.

func (*Table) Header

func (t *Table) Header(devices []string, unit Unit, showunit bool)

Header prints the table header lines only.

func (*Table) Write

func (t *Table) Write(opt TableWriteOpts) error

Write outputs the average throughput of each device in an aligned, absolutely gorgeous rendition of data, designed to instill feelings of joy at the beauty in the world. Each time Table is called, the average throughput since the last call to Table is printed to stdout.

type TableWriteOpts added in v0.4.0

type TableWriteOpts struct {
	// Stats should be a pointer to a Stats struct with network
	// device throughput data.
	Stats *Stats

	// Devices is a slice of devices from Stats to display.
	Devices []string

	// Unit specifies the unit to use when display the throughput.
	Unit Unit

	// ShowUnit will print the unit type in the table header when set
	// to true.
	ShowUnit bool
}

TableWriteOpts is used to specify options by Table.Write().

type Unit

type Unit int

A unit is used to format the value returned by Stat.Avg().

const (
	Bps Unit = iota
	Kbps
	Mbps
	Gbps
	Tbps
)

func ParseUnit

func ParseUnit(u string) (Unit, error)

ParseUnit returns the Unit associated with a unit name. Use the string form of the const name for the unit that you want. Parsing is not case sensitive. For example, an input of "Bps" or "bps", will return the unit Bps, "Mbps", "mbps", or "mBps" will return the Mbps unit, etc.

An error is returned if the input does not match any Unit const.

func (Unit) Base10 added in v0.5.0

func (u Unit) Base10() float64

Base10 returns the base 10 value of the unit.

func (Unit) Base2 added in v0.5.0

func (u Unit) Base2() float64

Base2 returns the base 2 value of the unit.

func (Unit) String

func (u Unit) String() string

String implements Stringer for a Unit.

Jump to

Keyboard shortcuts

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