Skip to content

Instantly share code, notes, and snippets.

@vsemozhetbyt
Created November 25, 2016 03:30
Show Gist options
  • Select an option

  • Save vsemozhetbyt/076a0f47779ebf89bc01dca6198ac484 to your computer and use it in GitHub Desktop.

Select an option

Save vsemozhetbyt/076a0f47779ebf89bc01dca6198ac484 to your computer and use it in GitHub Desktop.
Test Node.js buffer zeroing scheme
/******************************************************************************/
'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);
}
/******************************************************************************/
@vsemozhetbyt
Copy link
Copy Markdown
Author

alloc-unsafe-slow.log

  105: 0 2 16 17 18 24 25 26 40 41 42 48 49 50 64 65 66 72 73 74 88 89 90 96 97 98 99 112 113 114 120 121 122
* 111: 0 2 16 17 18 25 26 40 41 42 48 49 50 64 65 66 72 73 74 88 89 90 96 97 98 99 112 113 114 120 121 122
* 112: 0 2 16 17 18 24 25 26 40 41 42 48 49 50 64 65 66 72 73 74 88 89 90 96 97 98 99 112 113 114 120 121 122
* 118: 0 2 16 17 18 24 25 26 40 41 42 48 49 50 64 65 66 72 73 74 88 89 90 96 97 98 99 112 113 114 120 122
* 119: 0 2 16 17 18 24 25 26 40 41 42 48 49 50 64 65 66 72 73 74 88 89 90 96 97 98 99 112 113 114 120 121 122
* 143: 0 2 16 17 18 24 25 26 40 41 42 48 49 50 64 65 66 72 73 74 80 81 82 88 89 90 96 97 98 99 112 113 114 120 121 122
* 151: 0 6 7 8 12 13 14 15 16 17 18 19 20 21 25 26 30 31 32 33 34 35 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 62 63 64 65 69 70 71 72 73 74 75 79 80 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 109 113 114 118 119 120 121 122 123 124 125 126 127
* 152: 0 2 16 17 18 24 25 26 40 41 42 48 49 50 64 65 66 72 73 74 80 81 82 88 89 90 96 97 98 99 112 113 114 120 121 122
* 171: 0 2 3 16 17 18 19 24 25 26 27 40 41 42 43 48 49 50 51 64 65 66 67 72 73 74 75 80 81 82 83 88 89 90 91 96 97 98 99 112 113 114 115 120 121 122 123
* 172: 0 2 16 17 18 24 25 26 40 41 42 48 49 50 64 65 66 72 73 74 80 81 82 88 89 90 96 97 98 99 112 113 114 120 121 122
* 202: 0 2 16 17 18 25 26 40 41 42 48 49 50 64 65 66 72 73 74 80 81 82 88 89 90 96 97 98 99 112 113 114 120 121 122
* 203: 0 2 3 16 17 18 19 24 25 26 27 40 41 42 43 48 49 50 51 64 65 66 67 72 73 74 75 80 81 82 83 88 89 90 91 96 97 98 99 112 113 114 115 120 121 122 123
* 204: 0 2 16 17 18 24 25 26 40 41 42 48 49 50 64 65 66 72 73 74 80 81 82 88 89 90 96 97 98 99 112 113 114 120 121 122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment