From 15627154345474aafe58dafe03e9e2acf003ad84 Mon Sep 17 00:00:00 2001 From: Emanuele Feliziani Date: Wed, 23 Aug 2023 16:44:12 +0200 Subject: [PATCH] Add performance logging capability Signed-off-by: Emanuele Feliziani # Conflicts: # src/Scanner.js --- src/Scanner.js | 6 +++++- src/autofill-utils.js | 22 ++++++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Scanner.js b/src/Scanner.js index 6c4da2640..abc375a57 100644 --- a/src/Scanner.js +++ b/src/Scanner.js @@ -2,7 +2,7 @@ import { Form } from './Form/Form.js' import { SUBMIT_BUTTON_SELECTOR, FORM_INPUTS_SELECTOR } from './Form/selectors-css.js' import { constants } from './constants.js' import { createMatching } from './Form/matching.js' -import {isFormLikelyToBeUsedAsPageWrapper, shouldLog} from './autofill-utils.js' +import {isFormLikelyToBeUsedAsPageWrapper, shouldLog, shouldLogPerformance} from './autofill-utils.js' import { AddDebugFlagCall } from './deviceApiCalls/__generated__/deviceApiCalls.js' const { @@ -118,6 +118,10 @@ class DefaultScanner { window.performance?.mark?.('scanner:init:start') this.findEligibleInputs(document) window.performance?.mark?.('scanner:init:end') + if (shouldLogPerformance()) { + const measurement = window.performance?.measure('scanner:init', 'scanner:init:start', 'scanner:init:end') + console.log(`Initial scan took ${Math.round(measurement?.duration)}ms`) + } this.mutObs.observe(document.documentElement, { childList: true, subtree: true }) } diff --git a/src/autofill-utils.js b/src/autofill-utils.js index 3bcfe525d..ca29a70c0 100644 --- a/src/autofill-utils.js +++ b/src/autofill-utils.js @@ -401,13 +401,30 @@ const wasAutofilledByChrome = (input) => { } /** - * Checks if we should log debug info to the console + * Checks if we should log form analysis debug info to the console * @returns {boolean} */ function shouldLog () { + return readDebugSetting('ddg-autofill-debug') +} + +/** + * Checks if we should log performance info to the console + * @returns {boolean} + */ +function shouldLogPerformance () { + return readDebugSetting('ddg-autofill-perf') +} + +/** + * Check if a sessionStorage item is set to 'true' + * @param setting + * @returns {boolean} + */ +function readDebugSetting (setting) { // sessionStorage throws in invalid schemes like data: and file: try { - return window.sessionStorage?.getItem('ddg-autofill-debug') === 'true' + return window.sessionStorage?.getItem(setting) === 'true' } catch (e) { return false } @@ -490,6 +507,7 @@ export { isValidTLD, wasAutofilledByChrome, shouldLog, + shouldLogPerformance, whenIdle, truncateFromMiddle, isFormLikelyToBeUsedAsPageWrapper