Skip to content

Commit

Permalink
Add performance logging capability
Browse files Browse the repository at this point in the history
Signed-off-by: Emanuele Feliziani <[email protected]>

# Conflicts:
#	src/Scanner.js
  • Loading branch information
GioSensation committed Sep 11, 2023
1 parent 9edae2f commit 1562715
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 })
}

Expand Down
22 changes: 20 additions & 2 deletions src/autofill-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -490,6 +507,7 @@ export {
isValidTLD,
wasAutofilledByChrome,
shouldLog,
shouldLogPerformance,
whenIdle,
truncateFromMiddle,
isFormLikelyToBeUsedAsPageWrapper
Expand Down

0 comments on commit 1562715

Please sign in to comment.