From 4e891151f49e88f9df7b5b3d6017edff357e31ad Mon Sep 17 00:00:00 2001 From: Jonas Hermsmeier Date: Wed, 22 Nov 2017 21:12:38 +0100 Subject: [PATCH] feat(writer): Display actual write speed (#1863) As we've actually been displaying the read-speed in various forms during the flashing process, this is a venture into displaying the actual write-speed from the end of the pipeline. Change-Type: minor Changelog-Entry: Display actual write speed --- lib/writer/block-write-stream.js | 20 ++++++++++++++++++++ lib/writer/index.js | 1 + 2 files changed, 21 insertions(+) diff --git a/lib/writer/block-write-stream.js b/lib/writer/block-write-stream.js index 18ced4ef..06051d5c 100644 --- a/lib/writer/block-write-stream.js +++ b/lib/writer/block-write-stream.js @@ -18,9 +18,11 @@ const stream = require('readable-stream') const fs = require('fs') +const speedometer = require('speedometer') const debug = require('debug')('block-write-stream') const CHUNK_SIZE = 64 * 1024 +const UPDATE_INTERVAL_MS = 500 /** * @summary BlockWriteStream @@ -60,6 +62,23 @@ class BlockWriteStream extends stream.Writable { this.blocksRead = 0 this.bytesWritten = 0 this.blocksWritten = 0 + this.meter = speedometer() + this.delta = 0 + this.speed = 0 + + this.clear = () => { + clearInterval(this.timer) + } + + this.update = () => { + this.speed = this.meter(this.delta) + this.delta = 0 + } + + this.once('end', this.clear) + this.once('error', this.clear) + + this.timer = setInterval(this.update, UPDATE_INTERVAL_MS) this.closed = false this.destroyed = false @@ -116,6 +135,7 @@ class BlockWriteStream extends stream.Writable { fs.write(this.fd, chunk, 0, chunk.length, chunk.position, (error, bytesWritten) => { this.bytesWritten += bytesWritten + this.delta += bytesWritten this.blocksWritten += 1 this.position += bytesWritten next(error) diff --git a/lib/writer/index.js b/lib/writer/index.js index 7627bccc..4ff220a4 100644 --- a/lib/writer/index.js +++ b/lib/writer/index.js @@ -226,6 +226,7 @@ class ImageWriter extends EventEmitter { progressStream.on('progress', (state) => { state.device = options.path state.type = 'write' + state.speed = target.speed this.emit('progress', state) })