0
0
Fork 0
mirror of https://github.com/liabru/matter-js.git synced 2024-11-27 09:50:52 -05:00

added overlap metric to test tools

This commit is contained in:
liabru 2020-03-09 20:56:06 +00:00
parent fcdb4fad55
commit 3fd674d8f4
3 changed files with 50 additions and 12 deletions

View file

@ -38,13 +38,42 @@ const runExample = options => {
const example = Example[options.name]();
const engine = example.engine;
const startTime = process.hrtime();
let totalDuration = 0;
let overlapTotal = 0;
let overlapCount = 0;
for (let i = 0; i < options.totalUpdates; i += 1) {
Matter.Engine.update(engine, 1000 / 60);
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
});
}
}
const duration = process.hrtime(startTime);
for (let i = 0; i < options.totalUpdates; i += 1) {
const startTime = process.hrtime();
Matter.Engine.update(engine, 1000 / 60);
const duration = process.hrtime(startTime);
totalDuration += duration[0] * 1e9 + duration[1];
for (let p = 0; p < engine.pairs.list.length; p += 1) {
const pair = engine.pairs.list[p];
const separation = pair.separation - pair.slop;
if (pair.isActive && !pair.isSensor) {
overlapTotal += separation > 0 ? separation : 0;
overlapCount += 1;
}
}
}
global.console = consoleOriginal;
global.document = undefined;
@ -53,7 +82,8 @@ const runExample = options => {
return {
name: options.name,
duration: duration[0] * 1e9 + duration[1],
duration: totalDuration,
overlap: overlapTotal / (overlapCount || 1),
...engineCapture(engine)
};
};

View file

@ -11,7 +11,8 @@ const Worker = require('jest-worker').default;
const testComparison = process.env.COMPARE === 'true';
const saveComparison = process.env.SAVE === 'true';
const excludeExamples = [ 'svg', 'terrain' ];
const excludeExamples = ['svg', 'terrain'];
const excludeJitter = ['stack', 'circleStack', 'restitution', 'staticFriction', 'friction', 'newtonsCradle', 'catapult'];
const examples = Object.keys(Example).filter(key => !excludeExamples.includes(key));
const runExamples = async useDev => {
@ -22,7 +23,8 @@ const runExamples = async useDev => {
const result = await Promise.all(examples.map(name => worker.runExample({
name,
useDev,
totalUpdates: 120
totalUpdates: 120,
jitter: excludeJitter.includes(name) ? 0 : 1e-10
})));
await worker.end();

View file

@ -203,12 +203,14 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
let intrinsicChangeCount = 0;
let totalTimeBuild = 0;
let totalTimeDev = 0;
let totalOverlapBuild = 0;
let totalOverlapDev = 0;
const capturePerformance = Object.entries(capturesDev).map(([name]) => {
const buildDuration = capturesBuild[name].duration;
const devDuration = capturesDev[name].duration;
totalTimeBuild += buildDuration;
totalTimeDev += devDuration;
totalTimeBuild += capturesBuild[name].duration;
totalTimeDev += capturesDev[name].duration;
totalOverlapBuild += capturesBuild[name].overlap;
totalOverlapDev += capturesDev[name].overlap;
const changedIntrinsics = !equals(capturesDev[name].intrinsic, capturesBuild[name].intrinsic);
if (changedIntrinsics) {
@ -226,16 +228,18 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
capturePerformance.sort((a, b) => a.name.localeCompare(b.name));
similarityEntries.sort((a, b) => a[1] - b[1]);
let similarityAvg = 0;
let perfChange = 1 - (totalTimeDev / totalTimeBuild);
perfChange = perfChange < -0.05 || perfChange > 0.05 ? perfChange : 0;
let similarityAvg = 0;
similarityEntries.forEach(([_, similarity]) => {
similarityAvg += similarity;
});
similarityAvg /= similarityEntries.length;
const overlapChange = (totalOverlapDev / (totalOverlapBuild || 1)) - 1;
if (save) {
writeCaptures('examples-dev', devIntrinsicsChanged);
writeCaptures('examples-build', buildIntrinsicsChanged);
@ -249,6 +253,8 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
`${color(toPercent(similarityAvg), similarityAvg === 1 ? colors.Green : colors.Yellow)}%`,
`${color('Performance', colors.White)}`,
`${color((perfChange >= 0 ? '+' : '') + toPercent(perfChange), perfChange >= 0 ? colors.Green : colors.Red)}%`,
`${color('Overlap', colors.White)}`,
`${color((overlapChange >= 0 ? '+' : '') + toPercent(overlapChange), overlapChange > 0 ? colors.Red : colors.Green)}%`,
capturePerformance.reduce((output, p, i) => {
output += `${p.name} `;
output += `${similarityRatings(similaritys[p.name])} `;