Skip to content

Commit 366dcee

Browse files
committed
blockstore: add tests for event system and benchmarks
Benchmark results: BenchmarkEventFireSingle-12 1000000 24.1 ns/op BenchmarkEventFireMultiple-12 1000000 4.37 ns/op and zero allocations (to wide to fit). License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
1 parent 2910733 commit 366dcee

File tree

3 files changed

+179
-1
lines changed

3 files changed

+179
-1
lines changed

blocks/blockstore/blockstore.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func NewBlockstore(d ds.Batching) Blockstore {
9898
dsb = dd
9999
return &blockstore{
100100
datastore: dsb,
101-
events: &eventSystem{},
101+
events: newEventSystem(),
102102
}
103103
}
104104

blocks/blockstore/events.go

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ type eventSystem struct {
3232
handlers map[EventID][]EventFunc
3333
}
3434

35+
func newEventSystem() *eventSystem {
36+
return &eventSystem{handlers: make(map[EventID][]EventFunc)}
37+
}
38+
3539
func (es *eventSystem) addHandler(t EventID, ef EventFunc) {
3640
es.lock.Lock()
3741
defer es.lock.Unlock()

blocks/blockstore/events_test.go

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package blockstore
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ipfs/go-ipfs/blocks/blocksutil"
7+
)
8+
9+
func TestBasicEventHandling(t *testing.T) {
10+
es := newEventSystem()
11+
blkgen := blocksutil.NewBlockGenerator()
12+
b := blkgen.Next()
13+
e := &Event{
14+
cid: b.Cid(),
15+
block: b,
16+
}
17+
18+
ok := false
19+
handler := func(ei *Event) (bool, error) {
20+
if e != ei {
21+
t.Fatal("event is different")
22+
}
23+
ok = true
24+
return false, nil
25+
}
26+
27+
es.addHandler(EventPostPut, handler)
28+
es.fireEvent(EventPostPut, e)
29+
if !ok {
30+
t.Fatal("event was not fired")
31+
}
32+
}
33+
34+
func TestEventMarkedDone(t *testing.T) {
35+
es := newEventSystem()
36+
blkgen := blocksutil.NewBlockGenerator()
37+
b := blkgen.Next()
38+
e := &Event{
39+
cid: b.Cid(),
40+
block: b,
41+
}
42+
43+
count := 0
44+
handler := func(ei *Event) (bool, error) {
45+
if e != ei {
46+
t.Fatal("event is different")
47+
}
48+
count++
49+
return true, nil
50+
}
51+
52+
es.addHandler(EventPostPut, handler)
53+
es.addHandler(EventPostPut, handler)
54+
es.addHandler(EventPostPut, handler)
55+
es.fireEvent(EventPostPut, e)
56+
if count != 1 {
57+
t.Fatalf("expected to fire the handler 1 time, it was fired %d times", count)
58+
}
59+
}
60+
61+
func TestEventFireMultiple(t *testing.T) {
62+
es := newEventSystem()
63+
blkgen := blocksutil.NewBlockGenerator()
64+
N := 10
65+
evs := make([]*Event, N)
66+
for i, _ := range evs {
67+
b := blkgen.Next()
68+
evs[i] = &Event{
69+
cid: b.Cid(),
70+
block: b,
71+
}
72+
}
73+
74+
count := 0
75+
handler := func(ei *Event) (bool, error) {
76+
if evs[count] != ei {
77+
t.Fatal("event is different")
78+
}
79+
count++
80+
return false, nil
81+
}
82+
83+
es.addHandler(EventPostPut, handler)
84+
es.fireMany(EventPostPut, evs)
85+
if count != N {
86+
t.Fatalf("events were not fired, count should be %d, is %d", N, count)
87+
}
88+
}
89+
90+
func TestEventFireMultipleMarkDone(t *testing.T) {
91+
es := newEventSystem()
92+
blkgen := blocksutil.NewBlockGenerator()
93+
N := 10
94+
evs := make([]*Event, N)
95+
for i, _ := range evs {
96+
b := blkgen.Next()
97+
evs[i] = &Event{
98+
cid: b.Cid(),
99+
block: b,
100+
}
101+
}
102+
103+
count := 0
104+
handler := func(ei *Event) (bool, error) {
105+
if evs[count] != ei {
106+
t.Fatal("event is different")
107+
}
108+
count++
109+
return true, nil
110+
}
111+
failHandler := func(ei *Event) (bool, error) {
112+
t.Fatal("this should not fire")
113+
return false, nil
114+
}
115+
116+
es.addHandler(EventPostPut, handler)
117+
es.addHandler(EventPostPut, failHandler)
118+
es.fireMany(EventPostPut, evs)
119+
if count != N {
120+
t.Fatalf("expected to fire the handler 1 time, it was fired %d times", count)
121+
}
122+
}
123+
124+
func BenchmarkEventFireSingle(b *testing.B) {
125+
b.N = 1000000
126+
es := newEventSystem()
127+
blkgen := blocksutil.NewBlockGenerator()
128+
evs := make([]*Event, b.N)
129+
for i, _ := range evs {
130+
b := blkgen.Next()
131+
evs[i] = &Event{
132+
cid: b.Cid(),
133+
block: b,
134+
}
135+
}
136+
count := 0
137+
h := func(ei *Event) (bool, error) {
138+
count++
139+
return false, nil
140+
}
141+
es.addHandler(EventPostPut, h)
142+
143+
b.ReportAllocs()
144+
b.ResetTimer()
145+
146+
for i := 0; i < b.N; i++ {
147+
es.fireEvent(EventPostPut, evs[i])
148+
}
149+
}
150+
151+
func BenchmarkEventFireMultiple(b *testing.B) {
152+
b.N = 1000000
153+
es := newEventSystem()
154+
blkgen := blocksutil.NewBlockGenerator()
155+
evs := make([]*Event, b.N)
156+
for i, _ := range evs {
157+
b := blkgen.Next()
158+
evs[i] = &Event{
159+
cid: b.Cid(),
160+
block: b,
161+
}
162+
}
163+
count := 0
164+
h := func(ei *Event) (bool, error) {
165+
count++
166+
return false, nil
167+
}
168+
es.addHandler(EventPostPut, h)
169+
170+
b.ReportAllocs()
171+
b.ResetTimer()
172+
173+
es.fireMany(EventPostPut, evs)
174+
}

0 commit comments

Comments
 (0)