From 7835cac0f51e035e1249c81aabc54e3dbd714c1c Mon Sep 17 00:00:00 2001 From: Miodec Date: Thu, 6 Apr 2023 21:50:40 +0200 Subject: [PATCH] checking performance.now once, and passing it around checking performance.now before a setTimeout setting test end as soon as possible --- .../src/ts/controllers/input-controller.ts | 15 ++++++----- frontend/src/ts/test/test-input.ts | 11 +++----- frontend/src/ts/test/test-logic.ts | 26 +++++++++++++------ 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/frontend/src/ts/controllers/input-controller.ts b/frontend/src/ts/controllers/input-controller.ts index ded10b2d2..881ba3c7d 100644 --- a/frontend/src/ts/controllers/input-controller.ts +++ b/frontend/src/ts/controllers/input-controller.ts @@ -394,6 +394,9 @@ function handleChar( if (TestUI.resultCalculating || TestUI.resultVisible) { return; } + + const now = performance.now(); + const isCharKorean: boolean = TestInput.input.getKoreanStatus(); if (char === "…") { for (let i = 0; i < 3; i++) { @@ -442,7 +445,7 @@ function handleChar( } //start the test - if (!TestState.isActive && !TestLogic.startTest()) { + if (!TestState.isActive && !TestLogic.startTest(now)) { return; } @@ -464,7 +467,7 @@ function handleChar( } if (TestInput.input.current === "") { - TestInput.setBurstStart(performance.now()); + TestInput.setBurstStart(now); } if (!isCharKorean && !Config.language.startsWith("korean")) { @@ -958,21 +961,21 @@ $(document).keydown(async (event) => { $("#wordsInput").keydown((event) => { if (event.originalEvent?.repeat) return; - + const now = performance.now(); setTimeout(() => { const isAndroid = event.key === "Unidentified" && event.code === "" && event.which === 229; - TestInput.recordKeydownTime(isAndroid ? "Android" : event.code); + TestInput.recordKeydownTime(now, isAndroid ? "Android" : event.code); }, 0); }); $("#wordsInput").keyup((event) => { if (event.originalEvent?.repeat) return; - + const now = performance.now(); setTimeout(() => { const isAndroid = event.key === "Unidentified" && event.code === "" && event.which === 229; - TestInput.recordKeyupTime(isAndroid ? "Android" : event.code); + TestInput.recordKeyupTime(now, isAndroid ? "Android" : event.code); }, 0); }); diff --git a/frontend/src/ts/test/test-input.ts b/frontend/src/ts/test/test-input.ts index aa0381f4c..ecfd74711 100644 --- a/frontend/src/ts/test/test-input.ts +++ b/frontend/src/ts/test/test-input.ts @@ -285,7 +285,7 @@ export function incrementAccuracy(correctincorrect: boolean): void { } } -export function forceKeyup(): void { +export function forceKeyup(now: number): void { //using mean here because for words mode, the last keypress ends the test. //if we then force keyup on that last keypress, it will record a duration of 0 //skewing the average and standard deviation @@ -293,7 +293,7 @@ export function forceKeyup(): void { const keysOrder = Object.entries(keyDownData); keysOrder.sort((a, b) => a[1].timestamp - b[1].timestamp); for (let i = 0; i < keysOrder.length - 1; i++) { - recordKeyupTime(keysOrder[i][0]); + recordKeyupTime(now, keysOrder[i][0]); } const last = keysOrder[keysOrder.length - 1]; if (last !== undefined) { @@ -303,7 +303,7 @@ export function forceKeyup(): void { let androidIndex = 0; -export function recordKeyupTime(key: string): void { +export function recordKeyupTime(now: number, key: string): void { if (!keysToTrack.includes(key)) return; if (key === "Android") { @@ -313,7 +313,6 @@ export function recordKeyupTime(key: string): void { if (keyDownData[key] === undefined) return; - const now = performance.now(); const diff = Math.abs(keyDownData[key].timestamp - now); keypressTimings.duration.array[keyDownData[key].index] = diff; delete keyDownData[key]; @@ -321,7 +320,7 @@ export function recordKeyupTime(key: string): void { updateOverlap(now); } -export function recordKeydownTime(key: string): void { +export function recordKeydownTime(now: number, key: string): void { if (!keysToTrack.includes(key)) { if (spacingDebug) { console.log( @@ -353,8 +352,6 @@ export function recordKeydownTime(key: string): void { return; } - const now = performance.now(); - keyDownData[key] = { timestamp: now, index: keypressTimings.duration.array.length, diff --git a/frontend/src/ts/test/test-logic.ts b/frontend/src/ts/test/test-logic.ts index f4aa96748..b444df9a4 100644 --- a/frontend/src/ts/test/test-logic.ts +++ b/frontend/src/ts/test/test-logic.ts @@ -307,7 +307,7 @@ async function applyEnglishPunctuationToWord(word: string): Promise { return EnglishPunctuation.replace(word); } -export function startTest(): boolean { +export function startTest(now: number): boolean { if (PageTransition.get()) { return false; } @@ -348,7 +348,7 @@ export function startTest(): boolean { } } catch (e) {} //use a recursive self-adjusting timer to avoid time drift - TestStats.setStart(performance.now()); + TestStats.setStart(now); TestTimer.start(); return true; } @@ -1386,9 +1386,7 @@ function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent { Config.mode === "zen" ? 0 : Misc.roundTo2(TestStats.end - TestInput.keypressTimings.spacing.last), - startToFirstKey: Misc.roundTo2( - TestInput.keypressTimings.spacing.first - TestStats.start - ), + startToFirstKey: undefined, consistency: undefined, keyConsistency: undefined, funbox: Config.funbox, @@ -1403,6 +1401,16 @@ function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent { afkDuration: undefined, }; + const stfk = Misc.roundTo2( + TestInput.keypressTimings.spacing.first - TestStats.start + ); + + if (stfk > 0) { + completedEvent.startToFirstKey = stfk; + } else { + completedEvent.startToFirstKey = 0; + } + // stats const stats = TestStats.calculateStats(); if (stats.time % 1 != 0 && Config.mode !== "time") { @@ -1527,19 +1535,21 @@ function buildCompletedEvent(difficultyFailed: boolean): CompletedEvent { } export async function finish(difficultyFailed = false): Promise { - await Misc.sleep(1); //this is needed to make sure the last keypress is registered if (!TestState.isActive) return; + const now = performance.now(); + TestStats.setEnd(now); + + await Misc.sleep(1); //this is needed to make sure the last keypress is registered if (TestInput.input.current.length != 0) { TestInput.input.pushHistory(); TestInput.corrected.pushHistory(); Replay.replayGetWordsList(TestInput.input.history); } - TestInput.forceKeyup(); //this ensures that the last keypress(es) are registered + TestInput.forceKeyup(now); //this ensures that the last keypress(es) are registered TestUI.setResultCalculating(true); TestUI.setResultVisible(true); - TestStats.setEnd(performance.now()); TestState.setActive(false); Replay.stopReplayRecording(); Focus.set(false);