Created
November 25, 2016 03:30
-
-
Save vsemozhetbyt/076a0f47779ebf89bc01dca6198ac484 to your computer and use it in GitHub Desktop.
Test Node.js buffer zeroing scheme
This file contains hidden or 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
| /******************************************************************************/ | |
| 'use strict'; | |
| /******************************************************************************/ | |
| const fs = require('fs'); | |
| const cp = require('child_process'); | |
| const rl = require('readline'); | |
| /******************************************************************************/ | |
| const bufSize = 128; | |
| const fileSizeLimit = 256; | |
| const changeSycleSize = 8; | |
| const numberOfTests = 10; | |
| const modes = { | |
| newBuffer: { | |
| code: `console.log([...new Buffer(${bufSize})].reduce((a,v,i)=>\`\${a}\${v?\`\${i} \`:''}\`,''));\n`, | |
| scriptFileName: 'new-buffer.js', | |
| scriptFile: fs.openSync('new-buffer.js', 'w'), | |
| statFile: fs.openSync('new-buffer.log', 'w'), | |
| previousStat: '', | |
| }, | |
| allocUnsafe: { | |
| code: `console.log([...Buffer.allocUnsafe(${bufSize})].reduce((a,v,i)=>\`\${a}\${v?\`\${i} \`:''}\`,''));\n`, | |
| scriptFileName: 'alloc-unsafe.js', | |
| scriptFile: fs.openSync('alloc-unsafe.js', 'w'), | |
| statFile: fs.openSync('alloc-unsafe.log', 'w'), | |
| previousStat: '', | |
| }, | |
| allocUnsafeSlow: { | |
| code: `console.log([...Buffer.allocUnsafeSlow(${bufSize})].reduce((a,v,i)=>\`\${a}\${v?\`\${i} \`:''}\`,''));\n`, | |
| scriptFileName: 'alloc-unsafe-slow.js', | |
| scriptFile: fs.openSync('alloc-unsafe-slow.js', 'w'), | |
| statFile: fs.openSync('alloc-unsafe-slow.log', 'w'), | |
| previousStat: '', | |
| }, | |
| newSlowBuffer: { | |
| code: `console.log([...new require('buffer').SlowBuffer(${bufSize})].reduce((a,v,i)=>\`\${a}\${v?\`\${i} \`:''}\`,''));\n`, | |
| scriptFileName: 'new-slow-buffer.js', | |
| scriptFile: fs.openSync('new-slow-buffer.js', 'w'), | |
| statFile: fs.openSync('new-slow-buffer.log', 'w'), | |
| previousStat: '', | |
| }, | |
| alloc: { | |
| code: `console.log([...Buffer.alloc(${bufSize})].reduce((a,v,i)=>\`\${a}\${v?\`\${i} \`:''}\`,''));\n`, | |
| scriptFileName: 'alloc.js', | |
| scriptFile: fs.openSync('alloc.js', 'w'), | |
| statFile: fs.openSync('alloc.log', 'w'), | |
| previousStat: '', | |
| }, | |
| }; | |
| const maxCodeLength = Math.max(...Object.values(modes).map(mode => mode.code.length)); | |
| const remainder = maxCodeLength % changeSycleSize; | |
| const minCodeLengthInSycleStart = | |
| remainder === 1 ? maxCodeLength : maxCodeLength + changeSycleSize - remainder + 1; | |
| Object.values(modes).forEach((mode) => { | |
| mode.code += ' '.repeat(minCodeLengthInSycleStart - mode.code.length); | |
| }); | |
| /******************************************************************************/ | |
| Object.entries(modes).forEach(([modeName, mode], i) => { | |
| fs.writeSync(mode.scriptFile, mode.code); | |
| for (let codeSize = mode.code.length; codeSize <= fileSizeLimit; codeSize++) { | |
| info(`${i + 1}. ${modeName}: ${codeSize} bytes in file from ${fileSizeLimit}`); | |
| const stats = getStats(numberOfTests, mode.scriptFileName); | |
| const statsAverage = getStatsAverage(stats); | |
| if (statsAverage !== mode.previousStat) { | |
| fs.writeSync(mode.statFile, | |
| `${(codeSize - 1) % changeSycleSize === 0 ? ' ' : '* '}${codeSize}: ${statsAverage}\n` | |
| ); | |
| mode.previousStat = statsAverage; | |
| } | |
| fs.writeSync(mode.scriptFile, ' '); | |
| } | |
| }); | |
| info('Completed.\n'); | |
| /******************************************************************************/ | |
| function getStats(numberOfRepetitions, scriptFileName) { | |
| return new Array(numberOfRepetitions).fill('').map(() => | |
| cp.execSync(`node ${scriptFileName}`).toString().trim() | |
| ); | |
| } | |
| /******************************************************************************/ | |
| function getStatsAverage(stats) { | |
| const mostRepeatedEntryIndex = stats | |
| .map((stat1, i) => [i, stats.filter(stat2 => stat2 === stat1).length]) | |
| .sort((a, b) => b[1] - a[1])[0][0]; | |
| return stats[mostRepeatedEntryIndex]; | |
| } | |
| /******************************************************************************/ | |
| function info(message) { | |
| rl.cursorTo(process.stdout, 0); | |
| rl.clearLine(process.stdout, 0); | |
| process.stdout.write(message); | |
| } | |
| /******************************************************************************/ |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
alloc-unsafe-slow.log