Skip to content

Commit

Permalink
Merge pull request #6258 from myself659/bitswap-stat-human
Browse files Browse the repository at this point in the history
humanize for ipfs bitswap stat
  • Loading branch information
Stebalien authored Apr 26, 2019
2 parents 9e973e5 + 63e18da commit 6e5c251
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
18 changes: 15 additions & 3 deletions core/commands/bitswap.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Print out all blocks currently on the bitswap wantlist for the local peer.`,

const (
bitswapVerboseOptionName = "verbose"
bitswapHumanOptionName = "human"
)

var bitswapStatCmd = &cmds.Command{
Expand All @@ -99,6 +100,7 @@ var bitswapStatCmd = &cmds.Command{
},
Options: []cmdkit.Option{
cmdkit.BoolOption(bitswapVerboseOptionName, "v", "Print extra information"),
cmdkit.BoolOption(bitswapHumanOptionName, "Print sizes in human readable format (e.g., 1K 234M 2G)"),
},
Type: bitswap.Stat{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Expand Down Expand Up @@ -130,15 +132,25 @@ var bitswapStatCmd = &cmds.Command{
return err
}
verbose, _ := req.Options[bitswapVerboseOptionName].(bool)
human, _ := req.Options[bitswapHumanOptionName].(bool)

fmt.Fprintln(w, "bitswap status")
fmt.Fprintf(w, "\tprovides buffer: %d / %d\n", s.ProvideBufLen, bitswap.HasBlockBufferSize)
fmt.Fprintf(w, "\tblocks received: %d\n", s.BlocksReceived)
fmt.Fprintf(w, "\tblocks sent: %d\n", s.BlocksSent)
fmt.Fprintf(w, "\tdata received: %d\n", s.DataReceived)
fmt.Fprintf(w, "\tdata sent: %d\n", s.DataSent)
if human {
fmt.Fprintf(w, "\tdata received: %s\n", humanize.Bytes(s.DataReceived))
fmt.Fprintf(w, "\tdata sent: %s\n", humanize.Bytes(s.DataSent))
} else {
fmt.Fprintf(w, "\tdata received: %d\n", s.DataReceived)
fmt.Fprintf(w, "\tdata sent: %d\n", s.DataSent)
}
fmt.Fprintf(w, "\tdup blocks received: %d\n", s.DupBlksReceived)
fmt.Fprintf(w, "\tdup data received: %s\n", humanize.Bytes(s.DupDataReceived))
if human {
fmt.Fprintf(w, "\tdup data received: %s\n", humanize.Bytes(s.DupDataReceived))
} else {
fmt.Fprintf(w, "\tdup data received: %d\n", s.DupDataReceived)
}
fmt.Fprintf(w, "\twantlist [%d keys]\n", len(s.Wantlist))
for _, k := range s.Wantlist {
fmt.Fprintf(w, "\t\t%s\n", enc.Encode(k))
Expand Down
25 changes: 23 additions & 2 deletions test/sharness/t0220-bitswap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bitswap status
data received: 0
data sent: 0
dup blocks received: 0
dup data received: 0 B
dup data received: 0
wantlist [0 keys]
partners [0]
EOF
Expand Down Expand Up @@ -62,7 +62,7 @@ bitswap status
data received: 0
data sent: 0
dup blocks received: 0
dup data received: 0 B
dup data received: 0
wantlist [0 keys]
partners [0]
EOF
Expand All @@ -77,6 +77,27 @@ test_expect_success "'ipfs bitswap wantlist -p' output looks good" '
test_cmp wantlist_out wantlist_p_out
'

test_expect_success "'ipfs bitswap stat --human' succeeds" '
ipfs bitswap stat --human >stat_out_human
'


test_expect_success "'ipfs bitswap stat --human' output looks good" '
cat <<EOF | unexpand -t2 >expected &&
bitswap status
provides buffer: 0 / 256
blocks received: 0
blocks sent: 0
data received: 0 B
data sent: 0 B
dup blocks received: 0
dup data received: 0 B
wantlist [0 keys]
partners [0]
EOF
test_cmp expected stat_out_human
'

test_kill_ipfs_daemon

test_done

0 comments on commit 6e5c251

Please sign in to comment.