Skip to content

Commit 7f91916

Browse files
authored
Flattened PerformanceCollectionData (#4505)
* Merged CpuCollectionData and MemoryCollectionData into PerformanceCollectionData * PerformanceCollectionData now contains timestamp in nanoseconds, so we don't store Date objects anymore * Removed date objects from ProfileMeasurementValue
1 parent cd0db63 commit 7f91916

21 files changed

+147
-313
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
- Fix javadoc on TransportResult ([#4528](https://github.com/getsentry/sentry-java/pull/4528))
88

9+
### Internal
10+
11+
- Flattened PerformanceCollectionData ([#4505](https://github.com/getsentry/sentry-java/pull/4505))
12+
913
## 8.16.0
1014

1115
### Features

sentry-android-core/src/main/java/io/sentry/android/core/AndroidCpuCollector.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import android.os.SystemClock;
44
import android.system.Os;
55
import android.system.OsConstants;
6-
import io.sentry.CpuCollectionData;
76
import io.sentry.ILogger;
87
import io.sentry.IPerformanceSnapshotCollector;
98
import io.sentry.PerformanceCollectionData;
109
import io.sentry.SentryLevel;
11-
import io.sentry.SentryNanotimeDate;
1210
import io.sentry.util.FileUtils;
1311
import io.sentry.util.Objects;
1412
import java.io.File;
@@ -72,11 +70,8 @@ public void collect(final @NotNull PerformanceCollectionData performanceCollecti
7270
// number from 0 to 100, so we are going to multiply it by 100
7371
final double cpuUsagePercentage = cpuNanosDiff / (double) realTimeNanosDiff;
7472

75-
CpuCollectionData cpuData =
76-
new CpuCollectionData(
77-
(cpuUsagePercentage / (double) numCores) * 100.0, new SentryNanotimeDate());
78-
79-
performanceCollectionData.addCpuData(cpuData);
73+
performanceCollectionData.setCpuUsagePercentage(
74+
(cpuUsagePercentage / (double) numCores) * 100.0);
8075
}
8176

8277
/** Read the /proc/self/stat file and parses the result. */

sentry-android-core/src/main/java/io/sentry/android/core/AndroidMemoryCollector.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
import android.os.Debug;
44
import io.sentry.IPerformanceSnapshotCollector;
5-
import io.sentry.MemoryCollectionData;
65
import io.sentry.PerformanceCollectionData;
7-
import io.sentry.SentryNanotimeDate;
86
import org.jetbrains.annotations.ApiStatus;
97
import org.jetbrains.annotations.NotNull;
108

@@ -18,8 +16,7 @@ public void setup() {}
1816
public void collect(final @NotNull PerformanceCollectionData performanceCollectionData) {
1917
long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
2018
long usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize();
21-
MemoryCollectionData memoryData =
22-
new MemoryCollectionData(usedMemory, usedNativeMemory, new SentryNanotimeDate());
23-
performanceCollectionData.addMemoryData(memoryData);
19+
performanceCollectionData.setUsedHeapMemory(usedMemory);
20+
performanceCollectionData.setUsedNativeMemory(usedNativeMemory);
2421
}
2522
}

sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@
44
import android.os.Debug;
55
import android.os.Process;
66
import android.os.SystemClock;
7-
import io.sentry.CpuCollectionData;
87
import io.sentry.DateUtils;
98
import io.sentry.ILogger;
109
import io.sentry.ISentryExecutorService;
1110
import io.sentry.ISentryLifecycleToken;
12-
import io.sentry.MemoryCollectionData;
1311
import io.sentry.PerformanceCollectionData;
14-
import io.sentry.SentryDate;
1512
import io.sentry.SentryLevel;
1613
import io.sentry.SentryNanotimeDate;
1714
import io.sentry.SentryUUID;
@@ -156,7 +153,7 @@ public void onFrameMetricCollected(
156153
// profileStartNanos is calculated through SystemClock.elapsedRealtimeNanos(),
157154
// but frameEndNanos uses System.nanotime(), so we convert it to get the timestamp
158155
// relative to profileStartNanos
159-
final SentryDate timestamp = new SentryNanotimeDate();
156+
final long timestampNanos = new SentryNanotimeDate().nanoTimestamp();
160157
final long frameTimestampRelativeNanos =
161158
frameEndNanos
162159
- System.nanoTime()
@@ -171,17 +168,17 @@ public void onFrameMetricCollected(
171168
if (isFrozen) {
172169
frozenFrameRenderMeasurements.addLast(
173170
new ProfileMeasurementValue(
174-
frameTimestampRelativeNanos, durationNanos, timestamp));
171+
frameTimestampRelativeNanos, durationNanos, timestampNanos));
175172
} else if (isSlow) {
176173
slowFrameRenderMeasurements.addLast(
177174
new ProfileMeasurementValue(
178-
frameTimestampRelativeNanos, durationNanos, timestamp));
175+
frameTimestampRelativeNanos, durationNanos, timestampNanos));
179176
}
180177
if (refreshRate != lastRefreshRate) {
181178
lastRefreshRate = refreshRate;
182179
screenFrameRateMeasurements.addLast(
183180
new ProfileMeasurementValue(
184-
frameTimestampRelativeNanos, refreshRate, timestamp));
181+
frameTimestampRelativeNanos, refreshRate, timestampNanos));
185182
}
186183
}
187184
});
@@ -318,32 +315,28 @@ private void putPerformanceCollectionDataInMeasurements(
318315
new ArrayDeque<>(performanceCollectionData.size());
319316

320317
synchronized (performanceCollectionData) {
321-
for (PerformanceCollectionData performanceData : performanceCollectionData) {
322-
CpuCollectionData cpuData = performanceData.getCpuData();
323-
MemoryCollectionData memoryData = performanceData.getMemoryData();
324-
if (cpuData != null) {
318+
for (final @NotNull PerformanceCollectionData data : performanceCollectionData) {
319+
final long nanoTimestamp = data.getNanoTimestamp();
320+
final long relativeStartNs = nanoTimestamp + timestampDiff;
321+
final @Nullable Double cpuUsagePercentage = data.getCpuUsagePercentage();
322+
final @Nullable Long usedHeapMemory = data.getUsedHeapMemory();
323+
final @Nullable Long usedNativeMemory = data.getUsedNativeMemory();
324+
325+
if (cpuUsagePercentage != null) {
325326
cpuUsageMeasurements.add(
326-
new ProfileMeasurementValue(
327-
cpuData.getTimestamp().nanoTimestamp() + timestampDiff,
328-
cpuData.getCpuUsagePercentage(),
329-
cpuData.getTimestamp()));
327+
new ProfileMeasurementValue(relativeStartNs, cpuUsagePercentage, nanoTimestamp));
330328
}
331-
if (memoryData != null && memoryData.getUsedHeapMemory() > -1) {
329+
if (usedHeapMemory != null) {
332330
memoryUsageMeasurements.add(
333-
new ProfileMeasurementValue(
334-
memoryData.getTimestamp().nanoTimestamp() + timestampDiff,
335-
memoryData.getUsedHeapMemory(),
336-
memoryData.getTimestamp()));
331+
new ProfileMeasurementValue(relativeStartNs, usedHeapMemory, nanoTimestamp));
337332
}
338-
if (memoryData != null && memoryData.getUsedNativeMemory() > -1) {
333+
if (usedNativeMemory != null) {
339334
nativeMemoryUsageMeasurements.add(
340-
new ProfileMeasurementValue(
341-
memoryData.getTimestamp().nanoTimestamp() + timestampDiff,
342-
memoryData.getUsedNativeMemory(),
343-
memoryData.getTimestamp()));
335+
new ProfileMeasurementValue(relativeStartNs, usedNativeMemory, nanoTimestamp));
344336
}
345337
}
346338
}
339+
347340
if (!cpuUsageMeasurements.isEmpty()) {
348341
measurementsMap.put(
349342
ProfileMeasurement.ID_CPU_USAGE,

sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ import android.os.Build
55
import androidx.test.core.app.ApplicationProvider
66
import androidx.test.ext.junit.runners.AndroidJUnit4
77
import io.sentry.CompositePerformanceCollector
8-
import io.sentry.CpuCollectionData
98
import io.sentry.DataCategory
109
import io.sentry.IConnectionStatusProvider
1110
import io.sentry.ILogger
1211
import io.sentry.IScopes
13-
import io.sentry.MemoryCollectionData
1412
import io.sentry.PerformanceCollectionData
1513
import io.sentry.ProfileLifecycle
1614
import io.sentry.Sentry
1715
import io.sentry.SentryLevel
18-
import io.sentry.SentryNanotimeDate
1916
import io.sentry.SentryTracer
2017
import io.sentry.TracesSampler
2118
import io.sentry.TransactionContext
@@ -427,10 +424,11 @@ class AndroidContinuousProfilerTest {
427424
@Test
428425
fun `profiler sends chunk with measurements`() {
429426
val performanceCollector = mock<CompositePerformanceCollector>()
430-
val collectionData = PerformanceCollectionData()
427+
val collectionData = PerformanceCollectionData(10)
431428

432-
collectionData.addMemoryData(MemoryCollectionData(2, 3, SentryNanotimeDate()))
433-
collectionData.addCpuData(CpuCollectionData(3.0, SentryNanotimeDate()))
429+
collectionData.usedHeapMemory = 2
430+
collectionData.usedNativeMemory = 3
431+
collectionData.cpuUsagePercentage = 3.0
434432
whenever(performanceCollector.stop(any<String>())).thenReturn(listOf(collectionData))
435433

436434
fixture.options.compositePerformanceCollector = performanceCollector

sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@ class AndroidCpuCollectorTest {
2828

2929
@Test
3030
fun `collect works only after setup`() {
31-
val data = PerformanceCollectionData()
31+
val data = PerformanceCollectionData(10)
3232
fixture.getSut().collect(data)
33-
assertNull(data.cpuData)
33+
assertNull(data.cpuUsagePercentage)
3434
}
3535

3636
@Test
3737
fun `when collect cpu is collected`() {
38-
val data = PerformanceCollectionData()
38+
val data = PerformanceCollectionData(10)
3939
val collector = fixture.getSut()
4040
collector.setup()
4141
collector.collect(data)
42-
val cpuData = data.cpuData
42+
val cpuData = data.cpuUsagePercentage
4343
assertNotNull(cpuData)
44-
assertNotEquals(0.0, cpuData.cpuUsagePercentage)
45-
assertNotEquals(0, cpuData.timestamp.nanoTimestamp())
44+
assertNotEquals(0.0, cpuData)
4645
}
4746
}

sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ class AndroidMemoryCollectorTest {
1717

1818
@Test
1919
fun `when collect, both native and heap memory are collected`() {
20-
val data = PerformanceCollectionData()
20+
val data = PerformanceCollectionData(10)
2121
val usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize()
2222
val usedMemory = fixture.runtime.totalMemory() - fixture.runtime.freeMemory()
2323
fixture.collector.collect(data)
24-
val memoryData = data.memoryData
25-
assertNotNull(memoryData)
26-
assertNotEquals(-1, memoryData.usedNativeMemory)
27-
assertEquals(usedNativeMemory, memoryData.usedNativeMemory)
28-
assertEquals(usedMemory, memoryData.usedHeapMemory)
29-
assertNotEquals(0, memoryData.timestamp.nanoTimestamp())
24+
assertNotNull(data.usedHeapMemory)
25+
assertNotNull(data.usedNativeMemory)
26+
assertNotEquals(-1, data.usedNativeMemory)
27+
assertEquals(usedNativeMemory, data.usedNativeMemory)
28+
assertEquals(usedMemory, data.usedHeapMemory)
3029
}
3130
}

sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ package io.sentry.android.core
33
import android.content.Context
44
import androidx.test.core.app.ApplicationProvider
55
import androidx.test.ext.junit.runners.AndroidJUnit4
6-
import io.sentry.CpuCollectionData
76
import io.sentry.ILogger
87
import io.sentry.ISentryExecutorService
9-
import io.sentry.MemoryCollectionData
108
import io.sentry.PerformanceCollectionData
11-
import io.sentry.SentryDate
129
import io.sentry.SentryExecutorService
1310
import io.sentry.SentryLevel
1411
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector
@@ -276,15 +273,15 @@ class AndroidProfilerTest {
276273
fun `profiler includes performance measurements when passed on end`() {
277274
val profiler = fixture.getSut()
278275
val performanceCollectionData = ArrayList<PerformanceCollectionData>()
279-
var singleData = PerformanceCollectionData()
280-
val t1 = mock<SentryDate>()
281-
val t2 = mock<SentryDate>()
282-
singleData.addMemoryData(MemoryCollectionData(2, 3, t1))
283-
singleData.addCpuData(CpuCollectionData(1.4, t1))
276+
var singleData = PerformanceCollectionData(10)
277+
singleData.usedHeapMemory = 2
278+
singleData.usedNativeMemory = 3
279+
singleData.cpuUsagePercentage = 1.4
284280
performanceCollectionData.add(singleData)
285281

286-
singleData = PerformanceCollectionData()
287-
singleData.addMemoryData(MemoryCollectionData(3, 4, t2))
282+
singleData = PerformanceCollectionData(20)
283+
singleData.usedHeapMemory = 3
284+
singleData.usedNativeMemory = 4
288285
performanceCollectionData.add(singleData)
289286

290287
profiler.start()

sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import android.content.Context
44
import android.os.Build
55
import androidx.test.core.app.ApplicationProvider
66
import androidx.test.ext.junit.runners.AndroidJUnit4
7-
import io.sentry.CpuCollectionData
87
import io.sentry.ILogger
98
import io.sentry.IScopes
109
import io.sentry.ISentryExecutorService
11-
import io.sentry.MemoryCollectionData
1210
import io.sentry.PerformanceCollectionData
1311
import io.sentry.ProfilingTraceData
1412
import io.sentry.SentryLevel
@@ -472,13 +470,15 @@ class AndroidTransactionProfilerTest {
472470
fun `profiler includes performance measurements when passed on transaction finish`() {
473471
val profiler = fixture.getSut(context)
474472
val performanceCollectionData = ArrayList<PerformanceCollectionData>()
475-
var singleData = PerformanceCollectionData()
476-
singleData.addMemoryData(MemoryCollectionData(2, 3, mock()))
477-
singleData.addCpuData(CpuCollectionData(1.4, mock()))
473+
var singleData = PerformanceCollectionData(10)
474+
singleData.usedHeapMemory = 2
475+
singleData.usedNativeMemory = 3
476+
singleData.cpuUsagePercentage = 1.4
478477
performanceCollectionData.add(singleData)
479478

480-
singleData = PerformanceCollectionData()
481-
singleData.addMemoryData(MemoryCollectionData(3, 4, mock()))
479+
singleData = PerformanceCollectionData(20)
480+
singleData.usedHeapMemory = 3
481+
singleData.usedNativeMemory = 4
482482
performanceCollectionData.add(singleData)
483483

484484
profiler.start()

sentry/api/sentry.api

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -334,12 +334,6 @@ public abstract interface class io/sentry/CompositePerformanceCollector {
334334
public abstract fun stop (Ljava/lang/String;)Ljava/util/List;
335335
}
336336

337-
public final class io/sentry/CpuCollectionData {
338-
public fun <init> (DLio/sentry/SentryDate;)V
339-
public fun getCpuUsagePercentage ()D
340-
public fun getTimestamp ()Lio/sentry/SentryDate;
341-
}
342-
343337
public final class io/sentry/CustomSamplingContext {
344338
public fun <init> ()V
345339
public fun get (Ljava/lang/String;)Ljava/lang/Object;
@@ -791,10 +785,6 @@ public abstract interface class io/sentry/ILogger {
791785
public abstract fun log (Lio/sentry/SentryLevel;Ljava/lang/Throwable;Ljava/lang/String;[Ljava/lang/Object;)V
792786
}
793787

794-
public abstract interface class io/sentry/IMemoryCollector {
795-
public abstract fun collect ()Lio/sentry/MemoryCollectionData;
796-
}
797-
798788
public abstract interface class io/sentry/IOptionsObserver {
799789
public abstract fun setDist (Ljava/lang/String;)V
800790
public abstract fun setEnvironment (Ljava/lang/String;)V
@@ -1349,14 +1339,6 @@ public final class io/sentry/MeasurementUnit$Information : java/lang/Enum, io/se
13491339
public static fun values ()[Lio/sentry/MeasurementUnit$Information;
13501340
}
13511341

1352-
public final class io/sentry/MemoryCollectionData {
1353-
public fun <init> (JJLio/sentry/SentryDate;)V
1354-
public fun <init> (JLio/sentry/SentryDate;)V
1355-
public fun getTimestamp ()Lio/sentry/SentryDate;
1356-
public fun getUsedHeapMemory ()J
1357-
public fun getUsedNativeMemory ()J
1358-
}
1359-
13601342
public final class io/sentry/MonitorConfig : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
13611343
public fun <init> (Lio/sentry/MonitorSchedule;)V
13621344
public fun getCheckinMargin ()Ljava/lang/Long;
@@ -1931,11 +1913,14 @@ public final class io/sentry/OutboxSender : io/sentry/IEnvelopeSender {
19311913
}
19321914

19331915
public final class io/sentry/PerformanceCollectionData {
1934-
public fun <init> ()V
1935-
public fun addCpuData (Lio/sentry/CpuCollectionData;)V
1936-
public fun addMemoryData (Lio/sentry/MemoryCollectionData;)V
1937-
public fun getCpuData ()Lio/sentry/CpuCollectionData;
1938-
public fun getMemoryData ()Lio/sentry/MemoryCollectionData;
1916+
public fun <init> (J)V
1917+
public fun getCpuUsagePercentage ()Ljava/lang/Double;
1918+
public fun getNanoTimestamp ()J
1919+
public fun getUsedHeapMemory ()Ljava/lang/Long;
1920+
public fun getUsedNativeMemory ()Ljava/lang/Long;
1921+
public fun setCpuUsagePercentage (Ljava/lang/Double;)V
1922+
public fun setUsedHeapMemory (Ljava/lang/Long;)V
1923+
public fun setUsedNativeMemory (Ljava/lang/Long;)V
19391924
}
19401925

19411926
public final class io/sentry/ProfileChunk : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
@@ -4918,10 +4903,10 @@ public final class io/sentry/profilemeasurements/ProfileMeasurement$JsonKeys {
49184903

49194904
public final class io/sentry/profilemeasurements/ProfileMeasurementValue : io/sentry/JsonSerializable, io/sentry/JsonUnknown {
49204905
public fun <init> ()V
4921-
public fun <init> (Ljava/lang/Long;Ljava/lang/Number;Lio/sentry/SentryDate;)V
4906+
public fun <init> (Ljava/lang/Long;Ljava/lang/Number;J)V
49224907
public fun equals (Ljava/lang/Object;)Z
49234908
public fun getRelativeStartNs ()Ljava/lang/String;
4924-
public fun getTimestamp ()Ljava/lang/Double;
4909+
public fun getTimestamp ()D
49254910
public fun getUnknown ()Ljava/util/Map;
49264911
public fun getValue ()D
49274912
public fun hashCode ()I

0 commit comments

Comments
 (0)