2020-04-26 01:51:59 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const bhttp = require('bhttp');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const fsPromises = require('fs').promises;
|
|
|
|
const fs = require('fs');
|
|
|
|
const { PassThrough } = require('stream');
|
|
|
|
const blake2 = require('blake2');
|
|
|
|
const argv = require('yargs').argv;
|
|
|
|
|
|
|
|
const file = 'https://speed.hetzner.de/100MB.bin';
|
|
|
|
// const file = 'https://speed.hetzner.de/1GB.bin';
|
|
|
|
// const file = 'https://speed.hetzner.de/10GB.bin';
|
|
|
|
|
|
|
|
function getMemoryUsage() {
|
2020-05-14 02:26:05 +00:00
|
|
|
return process.memoryUsage().rss / (10 ** 6);
|
2020-04-26 01:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const stats = {
|
2020-05-14 02:26:05 +00:00
|
|
|
peakMemoryUsage: getMemoryUsage(),
|
|
|
|
done: false,
|
|
|
|
downloads: {},
|
2020-04-26 01:51:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function render() {
|
2020-05-14 02:26:05 +00:00
|
|
|
const downloads = Object.entries(stats.downloads);
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
process.stdout.clearScreenDown();
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
process.stdout.write(`peak memory: ${stats.peakMemoryUsage.toFixed(2)} MB\n`);
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
downloads.forEach(([download, progress]) => {
|
|
|
|
process.stdout.write(`${download}: ${progress}${typeof progress === 'string' ? '' : '%'}\n`);
|
|
|
|
});
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
process.stdout.moveCursor(0, -(downloads.length + 1));
|
|
|
|
process.stdout.cursorTo(0);
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (downloads.length === 0 || !downloads.every(([_label, download]) => typeof download === 'string')) {
|
|
|
|
setTimeout(() => render(), 1000);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
process.stdout.moveCursor(0, downloads.length + 1);
|
2020-04-26 01:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setProgress(label, completedBytes, totalBytes, hash) {
|
2020-05-14 02:26:05 +00:00
|
|
|
const memory = getMemoryUsage();
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
stats.peakMemoryUsage = Math.max(memory, stats.peakMemoryUsage);
|
|
|
|
stats.downloads[label] = hash || Math.round((completedBytes / totalBytes) * 100);
|
2020-04-26 01:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function buffered(label) {
|
2020-05-14 02:26:05 +00:00
|
|
|
const hash = new blake2.Hash('blake2b');
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const imageRes = await bhttp.get(file, {
|
|
|
|
onDownloadProgress(completedBytes, totalBytes) {
|
|
|
|
setProgress(label, completedBytes, totalBytes);
|
|
|
|
},
|
|
|
|
});
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
hash.update(imageRes.body);
|
|
|
|
setProgress(label, null, null, hash.digest('hex'));
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
await fsPromises.writeFile(`/mnt/stor/Pictures/traxxx/temp/buffered-${label}.bin`, imageRes.body);
|
2020-04-26 01:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function streamed(label) {
|
2020-05-14 02:26:05 +00:00
|
|
|
const hash = new blake2.Hash('blake2b');
|
|
|
|
hash.setEncoding('hex');
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const hashStream = new PassThrough();
|
|
|
|
const targetStream = fs.createWriteStream(`/mnt/stor/Pictures/traxxx/temp/streamed-${label}.bin`);
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const imageRes = await bhttp.get(file, {
|
|
|
|
stream: true,
|
|
|
|
});
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
const stream = imageRes
|
|
|
|
.pipe(hashStream)
|
|
|
|
.pipe(targetStream);
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
imageRes.on('progress', (completedBytes, totalBytes) => {
|
|
|
|
setProgress(label, completedBytes, totalBytes);
|
|
|
|
});
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
hashStream.on('data', (chunk) => {
|
|
|
|
hash.write(chunk);
|
|
|
|
});
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
stream.on('finish', () => {
|
|
|
|
hash.end();
|
|
|
|
setProgress(label, null, null, hash.read());
|
|
|
|
});
|
2020-04-26 01:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function init() {
|
2020-05-14 02:26:05 +00:00
|
|
|
const n = argv.n || 1;
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (argv._.includes('stream')) {
|
|
|
|
console.log('using streams');
|
|
|
|
render();
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
await Promise.map(Array.from({ length: n }), async (value, index) => streamed(index + 1));
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
if (argv._.includes('buffer')) {
|
|
|
|
console.log('using buffers');
|
|
|
|
render();
|
2020-04-26 01:51:59 +00:00
|
|
|
|
2020-05-14 02:26:05 +00:00
|
|
|
await Promise.map(Array.from({ length: n }), async (value, index) => buffered(index + 1));
|
|
|
|
}
|
2020-04-26 01:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init();
|