Skip to content

Commit bedf84c

Browse files
committed
added memory comparison to tests
1 parent cd289ec commit bedf84c

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

test/ExampleWorker.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ const runExample = options => {
3030
const consoleOriginal = global.console;
3131
const logs = [];
3232

33-
global.gc();
34-
3533
mock('matter-js', Matter);
3634
global.Matter = Matter;
3735

@@ -47,6 +45,7 @@ const runExample = options => {
4745
const example = Example[options.name]();
4846
const engine = example.engine;
4947

48+
let totalMemory = 0;
5049
let totalDuration = 0;
5150
let overlapTotal = 0;
5251
let overlapCount = 0;
@@ -64,13 +63,17 @@ const runExample = options => {
6463
}
6564
}
6665

66+
global.gc();
67+
6768
for (let i = 0; i < options.totalUpdates; i += 1) {
6869
const startTime = process.hrtime();
69-
70+
totalMemory += process.memoryUsage().heapUsed;
71+
7072
Matter.Engine.update(engine, 1000 / 60);
7173

7274
const duration = process.hrtime(startTime);
7375
totalDuration += duration[0] * 1e9 + duration[1];
76+
totalMemory += process.memoryUsage().heapUsed;
7477

7578
for (let p = 0; p < engine.pairs.list.length; p += 1) {
7679
const pair = engine.pairs.list[p];
@@ -93,6 +96,7 @@ const runExample = options => {
9396
name: options.name,
9497
duration: totalDuration,
9598
overlap: overlapTotal / (overlapCount || 1),
99+
memory: totalMemory,
96100
logs,
97101
...engineCapture(engine)
98102
};

test/TestTools.js

+19-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ const colors = { Red: 31, Green: 32, Yellow: 33, White: 37, BrightWhite: 90, Bri
3232
const color = (text, number) => number ? `\x1b[${number}m${text}\x1b[0m` : text;
3333
const limit = (val, precision=3) => parseFloat(val.toPrecision(precision));
3434
const toPercent = val => (100 * val).toPrecision(3);
35+
const toPercentRound = val => Math.round(100 * val);
36+
37+
const noiseThreshold = (val, threshold) => {
38+
const sign = val < 0 ? -1 : 1;
39+
const magnitude = Math.abs(val);
40+
return sign * Math.max(0, magnitude - threshold) / (1 - threshold);
41+
};
3542

3643
const engineCapture = (engine) => ({
3744
timestamp: limit(engine.timing.timestamp),
@@ -231,13 +238,19 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
231238
let totalTimeDev = 0;
232239
let totalOverlapBuild = 0;
233240
let totalOverlapDev = 0;
241+
let totalMemoryBuild = 0;
242+
let totalMemoryDev = 0;
234243

235244
const capturePerformance = Object.entries(capturesDev).map(([name]) => {
236245
totalTimeBuild += capturesBuild[name].duration;
237246
totalTimeDev += capturesDev[name].duration;
247+
238248
totalOverlapBuild += capturesBuild[name].overlap;
239249
totalOverlapDev += capturesDev[name].overlap;
240250

251+
totalMemoryBuild += capturesBuild[name].memory;
252+
totalMemoryDev += capturesDev[name].memory;
253+
241254
const changedIntrinsics = !equals(capturesDev[name].intrinsic, capturesBuild[name].intrinsic);
242255
if (changedIntrinsics) {
243256
capturesDev[name].changedIntrinsics = true;
@@ -254,10 +267,8 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
254267
capturePerformance.sort((a, b) => a.name.localeCompare(b.name));
255268
similarityEntries.sort((a, b) => a[1] - b[1]);
256269

257-
let perfChange = 1 - (totalTimeDev / totalTimeBuild);
258-
259-
const perfChangeThreshold = 0.01;
260-
perfChange = Math.abs(perfChange) > perfChangeThreshold ? perfChange : 0;
270+
let perfChange = noiseThreshold(1 - (totalTimeDev / totalTimeBuild), 0.01);
271+
let memoryChange = noiseThreshold((totalMemoryDev / totalMemoryBuild) - 1, 0.01);
261272

262273
let similarityAvg = 0;
263274
similarityEntries.forEach(([_, similarity]) => {
@@ -275,9 +286,11 @@ const comparisonReport = (capturesDev, capturesBuild, buildVersion, save) => {
275286
`\n\n${format('Similarity', colors.White)}`,
276287
`${format(toPercent(similarityAvg), similarityAvg === 1 ? colors.Green : colors.Yellow)}%`,
277288
`${format('Performance', colors.White)}`,
278-
`${format((perfChange >= 0 ? '+' : '') + toPercent(perfChange), perfChange >= 0 ? colors.Green : colors.Red)}%`,
289+
`${format((perfChange >= 0 ? '+' : '-') + toPercentRound(Math.abs(perfChange)), perfChange >= 0 ? colors.Green : colors.Red)}%`,
290+
`${format('Memory', colors.White)}`,
291+
`${format((memoryChange >= 0 ? '+' : '-') + toPercentRound(Math.abs(memoryChange)), memoryChange <= 0 ? colors.Green : colors.Red)}%`,
279292
`${format('Overlap', colors.White)}`,
280-
`${format((overlapChange >= 0 ? '+' : '') + toPercent(overlapChange), overlapChange > 0 ? colors.Red : colors.Green)}%`,
293+
`${format((overlapChange >= 0 ? '+' : '-') + toPercent(Math.abs(overlapChange)), overlapChange <= 0 ? colors.Green : colors.Red)}%`,
281294
capturePerformance.reduce((output, p, i) => {
282295
output += `${p.name} `;
283296
output += `${similarityRatings(similaritys[p.name])} `;

0 commit comments

Comments
 (0)