web: get rid of mitata; run benchmarks directly

This commit is contained in:
Abdullah Atta
2024-11-13 17:28:52 +05:00
parent 329248ebbd
commit 0a9bfd4b69
4 changed files with 65 additions and 59 deletions

View File

@@ -18,7 +18,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { chromium } from "playwright";
import { bench, run, summary } from "mitata";
import { spawn } from "child_process";
const TESTS = [
@@ -37,6 +36,16 @@ const TESTS = [
start: "load:database",
end: "render:app"
},
{
name: "initialize database",
start: "start:initializeDatabase",
end: "end:initializeDatabase"
},
{
name: "init db",
start: "start:initdb",
end: "end:initdb"
},
{
name: "signup page load",
start: "start:app",
@@ -45,9 +54,12 @@ const TESTS = [
}
];
const serverKillSignal = new AbortController();
async function startServer() {
return new Promise((resolve, reject) => {
const server = spawn("npx", ["serve", "-s", "build"]);
const server = spawn("npx", ["serve", "-s", "build"], {
signal: serverKillSignal.signal
});
server.stdout.on("data", (data) => {
if (data.toString().includes("Accepting connections")) {
@@ -69,10 +81,11 @@ async function startServer() {
const server = await startServer();
const browser = await chromium.launch();
const ITERATIONS = 10;
for (const testCase of TESTS) {
summary(() => {
bench(testCase.name, async function* () {
console.log(`Running ${testCase.name}`);
const durations = [];
for (let i = 0; i < ITERATIONS; i++) {
const context = await browser.newContext({
baseURL: "http://localhost:3000"
});
@@ -91,7 +104,6 @@ for (const testCase of TESTS) {
"${testCase.end}"
).duration
);
window.close();
}
});
});
@@ -99,24 +111,28 @@ for (const testCase of TESTS) {
});
const page = await context.newPage();
yield async () => {
await page.goto(testCase.route || "/");
await page.waitForEvent("console", {
predicate(consoleMessage) {
return consoleMessage.text().startsWith(`ended: ${testCase.name}`);
}
});
};
const result = await page.waitForEvent("console", (msg) =>
msg.text().startsWith(`ended: ${testCase.name}`)
);
durations.push(await result.args()[1].jsonValue());
await context.close();
});
});
}
await run();
const mean = durations.reduce((a, b) => a + b) / durations.length;
const max = Math.max(...durations);
const min = Math.min(...durations);
console.log(
`${testCase.name} took ${mean}ms on average | ${max}ms max | ${min}ms min`
);
}
console.log("done");
await browser.close();
server.stdout.destroy();
server.stderr.destroy();
server.kill();
server.kill("SIGKILL");
console.log(server.killed);
serverKillSignal.abort("finished");

View File

@@ -115,7 +115,6 @@
"happy-dom": "^8.9.0",
"ip": "^1.1.8",
"lorem-ipsum": "^2.0.4",
"mitata": "^1.0.10",
"otplib": "^12.0.1",
"rollup": "^4.18.0",
"rollup-plugin-visualizer": "^5.12.0",
@@ -47298,13 +47297,6 @@
"license": "ISC",
"optional": true
},
"node_modules/mitata": {
"version": "1.0.10",
"resolved": "https://registry.npmjs.org/mitata/-/mitata-1.0.10.tgz",
"integrity": "sha512-pn21sHg5+AiTqj7z7aCeNlkEXMYhAykl1zbGqp1sbTJKRe8lhNokoyubLmvwbY5sWb8B+VDQByn3UyRmdBDQ1w==",
"dev": true,
"license": "MIT"
},
"node_modules/mkdirp": {
"version": "1.0.4",
"license": "MIT",

View File

@@ -113,7 +113,6 @@
"happy-dom": "^8.9.0",
"ip": "^1.1.8",
"lorem-ipsum": "^2.0.4",
"mitata": "^1.0.10",
"otplib": "^12.0.1",
"rollup": "^4.18.0",
"rollup-plugin-visualizer": "^5.12.0",

View File

@@ -31,7 +31,7 @@ import { FileStorage } from "../interfaces/fs";
const db = database;
async function initializeDatabase(persistence: DatabasePersistence) {
logger.measure("Database initialization");
performance.mark("start:initializeDatabase");
let databaseKey = await useKeyStore.getState().getValue("databaseKey");
if (!databaseKey) {
@@ -106,9 +106,9 @@ async function initializeDatabase(persistence: DatabasePersistence) {
// });
// }
console.log("loading db");
performance.mark("start:initdb");
await db.init();
console.log("db loaded");
performance.mark("end:initdb");
window.addEventListener("beforeunload", async () => {
if (IS_DESKTOP_APP) {
@@ -117,14 +117,13 @@ async function initializeDatabase(persistence: DatabasePersistence) {
}
});
logger.measure("Database initialization");
if (db.migrations?.required()) {
await import("../dialogs/migration-dialog").then(({ MigrationDialog }) =>
MigrationDialog.show({})
);
}
performance.mark("end:initializeDatabase");
return db;
}