Skip to content

Commit

Permalink
Merge pull request #12 from Preetam/stats
Browse files Browse the repository at this point in the history
Add collection stats
  • Loading branch information
Preetam authored Sep 12, 2016
2 parents 5732751 + a004811 commit bd8d701
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Collection struct {
f *os.File
wal *wal
cache *recordCache
stats Stats

metaLock sync.RWMutex
}
Expand Down Expand Up @@ -240,6 +241,8 @@ func (c *Collection) readRecord(offset int64) (*record, error) {
c.cache.lock.RLock()
if rec := c.cache.cache[offset]; rec != nil {
c.cache.lock.RUnlock()
c.stats.incRecordsRead(1)
c.stats.incCacheHits(1)
return rec, nil
}
c.cache.lock.RUnlock()
Expand Down Expand Up @@ -277,6 +280,8 @@ func (c *Collection) readRecord(offset int64) (*record, error) {
Key: key,
Value: value,
}
c.stats.incRecordsRead(1)
c.stats.incCacheMisses(1)

c.cache.push(rec)

Expand Down Expand Up @@ -654,6 +659,7 @@ func (c *Collection) Update(wb *WriteBatch) (int64, error) {
}
}

c.stats.incRecordsWritten(uint64(len(newlyInserted)))
return c.LastCommit, c.f.Sync()
}

Expand Down Expand Up @@ -816,3 +822,8 @@ func (c *Collection) Version() int64 {
defer c.metaLock.RUnlock()
return c.LastCommit
}

// Stats returns collection statistics.
func (c *Collection) Stats() Stats {
return c.stats.clone()
}
5 changes: 5 additions & 0 deletions lm2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ func TestCopy(t *testing.T) {
if count1 != count2 || count1 != N {
t.Errorf("incorrect count. N = %d, count1 = %d, count2 = %d", N, count1, count2)
}
t.Logf("%+v", c.Stats())
t.Logf("%+v", c2.Stats())
}

func TestWriteBatch(t *testing.T) {
Expand Down Expand Up @@ -332,6 +334,7 @@ func TestWriteBatch2(t *testing.T) {
}
i++
}
t.Logf("%+v", c.Stats())
}

func TestWriteCloseOpen(t *testing.T) {
Expand Down Expand Up @@ -401,6 +404,7 @@ func TestWriteCloseOpen(t *testing.T) {
}
i++
}
t.Logf("%+v", c.Stats())
}

func TestReadLastEntry(t *testing.T) {
Expand Down Expand Up @@ -431,4 +435,5 @@ func TestReadLastEntry(t *testing.T) {
if entry.NumRecords != 2 {
t.Errorf("expected %d records, got %d", 2, entry.NumRecords)
}
t.Logf("%+v", c.Stats())
}
36 changes: 36 additions & 0 deletions stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package lm2

import "sync/atomic"

// Stats holds collection statistics.
type Stats struct {
RecordsWritten uint64
RecordsRead uint64
CacheHits uint64
CacheMisses uint64
}

func (s *Stats) incRecordsWritten(count uint64) {
atomic.AddUint64(&s.RecordsWritten, count)
}

func (s *Stats) incRecordsRead(count uint64) {
atomic.AddUint64(&s.RecordsRead, count)
}

func (s *Stats) incCacheHits(count uint64) {
atomic.AddUint64(&s.CacheHits, count)
}

func (s *Stats) incCacheMisses(count uint64) {
atomic.AddUint64(&s.CacheMisses, count)
}

func (s *Stats) clone() Stats {
return Stats{
RecordsWritten: atomic.LoadUint64(&s.RecordsWritten),
RecordsRead: atomic.LoadUint64(&s.RecordsRead),
CacheHits: atomic.LoadUint64(&s.CacheHits),
CacheMisses: atomic.LoadUint64(&s.CacheMisses),
}
}

0 comments on commit bd8d701

Please sign in to comment.