[perf_tests]Fixed discussions in GitRollback and HandleSpan commands

GitOrigin-RevId: 8e2c708128c44daa5f62550393f6037bdeea8a84
This commit is contained in:
Nikita Barkov
2024-08-26 21:06:22 +02:00
committed by intellij-monorepo-bot
parent 9809443391
commit ea9d95de6d
3 changed files with 17 additions and 19 deletions

View File

@@ -5,41 +5,38 @@ import com.intellij.openapi.application.EDT
import com.intellij.openapi.application.writeIntentReadAction
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.sync.Mutex
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 NAME = "gitRollbackFile"
const val PREFIX = "$CMD_PREFIX$NAME"
}
override suspend fun doExecute(context: PlaybackContext) {
val project = context.project
val filePath = extractCommandArgument(PREFIX)
val mutex = Mutex(true)
withContext(Dispatchers.EDT) {
val actionCallback = writeIntentReadAction {
writeIntentReadAction {
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 ac = ActionCallback()
RollbackWorker(project, operationName, false)
.doRollback(fileChanges, false, Runnable {
ac.setDone()
mutex.unlock()
}, null)
ac
}
actionCallback.toPromise().await()
mutex.lock()
}
}

View File

@@ -1085,7 +1085,7 @@ fun <T : CommandChain> T.gitCommitFile(pathToFile: String, commitMessage: String
}
fun <T : CommandChain> T.gitRollbackFile(pathToFile: String): T = apply {
addCommand("${CMD_PREFIX}gitRollback ${pathToFile}")
addCommand("${CMD_PREFIX}gitRollbackFile ${pathToFile}")
}
fun <T : CommandChain> T.replaceText(startOffset: Int? = null, endOffset: Int? = null, newText: String? = null): T = apply {
@@ -1181,10 +1181,10 @@ 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 {
fun <T : CommandChain> T.startNewSpan(spanName: String): T = apply {
addCommand("${CMD_PREFIX}handleSpan $spanName")
}
fun <T : CommandChain> T.stopSpan(spanName: String): T = apply {
addCommand("${CMD_PREFIX}handleSpan $spanName")
}

View File

@@ -8,26 +8,27 @@ import io.opentelemetry.api.trace.Span
/**
* The first call will create and start span.
* The second call with the same spanName will stop span.
* !!!The hierarchy of spans can't be created using it since there is no propagation of the scope.
* */
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()
private val createdSpans: MutableMap<String, Span> = mutableMapOf()
}
override suspend fun doExecute(context: PlaybackContext) {
val spanName = extractCommandArgument(PREFIX)
val span = CREATED_SPANS[spanName]
val span = createdSpans[spanName]
if (span != null) {
span.end()
CREATED_SPANS.remove(spanName)
createdSpans.remove(spanName)
} else {
val newSpan = TelemetryManager.getTracer(Scope("HandleSpanCommand"))
.spanBuilder(spanName)
.startSpan()
CREATED_SPANS[spanName] = newSpan
createdSpans[spanName] = newSpan
}
}