Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more info to bitswap stat #3635

Merged
merged 1 commit into from
Mar 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ var bitswapStatCmd = &cmds.Command{
fmt.Fprintln(buf, "bitswap status")
fmt.Fprintf(buf, "\tprovides buffer: %d / %d\n", out.ProvideBufLen, bitswap.HasBlockBufferSize)
fmt.Fprintf(buf, "\tblocks received: %d\n", out.BlocksReceived)
fmt.Fprintf(buf, "\tblocks sent: %d\n", out.BlocksSent)
fmt.Fprintf(buf, "\tdata received: %d\n", out.DataReceived)
fmt.Fprintf(buf, "\tdata sent: %d\n", out.DataSent)
fmt.Fprintf(buf, "\tdup blocks received: %d\n", out.DupBlksReceived)
fmt.Fprintf(buf, "\tdup data received: %s\n", humanize.Bytes(out.DupDataReceived))
fmt.Fprintf(buf, "\twantlist [%d keys]\n", len(out.Wantlist))
Expand Down
4 changes: 4 additions & 0 deletions exchange/bitswap/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ type Bitswap struct {
blocksRecvd int
dupBlocksRecvd int
dupDataRecvd uint64
blocksSent int
dataSent uint64
dataRecvd uint64

// Metrics interface metrics
dupMetric metrics.Histogram
Expand Down Expand Up @@ -401,6 +404,7 @@ func (bs *Bitswap) updateReceiveCounters(b blocks.Block) {
defer bs.counterLk.Unlock()

bs.blocksRecvd++
bs.dataRecvd += uint64(len(b.RawData()))
if has {
bs.dupBlocksRecvd++
bs.dupDataRecvd += uint64(blkLen)
Expand Down
38 changes: 38 additions & 0 deletions exchange/bitswap/bitswap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bitswap
import (
"bytes"
"context"
"fmt"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -299,6 +300,25 @@ func TestEmptyKey(t *testing.T) {
}
}

func assertStat(st *Stat, sblks, rblks int, sdata, rdata uint64) error {
if sblks != st.BlocksSent {
return fmt.Errorf("mismatch in blocks sent: %d vs %d", sblks, st.BlocksSent)
}

if rblks != st.BlocksReceived {
return fmt.Errorf("mismatch in blocks recvd: %d vs %d", rblks, st.BlocksReceived)
}

if sdata != st.DataSent {
return fmt.Errorf("mismatch in data sent: %d vs %d", sdata, st.DataSent)
}

if rdata != st.DataReceived {
return fmt.Errorf("mismatch in data recvd: %d vs %d", rdata, st.DataReceived)
}
return nil
}

func TestBasicBitswap(t *testing.T) {
net := tn.VirtualNetwork(mockrouting.NewServer(), delay.Fixed(kNetworkDelay))
sg := NewTestSessionGenerator(net)
Expand All @@ -321,6 +341,24 @@ func TestBasicBitswap(t *testing.T) {
t.Fatal(err)
}

st0, err := instances[0].Exchange.Stat()
if err != nil {
t.Fatal(err)
}

st1, err := instances[1].Exchange.Stat()
if err != nil {
t.Fatal(err)
}

if err := assertStat(st0, 1, 0, 1, 0); err != nil {
t.Fatal(err)
}

if err := assertStat(st1, 0, 1, 0, 1); err != nil {
t.Fatal(err)
}

t.Log(blk)
for _, inst := range instances {
err := inst.Exchange.Close()
Expand Down
6 changes: 6 additions & 0 deletions exchange/bitswap/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type Stat struct {
Wantlist []*cid.Cid
Peers []string
BlocksReceived int
DataReceived uint64
BlocksSent int
DataSent uint64
DupBlksReceived int
DupDataReceived uint64
}
Expand All @@ -23,6 +26,9 @@ func (bs *Bitswap) Stat() (*Stat, error) {
st.BlocksReceived = bs.blocksRecvd
st.DupBlksReceived = bs.dupBlocksRecvd
st.DupDataReceived = bs.dupDataRecvd
st.BlocksSent = bs.blocksSent
st.DataSent = bs.dataSent
st.DataReceived = bs.dataRecvd
bs.counterLk.Unlock()

for _, p := range bs.engine.Peers() {
Expand Down
4 changes: 4 additions & 0 deletions exchange/bitswap/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) {
})

bs.wm.SendBlock(ctx, envelope)
bs.counterLk.Lock()
bs.blocksSent++
bs.dataSent += uint64(len(envelope.Block.RawData()))
bs.counterLk.Unlock()
case <-ctx.Done():
return
}
Expand Down
101 changes: 101 additions & 0 deletions test/sharness/t0125-twonode.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#!/bin/sh
#
# Copyright (c) 2017 Jeromy Johnson
# MIT Licensed; see the LICENSE file in this repository.
#

test_description="Test two ipfs nodes transferring a file"

. lib/test-lib.sh

check_file_fetch() {
node=$1
fhash=$2
fname=$3

test_expect_success "can fetch file" '
ipfsi $node cat $fhash > fetch_out
'

test_expect_success "file looks good" '
test_cmp $fname fetch_out
'
}

check_dir_fetch() {
node=$1
ref=$2

test_expect_success "node can fetch all refs for dir" '
ipfsi $node refs -r $ref > /dev/null
'
}

run_single_file_test() {
test_expect_success "add a file on node1" '
random 1000000 > filea &&
FILEA_HASH=$(ipfsi 1 add -q filea)
'

check_file_fetch 0 $FILEA_HASH filea
}

run_random_dir_test() {
test_expect_success "create a bunch of random files" '
random-files -depth=3 -dirs=4 -files=5 -seed=5 foobar > /dev/null
'

test_expect_success "add those on node 0" '
DIR_HASH=$(ipfsi 0 add -r -q foobar | tail -n1)
'

check_dir_fetch 1 $DIR_HASH
}

run_advanced_test() {
startup_cluster 2 "$@"

test_expect_success "clean repo before test" '
ipfsi 0 repo gc > /dev/null &&
ipfsi 1 repo gc > /dev/null
'

run_single_file_test

run_random_dir_test

test_expect_success "node0 data transferred looks correct" '
ipfsi 0 bitswap stat > stat0 &&
grep "blocks sent: 126" stat0 > /dev/null &&
grep "blocks received: 5" stat0 > /dev/null &&
grep "data sent: 228113" stat0 > /dev/null &&
grep "data received: 1000256" stat0 > /dev/null
'

test_expect_success "node1 data transferred looks correct" '
ipfsi 1 bitswap stat > stat1 &&
grep "blocks received: 126" stat1 > /dev/null &&
grep "blocks sent: 5" stat1 > /dev/null &&
grep "data received: 228113" stat1 > /dev/null &&
grep "data sent: 1000256" stat1 > /dev/null
'

test_expect_success "shut down nodes" '
iptb stop
'
}

test_expect_success "set up tcp testbed" '
iptb init -n 2 -p 0 -f --bootstrap=none
'

# test multiplex muxer
export LIBP2P_MUX_PREFS="/mplex/6.7.0"
run_advanced_test "--enable-mplex-experiment"
unset LIBP2P_MUX_PREFS

# test default configuration
run_advanced_test


test_done
6 changes: 6 additions & 0 deletions test/sharness/t0220-bitswap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ test_expect_success "'ipfs bitswap stat' output looks good" '
bitswap status
provides buffer: 0 / 256
blocks received: 0
blocks sent: 0
data received: 0
data sent: 0
dup blocks received: 0
dup data received: 0 B
wantlist [0 keys]
Expand Down Expand Up @@ -55,6 +58,9 @@ test_expect_success "'ipfs bitswap stat' output looks good" '
bitswap status
provides buffer: 0 / 256
blocks received: 0
blocks sent: 0
data received: 0
data sent: 0
dup blocks received: 0
dup data received: 0 B
wantlist [0 keys]
Expand Down