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:
parent
fcdb4fad55
commit
3fd674d8f4
3 changed files with 50 additions and 12 deletions
|
@ -38,13 +38,42 @@ const runExample = options => {
|
||||||
|
|
||||||
const example = Example[options.name]();
|
const example = Example[options.name]();
|
||||||
const engine = example.engine;
|
const engine = example.engine;
|
||||||
const startTime = process.hrtime();
|
|
||||||
|
|
||||||
for (let i = 0; i < options.totalUpdates; i += 1) {
|
let totalDuration = 0;
|
||||||
Matter.Engine.update(engine, 1000 / 60);
|
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
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < options.totalUpdates; i += 1) {
|
||||||
|
const startTime = process.hrtime();
|
||||||
|
|
||||||
|
Matter.Engine.update(engine, 1000 / 60);
|
||||||
|
|
||||||
const duration = process.hrtime(startTime);
|
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.console = consoleOriginal;
|
||||||
global.document = undefined;
|
global.document = undefined;
|
||||||
|
@ -53,7 +82,8 @@ const runExample = options => {
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: options.name,
|
name: options.name,
|
||||||
duration: duration[0] * 1e9 + duration[1],
|
duration: totalDuration,
|
||||||
|
overlap: overlapTotal / (overlapCount || 1),
|
||||||
...engineCapture(engine)
|
...engineCapture(engine)
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,7 +11,8 @@ const Worker = require('jest-worker').default;
|
||||||
|
|
||||||
const testComparison = process.env.COMPARE === 'true';
|
const testComparison = process.env.COMPARE === 'true';
|
||||||
const saveComparison = process.env.SAVE === '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 examples = Object.keys(Example).filter(key => !excludeExamples.includes(key));
|
||||||
|
|
||||||
const runExamples = async useDev => {
|
const runExamples = async useDev => {
|
||||||
|
@ -22,7 +23,8 @@ const runExamples = async useDev => {
|
||||||
const result = await Promise.all(examples.map(name => worker.runExample({
|
const result = await Promise.all(examples.map(name => worker.runExample({
|
||||||
name,
|
name,
|
||||||
useDev,
|
useDev,
|
||||||
totalUpdates: 120
|
totalUpdates: 120,
|
||||||
|
jitter: excludeJitter.includes(name) ? 0 : 1e-10
|
||||||
})));
|
})));
|
||||||
|
|
||||||
await worker.end();
|
await worker.end();
|
||||||
|
|
|
@ -203,12 +203,14 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
|
||||||
let intrinsicChangeCount = 0;
|
let intrinsicChangeCount = 0;
|
||||||
let totalTimeBuild = 0;
|
let totalTimeBuild = 0;
|
||||||
let totalTimeDev = 0;
|
let totalTimeDev = 0;
|
||||||
|
let totalOverlapBuild = 0;
|
||||||
|
let totalOverlapDev = 0;
|
||||||
|
|
||||||
const capturePerformance = Object.entries(capturesDev).map(([name]) => {
|
const capturePerformance = Object.entries(capturesDev).map(([name]) => {
|
||||||
const buildDuration = capturesBuild[name].duration;
|
totalTimeBuild += capturesBuild[name].duration;
|
||||||
const devDuration = capturesDev[name].duration;
|
totalTimeDev += capturesDev[name].duration;
|
||||||
totalTimeBuild += buildDuration;
|
totalOverlapBuild += capturesBuild[name].overlap;
|
||||||
totalTimeDev += devDuration;
|
totalOverlapDev += capturesDev[name].overlap;
|
||||||
|
|
||||||
const changedIntrinsics = !equals(capturesDev[name].intrinsic, capturesBuild[name].intrinsic);
|
const changedIntrinsics = !equals(capturesDev[name].intrinsic, capturesBuild[name].intrinsic);
|
||||||
if (changedIntrinsics) {
|
if (changedIntrinsics) {
|
||||||
|
@ -226,16 +228,18 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
|
||||||
capturePerformance.sort((a, b) => a.name.localeCompare(b.name));
|
capturePerformance.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
similarityEntries.sort((a, b) => a[1] - b[1]);
|
similarityEntries.sort((a, b) => a[1] - b[1]);
|
||||||
|
|
||||||
let similarityAvg = 0;
|
|
||||||
let perfChange = 1 - (totalTimeDev / totalTimeBuild);
|
let perfChange = 1 - (totalTimeDev / totalTimeBuild);
|
||||||
perfChange = perfChange < -0.05 || perfChange > 0.05 ? perfChange : 0;
|
perfChange = perfChange < -0.05 || perfChange > 0.05 ? perfChange : 0;
|
||||||
|
|
||||||
|
let similarityAvg = 0;
|
||||||
similarityEntries.forEach(([_, similarity]) => {
|
similarityEntries.forEach(([_, similarity]) => {
|
||||||
similarityAvg += similarity;
|
similarityAvg += similarity;
|
||||||
});
|
});
|
||||||
|
|
||||||
similarityAvg /= similarityEntries.length;
|
similarityAvg /= similarityEntries.length;
|
||||||
|
|
||||||
|
const overlapChange = (totalOverlapDev / (totalOverlapBuild || 1)) - 1;
|
||||||
|
|
||||||
if (save) {
|
if (save) {
|
||||||
writeCaptures('examples-dev', devIntrinsicsChanged);
|
writeCaptures('examples-dev', devIntrinsicsChanged);
|
||||||
writeCaptures('examples-build', buildIntrinsicsChanged);
|
writeCaptures('examples-build', buildIntrinsicsChanged);
|
||||||
|
@ -249,6 +253,8 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
|
||||||
`${color(toPercent(similarityAvg), similarityAvg === 1 ? colors.Green : colors.Yellow)}%`,
|
`${color(toPercent(similarityAvg), similarityAvg === 1 ? colors.Green : colors.Yellow)}%`,
|
||||||
`${color('Performance', colors.White)}`,
|
`${color('Performance', colors.White)}`,
|
||||||
`${color((perfChange >= 0 ? '+' : '') + toPercent(perfChange), perfChange >= 0 ? colors.Green : colors.Red)}%`,
|
`${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) => {
|
capturePerformance.reduce((output, p, i) => {
|
||||||
output += `${p.name} `;
|
output += `${p.name} `;
|
||||||
output += `${similarityRatings(similaritys[p.name])} `;
|
output += `${similarityRatings(similaritys[p.name])} `;
|
||||||
|
|
Loading…
Reference in a new issue