Skip to content

Commit

Permalink
expose upload/download speed methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MKPLKN committed Sep 5, 2024
1 parent 8e85746 commit 2733b4f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
18 changes: 15 additions & 3 deletions lib/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = class Monitor extends ReadyResource {
this.blobs = null
this.name = opts.name
this.entry = opts.entry
this.speed = null

this._boundOnAppend = debounce(this._onAppend.bind(this))
this._boundOnUpload = this._onUpload.bind(this)
Expand All @@ -31,6 +30,9 @@ module.exports = class Monitor extends ReadyResource {
// Updated on each upload/download event
this.uploadStats = { ...stats }
this.downloadStats = { ...stats }

this.uploadSpeedometer = null
this.downloadSpeedometer = null
}

async _open () {
Expand Down Expand Up @@ -71,10 +73,14 @@ module.exports = class Monitor extends ReadyResource {
}

_onUpload (index, bytes, from) {
if (!this.uploadSpeedometer) this.uploadSpeedometer = speedometer()
this.uploadStats.speed = this.uploadSpeedometer(bytes)
this._updateStats(this.uploadStats, index, bytes, from)
}

_onDownload (index, bytes, from) {
if (!this.downloadSpeedometer) this.downloadSpeedometer = speedometer()
this.downloadStats.speed = this.downloadSpeedometer(bytes)
this._updateStats(this.downloadStats, index, bytes, from)
}

Expand All @@ -83,16 +89,22 @@ module.exports = class Monitor extends ReadyResource {
if (!isWithinRange(index, this.entry)) return

if (!stats.startTime) stats.startTime = Date.now()
if (!this.speed) this.speed = speedometer()
stats.peersCount = from.replicator.peers.length
stats.blocks++
stats.monitoringBytes += bytes
stats.totalBytes += bytes
stats.speed = this.speed(bytes)
stats.percentage = Number(((stats.totalBytes / stats.targetBytes) * 100).toFixed(2))

this.emit('update')
}

downloadSpeed () {
return this.downloadSpeedometer ? this.downloadSpeedometer() : 0
}

uploadSpeed () {
return this.uploadSpeedometer ? this.uploadSpeedometer() : 0
}
}

function isWithinRange (index, entry) {
Expand Down
4 changes: 3 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1563,7 +1563,7 @@ test('drive.list (recursive false) ignore', async (t) => {
})

test('upload/download can be monitored', async (t) => {
t.plan(21)
t.plan(25)
const { corestore, drive, swarm, mirror } = await testenv(t.teardown)
swarm.on('connection', (conn) => corestore.replicate(conn))
swarm.join(drive.discoveryKey, { server: true, client: false })
Expand All @@ -1589,6 +1589,7 @@ test('upload/download can be monitored', async (t) => {
t.is(monitor.uploadStats.monitoringBytes, expectedBytes.pop())
t.is(monitor.uploadStats.targetBlocks, 2)
t.is(monitor.uploadStats.targetBytes, bytes)
t.is(monitor.uploadSpeed(), monitor.uploadStats.speed)
t.absent(monitor.downloadStats.blocks)
})
}
Expand All @@ -1606,6 +1607,7 @@ test('upload/download can be monitored', async (t) => {
t.is(monitor.downloadStats.monitoringBytes, expectedBytes.pop())
t.is(monitor.downloadStats.targetBlocks, 2)
t.is(monitor.downloadStats.targetBytes, bytes)
t.is(monitor.downloadSpeed(), monitor.downloadStats.speed)
t.absent(monitor.uploadStats.blocks)
})
}
Expand Down

0 comments on commit 2733b4f

Please sign in to comment.