From 8d3121be52493440906c9788a90c69e4c8d05789 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Thu, 16 Mar 2023 23:11:29 +0330 Subject: [PATCH 1/2] feat(bench): improve bench mark logs --- demo/es-bench/bench.ts | 24 +++++++++++++----------- demo/es-bench/for-in-vs-of.ts | 7 ++++--- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/demo/es-bench/bench.ts b/demo/es-bench/bench.ts index b87bb566c..70d1564a0 100644 --- a/demo/es-bench/bench.ts +++ b/demo/es-bench/bench.ts @@ -1,12 +1,14 @@ -import {delay} from '@alwatr/util'; - -import type {MaybePromise} from '@alwatr/type'; - -export async function bench(name: string, func: () => MaybePromise): Promise { - await delay(1_000); - console.time(name); - for (let i = 100_000; i; i--) { - await func(); +export const bench = (name: string, func: () => void): void => { + globalThis.gc?.(); + const startMemory = process.memoryUsage.rss(); + const startTime = performance.now(); + for (let i = 1_000_000; i; i--) { + func(); } - console.timeEnd(name); -} + const duration = performance.now() - startTime; + const runPerSec = Math.round( 1_000_000 / duration * 1000); + const memoryUsage = Math.round((process.memoryUsage.rss() - startMemory) / 10) / 100; + + console.log(`run ${name} ${runPerSec.toLocaleString()}/s with ${memoryUsage.toLocaleString()}kb`); + globalThis.gc?.(); +}; diff --git a/demo/es-bench/for-in-vs-of.ts b/demo/es-bench/for-in-vs-of.ts index 778f94927..0527887d3 100644 --- a/demo/es-bench/for-in-vs-of.ts +++ b/demo/es-bench/for-in-vs-of.ts @@ -40,9 +40,10 @@ function test_for_of_keys(): void { } prepare(); -await bench('for-in', test_for_in); -await bench('for-of-values', test_for_of_values); -await bench('for-of-keys', test_for_of_keys); + +bench('for-of-values', test_for_of_values); +bench('for-of-keys', test_for_of_keys); +bench('for-in', test_for_in); globalThis.document?.body.append(' Done. Check the console.'); From 8ed98d8764834431c6806545660ba3a77eadc9c6 Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Thu, 16 Mar 2023 23:11:56 +0330 Subject: [PATCH 2/2] feat(bench): test logger benchmark impact --- demo/es-bench/logger.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 demo/es-bench/logger.ts diff --git a/demo/es-bench/logger.ts b/demo/es-bench/logger.ts new file mode 100644 index 000000000..f0c5c26e6 --- /dev/null +++ b/demo/es-bench/logger.ts @@ -0,0 +1,41 @@ +/* eslint-disable camelcase */ + +import {createLogger} from '@alwatr/logger'; +// import {delay} from '@alwatr/util'; + +import {bench} from './bench.js'; + +const logger = createLogger('logger-bench', null, false); + +const getUserList = (): Record> => { + const userList: Record> = {}; + for (let i = 10; i; i--) { + const userId = 'user_' + i; + userList[userId] = { + user: userId, + fname: 'ali', + lname: 'md', + email: 'i@ali.md', + token: '1234abcd', + }; + } + return userList; +}; + +function test_without_logger(obj: Record): number { + return Object.values(obj).length; +} + +function test_with_logger(obj: Record): number { + logger.logMethodArgs('test_with_logger', obj); + return Object.values(obj).length; +} + +bench('test_with_logger 1st', () => test_with_logger(getUserList())); +bench('test_without_logger 1st', () => test_without_logger(getUserList())); + +bench('test_with_logger 2nd', () => test_with_logger(getUserList())); +bench('test_without_logger 2nd', () => test_without_logger(getUserList())); + + +globalThis.document?.body.append(' Done. Check the console.');