Add more info to bitswap stat
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jan 26, 2017 at 16:25 UTC
332d760f12b1346183209fe5ced5a4c475cb6c3f
7 files changed
+162
core/commands/bitswap.go
+3
@@ -159,6 +159,9 @@ var bitswapStatCmd = &cmds.Command{
159
fmt.Fprintln(buf, "bitswap status")
160
fmt.Fprintf(buf, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize)
161
fmt.Fprintf(buf, "\tblocks received: %d\n", out.BlocksReceived)
162
+ fmt.Fprintf(buf, "\tblocks sent: %d\n", out.BlocksSent)
163
+ fmt.Fprintf(buf, "\tdata received: %d\n", out.DataReceived)
164
+ fmt.Fprintf(buf, "\tdata sent: %d\n", out.DataSent)
165
fmt.Fprintf(buf, "\tdup blocks received: %d\n", out.DupBlksReceived)
166
fmt.Fprintf(buf, "\tdup data received: %s\n", humanize.Bytes(out.DupDataReceived))
167
fmt.Fprintf(buf, "\twantlist [%d keys]\n", len(out.Wantlist))
exchange/bitswap/bitswap.go
+4
@@ -157,6 +157,9 @@ type Bitswap struct {
157
blocksRecvd int
158
dupBlocksRecvd int
159
dupDataRecvd uint64
160
+ blocksSent int
161
+ dataSent uint64
162
+ dataRecvd uint64
163
164
// Metrics interface metrics
165
dupMetric metrics.Histogram
@@ -401,6 +404,7 @@ func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
404
defer bs.counterLk.Unlock()
405
406
bs.blocksRecvd++
407
+ bs.dataRecvd += uint64(len(b.RawData()))
408
if has {
409
bs.dupBlocksRecvd++
410
bs.dupDataRecvd += uint64(blkLen)
exchange/bitswap/bitswap_test.go
+38
@@ -3,6 +3,7 @@ package bitswap
3
import (
4
"bytes"
5
"context"
6
+ "fmt"
7
"sync"
8
"testing"
9
"time"
@@ -299,6 +300,25 @@ func TestEmptyKey(t *testing.T) {
300
}
301
}
302
303
+func assertStat(st *Stat, sblks, rblks int, sdata, rdata uint64) error {
304
+ if sblks != st.BlocksSent {
305
+ return fmt.Errorf("mismatch in blocks sent: %d vs %d", sblks, st.BlocksSent)
306
+ }
307
+
308
+ if rblks != st.BlocksReceived {
309
+ return fmt.Errorf("mismatch in blocks recvd: %d vs %d", rblks, st.BlocksReceived)
310
+ }
311
+
312
+ if sdata != st.DataSent {
313
+ return fmt.Errorf("mismatch in data sent: %d vs %d", sdata, st.DataSent)
314
+ }
315
+
316
+ if rdata != st.DataReceived {
317
+ return fmt.Errorf("mismatch in data recvd: %d vs %d", rdata, st.DataReceived)
318
+ }
319
+ return nil
320
+}
321
+
322
func TestBasicBitswap(t *testing.T) {
323
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
324
sg := NewTestSessionGenerator(net)
@@ -321,6 +341,24 @@ func TestBasicBitswap(t *testing.T) {
341
t.Fatal(err)
342
}
343
344
+ st0, err := instances[0].Exchange.Stat()
345
+ if err != nil {
346
+ t.Fatal(err)
347
+ }
348
+
349
+ st1, err := instances[1].Exchange.Stat()
350
+ if err != nil {
351
+ t.Fatal(err)
352
+ }
353
+
354
+ if err := assertStat(st0, 1, 0, 1, 0); err != nil {
355
+ t.Fatal(err)
356
+ }
357
+
358
+ if err := assertStat(st1, 0, 1, 0, 1); err != nil {
359
+ t.Fatal(err)
360
+ }
361
+
362
t.Log(blk)
363
for _, inst := range instances {
364
err := inst.Exchange.Close()
exchange/bitswap/stat.go
+6
@@ -11,6 +11,9 @@ type Stat struct {
11
Wantlist []*cid.Cid
12
Peers []string
13
BlocksReceived int
14
+ DataReceived uint64
15
+ BlocksSent int
16
+ DataSent uint64
17
DupBlksReceived int
18
DupDataReceived uint64
19
}
@@ -23,6 +26,9 @@ func (bs *Bitswap) Stat() (*Stat, error) {
26
st.BlocksReceived = bs.blocksRecvd
27
st.DupBlksReceived = bs.dupBlocksRecvd
28
st.DupDataReceived = bs.dupDataRecvd
29
+ st.BlocksSent = bs.blocksSent
30
+ st.DataSent = bs.dataSent
31
+ st.DataReceived = bs.dataRecvd
32
bs.counterLk.Unlock()
33
34
for _, p := range bs.engine.Peers() {
exchange/bitswap/workers.go
+4
@@ -64,6 +64,10 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) {
64
})
65
66
bs.wm.SendBlock(ctx, envelope)
67
+ bs.counterLk.Lock()
68
+ bs.blocksSent++
69
+ bs.dataSent += uint64(len(envelope.Block.RawData()))
70
+ bs.counterLk.Unlock()
71
case <-ctx.Done():
72
return
73
}
test/sharness/t0125-twonode.sh
new
+101
@@ -0,0 +1,101 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2017 Jeromy Johnson
4
+# MIT Licensed; see the LICENSE file in this repository.
5
+#
6
+
7
+test_description="Test two ipfs nodes transferring a file"
8
+
9
+. lib/test-lib.sh
10
+
11
+check_file_fetch() {
12
+ node=$1
13
+ fhash=$2
14
+ fname=$3
15
+
16
+ test_expect_success "can fetch file" '
17
+ ipfsi $node cat $fhash > fetch_out
18
+ '
19
+
20
+ test_expect_success "file looks good" '
21
+ test_cmp $fname fetch_out
22
+ '
23
+}
24
+
25
+check_dir_fetch() {
26
+ node=$1
27
+ ref=$2
28
+
29
+ test_expect_success "node can fetch all refs for dir" '
30
+ ipfsi $node refs -r $ref > /dev/null
31
+ '
32
+}
33
+
34
+run_single_file_test() {
35
+ test_expect_success "add a file on node1" '
36
+ random 1000000 > filea &&
37
+ FILEA_HASH=$(ipfsi 1 add -q filea)
38
+ '
39
+
40
+ check_file_fetch 0 $FILEA_HASH filea
41
+}
42
+
43
+run_random_dir_test() {
44
+ test_expect_success "create a bunch of random files" '
45
+ random-files -depth=3 -dirs=4 -files=5 -seed=5 foobar > /dev/null
46
+ '
47
+
48
+ test_expect_success "add those on node 0" '
49
+ DIR_HASH=$(ipfsi 0 add -r -q foobar | tail -n1)
50
+ '
51
+
52
+ check_dir_fetch 1 $DIR_HASH
53
+}
54
+
55
+run_advanced_test() {
56
+ startup_cluster 2 "$@"
57
+
58
+ test_expect_success "clean repo before test" '
59
+ ipfsi 0 repo gc > /dev/null &&
60
+ ipfsi 1 repo gc > /dev/null
61
+ '
62
+
63
+ run_single_file_test
64
+
65
+ run_random_dir_test
66
+
67
+ test_expect_success "node0 data transferred looks correct" '
68
+ ipfsi 0 bitswap stat > stat0 &&
69
+ grep "blocks sent: 126" stat0 > /dev/null &&
70
+ grep "blocks received: 5" stat0 > /dev/null &&
71
+ grep "data sent: 228113" stat0 > /dev/null &&
72
+ grep "data received: 1000256" stat0 > /dev/null
73
+ '
74
+
75
+ test_expect_success "node1 data transferred looks correct" '
76
+ ipfsi 1 bitswap stat > stat1 &&
77
+ grep "blocks received: 126" stat1 > /dev/null &&
78
+ grep "blocks sent: 5" stat1 > /dev/null &&
79
+ grep "data received: 228113" stat1 > /dev/null &&
80
+ grep "data sent: 1000256" stat1 > /dev/null
81
+ '
82
+
83
+ test_expect_success "shut down nodes" '
84
+ iptb stop
85
+ '
86
+}
87
+
88
+test_expect_success "set up tcp testbed" '
89
+ iptb init -n 2 -p 0 -f --bootstrap=none
90
+'
91
+
92
+# test multiplex muxer
93
+export LIBP2P_MUX_PREFS="/mplex/6.7.0"
94
+run_advanced_test "--enable-mplex-experiment"
95
+unset LIBP2P_MUX_PREFS
96
+
97
+# test default configuration
98
+run_advanced_test
99
+
100
+
101
+test_done
test/sharness/t0220-bitswap.sh
+6
@@ -20,6 +20,9 @@ test_expect_success "'ipfs bitswap stat' output looks good" '
20
bitswap status
21
provides buffer: 0 / 256
22
blocks received: 0
23
+ blocks sent: 0
24
+ data received: 0
25
+ data sent: 0
26
dup blocks received: 0
27
dup data received: 0 B
28
wantlist [0 keys]
@@ -55,6 +58,9 @@ test_expect_success "'ipfs bitswap stat' output looks good" '
58
bitswap status
59
provides buffer: 0 / 256
60
blocks received: 0
61
+ blocks sent: 0
62
+ data received: 0
63
+ data sent: 0
64
dup blocks received: 0
65
dup data received: 0 B
66
wantlist [0 keys]