counters

package module
v0.0.0-...-2ee0466 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2020 License: MIT Imports: 13 Imported by: 0

README

counters

A GoLang implementation of counters.

It provides an object: counters.CounterBox and Counter, Min and Max implementation.

Installation:

$ go get -u github.com/orian/counters

The example usages:

import "github.com/orian/counters"

//... some code

cb := counters.NewCounterBox()
c := cb.GetCounter("ex")
c.Increment()
c.IncrementBy(6)
c.Value()  // returns 7
cb.GetCounter("ex").Value()  // Returns 7

For convenience there is also an http.HandleFunc provided which prints values of counters.

One may use subpackage globals if want to use a global counters.

import "github.com/orian/counters/global"

//... some code

c := global.GetCounter("ex")
c.Increment()
c.IncrementBy(6)
c.Value()  // returns 7
global.GetCounter("ex").Value()  // Returns 7

The library and all objects are thread safe.

Print into log or stdout on SIGINT

Please check the example/example.go to see how the interrupt can be handled in a nice way.

Performance and benchmarks

The library has two benchmarks. It's blazingly fast. One should cache counters which are used often. Every time the counter is requested, it's necessary to look up in map for a counter, this slows down the lib by a factor of 4.

$ go test --bench=. .
PASS
BenchmarkCounters        1000000      1287 ns/op
BenchmarkCountersCached  5000000       252 ns/op
ok      github.com/orian/counters   2.822s

Documentation

Overview

Package counters provides a simple counter, max and min functionalities. All counters are kept in CounterBox. Library is thread safe.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitCountersOnSignal

func InitCountersOnSignal(logger TrivialLogger, box Counters)

InitCountersOnSignal impl

func LogCountersEvery

func LogCountersEvery(logger TrivialLogger, box Counters, d time.Duration)

LogCountersEvery impl

Types

type Counter

type Counter interface {
	// Increment increases counter by one.
	Increment() int64
	// IncrementBy increases counter by a given number.
	IncrementBy(num int) int64
	// Decrement decreases counter by one.
	Decrement() int64
	// DecrementBy decreases counter by a given number.
	DecrementBy(num int) int64
	// Set sets a specific value.
	Set(num int)
	// Name returns a name of counter.
	Name() string
	// Value returns a current value of counter.
	Value() int64
}

Counter is an interface for integer increase only counter.

type CounterBox

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

CounterBox is a main type, it keeps references to all counters requested from it.

func NewCounterBox

func NewCounterBox() *CounterBox

NewCounterBox creates a new object to keep all counters.

func (*CounterBox) CreateHTTPHandler

func (c *CounterBox) CreateHTTPHandler() http.HandlerFunc

CreateHTTPHandler creates a simple handler printing values of all counters.

func (*CounterBox) Get

func (c *CounterBox) Get(name string) Counter

Get impl

func (*CounterBox) GetCounter

func (c *CounterBox) GetCounter(name string) Counter

GetCounter returns a counter of given name, if doesn't exist than create.

func (*CounterBox) GetMax

func (c *CounterBox) GetMax(name string) MaxMinValue

GetMax returns a maxima counter of given name, if doesn't exist than create.

func (*CounterBox) GetMin

func (c *CounterBox) GetMin(name string) MaxMinValue

GetMin returns a minima counter of given name, if doesn't exist than create.

func (*CounterBox) Max

func (c *CounterBox) Max(name string) MaxMinValue

Max impl

func (*CounterBox) Min

func (c *CounterBox) Min(name string) MaxMinValue

Min impl

func (*CounterBox) Names

func (c *CounterBox) Names() []string

Names impl

func (*CounterBox) Prefix

func (c *CounterBox) Prefix() string

Prefix impl

func (*CounterBox) String

func (c *CounterBox) String() string

func (*CounterBox) WithPrefix

func (c *CounterBox) WithPrefix(name string) Counters

WithPrefix impl

func (*CounterBox) WriteTo

func (c *CounterBox) WriteTo(w io.Writer) (int64, error)

WriteTo impl

type Counters

type Counters interface {
	Get(string) Counter
	Min(string) MaxMinValue
	Max(string) MaxMinValue
	WithPrefix(string) Counters
	GetCounter(string) Counter
	GetMin(string) MaxMinValue
	GetMax(string) MaxMinValue
	WriteTo(w io.Writer) (int64, error)
	Prefix() string
	String() string
}

Counters interface

func New

func New() Counters

New constructor

type MaxMinValue

type MaxMinValue interface {
	// Set allows to update value if necessary.
	Set(int)
	// Name returns a name of counter.
	Name() string
	// Value returns a current value.
	Value() int64
}

MaxMinValue is an interface for minima and maxima counters.

type TrivialLogger

type TrivialLogger interface {
	Print(...interface{})
}

TrivialLogger logger interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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