Skip to content

Commit

Permalink
Updated memory benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Voronov committed Nov 19, 2016
1 parent c97126d commit 149ab62
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 24 deletions.
37 changes: 37 additions & 0 deletions benchmarks/benchs/memory-with-dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import times from './utils/times';
import format from './utils/format-memory';
import report from './utils/reporter';
import Container from '../../src/index';

const cycles = 20;
const objects = 50000;
const container = new Container();
const initialMemory = process.memoryUsage().heapUsed;

report('Memory. With dependencies');
report(`Initial memory usage ${format(initialMemory)}`, 1, '-');

times(cycles, (cycle) => {
times(objects, (idx) => {
container.namespace(`${idx}_namespace`).value(`${idx}_value`, {});
container.factory(`${idx}_factory`, [
`${idx}_namespace/${idx}_value`
], (dep) => {
return [dep];
});

container.resolve(`${idx}_factory`);
});

report.update(`Allocated ${objects} objects in ${cycle} cycle`, 2);
container.clear();
global.gc();
});

report('Finished. Pause');
setTimeout(() => {
const releasedMemory = process.memoryUsage().heapUsed;

report(`Final released memory usage ${format(releasedMemory)}`, 1);
report(`Initial: ${format(initialMemory)}, final: ${format(releasedMemory)}`, 1);
}, 1000);
31 changes: 31 additions & 0 deletions benchmarks/benchs/memory-with-namespaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import times from './utils/times';
import format from './utils/format-memory';
import report from './utils/reporter';
import Container from '../../src/index';

const cycles = 20;
const objects = 50000;
const container = new Container();
const initialMemory = process.memoryUsage().heapUsed;

report('Memory. With namespaces');
report(`Initial memory usage ${format(initialMemory)}`, 1, '-');

times(cycles, (cycle) => {
times(objects, (idx) => {
container.namespace(`${idx}_namespace`).value(`${idx}_value`, {});
container.resolve(`${idx}_namespace/${idx}_value`);
});

report.update(`Allocated ${objects} objects in ${cycle} cycle`, 2);
container.clear();
global.gc();
});

report('Finished. Pause');
setTimeout(() => {
const releasedMemory = process.memoryUsage().heapUsed;

report(`Final released memory usage ${format(releasedMemory)}`, 1);
report(`Initial: ${format(initialMemory)}, final: ${format(releasedMemory)}`, 1);
}, 1000);
31 changes: 31 additions & 0 deletions benchmarks/benchs/memory-without-namespaces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import times from './utils/times';
import format from './utils/format-memory';
import report from './utils/reporter';
import Container from '../../src/index';

const cycles = 20;
const objects = 50000;
const container = new Container();
const initialMemory = process.memoryUsage().heapUsed;

report('Memory. Without namespaces');
report(`Initial memory usage ${format(initialMemory)}`, 1, '-');

times(cycles, (cycle) => {
times(objects, (idx) => {
container.value(`${idx}_${cycle}_value`, {});
container.resolve(`${idx}_${cycle}_value`);
});

report.update(`Allocated ${objects} objects in ${cycle} cycle`, 2);
container.clear();
global.gc();
});

report('Finished. Pause');
setTimeout(() => {
const releasedMemory = process.memoryUsage().heapUsed;

report(`Final released memory usage ${format(releasedMemory)}`, 1);
report(`Initial: ${format(initialMemory)}, final: ${format(releasedMemory)}`, 1);
}, 1000);
File renamed without changes.
23 changes: 23 additions & 0 deletions benchmarks/benchs/utils/reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import times from './times';

function format(message, tabs = 0, tabChar = ' ') {
const output = [];

times(tabs, () => {
output.push(`${tabChar}${tabChar}`);
});

output.push(message);

return output.join(' ');
}

function report(message, tabs = 0, tabChar = ' ') {
console.log(format(message, tabs, tabChar));
}

report.update = (message, tabs, tabChar) => {
console.log(format(message, tabs, tabChar));
};

export default report;
File renamed without changes.
12 changes: 10 additions & 2 deletions benchmarks/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
require('babel-register');
require('./memory');
import memory from './memory';

memory((err) => {
if (err) {
console.error(err);
return;
}

console.log('Done');
});
56 changes: 35 additions & 21 deletions benchmarks/memory.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
import times from './utils/times';
import format from './utils/format-memory';
import Container from '../src/index';
import glob from 'glob';
import path from 'path';
import { spawn } from 'child_process';

const cycles = 100;
const objects = 100000;
const container = new Container();
const initialMemory = process.memoryUsage().heapUsed;
export default function run(done) {
glob(path.join(__dirname, 'benchs/memory-*'), (err, files) => {
if (err) {
return done(err);
}

console.log('Initial memory usage', format(initialMemory));
console.log('Allocating', objects, 'objects in', cycles, 'cycles');
console.log('Found files', files.length);

times(cycles, () => {
times(objects, (idx) => {
container.const(idx.toString(), idx);
});
const next = () => {
if (!files.length) {
return done();
}

const file = files.shift();
const proc = spawn('node', [
'--expose-gc',
'-e',
`require("babel-register"); require("${file}");`
]);

proc.on('error', done);
proc.on('exit', next);

container.clear();
global.gc();
});
proc.stderr.on('data', (data) => {
done(data.toString());
});

console.log('Finished. Pause');
setTimeout(() => {
const releasedMemory = process.memoryUsage().heapUsed;
proc.stdout.on('data', (data) => {
console.log(data.toString());
});

console.log('Final released memory usage', format(releasedMemory));
}, 1000);
return null;
};

return next();
});
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"clean": "rimraf lib",
"build": "npm run test && npm run clean && babel src --out-dir lib && npm run doc",
"prepublish": "npm run build",
"benchmark": "node --expose-gc ./benchmarks/index.js"
"benchmark": "babel-register ./benchmarks/index.js"
},
"license": "MIT",
"keywords": [
Expand Down Expand Up @@ -58,8 +58,10 @@
"eslint-plugin-import": "^1.8.1",
"eslint-plugin-jsx-a11y": "^1.4.2",
"eslint-plugin-mocha": "^3.0.0",
"glob": "^7.1.1",
"istanbul": "^1.0.0-alpha",
"jsdoc": "^3.4.3",
"lodash": "^4.17.2",
"loke-jsdoc-theme": "^2.1.0",
"mocha": "^3.1.2",
"rimraf": "^2.5.4",
Expand Down

0 comments on commit 149ab62

Please sign in to comment.