From 969ec5d7cdffaf86b956c2dfe9e1b25847b0a61b Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Fri, 20 May 2022 13:10:21 +0530 Subject: [PATCH] chore: simple script to create profile log --- backend/database/manager.ts | 14 +++++++------- scripts/profile.sh | 15 +++++++++++++++ scripts/profile.ts | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100755 scripts/profile.sh create mode 100644 scripts/profile.ts diff --git a/backend/database/manager.ts b/backend/database/manager.ts index 2555a99a..772ec8a1 100644 --- a/backend/database/manager.ts +++ b/backend/database/manager.ts @@ -1,3 +1,4 @@ +import { constants } from 'fs'; import fs from 'fs/promises'; import { DatabaseDemuxBase, DatabaseMethod } from 'utils/db/types'; import { getSchemas } from '../../schemas'; @@ -105,14 +106,13 @@ export class DatabaseManager extends DatabaseDemuxBase { } async #unlinkIfExists(dbPath: string) { - try { - fs.unlink(dbPath); - } catch (err) { - if ((err as NodeJS.ErrnoException).code === 'ENOENT') { - return; - } + const exists = await fs + .access(dbPath, constants.W_OK) + .then(() => true) + .catch(() => false); - throw err; + if (exists) { + fs.unlink(dbPath); } } diff --git a/scripts/profile.sh b/scripts/profile.sh new file mode 100755 index 00000000..78f7fe6d --- /dev/null +++ b/scripts/profile.sh @@ -0,0 +1,15 @@ +#! /bin/sh + +# https://nodejs.org/en/docs/guides/simple-profiling/ + +export TS_NODE_COMPILER_OPTIONS='{"module":"commonjs"}' + +rm ./isolate-*-v8.log 2> /dev/null +rm ./profiler-output.log 2> /dev/null + +echo "running profile.ts" +node --require ts-node/register --require tsconfig-paths/register --prof ./scripts/profile.ts + +echo "processing tick file" +node --prof-process ./isolate-*-v8.log > ./profiler-output.log && echo "generated profiler-output.log" +rm ./isolate-*-v8.log \ No newline at end of file diff --git a/scripts/profile.ts b/scripts/profile.ts new file mode 100644 index 00000000..34c4557e --- /dev/null +++ b/scripts/profile.ts @@ -0,0 +1,22 @@ +import { DatabaseManager } from 'backend/database/manager'; +import { setupDummyInstance } from 'dummy'; +import { unlink } from 'fs/promises'; +import { Fyo } from 'fyo'; +import { DummyAuthDemux } from 'fyo/tests/helpers'; +import { getTestDbPath } from 'tests/helpers'; + +async function run() { + const fyo = new Fyo({ + DatabaseDemux: DatabaseManager, + AuthDemux: DummyAuthDemux, + isTest: true, + isElectron: false, + }); + const dbPath = getTestDbPath(); + + await setupDummyInstance(dbPath, fyo, 1, 100); + await fyo.close(); + await unlink(dbPath); +} + +run();