Giter Site home page Giter Site logo

atomicgo / counter Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 31 KB

🔢 Counter is a fast, thread-safe counter. It collects statstics, like current rate, min / max rate, etc.

Home Page: https://atomicgo.dev

License: MIT License

Go 100.00%
atomicgo go golang golang-library

counter's Introduction

AtomicGo | counter

Downloads Latest Release Tests Coverage Unit test count License: MIT Go report


Documentation | Contributing | Code of Conduct


AtomicGo

go get atomicgo.dev/counter

counter

import "atomicgo.dev/counter"

Package counter implements an advanced, fast and thread-safe counter. It collects statstics, like current rate, min / max rate, etc.

Index

type Counter

Counter is a fast, thread-safe counter. It collects statstics, like current rate, min / max rate, etc. The Counter can go up to `18446744073709551615` (2^64 - 1), as it uses uint64 internally.

type Counter struct {
    // contains filtered or unexported fields
}
func NewCounter() *Counter

NewCounter returns a new Counter.

func (*Counter) CalculateAverageRate

func (c *Counter) CalculateAverageRate(interval time.Duration) float64

CalculateAverageRate calculates the average rate of the counter. It returns the rate in `count / interval`.

Example

package main

import (
	"fmt"
	"time"

	"atomicgo.dev/counter"
)

func main() {
	c := counter.NewCounter().Start()
	for i := 0; i < 10; i++ {
		time.Sleep(100 * time.Millisecond)
		c.Increment()
	}
	c.Stop()

	fmt.Println(c.CalculateAverageRate(time.Second))
	// Output should be around 10, as we incremented 10 times in 1 second
}

func (*Counter) CalculateMaximumRate

func (c *Counter) CalculateMaximumRate(interval time.Duration) float64

CalculateMaximumRate calculates the maximum rate of the counter. It returns the rate in `count / interval`. It returns 0 if the counter has not been started yet. Needs to be enabled via WithAdvancedStats.

Example

package main

import (
	"fmt"
	"time"

	"atomicgo.dev/counter"
)

func main() {
	c := counter.NewCounter().WithAdvancedStats().Start()
	for i := 0; i < 10; i++ {
		time.Sleep(100 * time.Millisecond)
		c.Increment()
	}
	c.Stop()

	fmt.Println(c.CalculateMaximumRate(time.Second))
	// Output should be around 10, as we incremented 10 times in 1 second
}

func (*Counter) CalculateMinimumRate

func (c *Counter) CalculateMinimumRate(interval time.Duration) float64

CalculateMinimumRate calculates the minimum rate of the counter. It returns the rate in `count / interval`. It returns 0 if the counter has not been started yet. Needs to be enabled via WithAdvancedStats.

Example

package main

import (
	"fmt"
	"time"

	"atomicgo.dev/counter"
)

func main() {
	c := counter.NewCounter().WithAdvancedStats().Start()
	for i := 0; i < 10; i++ {
		time.Sleep(100 * time.Millisecond)
		c.Increment()
	}
	c.Stop()

	fmt.Println(c.CalculateMinimumRate(time.Second))
	// Output should be around 10, as we incremented 10 times in 1 second
}

func (*Counter) Count

func (c *Counter) Count() uint64

Count returns the current count.

func (*Counter) Increment

func (c *Counter) Increment()

Increment increments the counter by 1.

Example

package main

import (
	"fmt"

	"atomicgo.dev/counter"
)

func main() {
	c := counter.NewCounter().Start()
	for i := 0; i < 10; i++ {
		c.Increment()
	}
	c.Stop()

	fmt.Println(c.Count())
}

Output

10

func (*Counter) Reset

func (c *Counter) Reset()

Reset stops and resets the counter.

Example

package main

import (
	"fmt"

	"atomicgo.dev/counter"
)

func main() {
	c := counter.NewCounter().Start()
	for i := 0; i < 10; i++ {
		c.Increment()
	}
	c.Reset()

	fmt.Println(c.Count())
}

Output

0

func (*Counter) Start

func (c *Counter) Start() *Counter

Start starts the counter. It returns the counter itself, so you can chain it.

func (*Counter) Stop

func (c *Counter) Stop()

Stop stops the counter.

func (*Counter) WithAdvancedStats

func (c *Counter) WithAdvancedStats() *Counter

WithAdvancedStats enables the calculation of advanced statistics like CalculateMinimumRate and CalculateMaximumRate. CalculateAverageRate and CalculateCurrentRate are always enabled.

Generated by gomarkdoc


AtomicGo.dev  ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com

counter's People

Contributors

marvinjwendt avatar

Stargazers

 avatar Márk Bartos avatar Carlos Armando Marcano Vargas avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.