Files
GDevelop/Extensions/PlatformBehavior/benchmarks/platformerobjectruntimebehavior.benchmark.js
2026-10-09 14:36:50 +02:00

86 lines
2.5 KiB
JavaScript

describe('gdjs.PlatformerObjectRuntimeBehavior Benchmark', function () {
let runtimeScene;
let objects;
const duplicateCount = 60;
const stepCount = 1000;
beforeEach(function () {
runtimeScene = makePlatformerTestRuntimeScene();
objects = new Array(duplicateCount);
for (let i = 0; i < duplicateCount; ++i) {
// Put a platformer object on a platform
object = new gdjs.RuntimeObject(runtimeScene, {
name: 'obj1',
type: '',
behaviors: [
{
type: 'PlatformBehavior::PlatformerObjectBehavior',
name: 'auto1',
gravity: 1500,
maxFallingSpeed: 1500,
acceleration: 500,
deceleration: 1500,
maxSpeed: 500,
jumpSpeed: 900,
canGrabPlatforms: true,
ignoreDefaultControls: true,
slopeMaxAngle: 60,
jumpSustainTime: 0.2,
roundCoordinates: true,
},
],
variables: [],
effects: [],
});
object.getWidth = function () {
return 10;
};
object.getHeight = function () {
return 20;
};
runtimeScene.addObject(object);
object.setPosition(100 * i + 60 * 5, 400 * i - 32);
objects[i] = object;
// Put a platform.
for (let p = 0; p < 10; ++p) {
const platform = addPlatformObject(runtimeScene);
platform.setPosition(100 * i + p * platform.getWidth(), 400 * i - 10);
}
}
});
it('benchmark', function () {
this.timeout(60000);
// Many short batches: the median is less sensitive to a slow batch
// (garbage collection...).
const benchmarkSuite = makeBenchmarkSuite({
benchmarksCount: 60,
iterationsCount: stepCount,
});
// Counted across batches: the objects move the same way whatever the
// batch size.
let frameIndex = 0;
benchmarkSuite.add('platformer objects jumping in loop', () => {
const t = frameIndex++;
for (let i = 0; i < duplicateCount; ++i) {
const object = objects[i];
if (t % 60 == i % 60) {
object.getBehavior('auto1').simulateJumpKey();
}
// Go right then left, to stay on the platforms: the same work is
// measured by every batch.
if ((t + i) % 60 < 30) {
object.getBehavior('auto1').simulateRightKey();
} else {
object.getBehavior('auto1').simulateLeftKey();
}
}
runtimeScene.renderAndStep(1000 / 60);
});
console.log(benchmarkSuite.run());
});
});