Skip to content

Commit

Permalink
Merge pull request #24 from ipfs/feat/testing
Browse files Browse the repository at this point in the history
Add Datastore Tests
  • Loading branch information
aschmahmann authored Nov 27, 2019
2 parents d4c0bd7 + 788dc2a commit 0ebe79f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 88 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ os:
language: go

go:
- 1.11.x
- 1.12.x

env:
global:
- GOTFLAGS="-race"
matrix:
- BUILD_DEPTYPE=gx
- BUILD_DEPTYPE=gomod


Expand All @@ -24,7 +23,6 @@ script:

cache:
directories:
- $GOPATH/src/gx
- $GOPATH/pkg/mod
- $HOME/.cache/go-build

Expand Down
9 changes: 0 additions & 9 deletions Makefile

This file was deleted.

2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ require (
github.com/ipfs/go-datastore v0.1.1
github.com/ipfs/go-metrics-interface v0.0.1
)

go 1.12
99 changes: 53 additions & 46 deletions measure.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ func New(prefix string, ds datastore.Datastore) *measure {
duErr: metrics.New(prefix+".du.errors_total", "Number of errored Datastore.DiskUsage calls").Counter(),
duLatency: metrics.New(prefix+".du.latency_seconds",
"Latency distribution of Datastore.DiskUsage calls").Histogram(datastoreLatencyBuckets),

batchPutNum: metrics.New(prefix+".batchput_total", "Total number of Batch.Put calls").Counter(),
batchPutErr: metrics.New(prefix+".batchput.errors_total", "Number of errored Batch.Put calls").Counter(),
batchPutLatency: metrics.New(prefix+".batchput.latency_seconds",
"Latency distribution of Batch.Put calls").Histogram(datastoreLatencyBuckets),
batchPutSize: metrics.New(prefix+".batchput.size_bytes",
"Size distribution of byte slices put into batches").Histogram(datastoreSizeBuckets),

batchDeleteNum: metrics.New(prefix+".batchdelete_total", "Total number of Batch.Delete calls").Counter(),
batchDeleteErr: metrics.New(prefix+".batchdelete.errors_total", "Number of errored Batch.Delete calls").Counter(),
batchDeleteLatency: metrics.New(prefix+".batchdelete.latency_seconds",
"Latency distribution of Batch.Delete calls").Histogram(datastoreLatencyBuckets),

batchCommitNum: metrics.New(prefix+".batchcommit_total", "Total number of Batch.Commit calls").Counter(),
batchCommitErr: metrics.New(prefix+".batchcommit.errors_total", "Number of errored Batch.Commit calls").Counter(),
batchCommitLatency: metrics.New(prefix+".batchcommit.latency_seconds",
"Latency distribution of Batch.Commit calls").Histogram(datastoreLatencyBuckets),
}
return m
}
Expand Down Expand Up @@ -125,6 +142,19 @@ type measure struct {
duNum metrics.Counter
duErr metrics.Counter
duLatency metrics.Histogram

batchPutNum metrics.Counter
batchPutErr metrics.Counter
batchPutLatency metrics.Histogram
batchPutSize metrics.Histogram

batchDeleteNum metrics.Counter
batchDeleteErr metrics.Counter
batchDeleteLatency metrics.Histogram

batchCommitNum metrics.Counter
batchCommitErr metrics.Counter
batchCommitLatency metrics.Histogram
}

func recordLatency(h metrics.Histogram, start time.Time) {
Expand Down Expand Up @@ -251,12 +281,7 @@ func (m *measure) DiskUsage() (uint64, error) {
}

type measuredBatch struct {
puts int
deletes int

putts datastore.Batch
delts datastore.Batch

b datastore.Batch
m *measure
}

Expand All @@ -265,64 +290,46 @@ func (m *measure) Batch() (datastore.Batch, error) {
if !ok {
return nil, datastore.ErrBatchUnsupported
}
pb, err := bds.Batch()
if err != nil {
return nil, err
}

db, err := bds.Batch()
batch, err := bds.Batch()
if err != nil {
return nil, err
}

return &measuredBatch{
putts: pb,
delts: db,

b: batch,
m: m,
}, nil
}

func (mt *measuredBatch) Put(key datastore.Key, val []byte) error {
mt.puts++
mt.m.putSize.Observe(float64(len(val)))
return mt.putts.Put(key, val)
}

func (mt *measuredBatch) Delete(key datastore.Key) error {
mt.deletes++
return mt.delts.Delete(key)
}

func (mt *measuredBatch) Commit() error {
err := logBatchCommit(mt.delts, mt.deletes, mt.m.deleteNum, mt.m.deleteErr, mt.m.deleteLatency)
defer recordLatency(mt.m.batchPutLatency, time.Now())
mt.m.batchPutNum.Inc()
mt.m.batchPutSize.Observe(float64(len(val)))
err := mt.b.Put(key, val)
if err != nil {
return err
mt.m.batchPutErr.Inc()
}
return err
}

err = logBatchCommit(mt.putts, mt.puts, mt.m.putNum, mt.m.putErr, mt.m.putLatency)
func (mt *measuredBatch) Delete(key datastore.Key) error {
defer recordLatency(mt.m.batchDeleteLatency, time.Now())
mt.m.batchDeleteNum.Inc()
err := mt.b.Delete(key)
if err != nil {
return err
mt.m.batchDeleteErr.Inc()
}

return nil
return err
}

func logBatchCommit(b datastore.Batch, n int, num, errs metrics.Counter, lat metrics.Histogram) error {
if n > 0 {
before := time.Now()
err := b.Commit()
took := time.Since(before) / time.Duration(n)
num.Add(float64(n))
for i := 0; i < n; i++ {
lat.Observe(took.Seconds())
}
if err != nil {
errs.Inc()
return err
}
func (mt *measuredBatch) Commit() error {
defer recordLatency(mt.m.batchCommitLatency, time.Now())
mt.m.batchCommitNum.Inc()
err := mt.b.Commit()
if err != nil {
mt.m.batchCommitErr.Inc()
}
return nil
return err
}

func (m *measure) Close() error {
Expand Down
12 changes: 12 additions & 0 deletions measure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package measure

import (
"testing"

dstest "github.com/ipfs/go-datastore/test"
)

func TestSuite(t *testing.T) {
d := New("measure", dstest.NewTestDatastore(false))
dstest.SubtestAll(t, d)
}
30 changes: 0 additions & 30 deletions package.json

This file was deleted.

0 comments on commit 0ebe79f

Please sign in to comment.