Skip to content

Commit

Permalink
Merge pull request #12 from paulbellamy/codecov
Browse files Browse the repository at this point in the history
Add codecov, and gometalinter to the ci build
  • Loading branch information
paulbellamy committed Jul 19, 2017
2 parents ae7dd4e + bd63311 commit 524851a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 52 deletions.
48 changes: 4 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# ratecounter

[![CircleCI](https://circleci.com/gh/paulbellamy/ratecounter.svg?style=svg)](https://circleci.com/gh/paulbellamy/ratecounter)
[![Go Report Card](https://goreportcard.com/badge/github.com/paulbellamy/ratecounter)](https://goreportcard.com/report/github.com/paulbellamy/ratecounter)
[![GoDoc](https://godoc.org/github.com/paulbellamy/ratecounter?status.svg)](https://godoc.org/github.com/paulbellamy/ratecounter)
[![codecov](https://codecov.io/gh/paulbellamy/ratecounter/branch/master/graph/badge.svg)](https://codecov.io/gh/paulbellamy/ratecounter)

A Thread-Safe RateCounter implementation in Golang

Expand Down Expand Up @@ -54,48 +57,5 @@ counter.Rate()

## Documentation

```
type Counter struct {
// contains filtered or unexported fields
}
A Counter is a thread-safe counter implementation
func NewCounter() *Counter
NewCounter is used to construct a new Counter object
func (c *Counter) Incr(val int64)
Increment the counter by some value
func (c *Counter) Value() int64
Return the counter's current value
type RateCounter struct {
// contains filtered or unexported fields
}
A RateCounter is a thread-safe counter which returns the number of times
'Mark' has been called in the last interval
func NewRateCounter(intrvl time.Duration) *RateCounter
Constructs a new RateCounter, for the interval provided
Check latest documentation on [go doc](https://godoc.org/github.com/paulbellamy/ratecounter).

func (r *RateCounter) Mark()
Add 1 event into the RateCounter
func (r *RateCounter) Rate() int64
Return the current number of events in the last interval
type AvgRateCounter struct {
// contains filtered or unexported fields
}
An AvgRateCounter is a thread-safe counter which returns
the ratio between the number of calls 'Incr' and the counter value in the last interval
func NewAvgRateCounter(intrvl time.Duration) *AvgRateCounter
Constructs a new AvgRateCounter, for the interval provided
func (a *AvgRateCounter) Incr(val int64)
Adds an event into the AvgRateCounter
func (a *AvgRateCounter) Rate() float64
Returns the current ratio between the events count and its values during the last interval
```
13 changes: 8 additions & 5 deletions avgratecounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type AvgRateCounter struct {
interval time.Duration
}

// NewRateCounter Constructs a new AvgRateCounter, for the interval provided
// NewAvgRateCounter constructs a new AvgRateCounter, for the interval provided
func NewAvgRateCounter(intrvl time.Duration) *AvgRateCounter {
return &AvgRateCounter{
hits: NewRateCounter(intrvl),
Expand All @@ -22,15 +22,16 @@ func NewAvgRateCounter(intrvl time.Duration) *AvgRateCounter {
}
}

func (r *AvgRateCounter) WithResolution(resolution int) *AvgRateCounter {
// WithResolution determines the minimum resolution of this counter
func (a *AvgRateCounter) WithResolution(resolution int) *AvgRateCounter {
if resolution < 1 {
panic("AvgRateCounter resolution cannot be less than 1")
}

r.hits = r.hits.WithResolution(resolution)
r.counter = r.counter.WithResolution(resolution)
a.hits = a.hits.WithResolution(resolution)
a.counter = a.counter.WithResolution(resolution)

return r
return a
}

// Incr Adds an event into the AvgRateCounter
Expand All @@ -50,10 +51,12 @@ func (a *AvgRateCounter) Rate() float64 {
return float64(value) / float64(hits)
}

// Hits returns the number of calling method Incr during specified interval
func (a *AvgRateCounter) Hits() int64 {
return a.hits.Rate()
}

// String returns counter's rate formatted to string
func (a *AvgRateCounter) String() string {
return strconv.FormatFloat(a.Rate(), 'e', 5, 64)
}
21 changes: 21 additions & 0 deletions circle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
dependencies:
post:
- go get -u github.com/alecthomas/gometalinter
- gometalinter --install

test:
override:
- go test -v -race -coverprofile=coverage.txt -covermode=atomic ./...
- |
gometalinter \
--disable-all \
--enable=deadcode \
--enable=errcheck \
--enable=golint \
--enable=gosimple \
--enable=unconvert \
--enable=vet \
--enable=vetshadow \
./...
post:
- bash <(curl -s https://codecov.io/bash)
5 changes: 3 additions & 2 deletions counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import "sync/atomic"
// A Counter is a thread-safe counter implementation
type Counter int64

// Increment the counter by some value
// Incr method increments the counter by some value
func (c *Counter) Incr(val int64) {
atomic.AddInt64((*int64)(c), val)
}

// Reset method resets the counter's value to zero
func (c *Counter) Reset() {
atomic.StoreInt64((*int64)(c), 0)
}

// Return the counter's current value
// Value method returns the counter's current value
func (c *Counter) Value() int64 {
return atomic.LoadInt64((*int64)(c))
}
3 changes: 2 additions & 1 deletion ratecounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewRateCounter(intrvl time.Duration) *RateCounter {
return ratecounter.WithResolution(20)
}

// WithResolution determines the minimum resolution of this counter, default is 20
func (r *RateCounter) WithResolution(resolution int) *RateCounter {
if resolution < 1 {
panic("RateCounter resolution cannot be less than 1")
Expand All @@ -52,7 +53,7 @@ func (r *RateCounter) run() {
next := (int(current) + 1) % r.resolution
r.counter.Incr(-1 * r.partials[next].Value())
r.partials[next].Reset()
atomic.CompareAndSwapInt32(&r.current, int32(current), int32(next))
atomic.CompareAndSwapInt32(&r.current, current, int32(next))
if r.counter.Value() == 0 {
atomic.StoreInt32(&r.running, 0)
ticker.Stop()
Expand Down

0 comments on commit 524851a

Please sign in to comment.