-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhost_bench_test.go
121 lines (89 loc) · 2.71 KB
/
host_bench_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
package crabfs
import (
"context"
"testing"
"time"
blocks "github.com/ipfs/go-block-format"
ipfsDatastore "github.com/ipfs/go-datastore"
ipfsBlockstore "github.com/ipfs/go-ipfs-blockstore"
"github.com/runletapp/crabfs/identity"
"github.com/runletapp/crabfs/interfaces"
"github.com/runletapp/crabfs/options"
pb "github.com/runletapp/crabfs/protos"
"github.com/stretchr/testify/assert"
)
func setUpHostBenchWithRelay(t *testing.B) (interfaces.Host, ipfsBlockstore.Blockstore) {
assert := assert.New(t)
id, err := identity.CreateIdentity()
assert.Nil(err)
settings := &options.Settings{
Context: context.Background(),
Identity: id,
}
ds := ipfsDatastore.NewMapDatastore()
bs := ipfsBlockstore.NewBlockstore(ds)
relay, err := RelayNew(settings.Context, 0, []string{}, nil)
assert.Nil(err)
settings.BootstrapPeers = relay.GetAddrs()
host, err := HostNew(settings, ds, bs)
assert.Nil(err)
t.ResetTimer()
return host, bs
}
func setUpHostBench(t *testing.B) (interfaces.Host, ipfsBlockstore.Blockstore) {
assert := assert.New(t)
id, err := identity.CreateIdentity()
assert.Nil(err)
settings := &options.Settings{
Context: context.Background(),
Identity: id,
}
ds := ipfsDatastore.NewMapDatastore()
bs := ipfsBlockstore.NewBlockstore(ds)
host, err := HostNew(settings, ds, bs)
assert.Nil(err)
t.ResetTimer()
return host, bs
}
func BenchmarkHostPublishPeers(t *testing.B) {
assert := assert.New(t)
host, bs := setUpHostBenchWithRelay(t)
assert.Nil(host.Announce())
privKey, err := GenerateKeyPair()
assert.Nil(err)
assert.Nil(host.PutPublicKey(privKey.GetPublic()))
t.ResetTimer()
for i := 0; i < t.N; i++ {
block := blocks.NewBlock([]byte("abc"))
bs.Put(block)
blockMap := interfaces.BlockMap{}
blockMap[0] = &pb.BlockMetadata{
Start: 0,
Size: int64(len(block.RawData())),
Cid: block.Cid().Bytes(),
}
cipherKey := []byte("0123456789abcdef")
assert.Nil(host.Publish(context.Background(), privKey, cipherKey, "test", "test.txt", blockMap, time.Now(), int64(len(block.RawData()))))
}
}
func BenchmarkHostPublishNoPeers(t *testing.B) {
assert := assert.New(t)
host, bs := setUpHostBench(t)
assert.Nil(host.Announce())
privKey, err := GenerateKeyPair()
assert.Nil(err)
assert.Nil(host.PutPublicKey(privKey.GetPublic()))
t.ResetTimer()
for i := 0; i < t.N; i++ {
block := blocks.NewBlock([]byte("abc"))
bs.Put(block)
blockMap := interfaces.BlockMap{}
blockMap[0] = &pb.BlockMetadata{
Start: 0,
Size: int64(len(block.RawData())),
Cid: block.Cid().Bytes(),
}
cipherKey := []byte("0123456789abcdef")
assert.Nil(host.Publish(context.Background(), privKey, cipherKey, "test", "test.txt", blockMap, time.Now(), int64(len(block.RawData()))))
}
}