2016-11-21 15:18:12 +02:00
|
|
|
const prettyHrtime = require('pretty-hrtime');
|
|
|
|
|
|
|
|
// leak example from https://auth0.com/blog/four-types-of-leaks-in-your-javascript-code-and-how-to-get-rid-of-them/
|
|
|
|
let theLeak = null;
|
2016-11-23 01:03:33 +02:00
|
|
|
let anotherLeak = [];
|
2016-11-21 15:18:12 +02:00
|
|
|
|
|
|
|
const fibonacci = (num) => {
|
|
|
|
if (num <= 1) return 1;
|
|
|
|
|
|
|
|
return fibonacci(num - 1) + fibonacci(num - 2);
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = (server) => {
|
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
2016-11-23 01:03:33 +02:00
|
|
|
path: '/mem-fast',
|
|
|
|
config: {
|
|
|
|
handler: (req, reply) => {
|
2017-03-15 18:03:20 +02:00
|
|
|
console.log('got /mem-fast request');
|
2016-11-23 01:03:33 +02:00
|
|
|
const start = process.hrtime();
|
2017-03-15 18:03:20 +02:00
|
|
|
const length = (anotherLeak.length || 1);
|
2016-11-23 01:03:33 +02:00
|
|
|
|
|
|
|
anotherLeak.push({
|
2017-03-15 18:03:20 +02:00
|
|
|
longStr: Array.from({
|
|
|
|
length: length * length
|
|
|
|
}, (v, i) => i)
|
2016-11-23 01:03:33 +02:00
|
|
|
});
|
|
|
|
|
2017-03-15 18:03:20 +02:00
|
|
|
console.log('mem-fast', anotherLeak[length - 1].longStr.length);
|
2016-11-23 01:03:33 +02:00
|
|
|
|
|
|
|
const end = process.hrtime(start);
|
|
|
|
reply(prettyHrtime(end));
|
2017-03-15 18:03:20 +02:00
|
|
|
console.log('sent /mem-fast response');
|
2016-11-23 01:03:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/mem-slow',
|
2016-11-21 15:18:12 +02:00
|
|
|
config: {
|
|
|
|
handler: (req, reply) => {
|
2017-03-15 18:03:20 +02:00
|
|
|
console.log('got /mem-slow request');
|
|
|
|
|
2016-11-21 15:18:12 +02:00
|
|
|
const start = process.hrtime();
|
|
|
|
|
|
|
|
const originalLeak = theLeak;
|
|
|
|
|
|
|
|
const unused = () => {
|
|
|
|
// referencig something that is going to be replaced
|
|
|
|
if (originalLeak) {
|
|
|
|
console.log("hi");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
theLeak = {
|
2017-03-15 18:03:20 +02:00
|
|
|
longStr: Array.from({
|
|
|
|
length: 1000
|
|
|
|
}, (v, i) => i).join('*')
|
2016-11-21 15:18:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
anotherLeak.push(anotherLeak.length);
|
2016-11-23 01:03:33 +02:00
|
|
|
console.log('mem-slow %d', anotherLeak.length);
|
2016-11-21 15:18:12 +02:00
|
|
|
|
|
|
|
const end = process.hrtime(start);
|
|
|
|
reply(prettyHrtime(end));
|
2017-03-15 18:03:20 +02:00
|
|
|
console.log('sent /mem-slow response');
|
|
|
|
|
2016-11-21 15:18:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server.route({
|
|
|
|
method: 'GET',
|
|
|
|
path: '/cpu',
|
|
|
|
config: {
|
|
|
|
handler: (req, reply) => {
|
2017-03-15 18:03:20 +02:00
|
|
|
console.log('got /cpu request');
|
|
|
|
|
2016-11-21 15:18:12 +02:00
|
|
|
const start = process.hrtime();
|
|
|
|
|
|
|
|
fibonacci(40);
|
|
|
|
|
|
|
|
const end = process.hrtime(start);
|
|
|
|
reply(prettyHrtime(end));
|
2017-03-15 18:03:20 +02:00
|
|
|
|
|
|
|
console.log('sent /cpu response');
|
2016-11-21 15:18:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|