Unverified Commit 6e5c2515 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #6258 from myself659/bitswap-stat-human

humanize for ipfs bitswap stat
parents 9e973e52 63e18dac
...@@ -90,6 +90,7 @@ Print out all blocks currently on the bitswap wantlist for the local peer.`, ...@@ -90,6 +90,7 @@ Print out all blocks currently on the bitswap wantlist for the local peer.`,
const ( const (
bitswapVerboseOptionName = "verbose" bitswapVerboseOptionName = "verbose"
bitswapHumanOptionName = "human"
) )
var bitswapStatCmd = &cmds.Command{ var bitswapStatCmd = &cmds.Command{
...@@ -99,6 +100,7 @@ var bitswapStatCmd = &cmds.Command{ ...@@ -99,6 +100,7 @@ var bitswapStatCmd = &cmds.Command{
}, },
Options: []cmdkit.Option{ Options: []cmdkit.Option{
cmdkit.BoolOption(bitswapVerboseOptionName, "v", "Print extra information"), cmdkit.BoolOption(bitswapVerboseOptionName, "v", "Print extra information"),
cmdkit.BoolOption(bitswapHumanOptionName, "Print sizes in human readable format (e.g., 1K 234M 2G)"),
}, },
Type: bitswap.Stat{}, Type: bitswap.Stat{},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
...@@ -130,15 +132,25 @@ var bitswapStatCmd = &cmds.Command{ ...@@ -130,15 +132,25 @@ var bitswapStatCmd = &cmds.Command{
return err return err
} }
verbose, _ := req.Options[bitswapVerboseOptionName].(bool) verbose, _ := req.Options[bitswapVerboseOptionName].(bool)
human, _ := req.Options[bitswapHumanOptionName].(bool)
fmt.Fprintln(w, "bitswap status") fmt.Fprintln(w, "bitswap status")
fmt.Fprintf(w, "\tprovides buffer: %d / %d\n", s.ProvideBufLen, bitswap.HasBlockBufferSize) 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 received: %d\n", s.BlocksReceived)
fmt.Fprintf(w, "\tblocks sent: %d\n", s.BlocksSent) fmt.Fprintf(w, "\tblocks sent: %d\n", s.BlocksSent)
fmt.Fprintf(w, "\tdata received: %d\n", s.DataReceived) if human {
fmt.Fprintf(w, "\tdata sent: %d\n", s.DataSent) 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 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)) fmt.Fprintf(w, "\twantlist [%d keys]\n", len(s.Wantlist))
for _, k := range s.Wantlist { for _, k := range s.Wantlist {
fmt.Fprintf(w, "\t\t%s\n", enc.Encode(k)) fmt.Fprintf(w, "\t\t%s\n", enc.Encode(k))
......
...@@ -24,7 +24,7 @@ bitswap status ...@@ -24,7 +24,7 @@ bitswap status
data received: 0 data received: 0
data sent: 0 data sent: 0
dup blocks received: 0 dup blocks received: 0
dup data received: 0 B dup data received: 0
wantlist [0 keys] wantlist [0 keys]
partners [0] partners [0]
EOF EOF
...@@ -62,7 +62,7 @@ bitswap status ...@@ -62,7 +62,7 @@ bitswap status
data received: 0 data received: 0
data sent: 0 data sent: 0
dup blocks received: 0 dup blocks received: 0
dup data received: 0 B dup data received: 0
wantlist [0 keys] wantlist [0 keys]
partners [0] partners [0]
EOF EOF
...@@ -77,6 +77,27 @@ test_expect_success "'ipfs bitswap wantlist -p' output looks good" ' ...@@ -77,6 +77,27 @@ test_expect_success "'ipfs bitswap wantlist -p' output looks good" '
test_cmp wantlist_out wantlist_p_out 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_kill_ipfs_daemon
test_done test_done
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment