SkillAgentSearch skills...

Counter

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

Install / Use

/learn @atomicgo/Counter
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

<!-- โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚ IMPORTANT NOTE โ”‚ โ”‚ โ”‚ โ”‚ This file is automatically generated โ”‚ โ”‚ All manual modifications will be overwritten โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ --> <h1 align="center">AtomicGo | counter</h1> <p align="center"> <img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fatomicgo.dev%2Fapi%2Fshields%2Fcounter&style=flat-square" alt="Downloads"> <a href="https://github.com/atomicgo/counter/releases"> <img src="https://img.shields.io/github/v/release/atomicgo/counter?style=flat-square" alt="Latest Release"> </a> <a href="https://codecov.io/gh/atomicgo/counter" target="_blank"> <img src="https://img.shields.io/github/actions/workflow/status/atomicgo/counter/go.yml?style=flat-square" alt="Tests"> </a> <a href="https://codecov.io/gh/atomicgo/counter" target="_blank"> <img src="https://img.shields.io/codecov/c/gh/atomicgo/counter?color=magenta&logo=codecov&style=flat-square" alt="Coverage"> </a> <a href="https://codecov.io/gh/atomicgo/counter"> <!-- unittestcount:start --><img src="https://img.shields.io/badge/Unit_Tests-13-magenta?style=flat-square" alt="Unit test count"><!-- unittestcount:end --> </a> <a href="https://opensource.org/licenses/MIT" target="_blank"> <img src="https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square" alt="License: MIT"> </a> <a href="https://goreportcard.com/report/github.com/atomicgo/counter" target="_blank"> <img src="https://goreportcard.com/badge/github.com/atomicgo/counter?style=flat-square" alt="Go report"> </a> </p>
<p align="center"> <strong><a href="https://pkg.go.dev/atomicgo.dev/counter#section-documentation" target="_blank">Documentation</a></strong> | <strong><a href="https://github.com/atomicgo/atomicgo/blob/main/CONTRIBUTING.md" target="_blank">Contributing</a></strong> | <strong><a href="https://github.com/atomicgo/atomicgo/blob/main/CODE_OF_CONDUCT.md" target="_blank">Code of Conduct</a></strong> </p>
<p align="center"> <img src="https://raw.githubusercontent.com/atomicgo/atomicgo/main/assets/header.png" alt="AtomicGo"> </p> <p align="center"> <table> <tbody> </tbody> </table> </p> <h3 align="center"><pre>go get atomicgo.dev/counter</pre></h3> <p align="center"> <table> <tbody> </tbody> </table> </p> <!-- gomarkdoc:embed:start --> <!-- Code generated by gomarkdoc. DO NOT EDIT -->

counter

import "atomicgo.dev/counter"

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

Index

<a name="Counter"></a>

type Counter

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

Basic usage:

c := counter.NewCounter().Start()
c.Increment()
fmt.Println(c.Count()) // prints 1
c.Stop()
rate := c.CalculateAverageRate(time.Second) // events per second
type Counter struct {
    // contains filtered or unexported fields
}

<a name="NewCounter"></a>

func NewCounter

func NewCounter() *Counter

NewCounter returns a new Counter.

The counter starts in a stopped state. Call Start() to begin counting.

<a name="Counter.CalculateAverageRate"></a>

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`.

For example, to get events per second:

rate := counter.CalculateAverageRate(time.Second)
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
}

<a name="Counter.CalculateMaximumRate"></a>

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 or has no increments. Needs to be enabled via WithAdvancedStats.

The maximum rate represents the fastest pace at which events occurred.

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
}

<a name="Counter.CalculateMinimumRate"></a>

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 or has no increments. Needs to be enabled via WithAdvancedStats.

The minimum rate represents the slowest pace at which events occurred.

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
}

<a name="Counter.Count"></a>

func (*Counter) Count

func (c *Counter) Count() uint64

Count returns the current count.

This method is thread-safe and can be called concurrently from multiple goroutines.

<a name="Counter.Increment"></a>

func (*Counter) Increment

func (c *Counter) Increment()

Increment increments the counter by 1.

This method is thread-safe and can be called concurrently from multiple goroutines.

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

<a name="Counter.Reset"></a>

func (*Counter) Reset

func (c *Counter) Reset()

Reset stops and resets the counter.

This resets the count to 0 and clears all statistics.

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

<a name="Counter.Start"></a>

func (*Counter) Start

func (c *Counter) Start() *Counter

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

If the counter is already started, this is a no-op.

<a name="Counter.Stop"></a>

func (*Counter) Stop

func (c *Counter) Stop()

Stop stops the counter.

This freezes the counter for rate calculations but does not reset the count. If the counter is already stopped, this is a no-op.

<a name="Counter.WithAdvancedStats"></a>

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.

Note: Enabling advanced stats will increase memory usage proportional to the number of increments.

Generated by gomarkdoc

<!-- gomarkdoc:embed:end -->

AtomicGo.dev ย ยทย  with โค๏ธ by @MarvinJWendt | MarvinJWendt.com

View on GitHub
GitHub Stars9
CategoryDevelopment
Updated2mo ago
Forks1

Languages

Go

Security Score

90/100

Audited on Jan 18, 2026

No findings