Promise<Object> (final @NotNull PlaybackContext context) { final AsyncPromise<Object> result = new AsyncPromise<>(); String input = extractCommandArgument(PREFIX); String[] delayText = input.split("\\|"); final long delay = Integer.parseInt(delayText[0]); final String text = delayText[1] + END_CHAR; final boolean calculateAnalyzesTime = delayText.length > 2 && Boolean.parseBoolean(delayText[2]); Ref<Span> spanRef = new Ref<>(); var projectConnection = context.getProject().getMessageBus().simpleConnect(); var applicationConnection = ApplicationManager.getApplication().getMessageBus().connect(); var latencyRecorder = new LatencyRecord(); applicationConnection.subscribe(LatencyListener.TOPIC, new LatencyListener() { @Override public void recordTypingLatency(@NotNull Editor editor, String action, long latencyMs) { latencyRecorder.update(((int)latencyMs)); } }); Ref<DaemonCodeAnalyzerResult> job = new Ref<>(); ApplicationManager.getApplication().executeOnPooledThread(Context.current().wrap(() -> { TraceUtil.runWithSpanThrows(PerformanceTestSpan.TRACER, SPAN_NAME, span -> { span.addEvent("Finding typing target"); try { Waiter.checkCondition(() -> findTarget(context) != null).await(1, TimeUnit.MINUTES); } catch (InterruptedException e) { span.recordException(e); result.setError(e); return; } CountDownLatch allScheduled = new CountDownLatch(1); for (int i = 0; i < text.length(); i++) { char currentChar = text.charAt(i); boolean nextCharIsTheLast = ((i + 1) < text.length()) && (text.charAt(i + 1) == END_CHAR); myExecutor.schedule(() -> ApplicationManager.getApplication().invokeAndWait(Context.current().wrap( () -> { if (nextCharIsTheLast && calculateAnalyzesTime) { job.set(DaemonCodeAnalyzerListener.INSTANCE.listen(projectConnection, spanRef, 0, null)); var spanBuilder = PerformanceTestSpan.TRACER.spanBuilder(CODE_ANALYSIS_SPAN_NAME).setParent(Context.current().with(span)); spanRef.set(spanBuilder.startSpan()); } if (currentChar == END_CHAR) { allScheduled.countDown(); myExecutor.shutdown(); } else { span.addEvent("Calling find target second time in DelayTypeCommand"); TypingTarget typingTarget = findTarget(context); if (typingTarget != null) { span.addEvent("Typing " + currentChar); typingTarget.type(String.valueOf(currentChar)); } } })), i * delay, TimeUnit.MILLISECONDS); } try { allScheduled.await(); myExecutor.awaitTermination(1, TimeUnit.MINUTES); if (!latencyRecorder.getSamples().isEmpty()) { span.setAttribute("latency#max", latencyRecorder.getMaxLatency()); span.setAttribute("latency#p90", latencyRecorder.percentile(90)); span.setAttribute("latency#mean_value", latencyRecorder.getAverageLatency()); } } catch (InterruptedException e) { result.setError(e); } }); if (calculateAnalyzesTime) { job.get().blockingWaitForComplete(); } applicationConnection.disconnect(); result.setResult(null); })); return result; }