Skip to content

Instantly share code, notes, and snippets.

@rbranson
Created February 22, 2017 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rbranson/b86be2796050c8efdb19562370f32934 to your computer and use it in GitHub Desktop.
Save rbranson/b86be2796050c8efdb19562370f32934 to your computer and use it in GitHub Desktop.
Demonstrates additional memory / CPU usage when forcing use of the OS-provided root certificates
// run this in bash with 'time' and it'll show CPU usage totals at the end
var totalCycles = 250;
var concurrencyLevel = 25;
var useOSCerts = true;
var requestOptions = {
hostname: 'www.google.com',
port: 443,
path: '/',
method: 'GET'
};
var fs = require('fs');
var https = require('https');
if (useOSCerts) {
https.globalAgent.options.ca = (function(){
var path = '/etc/ssl/certs/ca-certificates.crt';
var re = /(?=-----begin certificate-----)/i;
var contents;
if (fs.existsSync(path)) {
console.log('using the OS cert chain');
contents = fs.readFileSync(path).toString();
return contents.split(re);
}
console.log('using the default nodejs cert chain');
return null;
}());
}
var active = 0;
var reqCount = 0;
var bytesReceived = 0;
function printStatus() {
console.log("completed:", reqCount,
"in-flight:", active,
"bytes:", bytesReceived,
"memory:", process.memoryUsage());
}
function genreq() {
if (reqCount > totalCycles) {
return;
}
reqCount++;
printStatus();
var req = https.request(requestOptions, function(res) {
active++;
res.on('data', function(chunk) {
bytesReceived += chunk.length;
});
res.on('end', function() {
active--;
genreq();
});
});
req.on('error', function(err) {
console.log(err);
});
req.end();
}
for (var i = 0; i < concurrencyLevel; i++) {
setTimeout(genreq, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment