Categorygithub.com/orian/counters
modulepackage
0.0.0-20171031171931-65f79717c6d1
Repository: https://github.com/orian/counters.git
Documentation: pkg.go.dev

# 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

# Packages

No description provided by the author
No description provided by the author

# Functions

No description provided by the author
No description provided by the author
No description provided by the author
NewCounterBox creates a new object to keep all counters.

# Structs

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

# Interfaces

Counter is an interface for integer increase only counter.
No description provided by the author
MaxMinValue is an interface for minima and maxima counters.
No description provided by the author