Skip to content

Commit

Permalink
Merge pull request #18 from rizkybiz/add-count
Browse files Browse the repository at this point in the history
Added the Count method
  • Loading branch information
maier authored Mar 24, 2021
2 parents f61134e + e6b5ee5 commit 2b950eb
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
26 changes: 26 additions & 0 deletions circonusllhist.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,32 @@ func (h *Histogram) Mean() float64 {
return h.ApproxMean()
}

//Count returns the number of recorded values.
func (h *Histogram) Count() uint64 {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
var count uint64
for _, bin := range h.bvs[0:h.used] {
if bin.isNaN() {
continue
}
count += bin.count
}
return count
}

//BinCount returns the number of used bins.
func (h *Histogram) BinCount() uint64 {
if h.useLocks {
h.mutex.RLock()
defer h.mutex.RUnlock()
}
binCount := h.used
return uint64(binCount)
}

// Reset forgets all bins in the histogram (they remain allocated)
func (h *Histogram) Reset() {
if h.useLocks {
Expand Down
35 changes: 35 additions & 0 deletions circonusllhist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,41 @@ func TestSerialize(t *testing.T) {
}
}

func TestCount(t *testing.T) {
h, err := NewFromStrings([]string{
"H[0.0e+00]=1",
"H[1.0e+01]=1",
"H[2.0e+02]=1",
}, true)
if err != nil {
t.Error("could not read from strings for test")
}
if h.Count() != 3 {
t.Error("the count is incorrect")
}
err = h.RecordValue(10)
if err != nil {
t.Error("could not record new value to histogram")
}
if h.Count() != 4 {
t.Error("the count is incorrect")
}
}

func TestBinCount(t *testing.T) {
h, err := NewFromStrings([]string{
"H[0.0e+00]=1",
"H[1.0e+01]=1",
"H[2.0e+02]=1",
}, true)
if err != nil {
t.Error("could not read from strings for test")
}
if h.BinCount() != 3 {
t.Error("bin count is incorrect")
}
}

func TestJSON(t *testing.T) {
h, err := NewFromStrings([]string{
"H[0.0e+00]=1",
Expand Down

0 comments on commit 2b950eb

Please sign in to comment.