LegacyCpuUsageDataSeries implements DataSeries<Long> { @Override public List<SeriesData<Long>> getDataForRange(@NotNull Range timeCurrentRangeUs) { long bufferNs = TimeUnit.SECONDS.toNanos(1); CpuDataRequest.Builder dataRequestBuilder = CpuDataRequest.newBuilder() .setSession(mySession) .setStartTimestamp(TimeUnit.MICROSECONDS.toNanos((long)timeCurrentRangeUs.getMin()) - bufferNs) .setEndTimestamp(TimeUnit.MICROSECONDS.toNanos((long)timeCurrentRangeUs.getMax()) + bufferNs); CpuDataResponse response = myClient.getData(dataRequestBuilder.build()); return IntStream.range(0, response.getDataList().size() - 1) .mapToObj(index -> CpuUsage.getCpuUsageData(response.getData(index), response.getData(index + 1), myIsOtherProcess)) .collect(Collectors.toList()); } LegacyCpuUsageDataSeries(@NotNull CpuServiceGrpc.CpuServiceBlockingStub client,
                                  Common.Session session,
                                  boolean isOtherProcess); @Override List<SeriesData<Long>> getDataForRange(@NotNull Range timeCurrentRangeUs);  }
@Test public void thisProcessCpuUsage() { mySeries = new LegacyCpuUsageDataSeries( new ProfilerClient(myGrpcChannel.getName()).getCpuClient(), ProfilersTestData.SESSION_DATA, false); int systemTime = (int)(0.6 * FakeCpuService.TOTAL_ELAPSED_TIME); int appTime = (int)(0.4 * FakeCpuService.TOTAL_ELAPSED_TIME); myService.setSystemTimeMs(systemTime); myService.setAppTimeMs(appTime); List<SeriesData<Long>> seriesData = mySeries.getDataForRange(ANY_RANGE); assertEquals(1, seriesData.size()); SeriesData<Long> appUsageData = seriesData.get(0); assertNotNull(appUsageData); assertEquals(40, (long)appUsageData.value); systemTime = (int)(0.6 * FakeCpuService.TOTAL_ELAPSED_TIME); appTime = (int)(0.8 * FakeCpuService.TOTAL_ELAPSED_TIME); myService.setSystemTimeMs(systemTime); myService.setAppTimeMs(appTime); seriesData = mySeries.getDataForRange(ANY_RANGE); assertEquals(1, seriesData.size()); appUsageData = seriesData.get(0); assertNotNull(appUsageData); assertEquals(systemTime, (long)appUsageData.value); appTime = (int)(-0.2 * FakeCpuService.TOTAL_ELAPSED_TIME); myService.setAppTimeMs(appTime); seriesData = mySeries.getDataForRange(ANY_RANGE); assertEquals(1, seriesData.size()); appUsageData = seriesData.get(0); assertNotNull(appUsageData); assertEquals(0, (long)appUsageData.value); }
@Test public void otherProcessesCpuUsage() { mySeries = new LegacyCpuUsageDataSeries( new ProfilerClient(myGrpcChannel.getName()).getCpuClient(), ProfilersTestData.SESSION_DATA, true); int systemTime = (int)(0.6 * FakeCpuService.TOTAL_ELAPSED_TIME); myService.setSystemTimeMs(systemTime); List<SeriesData<Long>> seriesData = mySeries.getDataForRange(ANY_RANGE); assertEquals(1, seriesData.size()); SeriesData<Long> systemUsageData = seriesData.get(0); assertNotNull(systemUsageData); assertEquals(60, (long)systemUsageData.value); systemTime = (int)(1.5 * FakeCpuService.TOTAL_ELAPSED_TIME); myService.setSystemTimeMs(systemTime); seriesData = mySeries.getDataForRange(ANY_RANGE); assertEquals(1, seriesData.size()); systemUsageData = seriesData.get(0); assertNotNull(systemUsageData); assertEquals(100, (long)systemUsageData.value); systemTime = (int)(-0.5 * FakeCpuService.TOTAL_ELAPSED_TIME); myService.setSystemTimeMs(systemTime); seriesData = mySeries.getDataForRange(ANY_RANGE); assertEquals(1, seriesData.size()); systemUsageData = seriesData.get(0); assertNotNull(systemUsageData); assertEquals(0, (long)systemUsageData.value); }
@Test public void emptyData() { mySeries = new LegacyCpuUsageDataSeries( new ProfilerClient(myGrpcChannel.getName()).getCpuClient(), ProfilersTestData.SESSION_DATA, false); assertNotNull(mySeries); assertFalse(mySeries.getDataForRange(ANY_RANGE).isEmpty()); myService.setEmptyUsageData(true); assertTrue(mySeries.getDataForRange(ANY_RANGE).isEmpty()); }