Skip to content

Commit b582766

Browse files
committed
Add more info to bitswap stat
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent 416f025 commit b582766

File tree

7 files changed

+166
-3
lines changed

7 files changed

+166
-3
lines changed

core/commands/bitswap.go

+3
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ var bitswapStatCmd = &cmds.Command{
159159
fmt.Fprintln(buf, "bitswap status")
160160
fmt.Fprintf(buf, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize)
161161
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)
162165
fmt.Fprintf(buf, "\tdup blocks received: %d\n", out.DupBlksReceived)
163166
fmt.Fprintf(buf, "\tdup data received: %s\n", humanize.Bytes(out.DupDataReceived))
164167
fmt.Fprintf(buf, "\twantlist [%d keys]\n", len(out.Wantlist))

exchange/bitswap/bitswap.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ type Bitswap struct {
145145
blocksRecvd int
146146
dupBlocksRecvd int
147147
dupDataRecvd uint64
148+
blocksSent int
149+
dataSent uint64
150+
dataRecvd uint64
148151
}
149152

150153
type blockRequest struct {
@@ -371,18 +374,20 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
371374
var ErrAlreadyHaveBlock = errors.New("already have block")
372375

373376
func (bs *Bitswap) updateReceiveCounters(b blocks.Block) error {
374-
bs.counterLk.Lock()
375-
defer bs.counterLk.Unlock()
376-
bs.blocksRecvd++
377377
has, err := bs.blockstore.Has(b.Cid())
378378
if err != nil {
379379
log.Infof("blockstore.Has error: %s", err)
380380
return err
381381
}
382+
383+
bs.counterLk.Lock()
384+
bs.blocksRecvd++
385+
bs.dataRecvd += uint64(len(b.RawData()))
382386
if err == nil && has {
383387
bs.dupBlocksRecvd++
384388
bs.dupDataRecvd += uint64(len(b.RawData()))
385389
}
390+
bs.counterLk.Unlock()
386391

387392
if has {
388393
return ErrAlreadyHaveBlock

exchange/bitswap/bitswap_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package bitswap
33
import (
44
"bytes"
55
"context"
6+
"fmt"
67
"sync"
78
"testing"
89
"time"
@@ -299,6 +300,25 @@ func TestEmptyKey(t *testing.T) {
299300
}
300301
}
301302

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+
302322
func TestBasicBitswap(t *testing.T) {
303323
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
304324
sg := NewTestSessionGenerator(net)
@@ -321,6 +341,24 @@ func TestBasicBitswap(t *testing.T) {
321341
t.Fatal(err)
322342
}
323343

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+
324362
t.Log(blk)
325363
for _, inst := range instances {
326364
err := inst.Exchange.Close()

exchange/bitswap/stat.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ type Stat struct {
1111
Wantlist []*cid.Cid
1212
Peers []string
1313
BlocksReceived int
14+
DataReceived uint64
15+
BlocksSent int
16+
DataSent uint64
1417
DupBlksReceived int
1518
DupDataReceived uint64
1619
}
@@ -23,6 +26,9 @@ func (bs *Bitswap) Stat() (*Stat, error) {
2326
st.BlocksReceived = bs.blocksRecvd
2427
st.DupBlksReceived = bs.dupBlocksRecvd
2528
st.DupDataReceived = bs.dupDataRecvd
29+
st.BlocksSent = bs.blocksSent
30+
st.DataSent = bs.dataSent
31+
st.DataReceived = bs.dataRecvd
2632
bs.counterLk.Unlock()
2733

2834
for _, p := range bs.engine.Peers() {

exchange/bitswap/workers.go

+4
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) {
6464
})
6565

6666
bs.wm.SendBlock(ctx, envelope)
67+
bs.counterLk.Lock()
68+
bs.blocksSent++
69+
bs.dataSent += uint64(len(envelope.Block.RawData()))
70+
bs.counterLk.Unlock()
6771
case <-ctx.Done():
6872
return
6973
}

test/sharness/t0125-twonode.sh

+101
Original file line numberDiff line numberDiff line change
@@ -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
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ test_expect_success "'ipfs bitswap stat' output looks good" '
2020
bitswap status
2121
provides buffer: 0 / 256
2222
blocks received: 0
23+
blocks sent: 0
24+
data received: 0
25+
data sent: 0
2326
dup blocks received: 0
2427
dup data received: 0 B
2528
wantlist [0 keys]
@@ -55,6 +58,9 @@ test_expect_success "'ipfs bitswap stat' output looks good" '
5558
bitswap status
5659
provides buffer: 0 / 256
5760
blocks received: 0
61+
blocks sent: 0
62+
data received: 0
63+
data sent: 0
5864
dup blocks received: 0
5965
dup data received: 0 B
6066
wantlist [0 keys]

0 commit comments

Comments
 (0)