Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat: add bitswap stat human option in CLI (#2619)
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroMiguelSS authored and Alan Shaw committed Nov 22, 2019
1 parent 5a1d266 commit 6a2ea52
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 19 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
"peer-book": "^0.9.1",
"peer-id": "^0.12.2",
"peer-info": "~0.15.1",
"pretty-bytes": "^5.3.0",
"progress": "^2.0.1",
"promise-nodeify": "^3.0.1",
"promisify-es6": "^1.0.3",
Expand Down
39 changes: 29 additions & 10 deletions src/cli/commands/bitswap/stat.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const multibase = require('multibase')
const { cidToString } = require('../../../utils/cid')
const prettyBytes = require('pretty-bytes')

module.exports = {
command: 'stat',
Expand All @@ -13,24 +14,42 @@ module.exports = {
describe: 'Number base to display CIDs in. Note: specifying a CID base for v0 CIDs will have no effect.',
type: 'string',
choices: multibase.names
},
human: {
type: 'boolean',
default: false
}
},

handler ({ getIpfs, print, cidBase, resolve }) {
handler ({ getIpfs, print, cidBase, resolve, human }) {
resolve((async () => {
const ipfs = await getIpfs()
const stats = await ipfs.bitswap.stat()
stats.wantlist = stats.wantlist.map(k => cidToString(k['/'], { base: cidBase, upgrade: false }))
stats.peers = stats.peers || []

if (human) {
stats.blocksReceived = stats.blocksReceived.toNumber()
stats.blocksSent = stats.blocksSent.toNumber()
stats.dataReceived = prettyBytes(stats.dataReceived.toNumber()).toUpperCase()
stats.dataSent = prettyBytes(stats.dataSent.toNumber()).toUpperCase()
stats.dupBlksReceived = stats.dupBlksReceived.toNumber()
stats.dupDataReceived = prettyBytes(stats.dupDataReceived.toNumber()).toUpperCase()
stats.wantlist = `[${stats.wantlist.length} keys]`
} else {
const wantlist = stats.wantlist.map((elem) => cidToString(elem['/'], { base: cidBase, upgrade: false }))
stats.wantlist = `[${wantlist.length} keys]
${wantlist.join('\n ')}`
}

print(`bitswap status
blocks received: ${stats.blocksReceived}
dup blocks received: ${stats.dupBlksReceived}
dup data received: ${stats.dupDataReceived}B
wantlist [${stats.wantlist.length} keys]
${stats.wantlist.join('\n ')}
partners [${stats.peers.length}]
${stats.peers.join('\n ')}`)
provides buffer: ${stats.provideBufLen}
blocks received: ${stats.blocksReceived}
blocks sent: ${stats.blocksSent}
data received: ${stats.dataReceived}
data sent: ${stats.dataSent}
dup blocks received: ${stats.dupBlksReceived}
dup data received: ${stats.dupDataReceived}
wantlist ${stats.wantlist}
partners [${stats.peers.length}]`)
})())
}
}
39 changes: 30 additions & 9 deletions test/cli/bitswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,38 @@ describe('bitswap', () => runOn((thing) => {
this.timeout(20 * 1000)

const out = await ipfs('bitswap stat')
expect(out).to.include([
'bitswap status',
' blocks received: 0',
' dup blocks received: 0',
' dup data received: 0B',
// We sometimes pick up partners while the tests run and the order of
// wanted keys is not defined so our assertion ends here.
' wantlist [2 keys]'
].join('\n'))

expect(out).to.include('bitswap status')
expect(out).to.match(/provides buffer:\s\d+$/m)
expect(out).to.match(/blocks received:\s\d+$/m)
expect(out).to.match(/blocks sent:\s\d+$/m)
expect(out).to.match(/data received:\s\d+$/m)
expect(out).to.match(/data sent:\s\d+$/m)
expect(out).to.match(/dup blocks received:\s\d+$/m)
expect(out).to.match(/dup data received:\s\d+$/m)
expect(out).to.match(/wantlist\s\[\d+\skeys\]$/m)
expect(out).to.include(key0)
expect(out).to.include(key1)
expect(out).to.match(/partners\s\[\d+\]$/m)
})

it('stat --human', async function () {
this.timeout(20 * 1000)

const out = await ipfs('bitswap stat --human')

expect(out).to.include('bitswap status')
expect(out).to.match(/provides buffer:\s\d+$/m)
expect(out).to.match(/blocks received:\s\d+$/m)
expect(out).to.match(/blocks sent:\s\d+$/m)
expect(out).to.match(/data received:\s+[\d.]+\s[PTGMK]?B$/m)
expect(out).to.match(/data sent:\s+[\d.]+\s[PTGMK]?B$/m)
expect(out).to.match(/dup blocks received:\s\d+$/m)
expect(out).to.match(/dup data received:\s+[\d.]+\s[PTGMK]?B$/m)
expect(out).to.match(/wantlist\s\[\d+\skeys\]$/m)
expect(out).to.not.include(key0)
expect(out).to.not.include(key1)
expect(out).to.match(/partners\s\[\d+\]$/m)
})

it('should get stats with wantlist CIDs encoded in specified base', async function () {
Expand Down

0 comments on commit 6a2ea52

Please sign in to comment.