leak spike initial commit
This commit is contained in:
parent
c866cfd63c
commit
f6b03fd5f6
4
spikes/leak/.gitignore
vendored
Normal file
4
spikes/leak/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
/node_modules
|
||||
coverage
|
||||
.nyc_output
|
||||
npm-debug.log
|
13
spikes/leak/package.json
Normal file
13
spikes/leak/package.json
Normal 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
2
spikes/leak/readme.md
Normal file
@ -0,0 +1,2 @@
|
||||
GET /mem
|
||||
GET /cpu
|
27
spikes/leak/src/index.js
Normal file
27
spikes/leak/src/index.js
Normal 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}`);
|
||||
});
|
||||
});
|
||||
});
|
1
spikes/leak/src/plugins.js
Normal file
1
spikes/leak/src/plugins.js
Normal file
@ -0,0 +1 @@
|
||||
module.exports = [];
|
57
spikes/leak/src/routes/leak.js
Normal file
57
spikes/leak/src/routes/leak.js
Normal 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));
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
18
spikes/leak/src/routes/version.js
Normal file
18
spikes/leak/src/routes/version.js
Normal 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)
|
||||
}
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue
Block a user