Last active
June 17, 2018 16:19
-
-
Save ChALkeR/aa392f5bb957279f0527e7f74e3b72da to your computer and use it in GitHub Desktop.
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
const http2 = require('http2'); | |
const fs = require('fs'); | |
const connection = http2.connect('https://localhost:8080', {ca: fs.readFileSync('./keys/localhost-cert.pem')}); // http2session | |
function request(callback) { | |
// http2stream | |
const req = connection.request({ | |
':method': 'GET', | |
':path': '/' | |
}); | |
req.on('response', (headers, flags) => {}); | |
req.on('data', (chunk) => {}); | |
req.on('end', () => { | |
// calling close or destroy on the stream makes no difference | |
// but technically we shouldn't do that manually in this case anyway | |
// req.close(); | |
// req.destroy(); | |
callback(); | |
}); | |
req.end(); | |
} | |
function launch(total, parallel, gci, callback) { | |
const done = () => { | |
total--; | |
if (total % 10000 === 0) console.log(total); | |
if (gci > 0 && total % gci === 0) { | |
//global.gc(true); global.gc(true); // Scavenge | |
// OR | |
global.gc(); global.gc(); // Mark-sweep | |
} | |
if (total >= parallel) request(done); | |
else if (total === 0) callback(); | |
}; | |
for (let i = 0; i < parallel; i++) request(done); | |
} | |
// we will establish a baseline memory usage here | |
// connect the socket and wait for the first request to come back | |
request(() => { | |
// grab current process memory usage | |
global.gc(); global.gc(); // We are comparing gc-d memory | |
var baselineMemory = process.memoryUsage(); | |
// total, parallel, gci | |
// gci = 0 means don't run gc manually | |
// gci = 100 means run gc manually every 100 requests | |
launch(30000, 5, /*0*/100, () => { | |
// GC what we can | |
for (let i = 0; i < 20; i++) global.gc(); | |
var finalMemory = process.memoryUsage(); | |
var rssDelta = (finalMemory.rss - baselineMemory.rss) / 1024; | |
var heapTotalDelta = (finalMemory.heapTotal - baselineMemory.heapTotal) / 1024; | |
var heapUsedDelta = Math.round((finalMemory.heapUsed - baselineMemory.heapUsed) / 1024); | |
console.log(`Deltas: ${heapTotalDelta} KB Heap Total, ${heapUsedDelta} KB Heap Used, ${rssDelta} KB RSS`); | |
process.exit(0); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment