diff --git a/core/commands/bitswap.go b/core/commands/bitswap.go index b3e2644570f..f322f4d90f1 100644 --- a/core/commands/bitswap.go +++ b/core/commands/bitswap.go @@ -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{ @@ -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 { @@ -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)) diff --git a/test/sharness/t0220-bitswap.sh b/test/sharness/t0220-bitswap.sh index fb1e3dbc72d..3575f0d33e2 100755 --- a/test/sharness/t0220-bitswap.sh +++ b/test/sharness/t0220-bitswap.sh @@ -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 @@ -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 @@ -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 <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