From 74178596776e01889d9734b9c2ebf7b8a8e8dc57 Mon Sep 17 00:00:00 2001 From: vinu-deriv Date: Wed, 5 Jul 2023 15:45:53 +0400 Subject: [PATCH 1/4] added code for performance mark in binary --- src/botPage/view/deriv/api-middleware.js | 76 +++++++++++++++++++ src/botPage/view/deriv/api.js | 2 + .../view/deriv/layout/ToolBox/ToolBox.jsx | 6 +- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/botPage/view/deriv/api-middleware.js diff --git a/src/botPage/view/deriv/api-middleware.js b/src/botPage/view/deriv/api-middleware.js new file mode 100644 index 000000000..961aef7ab --- /dev/null +++ b/src/botPage/view/deriv/api-middleware.js @@ -0,0 +1,76 @@ +export const REQUESTS = [ + 'active_symbols', + 'authorize', + 'balance', + 'buy', + 'proposal', + 'proposal_open_contract', + 'run-proposal', + 'transaction', + 'ticks_history', +]; + +class APIMiddleware { + constructor(config) { + this.config = config; + this.debounced_calls = {}; + this.addGlobalMethod(); + } + + getRequestType = request => { + let req_type; + REQUESTS.forEach(type => { + if (type in request && !req_type) req_type = type; + }); + + return req_type; + }; + + defineMeasure = res_type => { + if (res_type) { + let measure; + if (res_type === 'proposal') { + performance.mark('first_proposal_end'); + if (performance.getEntriesByName('bot-start', 'mark').length) { + measure = performance.measure('run-proposal', 'bot-start', 'first_proposal_end'); + console.log('run-proposal', measure); + performance.clearMarks('bot-start'); + } + } + if (res_type === 'history') { + performance.mark('ticks_history_end'); + measure = performance.measure('ticks_history', 'ticks_history_start', 'ticks_history_end'); + } else { + performance.mark(`${res_type}_end`); + measure = performance.measure(`${res_type}`, `${res_type}_start`, `${res_type}_end`); + } + return (measure.startTimeDate = new Date(Date.now() - measure.startTime)); + } + return false; + }; + + sendIsCalled = ({ response_promise, args: [request] }) => { + const req_type = this.getRequestType(request); + if (req_type) performance.mark(`${req_type}_start`); + response_promise.then(res => { + const res_type = this.getRequestType(res); + if (res_type) { + this.defineMeasure(res_type); + } + }); + return response_promise; + }; + + sendRequestsStatistic = () => { + // REQUESTS.forEach(req_type => { + // const measure = performance.getEntriesByName(req_type); + // }); + performance.clearMeasures(); + }; + + addGlobalMethod() { + if (window) window.sendRequestsStatistic = this.sendRequestsStatistic; + } +} + +export default APIMiddleware; diff --git a/src/botPage/view/deriv/api.js b/src/botPage/view/deriv/api.js index 8ff8a89bd..966f7a4dd 100644 --- a/src/botPage/view/deriv/api.js +++ b/src/botPage/view/deriv/api.js @@ -2,6 +2,7 @@ import DerivAPIBasic from '@deriv/deriv-api/dist/DerivAPIBasic'; import AppIdMap from '../../../common/appIdResolver'; import { supported_languages } from '../../../common/i18n'; import { setCookieLanguage } from '../../../common/utils/cookieManager'; +import APIMiddleware from './api-middleware'; // [Todo] getStorage, setStorage are duplicated here after update the structure of project we should remove them @@ -82,6 +83,7 @@ const socket_url = `wss://${getServerAddressFallback()}/websockets/v3?app_id=${g // and once the network is back we need to create a new api instance. const api = new DerivAPIBasic({ connection: new WebSocket(socket_url), + middleware: new APIMiddleware({}), }); export default api; diff --git a/src/botPage/view/deriv/layout/ToolBox/ToolBox.jsx b/src/botPage/view/deriv/layout/ToolBox/ToolBox.jsx index e61d18287..c4adcb214 100644 --- a/src/botPage/view/deriv/layout/ToolBox/ToolBox.jsx +++ b/src/botPage/view/deriv/layout/ToolBox/ToolBox.jsx @@ -203,7 +203,11 @@ const ToolBox = ({ blockly, is_workspace_rendered }) => { id_container="runButton" tooltip={translate("Run the bot")} position="bottom" - onClick={() => globalObserver.emit("blockly.start")} + onClick={() => { + globalObserver.emit("blockly.start") + performance.mark('bot-start'); + console.log('Bot started') + }} classes={classNames("toolbox-button icon-run", { "toolbox-hide": is_loading_balance || !is_workspace_rendered })} is_bot_running={is_bot_running} /> From 2210d396388dd4c8248a2f8bb5b2f98d43ac6da6 Mon Sep 17 00:00:00 2001 From: vinu-deriv Date: Tue, 29 Aug 2023 10:17:34 +0400 Subject: [PATCH 2/4] fix: added console log for performance --- src/botPage/view/deriv/api-middleware.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/botPage/view/deriv/api-middleware.js b/src/botPage/view/deriv/api-middleware.js index 79b393001..b3e8376dc 100644 --- a/src/botPage/view/deriv/api-middleware.js +++ b/src/botPage/view/deriv/api-middleware.js @@ -33,16 +33,20 @@ class APIMiddleware { performance.mark('first_proposal_end'); if (performance.getEntriesByName('bot-start', 'mark').length) { measure = performance.measure('run-proposal', 'bot-start', 'first_proposal_end'); - performance.clearMarks('bot-start'); + console.table('bot-first-run', measure.duration) } } if (res_type === 'history') { performance.mark('ticks_history_end'); measure = performance.measure('ticks_history', 'ticks_history_start', 'ticks_history_end'); + console.table('ticks_history', measure.duration) } else { performance.mark(`${res_type}_end`); measure = performance.measure(`${res_type}`, `${res_type}_start`, `${res_type}_end`); + if (res_type === 'proposal') { + console.table('proposal', measure.duration) + } } return (measure.startTimeDate = new Date(Date.now() - measure.startTime)); } From 3531bda9a6a2f45e6cf1c3ea39d49375e997bed7 Mon Sep 17 00:00:00 2001 From: vinu-deriv Date: Tue, 29 Aug 2023 15:37:19 +0400 Subject: [PATCH 3/4] fix: added ticks-history to performance mark --- src/botPage/view/deriv/api-middleware.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/botPage/view/deriv/api-middleware.js b/src/botPage/view/deriv/api-middleware.js index b3e8376dc..00d7b4cc4 100644 --- a/src/botPage/view/deriv/api-middleware.js +++ b/src/botPage/view/deriv/api-middleware.js @@ -8,6 +8,7 @@ export const REQUESTS = [ 'run-proposal', 'transaction', 'ticks_history', + 'history' ]; class APIMiddleware { From fdcb436e140ad70ed586ce619d0308d025632e7c Mon Sep 17 00:00:00 2001 From: vinu-deriv Date: Mon, 1 Apr 2024 10:33:54 +0400 Subject: [PATCH 4/4] fix: removed duplicate imports --- src/api-base.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/api-base.js b/src/api-base.js index bcd1845ff..6fa2ee9bb 100644 --- a/src/api-base.js +++ b/src/api-base.js @@ -10,7 +10,6 @@ import { import DerivAPIBasic from '@deriv/deriv-api/dist/DerivAPIBasic'; import APIMiddleware from './api-middleware'; import { observer as globalObserver } from '@utilities/observer'; -import APIMiddleware from './api-middleware'; const socket_url = `wss://${getServerAddressFallback()}/websockets/v3?app_id=${getAppIdFallback()}&l=${getLanguage().toUpperCase()}&brand=deriv`;