Skip to content

Instantly share code, notes, and snippets.

@MarkTiedemann
Last active February 10, 2017 21:27
Show Gist options
  • Save MarkTiedemann/9ed1ed70d329e81f77aeac411cb1d724 to your computer and use it in GitHub Desktop.
Save MarkTiedemann/9ed1ed70d329e81f77aeac411cb1d724 to your computer and use it in GitHub Desktop.
Generate a random file with a specified size
#!/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