2019-11-07 17:34:59 -05:00
|
|
|
/* eslint-env es6 */
|
|
|
|
/* eslint no-global-assign: 0 */
|
|
|
|
"use strict";
|
|
|
|
|
2021-01-31 12:32:47 -05:00
|
|
|
const mock = require('mock-require');
|
2021-11-28 16:25:11 -05:00
|
|
|
const { requireUncached, engineCapture } = require('./TestTools');
|
2019-11-07 17:34:59 -05:00
|
|
|
const Example = require('../examples/index');
|
2021-11-28 16:25:11 -05:00
|
|
|
const consoleOriginal = global.console;
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-28 16:25:11 -05:00
|
|
|
const prepareMatter = (options) => {
|
|
|
|
const Matter = requireUncached(options.useDev ? '../build/matter.dev' : '../build/matter');
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-28 16:25:11 -05:00
|
|
|
if (Matter.Common._nextId !== 0) {
|
|
|
|
throw 'Matter instance has already been used.';
|
|
|
|
}
|
|
|
|
|
|
|
|
const noop = () => ({ collisionFilter: {}, mouse: {} });
|
|
|
|
|
|
|
|
Matter.Render.create = () => ({ options: {}, bounds: { min: { x: 0, y: 0 }, max: { x: 800, y: 600 }}});
|
|
|
|
Matter.Render.run = Matter.Render.lookAt = noop;
|
|
|
|
Matter.Runner.create = Matter.Runner.run = noop;
|
|
|
|
Matter.MouseConstraint.create = Matter.Mouse.create = noop;
|
|
|
|
Matter.Common.info = Matter.Common.warn = Matter.Common.log;
|
|
|
|
|
|
|
|
return Matter;
|
|
|
|
};
|
|
|
|
|
|
|
|
const prepareEnvironment = Matter => {
|
2021-01-31 12:32:47 -05:00
|
|
|
mock('matter-js', Matter);
|
2019-11-07 17:34:59 -05:00
|
|
|
global.Matter = Matter;
|
2021-01-31 12:32:47 -05:00
|
|
|
|
2021-11-28 16:25:11 -05:00
|
|
|
const logs = [];
|
2021-01-31 12:32:47 -05:00
|
|
|
global.document = global.window = { addEventListener: () => {} };
|
2021-01-28 19:09:27 -05:00
|
|
|
global.console = {
|
|
|
|
log: (...args) => {
|
|
|
|
logs.push(args.join(' '));
|
|
|
|
}
|
|
|
|
};
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-28 16:25:11 -05:00
|
|
|
return logs;
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetEnvironment = () => {
|
|
|
|
global.console = consoleOriginal;
|
|
|
|
global.window = undefined;
|
|
|
|
global.document = undefined;
|
|
|
|
global.Matter = undefined;
|
|
|
|
mock.stopAll();
|
|
|
|
};
|
|
|
|
|
|
|
|
const runExample = options => {
|
|
|
|
const Matter = prepareMatter(options);
|
|
|
|
const logs = prepareEnvironment(Matter);
|
2019-11-07 17:34:59 -05:00
|
|
|
|
|
|
|
const example = Example[options.name]();
|
|
|
|
const engine = example.engine;
|
2020-03-09 16:56:06 -04:00
|
|
|
|
2021-11-20 07:27:14 -05:00
|
|
|
let totalMemory = 0;
|
2020-03-09 16:56:06 -04:00
|
|
|
let totalDuration = 0;
|
|
|
|
let overlapTotal = 0;
|
|
|
|
let overlapCount = 0;
|
|
|
|
|
|
|
|
const bodies = Matter.Composite.allBodies(engine.world);
|
|
|
|
|
|
|
|
if (options.jitter) {
|
|
|
|
for (let i = 0; i < bodies.length; i += 1) {
|
|
|
|
const body = bodies[i];
|
|
|
|
|
|
|
|
Matter.Body.applyForce(body, body.position, {
|
|
|
|
x: Math.cos(i * i) * options.jitter * body.mass,
|
|
|
|
y: Math.sin(i * i) * options.jitter * body.mass
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-20 07:27:14 -05:00
|
|
|
global.gc();
|
|
|
|
|
2019-11-07 17:34:59 -05:00
|
|
|
for (let i = 0; i < options.totalUpdates; i += 1) {
|
2020-03-09 16:56:06 -04:00
|
|
|
const startTime = process.hrtime();
|
2021-11-20 07:27:14 -05:00
|
|
|
totalMemory += process.memoryUsage().heapUsed;
|
|
|
|
|
2019-11-07 17:34:59 -05:00
|
|
|
Matter.Engine.update(engine, 1000 / 60);
|
|
|
|
|
2020-03-09 16:56:06 -04:00
|
|
|
const duration = process.hrtime(startTime);
|
|
|
|
totalDuration += duration[0] * 1e9 + duration[1];
|
2021-11-20 07:27:14 -05:00
|
|
|
totalMemory += process.memoryUsage().heapUsed;
|
2020-03-09 16:56:06 -04:00
|
|
|
|
2021-11-21 12:00:53 -05:00
|
|
|
const pairsList = engine.pairs.list;
|
|
|
|
const pairsListLength = engine.pairs.list.length;
|
|
|
|
|
|
|
|
for (let p = 0; p < pairsListLength; p += 1) {
|
|
|
|
const pair = pairsList[p];
|
2020-03-09 16:56:06 -04:00
|
|
|
const separation = pair.separation - pair.slop;
|
|
|
|
|
|
|
|
if (pair.isActive && !pair.isSensor) {
|
|
|
|
overlapTotal += separation > 0 ? separation : 0;
|
|
|
|
overlapCount += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 17:34:59 -05:00
|
|
|
|
2021-11-28 16:25:11 -05:00
|
|
|
resetEnvironment();
|
2019-11-07 17:34:59 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
name: options.name,
|
2020-03-09 16:56:06 -04:00
|
|
|
duration: totalDuration,
|
|
|
|
overlap: overlapTotal / (overlapCount || 1),
|
2021-11-20 07:27:14 -05:00
|
|
|
memory: totalMemory,
|
2021-01-28 19:09:27 -05:00
|
|
|
logs,
|
2019-11-07 17:34:59 -05:00
|
|
|
...engineCapture(engine)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { runExample };
|