CpuProfiler extends StudioProfiler { @Override public void stopProfiling(Common.Session session) { List<CpuTraceInfo> traces = getTraceInfoFromSession(myProfilers.getClient(), session, myProfilers.getIdeServices().getFeatureConfig().isUnifiedPipelineEnabled()); CpuTraceInfo mostRecentTrace = traces.isEmpty() ? null : traces.get(traces.size() - 1); if (mostRecentTrace != null && mostRecentTrace.getToTimestamp() == -1) { stopTracing(myProfilers, session, mostRecentTrace.getConfiguration(), null); } myProfilers.getClient().getCpuClient().stopMonitoringApp(CpuStopRequest.newBuilder().setSession(session).build()); } CpuProfiler(@NotNull StudioProfilers profilers); @Nullable File getTraceFile(Common.Session session); @Override ProfilerMonitor newMonitor(); @Override void startProfiling(Common.Session session); @Override void stopProfiling(Common.Session session); @NotNull static List<CpuTraceInfo> getTraceInfoFromRange(@NotNull ProfilerClient client,
                                                         @NotNull Common.Session session,
                                                         @NotNull Range rangeUs,
                                                         boolean newPipeline); @NotNull static CpuTraceInfo getTraceInfoFromId(@NotNull StudioProfilers profilers, long traceId); @NotNull static List<CpuTraceInfo> getTraceInfoFromSession(@NotNull ProfilerClient client,
                                                           @NotNull Common.Session session,
                                                           boolean newPipeline); @NotNull static Common.Event getTraceStatusEventFromId(@NotNull StudioProfilers profilers, long traceId); static void stopTracing(@NotNull StudioProfilers profilers,
                                 @NotNull Common.Session session,
                                 @NotNull Cpu.CpuTraceConfiguration configuration,
                                 @Nullable Consumer<Cpu.TraceStopStatus> responseHandler);  }
@Test public void stopProfilingCallStopMonitoringAppId() { Assume.assumeFalse("Unified pipeline does not go through StopMonitoringApp", myUnifiedPipeline); myCpuProfiler = new CpuProfiler(myProfilers); myCpuProfiler.stopProfiling(FAKE_SESSION); assertThat(myCpuService.getSession()).isEqualTo(FAKE_SESSION); }
@Test public void stopMonitoringStopsOngoingTraces() { myCpuProfiler = new CpuProfiler(myProfilers); myCpuProfiler.stopProfiling(FAKE_SESSION); if (myUnifiedPipeline) { StopCpuTrace stopCpuTrace = (StopCpuTrace)myTransportService.getRegisteredCommand(Commands.Command.CommandType.STOP_CPU_TRACE); assertThat(stopCpuTrace.getLastTraceInfo()).isEqualTo(Cpu.CpuTraceInfo.getDefaultInstance()); myTransportService.addEventToStream( FAKE_SESSION.getStreamId(), Common.Event.newBuilder() .setTimestamp(1).setGroupId(1).setPid(FAKE_SESSION.getPid()).setKind(Common.Event.Kind.CPU_TRACE) .setCpuTrace(Cpu.CpuTraceData.newBuilder().setTraceStarted( Cpu.CpuTraceData.TraceStarted.newBuilder().setTraceInfo(Cpu.CpuTraceInfo.newBuilder().setTraceId(1).setToTimestamp(-1)))) .build()); myCpuProfiler.stopProfiling(FAKE_SESSION); assertThat(stopCpuTrace.getLastTraceInfo()).isNotEqualTo(Cpu.CpuTraceInfo.getDefaultInstance()); } else { assertThat(myCpuService.getStartStopCapturingSession()).isNull(); myCpuService.addTraceInfo(Cpu.CpuTraceInfo.newBuilder().setTraceId(1).setFromTimestamp(10).setToTimestamp(-1).build()); myCpuProfiler.stopProfiling(FAKE_SESSION); assertThat(myCpuService.getStartStopCapturingSession()).isEqualTo(FAKE_SESSION); } }