0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2025-01-21 17:14:38 -05:00
liabru-matter-js/test/ExampleWorker.js

108 lines
2.9 KiB
JavaScript
Raw Normal View History

/* eslint-env es6 */
/* eslint no-global-assign: 0 */
"use strict";
const stubBrowserFeatures = M => {
const noop = () => ({ collisionFilter: {}, mouse: {} });
M.Render.create = () => ({ options: {}, bounds: { min: { x: 0, y: 0 }, max: { x: 800, y: 600 }}});
M.Render.run = M.Render.lookAt = noop;
M.Runner.create = M.Runner.run = noop;
M.MouseConstraint.create = M.Mouse.create = noop;
2021-01-29 00:09:27 +00:00
M.Common.info = M.Common.warn = M.Common.log;
return M;
};
const reset = M => {
M.Common._nextId = M.Common._seed = 0;
M.Body._nextCollidingGroupId = 1;
M.Body._nextNonCollidingGroupId = -1;
M.Body._nextCategory = 0x0001;
};
2021-01-31 17:32:47 +00:00
const mock = require('mock-require');
const { engineCapture } = require('./TestTools');
const MatterDev = stubBrowserFeatures(require('../build/matter.dev'));
2020-01-01 14:07:22 +00:00
const MatterBuild = stubBrowserFeatures(require('../build/matter'));
const Example = require('../examples/index');
const runExample = options => {
const Matter = options.useDev ? MatterDev : MatterBuild;
const consoleOriginal = global.console;
2021-01-29 00:09:27 +00:00
const logs = [];
2021-01-31 17:32:47 +00:00
mock('matter-js', Matter);
global.Matter = Matter;
2021-01-31 17:32:47 +00:00
global.document = global.window = { addEventListener: () => {} };
2021-01-29 00:09:27 +00:00
global.console = {
log: (...args) => {
logs.push(args.join(' '));
}
};
reset(Matter);
const example = Example[options.name]();
const engine = example.engine;
2020-03-09 20:56:06 +00:00
2021-11-20 12:27:14 +00:00
let totalMemory = 0;
2020-03-09 20:56:06 +00: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
});
}
}
2021-11-20 12:27:14 +00:00
global.gc();
for (let i = 0; i < options.totalUpdates; i += 1) {
2020-03-09 20:56:06 +00:00
const startTime = process.hrtime();
2021-11-20 12:27:14 +00:00
totalMemory += process.memoryUsage().heapUsed;
Matter.Engine.update(engine, 1000 / 60);
2020-03-09 20:56:06 +00:00
const duration = process.hrtime(startTime);
totalDuration += duration[0] * 1e9 + duration[1];
2021-11-20 12:27:14 +00:00
totalMemory += process.memoryUsage().heapUsed;
2020-03-09 20:56:06 +00: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 20:56:06 +00:00
const separation = pair.separation - pair.slop;
if (pair.isActive && !pair.isSensor) {
overlapTotal += separation > 0 ? separation : 0;
overlapCount += 1;
}
}
}
global.console = consoleOriginal;
global.window = undefined;
global.document = undefined;
global.Matter = undefined;
2021-01-31 17:32:47 +00:00
mock.stopAll();
return {
name: options.name,
2020-03-09 20:56:06 +00:00
duration: totalDuration,
overlap: overlapTotal / (overlapCount || 1),
2021-11-20 12:27:14 +00:00
memory: totalMemory,
2021-01-29 00:09:27 +00:00
logs,
...engineCapture(engine)
};
};
module.exports = { runExample };