[perf_tests]Implemented GitRollbackCommand and HandleSpanCommand

GitOrigin-RevId: defcfa797d687b51efb71bcf809e654ede363436
This commit is contained in:
Nikita Barkov
2024-08-02 17:50:43 +00:00
committed by intellij-monorepo-bot
parent 36a929c19d
commit 9ea659bc92
5 changed files with 99 additions and 2 deletions
@@ -19,7 +19,8 @@ public final class GitCommandProvider implements CommandProvider {
Map.entry(ShowFileAnnotationCommand.PREFIX, ShowFileAnnotationCommand::new),
Map.entry(CheckGitLogIndexedCommand.PREFIX, CheckGitLogIndexedCommand::new),
Map.entry(WaitVcsLogIndexingCommand.PREFIX, WaitVcsLogIndexingCommand::new),
Map.entry(WaitForVcsLogUpdateCommand.PREFIX, WaitForVcsLogUpdateCommand::new)
Map.entry(WaitForVcsLogUpdateCommand.PREFIX, WaitForVcsLogUpdateCommand::new),
Map.entry(GitRollbackCommand.PREFIX, GitRollbackCommand::new)
);
}
}
@@ -0,0 +1,46 @@
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package git4idea.performanceTesting
import com.intellij.openapi.application.EDT
import com.intellij.openapi.fileEditor.FileDocumentManager
import com.intellij.openapi.ui.playback.PlaybackContext
import com.intellij.openapi.util.ActionCallback
import com.intellij.openapi.vcs.changes.ChangeListManager
import com.intellij.openapi.vcs.changes.ui.RollbackWorker
import com.intellij.vcsUtil.RollbackUtil
import com.jetbrains.performancePlugin.commands.OpenFileCommand
import com.jetbrains.performancePlugin.commands.PerformanceCommandCoroutineAdapter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.jetbrains.concurrency.await
import org.jetbrains.concurrency.toPromise
class GitRollbackCommand(text: String, line: Int) : PerformanceCommandCoroutineAdapter(text, line) {
companion object {
const val NAME = "gitRollback"
const val PREFIX = "$CMD_PREFIX$NAME"
}
override suspend fun doExecute(context: PlaybackContext) {
val project = context.project
val filePath = extractCommandArgument(PREFIX)
withContext(Dispatchers.EDT) {
val file = OpenFileCommand.findFile(filePath, project) ?: throw IllegalArgumentException("There is no file ${filePath}")
val changes = ChangeListManager.getInstance(project).defaultChangeList.changes
val fileChanges = changes.filter { it.virtualFile == file }
FileDocumentManager.getInstance().saveAllDocuments()
val operationName = RollbackUtil.getRollbackOperationName(project)
val actionCallback = ActionCallback()
RollbackWorker(project, operationName, false)
.doRollback(fileChanges, false, Runnable {
actionCallback.setDone()
}, null)
actionCallback.toPromise().await()
}
}
override fun getName(): String {
return NAME
}
}
@@ -1071,6 +1071,10 @@ fun <T : CommandChain> T.gitCommitFile(pathToFile: String, commitMessage: String
addCommand("${CMD_PREFIX}gitCommit ${pathToFile},${commitMessage}")
}
fun <T : CommandChain> T.gitRollbackFile(pathToFile: String): T = apply {
addCommand("${CMD_PREFIX}gitRollback ${pathToFile}")
}
fun <T : CommandChain> T.replaceText(startOffset: Int? = null, endOffset: Int? = null, newText: String? = null): T = apply {
val options = StringBuilder()
if (startOffset != null) {
@@ -1162,4 +1166,12 @@ fun <T : CommandChain> T.waitForProjectView(): T = apply {
*/
fun <T : CommandChain> T.expandProjectView(relativePath: String): T = apply {
addCommand("${CMD_PREFIX}expandProjectView $relativePath")
}
/**
* The first call will create and start span.
* The second call with the same spanName will stop span.
* */
fun <T : CommandChain> T.handleSpan(spanName: String): T = apply {
addCommand("${CMD_PREFIX}handleSpan $spanName")
}
@@ -120,7 +120,8 @@ public final class BaseCommandProvider implements CommandProvider {
Map.entry(WaitForProjectViewCommand.PREFIX, WaitForProjectViewCommand::new),
Map.entry(ExpandProjectViewCommand.PREFIX, ExpandProjectViewCommand::new),
Map.entry(DebugToggleBreakpointCommand.PREFIX, DebugToggleBreakpointCommand::new),
Map.entry(MoveFilesCommand.PREFIX, MoveFilesCommand::new)
Map.entry(MoveFilesCommand.PREFIX, MoveFilesCommand::new),
Map.entry(HandleSpanCommand.PREFIX, HandleSpanCommand::new)
);
}
}
@@ -0,0 +1,37 @@
package com.jetbrains.performancePlugin.commands
import com.intellij.openapi.ui.playback.PlaybackContext
import com.intellij.platform.diagnostic.telemetry.Scope
import com.intellij.platform.diagnostic.telemetry.TelemetryManager
import io.opentelemetry.api.trace.Span
/**
* The first call will create and start span.
* The second call with the same spanName will stop span.
* */
class HandleSpanCommand(text: String, line: Int) : PerformanceCommandCoroutineAdapter(text, line) {
companion object {
const val NAME = "handleSpan"
const val PREFIX = "$CMD_PREFIX$NAME"
private val CREATED_SPANS: MutableMap<String, Span> = mutableMapOf()
}
override suspend fun doExecute(context: PlaybackContext) {
val spanName = extractCommandArgument(PREFIX)
val span = CREATED_SPANS[spanName]
if (span != null) {
span.end()
CREATED_SPANS.remove(spanName)
} else {
val newSpan = TelemetryManager.getTracer(Scope("HandleSpanCommand"))
.spanBuilder(spanName)
.startSpan()
CREATED_SPANS[spanName] = newSpan
}
}
override fun getName(): String {
return NAME
}
}