Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add benchmark to test parallel perf #9

Open
wants to merge 1 commit into
base: fix/6-remove-recursion
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 79 additions & 3 deletions crdt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ type mockBroadcaster struct {
chans []chan []byte
myChan chan []byte
dropProb int // probability of dropping a message instead of receiving it
t *testing.T
t testing.TB
}

func newBroadcasters(t *testing.T, n int) ([]*mockBroadcaster, context.CancelFunc) {
func newBroadcasters(t testing.TB, n int) ([]*mockBroadcaster, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
broadcasters := make([]*mockBroadcaster, n, n)
chans := make([]chan []byte, n, n)
Expand Down Expand Up @@ -195,7 +195,7 @@ func (mds *mockDAGSync) GetMany(ctx context.Context, cids []cid.Cid) <-chan *ipl
return ch
}

func makeReplicas(t *testing.T, opts *Options) ([]*Datastore, func()) {
func makeReplicas(t testing.TB, opts *Options) ([]*Datastore, func()) {
bcasts, bcastCancel := newBroadcasters(t, numReplicas)
bs := mdutils.Bserv()
dagserv := merkledag.NewDAGService(bs)
Expand Down Expand Up @@ -557,3 +557,79 @@ func TestHooks(t *testing.T) {
t.Error("all replicas should have notified Remove", deleted)
}
}

func BenchmarkSync(b *testing.B) {
testcases := []struct {
numWorkers int
}{
{1},
{2},
{3},
{4},
{5},
{10},
{15},
{30},
{50},
}

for _, tt := range testcases {
b.Run(fmt.Sprintf("%d", tt.numWorkers), func(b *testing.B) {

nItems := 500
opts := &Options{NumWorkers: tt.numWorkers}

for i := 0; i < b.N; i++ {
replicas, closeReplicas := makeReplicas(b, opts)
defer closeReplicas()

b.StartTimer()

// Add nItems choosing the replica randomly
for i := 0; i < nItems; i++ {
k := ds.RandomKey()
v := []byte(fmt.Sprintf("%d", i))
n := rand.Intn(len(replicas))
err := replicas[n].Put(k, v)
if err != nil {
b.Fatal(err)
}
}

time.Sleep(500 * time.Millisecond)

// Query all items
q := query.Query{
KeysOnly: true,
}
results, err := replicas[0].Query(q)
if err != nil {
b.Fatal(err)
}
defer results.Close()
rest, err := results.Rest()
if err != nil {
b.Fatal(err)
}
if len(rest) != nItems {
b.Fatalf("expected %d elements", nItems)
}

// make sure each item has arrived to every replica
for _, res := range rest {
for _, r := range replicas {
ok, err := r.Has(ds.NewKey(res.Key))
if err != nil {
b.Error(err)
}
if !ok {
b.Error("replica should have key")
}
}
}

b.StopTimer()
}
})
}
}