@cryptotaxi247 / kubo / commits / 317ca2f86

benchmark secure channel

Jeromy committed Oct 26, 2014 at 08:01 UTC 317ca2f86541b117ea3738dcf2dc228c98a20448
6 files changed +196 -26
blockservice/blockservice.go
+1
@@ -37,6 +37,7 @@ func NewBlockService(d ds.Datastore, rem exchange.Interface) (*BlockService, err
37 // AddBlock adds a particular block to the service, Putting it into the datastore.
38 func (s *BlockService) AddBlock(b *blocks.Block) (u.Key, error) {
39 k := b.Key()
40 + log.Debugf("blockservice: storing [%s] in datastore", k)
41 // TODO(brian): define a block datastore with a Put method which accepts a
42 // block parameter
43
crypto/spipe/handshake.go
+11 -6
@@ -275,6 +275,7 @@ func (s *SecurePipe) handleSecureOut(hashType string, mIV, mCKey, mMKey []byte)
275
276 myMac, macSize := makeMac(hashType, mMKey)
277
278 + basebuf := make([]byte, 1<<22)
279 for {
280 var data []byte
281 ok := true
@@ -294,19 +295,23 @@ func (s *SecurePipe) handleSecureOut(hashType string, mIV, mCKey, mMKey []byte)
295 continue
296 }
297
297 - buff := make([]byte, len(data)+macSize)
298 + buff := basebuf[:len(data)+macSize]
299
299 - myCipher.XORKeyStream(buff, data)
300 -
301 - myMac.Write(buff[0:len(data)])
302 - copy(buff[len(data):], myMac.Sum(nil))
303 - myMac.Reset()
300 + encData(data, buff, myCipher, myMac)
301
302 // log.Debug("[peer %s] secure out [to = %s] %d", s.local, s.remote, len(buff))
303 s.insecure.Out <- buff
304 }
305 }
306
307 +func encData(data, buff []byte, ciph cipher.Stream, mac hash.Hash) {
308 + ciph.XORKeyStream(buff, data)
309 +
310 + mac.Write(buff[0:len(data)])
311 + copy(buff[len(data):], mac.Sum(nil))
312 + mac.Reset()
313 +}
314 +
315 // Determines which algorithm to use. Note: f(a, b) = f(b, a)
316 func selectBest(myPrefs, theirPrefs string) (string, error) {
317 // Person with greatest hash gets first choice.
crypto/spipe/spipe_test.go new
+161
@@ -0,0 +1,161 @@
1 +package spipe
2 +
3 +import (
4 + "testing"
5 +
6 + "code.google.com/p/go.net/context"
7 +
8 + ci "github.com/jbenet/go-ipfs/crypto"
9 + "github.com/jbenet/go-ipfs/peer"
10 + "github.com/jbenet/go-ipfs/util"
11 +)
12 +
13 +func getPeer(tb testing.TB) peer.Peer {
14 + privk, pubk, err := ci.GenerateKeyPair(ci.RSA, 1024)
15 + if err != nil {
16 + tb.Fatal(err)
17 + }
18 +
19 + p, err := peer.WithKeyPair(privk, pubk)
20 + if err != nil {
21 + tb.Fatal(err)
22 + }
23 +
24 + return p
25 +}
26 +
27 +func bindDuplexNoCopy(a, b Duplex) {
28 + go func() {
29 + for m := range b.Out {
30 + a.In <- m
31 + }
32 + }()
33 + for m := range a.Out {
34 + b.In <- m
35 + }
36 +}
37 +
38 +func bindDuplexWithCopy(a, b Duplex) {
39 + dup := func(byt []byte) []byte {
40 + n := make([]byte, len(byt))
41 + copy(n, byt)
42 + return n
43 + }
44 + go func() {
45 + for m := range b.Out {
46 + a.In <- dup(m)
47 + }
48 + }()
49 + for m := range a.Out {
50 + b.In <- dup(m)
51 + }
52 +}
53 +
54 +func BenchmarkDataEncryptDefault(b *testing.B) {
55 + SupportedExchanges = "P-256,P-224,P-384,P-521"
56 + SupportedCiphers = "AES-256,AES-128"
57 + SupportedHashes = "SHA256,SHA512,SHA1"
58 +
59 + runEncryptBenchmark(b)
60 +}
61 +
62 +func BenchmarkDataEncryptLite(b *testing.B) {
63 + SupportedExchanges = "P-256"
64 + SupportedCiphers = "AES-128"
65 + SupportedHashes = "SHA1"
66 +
67 + runEncryptBenchmark(b)
68 +}
69 +
70 +func runEncryptBenchmark(b *testing.B) {
71 + pstore := peer.NewPeerstore()
72 + ctx := context.TODO()
73 + bufsize := 1024 * 1024
74 +
75 + pa := getPeer(b)
76 + pb := getPeer(b)
77 + duplexa := Duplex{
78 + In: make(chan []byte),
79 + Out: make(chan []byte),
80 + }
81 + duplexb := Duplex{
82 + In: make(chan []byte),
83 + Out: make(chan []byte),
84 + }
85 +
86 + go bindDuplexNoCopy(duplexa, duplexb)
87 +
88 + var spb *SecurePipe
89 + done := make(chan struct{})
90 + go func() {
91 + var err error
92 + spb, err = NewSecurePipe(ctx, bufsize, pb, pstore, duplexb)
93 + if err != nil {
94 + b.Fatal(err)
95 + }
96 + done <- struct{}{}
97 + }()
98 +
99 + spa, err := NewSecurePipe(ctx, bufsize, pa, pstore, duplexa)
100 + if err != nil {
101 + b.Fatal(err)
102 + }
103 +
104 + <-done
105 +
106 + go func() {
107 + for _ = range spa.In {
108 + // Throw it all away,
109 + // all of your hopes and dreams
110 + // piped out to /dev/null...
111 + done <- struct{}{}
112 + }
113 + }()
114 +
115 + data := make([]byte, 1024*512)
116 + util.NewFastRand().Read(data)
117 + // Begin actual benchmarking
118 + b.ResetTimer()
119 +
120 + for i := 0; i < b.N; i++ {
121 + b.SetBytes(int64(len(data)))
122 + spb.Out <- data
123 + <-done
124 + }
125 +
126 +}
127 +
128 +func BenchmarkDataTransfer(b *testing.B) {
129 + duplexa := Duplex{
130 + In: make(chan []byte),
131 + Out: make(chan []byte),
132 + }
133 + duplexb := Duplex{
134 + In: make(chan []byte),
135 + Out: make(chan []byte),
136 + }
137 +
138 + go bindDuplexWithCopy(duplexa, duplexb)
139 +
140 + done := make(chan struct{})
141 + go func() {
142 + for _ = range duplexa.In {
143 + // Throw it all away,
144 + // all of your hopes and dreams
145 + // piped out to /dev/null...
146 + done <- struct{}{}
147 + }
148 + }()
149 +
150 + data := make([]byte, 1024*512)
151 + util.NewFastRand().Read(data)
152 + // Begin actual benchmarking
153 + b.ResetTimer()
154 +
155 + for i := 0; i < b.N; i++ {
156 + b.SetBytes(int64(len(data)))
157 + duplexb.Out <- data
158 + <-done
159 + }
160 +
161 +}
exchange/bitswap/bitswap.go
+7 -1
@@ -1,6 +1,8 @@
1 package bitswap
2
3 import (
4 + "time"
5 +
6 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
7 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
8
@@ -67,6 +69,10 @@ type bitswap struct {
69 // TODO ensure only one active request per key
70 func (bs *bitswap) Block(parent context.Context, k u.Key) (*blocks.Block, error) {
71 log.Debugf("Get Block %v", k)
72 + now := time.Now()
73 + defer func() {
74 + log.Errorf("GetBlock took %f secs", time.Now().Sub(now).Seconds())
75 + }()
76
77 ctx, cancelFunc := context.WithCancel(parent)
78 bs.wantlist.Add(k)
@@ -160,7 +166,7 @@ func (bs *bitswap) ReceiveMessage(ctx context.Context, p peer.Peer, incoming bsm
166 go func(block blocks.Block) {
167 err := bs.HasBlock(ctx, block) // FIXME err ignored
168 if err != nil {
163 - log.Errorf("HasBlock errored: %s", err)
169 + log.Warningf("HasBlock errored: %s", err)
170 }
171 }(block)
172 }
importer/importer.go
+1
@@ -36,6 +36,7 @@ func NewDagFromReaderWithSplitter(r io.Reader, spl chunk.BlockSplitter) (*dag.No
36
37 mbf := new(ft.MultiBlock)
38 for blk := range blkChan {
39 + log.Debugf("created block, size %d", len(blk))
40 mbf.AddBlockSize(uint64(len(blk)))
41 child := &dag.Node{Data: ft.WrapData(blk)}
42 err := root.AddNodeLink("", child)
routing/kbucket/table_test.go
+15 -19
@@ -7,16 +7,12 @@ import (
7 "testing"
8 "time"
9
10 + tu "github.com/jbenet/go-ipfs/util/testutil"
11 +
12 peer "github.com/jbenet/go-ipfs/peer"
13 )
14
13 -func _randPeer() peer.Peer {
14 - id := make(peer.ID, 16)
15 - crand.Read(id)
16 - return peer.WithID(id)
17 -}
18 -
19 -func _randID() ID {
15 +func RandID() ID {
16 buf := make([]byte, 16)
17 crand.Read(buf)
18
@@ -30,11 +26,11 @@ func TestBucket(t *testing.T) {
26
27 peers := make([]peer.Peer, 100)
28 for i := 0; i < 100; i++ {
33 - peers[i] = _randPeer()
29 + peers[i] = tu.RandPeer()
30 b.pushFront(peers[i])
31 }
32
37 - local := _randPeer()
33 + local := tu.RandPeer()
34 localID := ConvertPeerID(local.ID())
35
36 i := rand.Intn(len(peers))
@@ -65,12 +61,12 @@ func TestBucket(t *testing.T) {
61
62 // Right now, this just makes sure that it doesnt hang or crash
63 func TestTableUpdate(t *testing.T) {
68 - local := _randPeer()
64 + local := tu.RandPeer()
65 rt := NewRoutingTable(10, ConvertPeerID(local.ID()), time.Hour)
66
67 peers := make([]peer.Peer, 100)
68 for i := 0; i < 100; i++ {
73 - peers[i] = _randPeer()
69 + peers[i] = tu.RandPeer()
70 }
71
72 // Testing Update
@@ -82,7 +78,7 @@ func TestTableUpdate(t *testing.T) {
78 }
79
80 for i := 0; i < 100; i++ {
85 - id := _randID()
81 + id := RandID()
82 ret := rt.NearestPeers(id, 5)
83 if len(ret) == 0 {
84 t.Fatal("Failed to find node near ID.")
@@ -91,12 +87,12 @@ func TestTableUpdate(t *testing.T) {
87 }
88
89 func TestTableFind(t *testing.T) {
94 - local := _randPeer()
90 + local := tu.RandPeer()
91 rt := NewRoutingTable(10, ConvertPeerID(local.ID()), time.Hour)
92
93 peers := make([]peer.Peer, 100)
94 for i := 0; i < 5; i++ {
99 - peers[i] = _randPeer()
95 + peers[i] = tu.RandPeer()
96 rt.Update(peers[i])
97 }
98
@@ -108,12 +104,12 @@ func TestTableFind(t *testing.T) {
104 }
105
106 func TestTableFindMultiple(t *testing.T) {
111 - local := _randPeer()
107 + local := tu.RandPeer()
108 rt := NewRoutingTable(20, ConvertPeerID(local.ID()), time.Hour)
109
110 peers := make([]peer.Peer, 100)
111 for i := 0; i < 18; i++ {
116 - peers[i] = _randPeer()
112 + peers[i] = tu.RandPeer()
113 rt.Update(peers[i])
114 }
115
@@ -132,7 +128,7 @@ func TestTableMultithreaded(t *testing.T) {
128 tab := NewRoutingTable(20, ConvertPeerID(local), time.Hour)
129 var peers []peer.Peer
130 for i := 0; i < 500; i++ {
135 - peers = append(peers, _randPeer())
131 + peers = append(peers, tu.RandPeer())
132 }
133
134 done := make(chan struct{})
@@ -171,7 +167,7 @@ func BenchmarkUpdates(b *testing.B) {
167
168 var peers []peer.Peer
169 for i := 0; i < b.N; i++ {
174 - peers = append(peers, _randPeer())
170 + peers = append(peers, tu.RandPeer())
171 }
172
173 b.StartTimer()
@@ -187,7 +183,7 @@ func BenchmarkFinds(b *testing.B) {
183
184 var peers []peer.Peer
185 for i := 0; i < b.N; i++ {
190 - peers = append(peers, _randPeer())
186 + peers = append(peers, tu.RandPeer())
187 tab.Update(peers[i])
188 }
189