Documentation ¶
Overview ¶
Package counter implements an advanced, fast and thread-safe counter. It collects statstics, like current rate, min / max rate, etc.
Index ¶
- type Counter
- func (c *Counter) CalculateAverageRate(interval time.Duration) float64
- func (c *Counter) CalculateMaximumRate(interval time.Duration) float64
- func (c *Counter) CalculateMinimumRate(interval time.Duration) float64
- func (c *Counter) Count() uint64
- func (c *Counter) Increment()
- func (c *Counter) Reset()
- func (c *Counter) Start() *Counter
- func (c *Counter) Stop()
- func (c *Counter) WithAdvancedStats() *Counter
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Counter ¶
type Counter struct {
// contains filtered or unexported fields
}
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.
func (*Counter) CalculateAverageRate ¶
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 }
Output:
func (*Counter) CalculateMaximumRate ¶
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 }
Output:
func (*Counter) CalculateMinimumRate ¶
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 }
Output:
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 ¶
Start starts the counter. It returns the counter itself, so you can chain it.
func (*Counter) WithAdvancedStats ¶
WithAdvancedStats enables the calculation of advanced statistics like CalculateMinimumRate and CalculateMaximumRate. CalculateAverageRate and CalculateCurrentRate are always enabled.