Skip to content

Commit bdc5544

Browse files
committed
fix perceivedLatency being calculated incorrectly
1. perceivedLatency was previously calculated at the time of sending STE, but the timeOfLastCharTyped value is no longer accurate, so calculate the perceivedLatency value at suggestion showing time to get the most accurate timeOfLastCharTyped
1 parent a81caf6 commit bdc5544

File tree

6 files changed

+26
-27
lines changed

6 files changed

+26
-27
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/credentials/CodeWhispererClientAdaptor.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,7 @@ open class CodeWhispererClientAdaptorImpl(override val project: Project) : CodeW
316316
it.sessionId(responseContext.sessionId)
317317
it.recommendationLatencyMilliseconds(e2eLatency)
318318
it.triggerToResponseLatencyMilliseconds(requestContext.latencyContext.paginationFirstCompletionTime)
319-
it.perceivedLatencyMilliseconds(
320-
requestContext.latencyContext.getPerceivedLatency(requestContext.triggerTypeInfo.triggerType)
321-
)
319+
it.perceivedLatencyMilliseconds(requestContext.latencyContext.perceivedLatency)
322320
it.suggestionState(suggestionState.toCodeWhispererSdkType())
323321
it.timestamp(Instant.now())
324322
it.suggestionReferenceCount(suggestionReferenceCount)
@@ -364,6 +362,7 @@ open class CodeWhispererClientAdaptorImpl(override val project: Project) : CodeW
364362
it.sessionId(responseContext.sessionId)
365363
it.recommendationLatencyMilliseconds(e2eLatency)
366364
it.triggerToResponseLatencyMilliseconds(sessionContext.latencyContext.paginationFirstCompletionTime)
365+
it.perceivedLatencyMilliseconds(sessionContext.latencyContext.perceivedLatency)
367366
it.suggestionState(suggestionState.toCodeWhispererSdkType())
368367
it.timestamp(Instant.now())
369368
it.suggestionReferenceCount(suggestionReferenceCount)

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/model/CodeWhispererModel.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ data class SessionContextNew(
155155
var popup: JBPopup? = null,
156156
var selectedIndex: Int = -1,
157157
val seen: MutableSet<Int> = mutableSetOf(),
158-
var isFirstTimeShowingPopup: Boolean = true,
159158
var toBeRemovedHighlighter: RangeHighlighter? = null,
160159
var insertEndOffset: Int = -1,
161160
var popupOffset: Int = -1,
@@ -278,6 +277,7 @@ data class LatencyContext(
278277
var codewhispererPreprocessingEnd: Long = 0L,
279278

280279
var paginationFirstCompletionTime: Double = 0.0,
280+
var perceivedLatency: Double = 0.0,
281281

282282
var codewhispererPostprocessingStart: Long = 0L,
283283
var codewhispererPostprocessingEnd: Long = 0L,
@@ -316,10 +316,9 @@ data class LatencyContext(
316316
if (triggerType == CodewhispererTriggerType.OnDemand) {
317317
getCodeWhispererEndToEndLatency()
318318
} else {
319-
(
320-
TimeUnit.NANOSECONDS.toMillis(codewhispererEndToEndEnd) -
321-
CodeWhispererAutoTriggerService.getInstance().timeAtLastCharTyped.toEpochMilli()
322-
).toDouble()
319+
TimeUnit.NANOSECONDS.toMillis(
320+
codewhispererEndToEndEnd - CodeWhispererAutoTriggerService.getInstance().timeAtLastCharTyped
321+
).toDouble()
323322
}
324323
}
325324

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/popup/CodeWhispererPopupManager.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,14 @@ class CodeWhispererPopupManager {
235235
// emit any events.
236236
// 4. User navigating through the completions or typing as the completion shows. We should not update the latency
237237
// end time and should not emit any events in this case.
238+
if (!CodeWhispererInvocationStatus.getInstance().isPopupActive()) {
239+
states.requestContext.latencyContext.codewhispererPostprocessingEnd = System.nanoTime()
240+
states.requestContext.latencyContext.codewhispererEndToEndEnd = System.nanoTime()
241+
states.requestContext.latencyContext.perceivedLatency =
242+
states.requestContext.latencyContext.getPerceivedLatency(states.requestContext.triggerTypeInfo.triggerType)
243+
}
238244
if (!isRecommendationAdded) {
239245
showPopup(states, sessionContext, states.popup, caretPoint, overlappingLinesCount)
240-
if (!isScrolling) {
241-
states.requestContext.latencyContext.codewhispererPostprocessingEnd = System.nanoTime()
242-
states.requestContext.latencyContext.codewhispererEndToEndEnd = System.nanoTime()
243-
}
244246
}
245247
if (isScrolling ||
246248
CodeWhispererInvocationStatus.getInstance().hasExistingServiceInvocation() ||

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/popup/CodeWhispererPopupManagerNew.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ class CodeWhispererPopupManagerNew {
122122
val selectedIndex = findNewSelectedIndex(isReverse, sessionContext.selectedIndex + indexChange)
123123

124124
sessionContext.selectedIndex = selectedIndex
125-
sessionContext.isFirstTimeShowingPopup = false
126125

127126
ApplicationManager.getApplication().messageBus.syncPublisher(CODEWHISPERER_POPUP_STATE_CHANGED).stateChanged(
128127
sessionContext
@@ -137,7 +136,6 @@ class CodeWhispererPopupManagerNew {
137136
) {
138137
if (!updateTypeahead(typeaheadChange, typeaheadAdded)) return
139138
if (!updateSessionSelectedIndex(sessionContext)) return
140-
sessionContext.isFirstTimeShowingPopup = false
141139

142140
ApplicationManager.getApplication().messageBus.syncPublisher(CODEWHISPERER_POPUP_STATE_CHANGED).stateChanged(
143141
sessionContext
@@ -146,7 +144,6 @@ class CodeWhispererPopupManagerNew {
146144

147145
@RequiresEdt
148146
fun changeStatesForShowing(sessionContext: SessionContextNew, states: InvocationContextNew, recommendationAdded: Boolean = false) {
149-
sessionContext.isFirstTimeShowingPopup = !recommendationAdded
150147
if (recommendationAdded) {
151148
ApplicationManager.getApplication().messageBus.syncPublisher(CODEWHISPERER_POPUP_STATE_CHANGED)
152149
.recommendationAdded(states, sessionContext)
@@ -210,7 +207,7 @@ class CodeWhispererPopupManagerNew {
210207
updateCodeReferencePanel(sessionContext.project, previews[selectedIndex].detail.recommendation.references())
211208
}
212209

213-
fun render(sessionContext: SessionContextNew, isRecommendationAdded: Boolean, isScrolling: Boolean) {
210+
fun render(sessionContext: SessionContextNew, isRecommendationAdded: Boolean) {
214211
updatePopupPanel(sessionContext)
215212

216213
// There are four cases that render() is called:
@@ -222,11 +219,16 @@ class CodeWhispererPopupManagerNew {
222219
// emit any events.
223220
// 4. User navigating through the completions or typing as the completion shows. We should not update the latency
224221
// end time and should not emit any events in this case.
222+
if (!CodeWhispererInvocationStatusNew.getInstance().isDisplaySessionActive()) {
223+
sessionContext.latencyContext.codewhispererPostprocessingEnd = System.nanoTime()
224+
sessionContext.latencyContext.codewhispererEndToEndEnd = System.nanoTime()
225+
val triggerTypeOfLastTrigger = CodeWhispererServiceNew.getInstance().getAllPaginationSessions()
226+
.values.filterNotNull().last().requestContext.triggerTypeInfo.triggerType
227+
sessionContext.latencyContext.perceivedLatency =
228+
sessionContext.latencyContext.getPerceivedLatency(triggerTypeOfLastTrigger)
229+
}
225230
if (isRecommendationAdded) return
226231
showPopup(sessionContext)
227-
if (isScrolling) return
228-
sessionContext.latencyContext.codewhispererPostprocessingEnd = System.nanoTime()
229-
sessionContext.latencyContext.codewhispererEndToEndEnd = System.nanoTime()
230232
}
231233

232234
fun dontClosePopupAndRun(runnable: () -> Unit) {

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/popup/CodeWhispererUIChangeListenerNew.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,15 @@ class CodeWhispererUIChangeListenerNew : CodeWhispererPopupStateChangeListener {
9494
CodeWhispererInlayManagerNew.getInstance().updateInlays(sessionContext, inlayChunks)
9595
CodeWhispererPopupManagerNew.getInstance().render(
9696
sessionContext,
97-
isRecommendationAdded = false,
98-
isScrolling = false
97+
isRecommendationAdded = false
9998
)
10099
}
101100

102101
override fun scrolled(sessionContext: SessionContextNew) {
103-
sessionContext.isFirstTimeShowingPopup = false
104-
CodeWhispererPopupManagerNew.getInstance().render(sessionContext, isRecommendationAdded = false, isScrolling = true)
102+
CodeWhispererPopupManagerNew.getInstance().render(sessionContext, isRecommendationAdded = false)
105103
}
106104

107105
override fun recommendationAdded(states: InvocationContextNew, sessionContext: SessionContextNew) {
108-
sessionContext.isFirstTimeShowingPopup = false
109-
CodeWhispererPopupManagerNew.getInstance().render(sessionContext, isRecommendationAdded = true, isScrolling = false)
106+
CodeWhispererPopupManagerNew.getInstance().render(sessionContext, isRecommendationAdded = true)
110107
}
111108
}

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererAutoTriggerService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class CodeWhispererAutoTriggerService : CodeWhispererAutoTriggerHandler, Disposa
4040

4141
private var lastInvocationTime: Instant? = null
4242
private var lastInvocationLineNum: Int? = null
43-
var timeAtLastCharTyped: Instant = Instant.now()
43+
var timeAtLastCharTyped: Long = System.nanoTime()
4444
private set
4545

4646
init {
@@ -54,7 +54,7 @@ class CodeWhispererAutoTriggerService : CodeWhispererAutoTriggerHandler, Disposa
5454
// a util wrapper
5555
fun tryInvokeAutoTrigger(editor: Editor, triggerType: CodeWhispererAutomatedTriggerType): Job? {
5656
// only needed for Classifier group, thus calculate it lazily
57-
timeAtLastCharTyped = Instant.now()
57+
timeAtLastCharTyped = System.nanoTime()
5858
val classifierResult: ClassifierResult by lazy { shouldTriggerClassifier(editor, triggerType.telemetryType) }
5959

6060
// we need classifier result for any type of triggering for classifier group for supported languages

0 commit comments

Comments
 (0)