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
This commit is contained in:
Jonas Hermsmeier 2017-11-22 21:12:38 +01:00 committed by GitHub
parent 8e79e9e459
commit 4e891151f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -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)

View File

@ -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)
})