MemoryDataSeries implements DataSeries<Long> { @Override public List<SeriesData<Long>> getDataForRange(@NotNull Range timeCurrentRangeUs) { long bufferNs = TimeUnit.SECONDS.toNanos(1); MemoryRequest.Builder dataRequestBuilder = MemoryRequest.newBuilder() .setSession(mySession) .setStartTime(TimeUnit.MICROSECONDS.toNanos((long)timeCurrentRangeUs.getMin()) - bufferNs) .setEndTime(TimeUnit.MICROSECONDS.toNanos((long)timeCurrentRangeUs.getMax()) + bufferNs); MemoryData response = myClient.getData(dataRequestBuilder.build()); List<SeriesData<Long>> seriesData = new ArrayList<>(); for (MemoryData.MemorySample sample : response.getMemSamplesList()) { long dataTimestamp = TimeUnit.NANOSECONDS.toMicros(sample.getTimestamp()); seriesData.add(new SeriesData<>(dataTimestamp, mySampleTransformer.apply(sample))); } return seriesData; } MemoryDataSeries(@NotNull MemoryServiceGrpc.MemoryServiceBlockingStub client,
                          @NotNull Common.Session session,
                          @NotNull Function<MemorySample, Long> transformer); @Override List<SeriesData<Long>> getDataForRange(@NotNull Range timeCurrentRangeUs);  }
@Test public void testTransformSingleSampleData() { MemoryData memoryData = MemoryData.newBuilder() .setEndTimestamp(TimeUnit.MICROSECONDS.toNanos(222)) .addMemSamples(MemoryData.MemorySample.newBuilder() .setTimestamp(TimeUnit.MICROSECONDS.toNanos(222)) .setMemoryUsage(Memory.MemoryUsageData.newBuilder() .setJavaMem(222).setTotalMem(222))) .build(); myService.setMemoryData(memoryData); MemoryDataSeries series = new MemoryDataSeries(new ProfilerClient(myGrpcChannel.getName()).getMemoryClient(), ProfilersTestData.SESSION_DATA, data -> 111L); List<SeriesData<Long>> seriesDataList = series.getDataForRange(new Range(0, Double.MAX_VALUE)); assertEquals(1, seriesDataList.size()); assertEquals(222, seriesDataList.get(0).x); assertEquals(111, seriesDataList.get(0).value.longValue()); }
@Test public void testDataIncludeMultipleSamples() { MemoryData memoryData = MemoryData.newBuilder() .setEndTimestamp(TimeUnit.MICROSECONDS.toNanos(555)) .addMemSamples(0, MemoryData.MemorySample.newBuilder() .setTimestamp(TimeUnit.MICROSECONDS.toNanos(333)) .setMemoryUsage(Memory.MemoryUsageData.newBuilder() .setJavaMem(333).setTotalMem(333))) .addMemSamples(1, MemoryData.MemorySample.newBuilder() .setTimestamp(TimeUnit.MICROSECONDS.toNanos(444)) .setMemoryUsage(Memory.MemoryUsageData.newBuilder() .setNativeMem(444).setTotalMem(444))) .build(); myService.setMemoryData(memoryData); MemoryDataSeries series = new MemoryDataSeries(new ProfilerClient(myGrpcChannel.getName()).getMemoryClient(), ProfilersTestData.SESSION_DATA, data -> 111L); List<SeriesData<Long>> seriesDataList = series.getDataForRange(new Range(0, Double.MAX_VALUE)); assertEquals(2, seriesDataList.size()); assertEquals(333, seriesDataList.get(0).x); assertEquals(111, seriesDataList.get(0).value.longValue()); assertEquals(444, seriesDataList.get(1).x); assertEquals(111, seriesDataList.get(1).value.longValue()); }