AllocStatsDataSeries implements DataSeries<Long> { @Override public List<SeriesData<Long>> getDataForRange(@NotNull Range rangeUs) { if (!myIsAgentAttached) { return Collections.emptyList(); } if (!myProfilers.getIdeServices().getFeatureConfig().isUnifiedPipelineEnabled()) { return getLegacyAllocationStats(rangeUs); } Transport.GetEventGroupsRequest request = Transport.GetEventGroupsRequest.newBuilder() .setStreamId(mySession.getStreamId()) .setPid(mySession.getPid()) .setKind(Common.Event.Kind.MEMORY_ALLOC_STATS) .setFromTimestamp(TimeUnit.MICROSECONDS.toNanos((long)rangeUs.getMin())) .setToTimestamp(TimeUnit.MICROSECONDS.toNanos((long)rangeUs.getMax())) .build(); Transport.GetEventGroupsResponse response = myClient.getTransportClient().getEventGroups(request); assert response.getGroupsCount() <= 1; List<SeriesData<Long>> seriesData = new ArrayList<>(); if (response.getGroupsCount() > 0) { response.getGroups(0).getEventsList().forEach(event -> { long dataTimestamp = TimeUnit.NANOSECONDS.toMicros(event.getTimestamp()); seriesData.add(new SeriesData<>(dataTimestamp, myFilter.apply(event.getMemoryAllocStats()))); }); } return seriesData; } AllocStatsDataSeries(@NotNull StudioProfilers profilers,
                              @NotNull Function<MemoryAllocStatsData, Long> filter); @Override List<SeriesData<Long>> getDataForRange(@NotNull Range rangeUs);  }
@Test public void testGetDataForXRange() { myTransportService.setAgentStatus(DEFAULT_AGENT_ATTACHED_RESPONSE); StudioProfilers studioProfilers = new StudioProfilers(new ProfilerClient(myGrpcChannel.getName()), myIdeProfilerServices, myTimer); studioProfilers.setPreferredProcess(FAKE_DEVICE_NAME, FAKE_PROCESS_NAME, null); myTimer.tick(TimeUnit.SECONDS.toNanos(1)); MemoryAllocStatsData stats1 = MemoryAllocStatsData.newBuilder().setJavaAllocationCount(1000).setJavaFreeCount(2000).build(); MemoryAllocStatsData stats2 = MemoryAllocStatsData.newBuilder().setJavaAllocationCount(1500).setJavaFreeCount(2500).build(); long timestamp1 = TimeUnit.MICROSECONDS.toNanos(3); long timestamp2 = TimeUnit.MICROSECONDS.toNanos(14); if (myNewDataPipeline) { myTransportService.addEventToStream(FakeTransportService.FAKE_DEVICE_ID, Common.Event.newBuilder() .setPid(FakeTransportService.FAKE_PROCESS.getPid()) .setTimestamp(timestamp1) .setKind(Common.Event.Kind.MEMORY_ALLOC_STATS) .setMemoryAllocStats(stats1) .build()); myTransportService.addEventToStream(FakeTransportService.FAKE_DEVICE_ID, Common.Event.newBuilder() .setPid(FakeTransportService.FAKE_PROCESS.getPid()) .setTimestamp(timestamp2) .setKind(Common.Event.Kind.MEMORY_ALLOC_STATS) .setMemoryAllocStats(stats2) .build()); } else { MemoryData memoryData = MemoryData.newBuilder() .setEndTimestamp(1) .addAllocStatsSamples( MemoryData.AllocStatsSample.newBuilder().setTimestamp(TimeUnit.MICROSECONDS.toNanos(3)).setAllocStats(stats1)) .addAllocStatsSamples( MemoryData.AllocStatsSample.newBuilder().setTimestamp(TimeUnit.MICROSECONDS.toNanos(14)).setAllocStats(stats2)) .build(); myService.setMemoryData(memoryData); } AllocStatsDataSeries series = new AllocStatsDataSeries(studioProfilers, sample -> (long)sample.getJavaAllocationCount()); List<SeriesData<Long>> dataList = series.getDataForRange(new Range(0, Double.MAX_VALUE)); assertEquals(2, dataList.size()); assertEquals(3, dataList.get(0).x); assertEquals(1000, dataList.get(0).value.longValue()); assertEquals(14, dataList.get(1).x); assertEquals(1500, dataList.get(1).value.longValue()); series = new AllocStatsDataSeries(studioProfilers, sample -> (long)sample.getJavaFreeCount()); dataList = series.getDataForRange(new Range(0, Double.MAX_VALUE)); assertEquals(2, dataList.size()); assertEquals(3, dataList.get(0).x); assertEquals(2000, dataList.get(0).value.longValue()); assertEquals(14, dataList.get(1).x); assertEquals(2500, dataList.get(1).value.longValue()); }