Skip to content

Commit da8fc99

Browse files
authored
fix(amazonq): unified timestamp with epoch time (aws#8086)
## Problem 1. timestamp used in LSP path is cross-process, which requires a common starting point, so should use epoch based time instead of `peformance.now()` which starts at process starting point. ## Solution 1. replace all `performance.now` with `Date.now()` --- - Treat all work as PUBLIC. Private `feature/x` branches will not be squash-merged at release time. - Your code changes must meet the guidelines in [CONTRIBUTING.md](https://github.com/aws/aws-toolkit-vscode/blob/master/CONTRIBUTING.md#guidelines). - License: I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 8e78168 commit da8fc99

16 files changed

+68
-68
lines changed

packages/amazonq/src/app/inline/activation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ export async function activate(languageClient: LanguageClient) {
9898

9999
if (vsCodeState.lastUserModificationTime) {
100100
TelemetryHelper.instance.setTimeSinceLastModification(
101-
performance.now() - vsCodeState.lastUserModificationTime
101+
Date.now() - vsCodeState.lastUserModificationTime
102102
)
103103
}
104-
vsCodeState.lastUserModificationTime = performance.now()
104+
vsCodeState.lastUserModificationTime = Date.now()
105105
/**
106106
* Important: Doing this sleep(10) is to make sure
107107
* 1. this event is processed by vs code first

packages/amazonq/src/app/inline/completion.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class InlineCompletionManager implements Disposable {
115115
const startLine = position.line
116116
// TODO: also log the seen state for other suggestions in session
117117
// Calculate timing metrics before diagnostic delay
118-
const totalSessionDisplayTime = performance.now() - requestStartTime
118+
const totalSessionDisplayTime = Date.now() - requestStartTime
119119
await sleep(500)
120120
const diagnosticDiff = getDiagnosticsDifferences(
121121
this.sessionManager.getActiveSession()?.diagnosticsBeforeAccept,
@@ -175,7 +175,7 @@ export class InlineCompletionManager implements Disposable {
175175
return
176176
}
177177
const requestStartTime = session.requestStartTime
178-
const totalSessionDisplayTime = performance.now() - requestStartTime
178+
const totalSessionDisplayTime = Date.now() - requestStartTime
179179
await commands.executeCommand('editor.action.inlineSuggest.hide')
180180
// TODO: also log the seen state for other suggestions in session
181181
this.disposable.dispose()
@@ -249,7 +249,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
249249
// Use VS Code command to check if inline suggestion is actually visible on screen
250250
// This command only executes when inlineSuggestionVisible context is true
251251
await vscode.commands.executeCommand('aws.amazonq.checkInlineSuggestionVisibility')
252-
const isInlineSuggestionVisible = performance.now() - session.lastVisibleTime < 50
252+
const isInlineSuggestionVisible = Date.now() - session.lastVisibleTime < 50
253253
return isInlineSuggestionVisible
254254
}
255255

@@ -278,7 +278,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
278278
sessionId: session.sessionId,
279279
completionSessionResult,
280280
firstCompletionDisplayLatency: session.firstCompletionDisplayLatency,
281-
totalSessionDisplayTime: performance.now() - session.requestStartTime,
281+
totalSessionDisplayTime: Date.now() - session.requestStartTime,
282282
}
283283
this.languageClient.sendNotification(this.logSessionResultMessageName, params)
284284
}
@@ -309,7 +309,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
309309
// when hitting other keystrokes, the context.triggerKind is Automatic (1)
310310
// we only mark option + C as manual trigger
311311
// this is a workaround since the inlineSuggest.trigger command take no params
312-
const isAutoTrigger = performance.now() - vsCodeState.lastManualTriggerTime > 50
312+
const isAutoTrigger = Date.now() - vsCodeState.lastManualTriggerTime > 50
313313
if (isAutoTrigger && !CodeSuggestionsState.instance.isSuggestionsEnabled()) {
314314
// return early when suggestions are disabled with auto trigger
315315
return []
@@ -318,9 +318,9 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
318318
// yield event loop to let the document listen catch updates
319319
await sleep(1)
320320

321-
let logstr = `GenerateCompletion metadata:\\n`
321+
let logstr = `GenerateCompletion activity:\\n`
322322
try {
323-
const t0 = performance.now()
323+
const t0 = Date.now()
324324
vsCodeState.isRecommendationsActive = true
325325
// handling previous session
326326
const prevSession = this.sessionManager.getActiveSession()
@@ -365,7 +365,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
365365
// re-use previous suggestions as long as new typed prefix matches
366366
if (prevItemMatchingPrefix.length > 0) {
367367
logstr += `- not call LSP and reuse previous suggestions that match user typed characters
368-
- duration between trigger to completion suggestion is displayed ${performance.now() - t0}`
368+
- duration between trigger to completion suggestion is displayed ${Date.now() - t0}`
369369
void this.checkWhetherInlineCompletionWasShown()
370370
return prevItemMatchingPrefix
371371
}
@@ -381,7 +381,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
381381
},
382382
},
383383
firstCompletionDisplayLatency: prevSession.firstCompletionDisplayLatency,
384-
totalSessionDisplayTime: performance.now() - prevSession.requestStartTime,
384+
totalSessionDisplayTime: Date.now() - prevSession.requestStartTime,
385385
}
386386
this.languageClient.sendNotification(this.logSessionResultMessageName, params)
387387
this.sessionManager.clear()
@@ -396,7 +396,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
396396
TelemetryHelper.instance.setInvokeSuggestionStartTime()
397397
TelemetryHelper.instance.setTriggerType(context.triggerKind)
398398

399-
const t1 = performance.now()
399+
const t1 = Date.now()
400400

401401
await this.recommendationService.getAllRecommendations(
402402
this.languageClient,
@@ -418,7 +418,7 @@ export class AmazonQInlineCompletionItemProvider implements InlineCompletionItem
418418
// eslint-disable-next-line @typescript-eslint/no-base-to-string
419419
const itemLog = items[0] ? `${items[0].insertText.toString()}` : `no suggestion`
420420

421-
const t2 = performance.now()
421+
const t2 = Date.now()
422422

423423
logstr += `- number of suggestions: ${items.length}
424424
- sessionId: ${this.sessionManager.getActiveSession()?.sessionId}
@@ -468,7 +468,7 @@ ${itemLog}
468468
const lastDocumentChange = this.documentEventListener.getLastDocumentChangeEvent(document.uri.fsPath)
469469
if (
470470
lastDocumentChange &&
471-
performance.now() - lastDocumentChange.timestamp < CodeWhispererConstants.inlineSuggestionShowDelay
471+
Date.now() - lastDocumentChange.timestamp < CodeWhispererConstants.inlineSuggestionShowDelay
472472
) {
473473
await sleep(CodeWhispererConstants.showRecommendationTimerPollPeriod)
474474
} else {
@@ -486,7 +486,7 @@ ${itemLog}
486486
// Check if Next Edit Prediction feature flag is enabled
487487
if (Experiments.instance.get('amazonqLSPNEP', true)) {
488488
await showEdits(item, editor, session, this.languageClient, this)
489-
logstr += `- duration between trigger to edits suggestion is displayed: ${performance.now() - t0}ms`
489+
logstr += `- duration between trigger to edits suggestion is displayed: ${Date.now() - t0}ms`
490490
}
491491
return []
492492
}
@@ -530,7 +530,7 @@ ${itemLog}
530530

531531
this.sessionManager.updateCodeReferenceAndImports()
532532
// suggestions returned here will be displayed on screen
533-
logstr += `- duration between trigger to completion suggestion is displayed: ${performance.now() - t0}ms`
533+
logstr += `- duration between trigger to completion suggestion is displayed: ${Date.now() - t0}ms`
534534
void this.checkWhetherInlineCompletionWasShown()
535535
return itemsMatchingTypeahead as InlineCompletionItem[]
536536
} catch (e) {

packages/amazonq/src/app/inline/documentEventListener.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class DocumentEventListener {
2020
if (this.lastDocumentChangeEventMap.size > this._maxDocument) {
2121
this.lastDocumentChangeEventMap.clear()
2222
}
23-
this.lastDocumentChangeEventMap.set(e.document.uri.fsPath, { event: e, timestamp: performance.now() })
23+
this.lastDocumentChangeEventMap.set(e.document.uri.fsPath, { event: e, timestamp: Date.now() })
2424
// The VS Code provideInlineCompletionCallback may not trigger when Enter is pressed, especially in Python files
2525
// manually make this trigger. In case of duplicate, the provideInlineCompletionCallback is already debounced
2626
if (this.isEnter(e) && vscode.window.activeTextEditor) {
@@ -37,7 +37,7 @@ export class DocumentEventListener {
3737
const eventTime = result.timestamp
3838
const isDelete =
3939
(event && event.contentChanges.length === 1 && event.contentChanges[0].text === '') || false
40-
const timeDiff = Math.abs(performance.now() - eventTime)
40+
const timeDiff = Math.abs(Date.now() - eventTime)
4141
return timeDiff < 500 && isDelete
4242
}
4343
return false

packages/amazonq/src/app/inline/editSuggestionState.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
*/
99
export class EditSuggestionState {
1010
private static isEditSuggestionCurrentlyActive = false
11-
private static displayStartTime = performance.now()
11+
private static displayStartTime = Date.now()
1212

1313
static setEditSuggestionActive(active: boolean): void {
1414
this.isEditSuggestionCurrentlyActive = active
1515
if (active) {
16-
this.displayStartTime = performance.now()
16+
this.displayStartTime = Date.now()
1717
}
1818
}
1919

@@ -22,6 +22,6 @@ export class EditSuggestionState {
2222
}
2323

2424
static isEditSuggestionDisplayingOverOneSecond(): boolean {
25-
return this.isEditSuggestionActive() && performance.now() - this.displayStartTime > 1000
25+
return this.isEditSuggestionActive() && Date.now() - this.displayStartTime > 1000
2626
}
2727
}

packages/amazonq/src/app/inline/recommendationService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class RecommendationService {
102102
if (document.uri.scheme === 'vscode-notebook-cell') {
103103
request.fileContextOverride = extractFileContextInNotebooks(document, position)
104104
}
105-
const requestStartTime = performance.now()
105+
const requestStartTime = Date.now()
106106
const statusBar = CodeWhispererStatusBarManager.instance
107107

108108
// Only track telemetry if enabled
@@ -126,7 +126,7 @@ export class RecommendationService {
126126
nextToken: request.partialResultToken,
127127
},
128128
})
129-
const t0 = performance.now()
129+
const t0 = Date.now()
130130

131131
// Best effort estimate of deletion
132132
const isTriggerByDeletion = documentEventListener.isLastEventDeletion(document.uri.fsPath)
@@ -176,7 +176,7 @@ export class RecommendationService {
176176

177177
getLogger().info('Received inline completion response from LSP: %O', {
178178
sessionId: result.sessionId,
179-
latency: performance.now() - t0,
179+
latency: Date.now() - t0,
180180
itemCount: result.items?.length || 0,
181181
items: result.items?.map((item) => ({
182182
itemId: item.itemId,
@@ -228,7 +228,7 @@ export class RecommendationService {
228228
}
229229
TelemetryHelper.instance.setFirstSuggestionShowTime()
230230

231-
const firstCompletionDisplayLatency = performance.now() - requestStartTime
231+
const firstCompletionDisplayLatency = Date.now() - requestStartTime
232232
this.sessionManager.startSession(
233233
result.sessionId,
234234
result.items,

packages/amazonq/src/app/inline/sessionManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export class SessionManager {
137137
public checkInlineSuggestionVisibility() {
138138
if (this.activeSession) {
139139
this.activeSession.displayed = true
140-
this.activeSession.lastVisibleTime = performance.now()
140+
this.activeSession.lastVisibleTime = Date.now()
141141
}
142142
}
143143

packages/amazonq/src/app/inline/telemetryHelper.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ export class TelemetryHelper {
4141

4242
public setInvokeSuggestionStartTime() {
4343
this.resetClientComponentLatencyTime()
44-
this._invokeSuggestionStartTime = performance.now()
44+
this._invokeSuggestionStartTime = Date.now()
4545
}
4646

4747
get invokeSuggestionStartTime(): number {
4848
return this._invokeSuggestionStartTime
4949
}
5050

5151
public setPreprocessEndTime() {
52-
this._preprocessEndTime = performance.now()
52+
this._preprocessEndTime = Date.now()
5353
}
5454

5555
get preprocessEndTime(): number {
@@ -58,7 +58,7 @@ export class TelemetryHelper {
5858

5959
public setSdkApiCallStartTime() {
6060
if (this._sdkApiCallStartTime === 0) {
61-
this._sdkApiCallStartTime = performance.now()
61+
this._sdkApiCallStartTime = Date.now()
6262
}
6363
}
6464

@@ -68,7 +68,7 @@ export class TelemetryHelper {
6868

6969
public setSdkApiCallEndTime() {
7070
if (this._sdkApiCallEndTime === 0 && this._sdkApiCallStartTime !== 0) {
71-
this._sdkApiCallEndTime = performance.now()
71+
this._sdkApiCallEndTime = Date.now()
7272
}
7373
}
7474

@@ -78,7 +78,7 @@ export class TelemetryHelper {
7878

7979
public setAllPaginationEndTime() {
8080
if (this._allPaginationEndTime === 0 && this._sdkApiCallEndTime !== 0) {
81-
this._allPaginationEndTime = performance.now()
81+
this._allPaginationEndTime = Date.now()
8282
}
8383
}
8484

@@ -88,7 +88,7 @@ export class TelemetryHelper {
8888

8989
public setFirstSuggestionShowTime() {
9090
if (this._firstSuggestionShowTime === 0 && this._sdkApiCallEndTime !== 0) {
91-
this._firstSuggestionShowTime = performance.now()
91+
this._firstSuggestionShowTime = Date.now()
9292
}
9393
}
9494

packages/amazonq/test/unit/app/inline/completion.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ describe('AmazonQInlineCompletionItemProvider', function () {
4343
const session = {
4444
sessionId: 'test-session',
4545
firstCompletionDisplayLatency: 100,
46-
requestStartTime: performance.now() - 1000,
46+
requestStartTime: Date.now() - 1000,
4747
}
4848

4949
provider.batchDiscardTelemetryForEditSuggestion(items, session)
@@ -84,7 +84,7 @@ describe('AmazonQInlineCompletionItemProvider', function () {
8484
const session = {
8585
sessionId: 'test-session',
8686
firstCompletionDisplayLatency: 100,
87-
requestStartTime: performance.now() - 1000,
87+
requestStartTime: Date.now() - 1000,
8888
}
8989

9090
provider.batchDiscardTelemetryForEditSuggestion(items, session)
@@ -108,7 +108,7 @@ describe('AmazonQInlineCompletionItemProvider', function () {
108108
const session = {
109109
sessionId: 'test-session',
110110
firstCompletionDisplayLatency: 100,
111-
requestStartTime: performance.now() - 1000,
111+
requestStartTime: Date.now() - 1000,
112112
}
113113

114114
provider.batchDiscardTelemetryForEditSuggestion(items, session)
@@ -166,7 +166,7 @@ describe('AmazonQInlineCompletionItemProvider', function () {
166166
mockSessionManager.getActiveSession.returns({
167167
displayed: true,
168168
suggestions: [{ isInlineEdit: true }],
169-
lastVisibleTime: performance.now(),
169+
lastVisibleTime: Date.now(),
170170
})
171171

172172
const result = await provider.isCompletionActive()
@@ -176,7 +176,7 @@ describe('AmazonQInlineCompletionItemProvider', function () {
176176
})
177177

178178
it('should return true when VS Code command executes successfully', async function () {
179-
const currentTime = performance.now()
179+
const currentTime = Date.now()
180180
mockSessionManager.getActiveSession.returns({
181181
displayed: true,
182182
suggestions: [{ isInlineEdit: false }],
@@ -192,7 +192,7 @@ describe('AmazonQInlineCompletionItemProvider', function () {
192192
})
193193

194194
it('should return false when VS Code command fails', async function () {
195-
const oldTime = performance.now() - 100 // Old timestamp (>50ms ago)
195+
const oldTime = Date.now() - 100 // Old timestamp (>50ms ago)
196196
mockSessionManager.getActiveSession.returns({
197197
displayed: true,
198198
suggestions: [{ isInlineEdit: false }],

packages/core/src/codewhisperer/service/inlineCompletionService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class InlineCompletionService {
5555
this._showRecommendationTimer = undefined
5656
}
5757
this._showRecommendationTimer = setInterval(() => {
58-
const delay = performance.now() - vsCodeState.lastUserModificationTime
58+
const delay = Date.now() - vsCodeState.lastUserModificationTime
5959
if (delay < CodeWhispererConstants.inlineSuggestionShowDelay) {
6060
return
6161
}

packages/core/src/codewhisperer/service/keyStrokeHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class KeyStrokeHandler {
5656
return
5757
}
5858
this.idleTriggerTimer = setInterval(() => {
59-
const duration = (performance.now() - RecommendationHandler.instance.lastInvocationTime) / 1000
59+
const duration = (Date.now() - RecommendationHandler.instance.lastInvocationTime) / 1000
6060
if (duration < CodeWhispererConstants.invocationTimeIntervalThreshold) {
6161
return
6262
}

0 commit comments

Comments
 (0)