mirror of
https://github.com/Heretek-AI/GDevelop.git
synced 2026-07-21 09:35:27 -04:00
a996c5413f
This makes autosave 70% faster, saving a project 30% faster, preview slighlty faster. - Switch to RapidJSON for serialization inside the Core. - Avoid extra pair of JSON.parse/JSON.stringify for autosaves - Fix float used instead of double in the SerializerElement/Value JS bindings
56 lines
1.6 KiB
JavaScript
56 lines
1.6 KiB
JavaScript
const { performance } = require('perf_hooks');
|
|
|
|
/**
|
|
* Helper allowing to run a benchmark of the time spent the execute a certain
|
|
* number of iterations of one or more functions.
|
|
*
|
|
* Note that this could surely be replaced by a more robust solution like
|
|
* Benchmark.js
|
|
*
|
|
* @param {{benchmarksCount?: number, iterationsCount?: number}} options
|
|
*/
|
|
let makeBenchmarkSuite = (options = {}) => {
|
|
const benchmarkTimings = {};
|
|
const benchmarksCount = options.benchmarksCount || 1000;
|
|
const iterationsCount = options.iterationsCount || 100000;
|
|
const testCases = [];
|
|
|
|
const suite = {};
|
|
/**
|
|
* @param {string} title
|
|
* @param {(i: number) => void} fn
|
|
*/
|
|
suite.add = (title, fn) => {
|
|
testCases.push({ title, fn });
|
|
return suite;
|
|
};
|
|
suite.run = () => {
|
|
for (
|
|
let benchmarkIndex = 0;
|
|
benchmarkIndex < benchmarksCount;
|
|
benchmarkIndex++
|
|
) {
|
|
testCases.forEach((testCase) => {
|
|
const description = testCase.title + '(' + iterationsCount + 'x)';
|
|
const start = performance.now();
|
|
for (let i = 0; i < iterationsCount; i++) {
|
|
testCase.fn(i);
|
|
}
|
|
benchmarkTimings[description] = benchmarkTimings[description] || [];
|
|
benchmarkTimings[description].push(performance.now() - start);
|
|
});
|
|
}
|
|
|
|
const results = {};
|
|
for (let benchmarkName in benchmarkTimings) {
|
|
results[benchmarkName] =
|
|
benchmarkTimings[benchmarkName].reduce((sum, value) => sum + value, 0) /
|
|
benchmarksCount;
|
|
}
|
|
return results;
|
|
};
|
|
return suite;
|
|
};
|
|
|
|
module.exports = { makeBenchmarkSuite };
|