Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion amt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"math/rand"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -53,14 +54,17 @@ func runBenchmarkWithBitWidths(b *testing.B, bitwidths []uint, fn func(*testing.

type mockBlocks struct {
data map[cid.Cid]block.Block
dataMu sync.Mutex
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required to prevent race condition in test since ParallelDiff accesses the store in parallel

getCount, putCount int
}

func newMockBlocks() *mockBlocks {
return &mockBlocks{make(map[cid.Cid]block.Block), 0, 0}
return &mockBlocks{make(map[cid.Cid]block.Block), sync.Mutex{}, 0, 0}
}

func (mb *mockBlocks) Get(c cid.Cid) (block.Block, error) {
mb.dataMu.Lock()
defer mb.dataMu.Unlock()
d, ok := mb.data[c]
mb.getCount++
if ok {
Expand All @@ -70,12 +74,16 @@ func (mb *mockBlocks) Get(c cid.Cid) (block.Block, error) {
}

func (mb *mockBlocks) Put(b block.Block) error {
mb.dataMu.Lock()
defer mb.dataMu.Unlock()
mb.putCount++
mb.data[b.Cid()] = b
return nil
}

func (mb *mockBlocks) report(b *testing.B) {
mb.dataMu.Lock()
defer mb.dataMu.Unlock()
b.ReportMetric(float64(mb.getCount)/float64(b.N), "gets/op")
b.ReportMetric(float64(mb.putCount)/float64(b.N), "puts/op")
}
Expand Down
Loading