leak spike initial commit

This commit is contained in:
Sérgio Ramos 2016-11-21 13:18:12 +00:00
parent c866cfd63c
commit f6b03fd5f6
7 changed files with 122 additions and 0 deletions

4
spikes/leak/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/node_modules
coverage
.nyc_output
npm-debug.log

13
spikes/leak/package.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "leak-spike",
"version": "1.0.0",
"private": true,
"license": "private",
"main": "src/index.js",
"dependencies": {
"clone": "^2.0.0",
"hapi": "^15.2.0",
"pretty-hrtime": "^1.0.3",
"require-dir": "^0.3.1"
}
}

2
spikes/leak/readme.md Normal file
View File

@ -0,0 +1,2 @@
GET /mem
GET /cpu

27
spikes/leak/src/index.js Normal file
View File

@ -0,0 +1,27 @@
const requireDir = require('require-dir');
const plugins = require('./plugins');
const routes = requireDir('./routes');
const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({
host: 'localhost',
port: 8000
});
server.register(plugins, (err) => {
if (err) {
throw err;
}
Object.keys(routes).forEach((name) => {
routes[name](server);
});
server.start((err) => {
server.connections.forEach((conn) => {
console.log(`started at: ${conn.info.uri}`);
});
});
});

View File

@ -0,0 +1 @@
module.exports = [];

View File

@ -0,0 +1,57 @@
const prettyHrtime = require('pretty-hrtime');
const clone = require('clone');
// 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;
const anotherLeak = [];
const fibonacci = (num) => {
if (num <= 1) return 1;
return fibonacci(num - 1) + fibonacci(num - 2);
};
module.exports = (server) => {
server.route({
method: 'GET',
path: '/mem',
config: {
handler: (req, reply) => {
const start = process.hrtime();
const originalLeak = theLeak;
const unused = () => {
// referencig something that is going to be replaced
if (originalLeak) {
console.log("hi");
}
};
theLeak = {
longStr: new Array(1000000).join('*')
};
anotherLeak.push(anotherLeak.length);
const end = process.hrtime(start);
reply(prettyHrtime(end));
}
}
});
server.route({
method: 'GET',
path: '/cpu',
config: {
handler: (req, reply) => {
const start = process.hrtime();
fibonacci(40);
const end = process.hrtime(start);
reply(prettyHrtime(end));
}
}
});
};

View File

@ -0,0 +1,18 @@
const Pkg = require('../../package.json');
const internals = {
response: {
version: Pkg.version
}
};
module.exports = (server) => {
server.route({
method: 'GET',
path: '/ops/version',
config: {
description: 'Returns the version of the server',
handler: (request, reply) => reply(internals.response)
}
});
};