forked from ipfs/go-ipld-cbor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap_test.go
122 lines (112 loc) · 1.99 KB
/
wrap_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cbornode
import (
"sync"
"testing"
mh "github.com/multiformats/go-multihash"
)
type MyStruct struct {
Items map[string]MyStruct
Foo string
Bar []byte
Baz []int
}
func init() {
RegisterCborType(MyStruct{})
}
func testStruct() MyStruct {
return MyStruct{
Items: map[string]MyStruct{
"Foo": {
Foo: "Foo",
Bar: []byte("Bar"),
Baz: []int{1, 2, 3, 4},
},
"Bar": {
Bar: []byte("Bar"),
Baz: []int{1, 2, 3, 4},
},
},
Baz: []int{5, 1, 2},
}
}
func BenchmarkWrapObject(b *testing.B) {
obj := testStruct()
b.ResetTimer()
for i := 0; i < b.N; i++ {
nd, err := WrapObject(obj, mh.SHA2_256, -1)
if err != nil {
b.Fatal(err, nd)
}
}
}
func BenchmarkDecodeBlock(b *testing.B) {
obj := testStruct()
nd, err := WrapObject(obj, mh.SHA2_256, -1)
if err != nil {
b.Fatal(err, nd)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
nd2, err := DecodeBlock(nd)
if err != nil {
b.Fatal(err, nd2)
}
}
}
func BenchmarkWrapObjectParallel(b *testing.B) {
obj := testStruct()
b.ResetTimer()
var wg sync.WaitGroup
wg.Add(100)
errors := make(chan error, 100)
for j := 0; j < 100; j++ {
go func() {
defer wg.Done()
for i := 0; i < b.N; i++ {
if _, err := WrapObject(obj, mh.SHA2_256, -1); err != nil {
errors <- err
}
}
}()
}
wg.Wait()
close(errors)
for e := range errors {
b.Fatal(e)
}
}
func BenchmarkDecodeBlockParallel(b *testing.B) {
obj := testStruct()
nd, err := WrapObject(obj, mh.SHA2_256, -1)
if err != nil {
b.Fatal(err, nd)
}
b.ResetTimer()
var wg sync.WaitGroup
wg.Add(100)
errs := make(chan error, 100)
for j := 0; j < 100; j++ {
go func() {
defer wg.Done()
for i := 0; i < b.N; i++ {
if _, err := DecodeBlock(nd); err != nil {
errs <- err
}
}
}()
}
wg.Wait()
close(errs)
for e := range errs {
b.Fatal(e)
}
}
func BenchmarkDumpObject(b *testing.B) {
obj := testStruct()
for i := 0; i < b.N; i++ {
bytes, err := DumpObject(obj)
if err != nil {
b.Fatal(err, bytes)
}
}
}