Add message bus topic for typing latency events

This allows plugins to accurately measure typing latency.
This commit is contained in:
Matthew Gharrity
2019-02-06 12:30:51 +01:00
committed by Dmitry Jemerov
parent e95e921556
commit 1eaa4e7aff
4 changed files with 41 additions and 10 deletions
@@ -0,0 +1,15 @@
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.editor.actionSystem;
import com.intellij.openapi.editor.Editor;
import com.intellij.util.messages.Topic;
/**
* Reports typing latency measurements on the application-level {@link com.intellij.util.messages.MessageBus}.
*/
public interface LatencyListener {
Topic<LatencyListener> TOPIC = new Topic<>("Typing latency notifications", LatencyListener.class);
/** Record latency for a single key typed. */
void recordTypingLatency(Editor editor, String action, long latencyMs);
}
@@ -1,7 +1,9 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.internal.performance
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.LatencyListener
import com.intellij.openapi.editor.actionSystem.LatencyRecorder
import com.intellij.openapi.editor.impl.EditorImpl
import com.intellij.openapi.fileEditor.FileDocumentManager
@@ -64,15 +66,22 @@ var currentLatencyRecordKey: LatencyDistributionRecordKey? = null
val latencyRecorderProperties: MutableMap<String, String> = mutableMapOf()
fun recordTypingLatency(editor: Editor, action: String, latencyInMS: Long) {
val key = currentLatencyRecordKey ?: run {
val fileType = FileDocumentManager.getInstance().getFile(editor.document)?.fileType ?: return
LatencyDistributionRecordKey(fileType.name)
class LatenciometerListener : LatencyListener {
init {
ApplicationManager.getApplication().messageBus.connect().subscribe(LatencyListener.TOPIC, this)
}
val latencyRecord = latencyMap.getOrPut(key) {
LatencyDistributionRecord(key)
override fun recordTypingLatency(editor: Editor, action: String, latencyInMS: Long) {
val key = currentLatencyRecordKey ?: run {
val fileType = FileDocumentManager.getInstance().getFile(editor.document)?.fileType ?: return
LatencyDistributionRecordKey(fileType.name)
}
val latencyRecord = latencyMap.getOrPut(key) {
LatencyDistributionRecord(key)
}
latencyRecord.update(getActionKey(action), latencyInMS.toInt())
}
latencyRecord.update(getActionKey(action), latencyInMS.toInt())
}
fun getActionKey(action: String): String =
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.openapi.editor.impl;
import com.intellij.application.options.EditorFontsConstants;
@@ -154,6 +154,7 @@ public final class EditorImpl extends UserDataHolderBase implements EditorEx, Hi
private final FocusModeModel myFocusModeModel;
private volatile long myLastTypedActionTimestamp = -1;
private String myLastTypedAction;
private final LatencyListener myLatencyPublisher;
private static final Cursor EMPTY_CURSOR;
private final Map<Object, Cursor> myCustomCursors = new LinkedHashMap<>();
@@ -551,6 +552,8 @@ public final class EditorImpl extends UserDataHolderBase implements EditorEx, Hi
myFocusModeModel = new FocusModeModel(this);
myPopupHandlers.add(new DefaultPopupHandler());
myLatencyPublisher = ApplicationManager.getApplication().getMessageBus().syncPublisher(LatencyListener.TOPIC);
}
public void applyFocusMode() {
@@ -3379,7 +3382,7 @@ public final class EditorImpl extends UserDataHolderBase implements EditorEx, Hi
void measureTypingLatency() {
if (myLastTypedActionTimestamp != -1) {
LatenciometerKt.recordTypingLatency(this, myLastTypedAction, System.currentTimeMillis() - myLastTypedActionTimestamp);
myLatencyPublisher.recordTypingLatency(this, myLastTypedAction, System.currentTimeMillis() - myLastTypedActionTimestamp);
myLastTypedActionTimestamp = -1;
}
}
@@ -139,6 +139,10 @@
<implementation-class>com.intellij.openapi.editor.impl.EditorLastActionTrackerImpl</implementation-class>
</component>
<component>
<implementation-class>com.intellij.internal.performance.LatenciometerListener</implementation-class>
</component>
<component>
<interface-class>org.jetbrains.ide.BuiltInServerManager</interface-class>
<implementation-class>org.jetbrains.ide.BuiltInServerManagerImpl</implementation-class>