Last active
February 10, 2017 21:27
-
-
Save MarkTiedemann/9ed1ed70d329e81f77aeac411cb1d724 to your computer and use it in GitHub Desktop.
Generate a random file with a specified size
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* Usage: node file-gen [mb] | |
* | |
* Example: Generate a 1 GB file | |
* $ node file-gen 1024 | |
* time: 9163.383ms | |
*/ | |
const fs = require('fs') | |
const crypto = require('crypto') | |
const arg = process.argv.slice(2).shift() | |
const chunkCount = parseInt(arg) || 0 | |
const chunkSize = 1024 * 1024 | |
const stream = fs.createWriteStream(chunkCount + 'mb-random.bytes') | |
console.time('time') | |
new Array(chunkCount).fill(void 0).forEach(() => | |
crypto.randomBytes(chunkSize, (_, buffer) => | |
stream.write(buffer) | |
) | |
) | |
process.on('beforeExit', () => console.timeEnd('time')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment