diff --git a/CHANGELOG.md b/CHANGELOG.md index d56be095db..53a7547874 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ - Fix javadoc on TransportResult ([#4528](https://github.com/getsentry/sentry-java/pull/4528)) +### Internal + +- Flattened PerformanceCollectionData ([#4505](https://github.com/getsentry/sentry-java/pull/4505)) + ## 8.16.0 ### Features diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidCpuCollector.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidCpuCollector.java index 800d4826fe..ea7a20deab 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidCpuCollector.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidCpuCollector.java @@ -3,12 +3,10 @@ import android.os.SystemClock; import android.system.Os; import android.system.OsConstants; -import io.sentry.CpuCollectionData; import io.sentry.ILogger; import io.sentry.IPerformanceSnapshotCollector; import io.sentry.PerformanceCollectionData; import io.sentry.SentryLevel; -import io.sentry.SentryNanotimeDate; import io.sentry.util.FileUtils; import io.sentry.util.Objects; import java.io.File; @@ -72,11 +70,8 @@ public void collect(final @NotNull PerformanceCollectionData performanceCollecti // number from 0 to 100, so we are going to multiply it by 100 final double cpuUsagePercentage = cpuNanosDiff / (double) realTimeNanosDiff; - CpuCollectionData cpuData = - new CpuCollectionData( - (cpuUsagePercentage / (double) numCores) * 100.0, new SentryNanotimeDate()); - - performanceCollectionData.addCpuData(cpuData); + performanceCollectionData.setCpuUsagePercentage( + (cpuUsagePercentage / (double) numCores) * 100.0); } /** Read the /proc/self/stat file and parses the result. */ diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidMemoryCollector.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidMemoryCollector.java index 41c2d2da03..6775d818b4 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidMemoryCollector.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidMemoryCollector.java @@ -2,9 +2,7 @@ import android.os.Debug; import io.sentry.IPerformanceSnapshotCollector; -import io.sentry.MemoryCollectionData; import io.sentry.PerformanceCollectionData; -import io.sentry.SentryNanotimeDate; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -18,8 +16,7 @@ public void setup() {} public void collect(final @NotNull PerformanceCollectionData performanceCollectionData) { long usedMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory(); long usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize(); - MemoryCollectionData memoryData = - new MemoryCollectionData(usedMemory, usedNativeMemory, new SentryNanotimeDate()); - performanceCollectionData.addMemoryData(memoryData); + performanceCollectionData.setUsedHeapMemory(usedMemory); + performanceCollectionData.setUsedNativeMemory(usedNativeMemory); } } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java index e51e06fc6e..c677252981 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AndroidProfiler.java @@ -4,14 +4,11 @@ import android.os.Debug; import android.os.Process; import android.os.SystemClock; -import io.sentry.CpuCollectionData; import io.sentry.DateUtils; import io.sentry.ILogger; import io.sentry.ISentryExecutorService; import io.sentry.ISentryLifecycleToken; -import io.sentry.MemoryCollectionData; import io.sentry.PerformanceCollectionData; -import io.sentry.SentryDate; import io.sentry.SentryLevel; import io.sentry.SentryNanotimeDate; import io.sentry.SentryUUID; @@ -156,7 +153,7 @@ public void onFrameMetricCollected( // profileStartNanos is calculated through SystemClock.elapsedRealtimeNanos(), // but frameEndNanos uses System.nanotime(), so we convert it to get the timestamp // relative to profileStartNanos - final SentryDate timestamp = new SentryNanotimeDate(); + final long timestampNanos = new SentryNanotimeDate().nanoTimestamp(); final long frameTimestampRelativeNanos = frameEndNanos - System.nanoTime() @@ -171,17 +168,17 @@ public void onFrameMetricCollected( if (isFrozen) { frozenFrameRenderMeasurements.addLast( new ProfileMeasurementValue( - frameTimestampRelativeNanos, durationNanos, timestamp)); + frameTimestampRelativeNanos, durationNanos, timestampNanos)); } else if (isSlow) { slowFrameRenderMeasurements.addLast( new ProfileMeasurementValue( - frameTimestampRelativeNanos, durationNanos, timestamp)); + frameTimestampRelativeNanos, durationNanos, timestampNanos)); } if (refreshRate != lastRefreshRate) { lastRefreshRate = refreshRate; screenFrameRateMeasurements.addLast( new ProfileMeasurementValue( - frameTimestampRelativeNanos, refreshRate, timestamp)); + frameTimestampRelativeNanos, refreshRate, timestampNanos)); } } }); @@ -318,32 +315,28 @@ private void putPerformanceCollectionDataInMeasurements( new ArrayDeque<>(performanceCollectionData.size()); synchronized (performanceCollectionData) { - for (PerformanceCollectionData performanceData : performanceCollectionData) { - CpuCollectionData cpuData = performanceData.getCpuData(); - MemoryCollectionData memoryData = performanceData.getMemoryData(); - if (cpuData != null) { + for (final @NotNull PerformanceCollectionData data : performanceCollectionData) { + final long nanoTimestamp = data.getNanoTimestamp(); + final long relativeStartNs = nanoTimestamp + timestampDiff; + final @Nullable Double cpuUsagePercentage = data.getCpuUsagePercentage(); + final @Nullable Long usedHeapMemory = data.getUsedHeapMemory(); + final @Nullable Long usedNativeMemory = data.getUsedNativeMemory(); + + if (cpuUsagePercentage != null) { cpuUsageMeasurements.add( - new ProfileMeasurementValue( - cpuData.getTimestamp().nanoTimestamp() + timestampDiff, - cpuData.getCpuUsagePercentage(), - cpuData.getTimestamp())); + new ProfileMeasurementValue(relativeStartNs, cpuUsagePercentage, nanoTimestamp)); } - if (memoryData != null && memoryData.getUsedHeapMemory() > -1) { + if (usedHeapMemory != null) { memoryUsageMeasurements.add( - new ProfileMeasurementValue( - memoryData.getTimestamp().nanoTimestamp() + timestampDiff, - memoryData.getUsedHeapMemory(), - memoryData.getTimestamp())); + new ProfileMeasurementValue(relativeStartNs, usedHeapMemory, nanoTimestamp)); } - if (memoryData != null && memoryData.getUsedNativeMemory() > -1) { + if (usedNativeMemory != null) { nativeMemoryUsageMeasurements.add( - new ProfileMeasurementValue( - memoryData.getTimestamp().nanoTimestamp() + timestampDiff, - memoryData.getUsedNativeMemory(), - memoryData.getTimestamp())); + new ProfileMeasurementValue(relativeStartNs, usedNativeMemory, nanoTimestamp)); } } } + if (!cpuUsageMeasurements.isEmpty()) { measurementsMap.put( ProfileMeasurement.ID_CPU_USAGE, diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt index 53223b70c9..8539d27143 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidContinuousProfilerTest.kt @@ -5,17 +5,14 @@ import android.os.Build import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 import io.sentry.CompositePerformanceCollector -import io.sentry.CpuCollectionData import io.sentry.DataCategory import io.sentry.IConnectionStatusProvider import io.sentry.ILogger import io.sentry.IScopes -import io.sentry.MemoryCollectionData import io.sentry.PerformanceCollectionData import io.sentry.ProfileLifecycle import io.sentry.Sentry import io.sentry.SentryLevel -import io.sentry.SentryNanotimeDate import io.sentry.SentryTracer import io.sentry.TracesSampler import io.sentry.TransactionContext @@ -427,10 +424,11 @@ class AndroidContinuousProfilerTest { @Test fun `profiler sends chunk with measurements`() { val performanceCollector = mock() - val collectionData = PerformanceCollectionData() + val collectionData = PerformanceCollectionData(10) - collectionData.addMemoryData(MemoryCollectionData(2, 3, SentryNanotimeDate())) - collectionData.addCpuData(CpuCollectionData(3.0, SentryNanotimeDate())) + collectionData.usedHeapMemory = 2 + collectionData.usedNativeMemory = 3 + collectionData.cpuUsagePercentage = 3.0 whenever(performanceCollector.stop(any())).thenReturn(listOf(collectionData)) fixture.options.compositePerformanceCollector = performanceCollector diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt index 028ccb4a5a..1b855aabc6 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidCpuCollectorTest.kt @@ -28,20 +28,19 @@ class AndroidCpuCollectorTest { @Test fun `collect works only after setup`() { - val data = PerformanceCollectionData() + val data = PerformanceCollectionData(10) fixture.getSut().collect(data) - assertNull(data.cpuData) + assertNull(data.cpuUsagePercentage) } @Test fun `when collect cpu is collected`() { - val data = PerformanceCollectionData() + val data = PerformanceCollectionData(10) val collector = fixture.getSut() collector.setup() collector.collect(data) - val cpuData = data.cpuData + val cpuData = data.cpuUsagePercentage assertNotNull(cpuData) - assertNotEquals(0.0, cpuData.cpuUsagePercentage) - assertNotEquals(0, cpuData.timestamp.nanoTimestamp()) + assertNotEquals(0.0, cpuData) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt index 4d59f9f609..23214c040c 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidMemoryCollectorTest.kt @@ -17,15 +17,14 @@ class AndroidMemoryCollectorTest { @Test fun `when collect, both native and heap memory are collected`() { - val data = PerformanceCollectionData() + val data = PerformanceCollectionData(10) val usedNativeMemory = Debug.getNativeHeapSize() - Debug.getNativeHeapFreeSize() val usedMemory = fixture.runtime.totalMemory() - fixture.runtime.freeMemory() fixture.collector.collect(data) - val memoryData = data.memoryData - assertNotNull(memoryData) - assertNotEquals(-1, memoryData.usedNativeMemory) - assertEquals(usedNativeMemory, memoryData.usedNativeMemory) - assertEquals(usedMemory, memoryData.usedHeapMemory) - assertNotEquals(0, memoryData.timestamp.nanoTimestamp()) + assertNotNull(data.usedHeapMemory) + assertNotNull(data.usedNativeMemory) + assertNotEquals(-1, data.usedNativeMemory) + assertEquals(usedNativeMemory, data.usedNativeMemory) + assertEquals(usedMemory, data.usedHeapMemory) } } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt index fc5bd0ec34..998a24b3d2 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidProfilerTest.kt @@ -3,12 +3,9 @@ package io.sentry.android.core import android.content.Context import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.sentry.CpuCollectionData import io.sentry.ILogger import io.sentry.ISentryExecutorService -import io.sentry.MemoryCollectionData import io.sentry.PerformanceCollectionData -import io.sentry.SentryDate import io.sentry.SentryExecutorService import io.sentry.SentryLevel import io.sentry.android.core.internal.util.SentryFrameMetricsCollector @@ -276,15 +273,15 @@ class AndroidProfilerTest { fun `profiler includes performance measurements when passed on end`() { val profiler = fixture.getSut() val performanceCollectionData = ArrayList() - var singleData = PerformanceCollectionData() - val t1 = mock() - val t2 = mock() - singleData.addMemoryData(MemoryCollectionData(2, 3, t1)) - singleData.addCpuData(CpuCollectionData(1.4, t1)) + var singleData = PerformanceCollectionData(10) + singleData.usedHeapMemory = 2 + singleData.usedNativeMemory = 3 + singleData.cpuUsagePercentage = 1.4 performanceCollectionData.add(singleData) - singleData = PerformanceCollectionData() - singleData.addMemoryData(MemoryCollectionData(3, 4, t2)) + singleData = PerformanceCollectionData(20) + singleData.usedHeapMemory = 3 + singleData.usedNativeMemory = 4 performanceCollectionData.add(singleData) profiler.start() diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt index 6de23ebbd0..0490bc30d0 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AndroidTransactionProfilerTest.kt @@ -4,11 +4,9 @@ import android.content.Context import android.os.Build import androidx.test.core.app.ApplicationProvider import androidx.test.ext.junit.runners.AndroidJUnit4 -import io.sentry.CpuCollectionData import io.sentry.ILogger import io.sentry.IScopes import io.sentry.ISentryExecutorService -import io.sentry.MemoryCollectionData import io.sentry.PerformanceCollectionData import io.sentry.ProfilingTraceData import io.sentry.SentryLevel @@ -472,13 +470,15 @@ class AndroidTransactionProfilerTest { fun `profiler includes performance measurements when passed on transaction finish`() { val profiler = fixture.getSut(context) val performanceCollectionData = ArrayList() - var singleData = PerformanceCollectionData() - singleData.addMemoryData(MemoryCollectionData(2, 3, mock())) - singleData.addCpuData(CpuCollectionData(1.4, mock())) + var singleData = PerformanceCollectionData(10) + singleData.usedHeapMemory = 2 + singleData.usedNativeMemory = 3 + singleData.cpuUsagePercentage = 1.4 performanceCollectionData.add(singleData) - singleData = PerformanceCollectionData() - singleData.addMemoryData(MemoryCollectionData(3, 4, mock())) + singleData = PerformanceCollectionData(20) + singleData.usedHeapMemory = 3 + singleData.usedNativeMemory = 4 performanceCollectionData.add(singleData) profiler.start() diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 1f1a3d9a74..72d0bc1d62 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -334,12 +334,6 @@ public abstract interface class io/sentry/CompositePerformanceCollector { public abstract fun stop (Ljava/lang/String;)Ljava/util/List; } -public final class io/sentry/CpuCollectionData { - public fun (DLio/sentry/SentryDate;)V - public fun getCpuUsagePercentage ()D - public fun getTimestamp ()Lio/sentry/SentryDate; -} - public final class io/sentry/CustomSamplingContext { public fun ()V public fun get (Ljava/lang/String;)Ljava/lang/Object; @@ -791,10 +785,6 @@ public abstract interface class io/sentry/ILogger { public abstract fun log (Lio/sentry/SentryLevel;Ljava/lang/Throwable;Ljava/lang/String;[Ljava/lang/Object;)V } -public abstract interface class io/sentry/IMemoryCollector { - public abstract fun collect ()Lio/sentry/MemoryCollectionData; -} - public abstract interface class io/sentry/IOptionsObserver { public abstract fun setDist (Ljava/lang/String;)V public abstract fun setEnvironment (Ljava/lang/String;)V @@ -1349,14 +1339,6 @@ public final class io/sentry/MeasurementUnit$Information : java/lang/Enum, io/se public static fun values ()[Lio/sentry/MeasurementUnit$Information; } -public final class io/sentry/MemoryCollectionData { - public fun (JJLio/sentry/SentryDate;)V - public fun (JLio/sentry/SentryDate;)V - public fun getTimestamp ()Lio/sentry/SentryDate; - public fun getUsedHeapMemory ()J - public fun getUsedNativeMemory ()J -} - public final class io/sentry/MonitorConfig : io/sentry/JsonSerializable, io/sentry/JsonUnknown { public fun (Lio/sentry/MonitorSchedule;)V public fun getCheckinMargin ()Ljava/lang/Long; @@ -1931,11 +1913,14 @@ public final class io/sentry/OutboxSender : io/sentry/IEnvelopeSender { } public final class io/sentry/PerformanceCollectionData { - public fun ()V - public fun addCpuData (Lio/sentry/CpuCollectionData;)V - public fun addMemoryData (Lio/sentry/MemoryCollectionData;)V - public fun getCpuData ()Lio/sentry/CpuCollectionData; - public fun getMemoryData ()Lio/sentry/MemoryCollectionData; + public fun (J)V + public fun getCpuUsagePercentage ()Ljava/lang/Double; + public fun getNanoTimestamp ()J + public fun getUsedHeapMemory ()Ljava/lang/Long; + public fun getUsedNativeMemory ()Ljava/lang/Long; + public fun setCpuUsagePercentage (Ljava/lang/Double;)V + public fun setUsedHeapMemory (Ljava/lang/Long;)V + public fun setUsedNativeMemory (Ljava/lang/Long;)V } public final class io/sentry/ProfileChunk : io/sentry/JsonSerializable, io/sentry/JsonUnknown { @@ -4918,10 +4903,10 @@ public final class io/sentry/profilemeasurements/ProfileMeasurement$JsonKeys { public final class io/sentry/profilemeasurements/ProfileMeasurementValue : io/sentry/JsonSerializable, io/sentry/JsonUnknown { public fun ()V - public fun (Ljava/lang/Long;Ljava/lang/Number;Lio/sentry/SentryDate;)V + public fun (Ljava/lang/Long;Ljava/lang/Number;J)V public fun equals (Ljava/lang/Object;)Z public fun getRelativeStartNs ()Ljava/lang/String; - public fun getTimestamp ()Ljava/lang/Double; + public fun getTimestamp ()D public fun getUnknown ()Ljava/util/Map; public fun getValue ()D public fun hashCode ()I diff --git a/sentry/src/main/java/io/sentry/CpuCollectionData.java b/sentry/src/main/java/io/sentry/CpuCollectionData.java deleted file mode 100644 index bcbab7c136..0000000000 --- a/sentry/src/main/java/io/sentry/CpuCollectionData.java +++ /dev/null @@ -1,23 +0,0 @@ -package io.sentry; - -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; - -@ApiStatus.Internal -public final class CpuCollectionData { - final double cpuUsagePercentage; - final @NotNull SentryDate timestamp; - - public CpuCollectionData(final double cpuUsagePercentage, final @NotNull SentryDate timestamp) { - this.cpuUsagePercentage = cpuUsagePercentage; - this.timestamp = timestamp; - } - - public @NotNull SentryDate getTimestamp() { - return timestamp; - } - - public double getCpuUsagePercentage() { - return cpuUsagePercentage; - } -} diff --git a/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java b/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java index ae99fe00c7..4189ec7b6a 100644 --- a/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java +++ b/sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java @@ -130,7 +130,8 @@ public void run() { return; } lastCollectionTimestamp = now; - final @NotNull PerformanceCollectionData tempData = new PerformanceCollectionData(); + final @NotNull PerformanceCollectionData tempData = + new PerformanceCollectionData(new SentryNanotimeDate().nanoTimestamp()); for (IPerformanceSnapshotCollector collector : snapshotCollectors) { collector.collect(tempData); diff --git a/sentry/src/main/java/io/sentry/IMemoryCollector.java b/sentry/src/main/java/io/sentry/IMemoryCollector.java deleted file mode 100644 index 58ac490218..0000000000 --- a/sentry/src/main/java/io/sentry/IMemoryCollector.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.sentry; - -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.Nullable; - -/** Used for collecting data about memory load when a transaction is active. */ -@ApiStatus.Internal -public interface IMemoryCollector { - /** Used for collecting data about memory load when a transaction is active. */ - @Nullable - MemoryCollectionData collect(); -} diff --git a/sentry/src/main/java/io/sentry/JavaMemoryCollector.java b/sentry/src/main/java/io/sentry/JavaMemoryCollector.java index 9ede59ba07..0ed45f942a 100644 --- a/sentry/src/main/java/io/sentry/JavaMemoryCollector.java +++ b/sentry/src/main/java/io/sentry/JavaMemoryCollector.java @@ -14,8 +14,6 @@ public void setup() {} @Override public void collect(final @NotNull PerformanceCollectionData performanceCollectionData) { final long usedMemory = runtime.totalMemory() - runtime.freeMemory(); - MemoryCollectionData memoryData = - new MemoryCollectionData(usedMemory, new SentryNanotimeDate()); - performanceCollectionData.addMemoryData(memoryData); + performanceCollectionData.setUsedHeapMemory(usedMemory); } } diff --git a/sentry/src/main/java/io/sentry/MemoryCollectionData.java b/sentry/src/main/java/io/sentry/MemoryCollectionData.java deleted file mode 100644 index 1155e00b4b..0000000000 --- a/sentry/src/main/java/io/sentry/MemoryCollectionData.java +++ /dev/null @@ -1,34 +0,0 @@ -package io.sentry; - -import org.jetbrains.annotations.ApiStatus; -import org.jetbrains.annotations.NotNull; - -@ApiStatus.Internal -public final class MemoryCollectionData { - final long usedHeapMemory; - final long usedNativeMemory; - final @NotNull SentryDate timestamp; - - public MemoryCollectionData( - final long usedHeapMemory, final long usedNativeMemory, final @NotNull SentryDate timestamp) { - this.usedHeapMemory = usedHeapMemory; - this.usedNativeMemory = usedNativeMemory; - this.timestamp = timestamp; - } - - public MemoryCollectionData(final long usedHeapMemory, final @NotNull SentryDate timestamp) { - this(usedHeapMemory, -1, timestamp); - } - - public @NotNull SentryDate getTimestamp() { - return timestamp; - } - - public long getUsedHeapMemory() { - return usedHeapMemory; - } - - public long getUsedNativeMemory() { - return usedNativeMemory; - } -} diff --git a/sentry/src/main/java/io/sentry/PerformanceCollectionData.java b/sentry/src/main/java/io/sentry/PerformanceCollectionData.java index af8837386a..bc32fcd820 100644 --- a/sentry/src/main/java/io/sentry/PerformanceCollectionData.java +++ b/sentry/src/main/java/io/sentry/PerformanceCollectionData.java @@ -5,28 +5,41 @@ @ApiStatus.Internal public final class PerformanceCollectionData { - private @Nullable MemoryCollectionData memoryData = null; - private @Nullable CpuCollectionData cpuData = null; + private @Nullable Double cpuUsagePercentage = null; + private @Nullable Long usedHeapMemory = null; + private @Nullable Long usedNativeMemory = null; + private final long nanoTimestamp; - /** Store a {@link io.sentry.MemoryCollectionData}, if not null. */ - public void addMemoryData(final @Nullable MemoryCollectionData memoryCollectionData) { - if (memoryCollectionData != null) { - memoryData = memoryCollectionData; - } + public PerformanceCollectionData(final long nanoTimestamp) { + this.nanoTimestamp = nanoTimestamp; } - /** Store a {@link io.sentry.CpuCollectionData}, if not null. */ - public void addCpuData(final @Nullable CpuCollectionData cpuCollectionData) { - if (cpuCollectionData != null) { - cpuData = cpuCollectionData; - } + /** Set the cpu usage percentage. */ + public void setCpuUsagePercentage(final @Nullable Double cpuUsagePercentage) { + this.cpuUsagePercentage = cpuUsagePercentage; } - public @Nullable CpuCollectionData getCpuData() { - return cpuData; + public @Nullable Double getCpuUsagePercentage() { + return cpuUsagePercentage; } - public @Nullable MemoryCollectionData getMemoryData() { - return memoryData; + public void setUsedHeapMemory(final @Nullable Long usedHeapMemory) { + this.usedHeapMemory = usedHeapMemory; + } + + public @Nullable Long getUsedHeapMemory() { + return usedHeapMemory; + } + + public void setUsedNativeMemory(final @Nullable Long usedNativeMemory) { + this.usedNativeMemory = usedNativeMemory; + } + + public @Nullable Long getUsedNativeMemory() { + return usedNativeMemory; + } + + public long getNanoTimestamp() { + return nanoTimestamp; } } diff --git a/sentry/src/main/java/io/sentry/profilemeasurements/ProfileMeasurementValue.java b/sentry/src/main/java/io/sentry/profilemeasurements/ProfileMeasurementValue.java index 12972d36e4..2f9ba5e131 100644 --- a/sentry/src/main/java/io/sentry/profilemeasurements/ProfileMeasurementValue.java +++ b/sentry/src/main/java/io/sentry/profilemeasurements/ProfileMeasurementValue.java @@ -7,8 +7,6 @@ import io.sentry.JsonUnknown; import io.sentry.ObjectReader; import io.sentry.ObjectWriter; -import io.sentry.SentryDate; -import io.sentry.SentryNanotimeDate; import io.sentry.util.Objects; import io.sentry.vendor.gson.stream.JsonToken; import java.io.IOException; @@ -25,25 +23,23 @@ public final class ProfileMeasurementValue implements JsonUnknown, JsonSerializable { private @Nullable Map unknown; - private @Nullable Double timestamp; + private double timestamp; private @NotNull String relativeStartNs; // timestamp in nanoseconds this frame was started private double value; // frame duration in nanoseconds @SuppressWarnings("JavaUtilDate") public ProfileMeasurementValue() { - this(0L, 0, new SentryNanotimeDate(new Date(0), 0)); + this(0L, 0, 0); } public ProfileMeasurementValue( - final @NotNull Long relativeStartNs, - final @NotNull Number value, - final @NotNull SentryDate timestamp) { + final @NotNull Long relativeStartNs, final @NotNull Number value, final long nanoTimestamp) { this.relativeStartNs = relativeStartNs.toString(); this.value = value.doubleValue(); - this.timestamp = DateUtils.nanosToSeconds(timestamp.nanoTimestamp()); + this.timestamp = DateUtils.nanosToSeconds(nanoTimestamp); } - public @Nullable Double getTimestamp() { + public double getTimestamp() { return timestamp; } @@ -63,7 +59,7 @@ public boolean equals(Object o) { return Objects.equals(unknown, that.unknown) && relativeStartNs.equals(that.relativeStartNs) && value == that.value - && Objects.equals(timestamp, that.timestamp); + && timestamp == that.timestamp; } @Override @@ -85,9 +81,7 @@ public void serialize(final @NotNull ObjectWriter writer, final @NotNull ILogger writer.beginObject(); writer.name(JsonKeys.VALUE).value(logger, value); writer.name(JsonKeys.START_NS).value(logger, relativeStartNs); - if (timestamp != null) { - writer.name(JsonKeys.TIMESTAMP).value(logger, doubleToBigDecimal(timestamp)); - } + writer.name(JsonKeys.TIMESTAMP).value(logger, doubleToBigDecimal(timestamp)); if (unknown != null) { for (String key : unknown.keySet()) { Object value = unknown.get(key); diff --git a/sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt b/sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt index b8ba56448a..99b2822aed 100644 --- a/sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt +++ b/sentry/src/test/java/io/sentry/DefaultCompositePerformanceCollectorTest.kt @@ -43,7 +43,7 @@ class DefaultCompositePerformanceCollectorTest { override fun setup() {} override fun collect(performanceCollectionData: PerformanceCollectionData) { - performanceCollectionData.addCpuData(mock()) + performanceCollectionData.cpuUsagePercentage = 1.0 } } @@ -174,20 +174,12 @@ class DefaultCompositePerformanceCollectorTest { assertNotNull(data1) assertNotNull(data2) assertNotNull(data3) - val memoryData1 = data1.map { it.memoryData } - val cpuData1 = data1.map { it.cpuData } - val memoryData2 = data2.map { it.memoryData } - val cpuData2 = data2.map { it.cpuData } - val memoryData3 = data3.map { it.memoryData } - val cpuData3 = data3.map { it.cpuData } - - // The data returned by the collector is not empty - assertFalse(memoryData1.isEmpty()) - assertFalse(cpuData1.isEmpty()) - assertFalse(memoryData2.isEmpty()) - assertFalse(cpuData2.isEmpty()) - assertFalse(memoryData3.isEmpty()) - assertFalse(cpuData3.isEmpty()) + assertFalse(data1.mapNotNull { it.usedHeapMemory }.isEmpty()) + assertFalse(data1.mapNotNull { it.cpuUsagePercentage }.isEmpty()) + assertFalse(data2.mapNotNull { it.usedHeapMemory }.isEmpty()) + assertFalse(data2.mapNotNull { it.cpuUsagePercentage }.isEmpty()) + assertFalse(data3.mapNotNull { it.usedHeapMemory }.isEmpty()) + assertFalse(data3.mapNotNull { it.cpuUsagePercentage }.isEmpty()) } @Test @@ -235,8 +227,8 @@ class DefaultCompositePerformanceCollectorTest { Thread.sleep(300) val data1 = collector.stop(fixture.transaction1) assertNotNull(data1) - val memoryData = data1.map { it.memoryData } - val cpuData = data1.map { it.cpuData } + val memoryData = data1.map { it.usedHeapMemory } + val cpuData = data1.map { it.cpuUsagePercentage } // The data returned by the collector is not empty assertFalse(memoryData.isEmpty()) diff --git a/sentry/src/test/java/io/sentry/JavaMemoryCollectorTest.kt b/sentry/src/test/java/io/sentry/JavaMemoryCollectorTest.kt index 17479a142c..5f4aa7fb03 100644 --- a/sentry/src/test/java/io/sentry/JavaMemoryCollectorTest.kt +++ b/sentry/src/test/java/io/sentry/JavaMemoryCollectorTest.kt @@ -2,8 +2,7 @@ package io.sentry import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertNotEquals -import kotlin.test.assertNotNull +import kotlin.test.assertNull class JavaMemoryCollectorTest { private val fixture = Fixture() @@ -15,13 +14,11 @@ class JavaMemoryCollectorTest { @Test fun `when collect, only heap memory is collected`() { - val data = PerformanceCollectionData() + val data = PerformanceCollectionData(10) val usedMemory = fixture.runtime.totalMemory() - fixture.runtime.freeMemory() fixture.collector.collect(data) - val memoryData = data.memoryData - assertNotNull(memoryData) - assertEquals(-1, memoryData.usedNativeMemory) - assertEquals(usedMemory, memoryData.usedHeapMemory) - assertNotEquals(0, memoryData.timestamp.nanoTimestamp()) + assertNull(data.usedNativeMemory) + assertEquals(usedMemory, data.usedHeapMemory) + assertEquals(10, data.nanoTimestamp) } } diff --git a/sentry/src/test/java/io/sentry/JsonSerializerTest.kt b/sentry/src/test/java/io/sentry/JsonSerializerTest.kt index 42591ae22c..d0c3dcc732 100644 --- a/sentry/src/test/java/io/sentry/JsonSerializerTest.kt +++ b/sentry/src/test/java/io/sentry/JsonSerializerTest.kt @@ -576,9 +576,9 @@ class JsonSerializerTest { fun `serializes profilingTraceData`() { val profilingTraceData = ProfilingTraceData(fixture.traceFile, NoOpTransaction.getInstance()) val now = Date() - val measurementNow = SentryNanotimeDate() + val measurementNow = SentryNanotimeDate().nanoTimestamp() val measurementNowSeconds = - BigDecimal.valueOf(DateUtils.nanosToSeconds(measurementNow.nanoTimestamp())) + BigDecimal.valueOf(DateUtils.nanosToSeconds(measurementNow)) .setScale(6, RoundingMode.DOWN) .toDouble() profilingTraceData.androidApiLevel = 21 @@ -777,31 +777,31 @@ class JsonSerializerTest { "screen_frame_rates": { "unit":"hz", "values":[ - {"value":"60.1","elapsed_since_start_ns":"1"} + {"value":"60.1","elapsed_since_start_ns":"1", "timestamp": 0.000000001} ] }, "frozen_frame_renders": { "unit":"nanosecond", "values":[ - {"value":"100","elapsed_since_start_ns":"2"} + {"value":"100","elapsed_since_start_ns":"2", "timestamp": 0.000000002} ] }, "memory_footprint": { "unit":"byte", "values":[ - {"value":"1000","elapsed_since_start_ns":"3"} + {"value":"1000","elapsed_since_start_ns":"3", "timestamp": 0.000000003} ] }, "memory_native_footprint": { "unit":"byte", "values":[ - {"value":"1100","elapsed_since_start_ns":"4"} + {"value":"1100","elapsed_since_start_ns":"4", "timestamp": 0.000000004} ] }, "cpu_usage": { "unit":"percent", "values":[ - {"value":"17.04","elapsed_since_start_ns":"5"} + {"value":"17.04","elapsed_since_start_ns":"5","timestamp": 0.000000005} ] } }, @@ -856,27 +856,27 @@ class JsonSerializerTest { ProfileMeasurement.ID_SCREEN_FRAME_RATES to ProfileMeasurement( ProfileMeasurement.UNIT_HZ, - listOf(ProfileMeasurementValue(1, 60.1, mock())), + listOf(ProfileMeasurementValue(1, 60.1, 1)), ), ProfileMeasurement.ID_FROZEN_FRAME_RENDERS to ProfileMeasurement( ProfileMeasurement.UNIT_NANOSECONDS, - listOf(ProfileMeasurementValue(2, 100, mock())), + listOf(ProfileMeasurementValue(2, 100, 2)), ), ProfileMeasurement.ID_MEMORY_FOOTPRINT to ProfileMeasurement( ProfileMeasurement.UNIT_BYTES, - listOf(ProfileMeasurementValue(3, 1000, mock())), + listOf(ProfileMeasurementValue(3, 1000, 3)), ), ProfileMeasurement.ID_MEMORY_NATIVE_FOOTPRINT to ProfileMeasurement( ProfileMeasurement.UNIT_BYTES, - listOf(ProfileMeasurementValue(4, 1100, mock())), + listOf(ProfileMeasurementValue(4, 1100, 4)), ), ProfileMeasurement.ID_CPU_USAGE to ProfileMeasurement( ProfileMeasurement.UNIT_PERCENT, - listOf(ProfileMeasurementValue(5, 17.04, mock())), + listOf(ProfileMeasurementValue(5, 17.04, 5)), ), ) assertEquals(expectedMeasurements, profilingTraceData.measurementsMap) @@ -893,9 +893,8 @@ class JsonSerializerTest { @Test fun `serializes profileMeasurement`() { - val now = SentryNanotimeDate(Date(1), 1) val measurementValues = - listOf(ProfileMeasurementValue(1, 2, now), ProfileMeasurementValue(3, 4, now)) + listOf(ProfileMeasurementValue(1, 2, 1000000), ProfileMeasurementValue(3, 4, 1000000)) val profileMeasurement = ProfileMeasurement(ProfileMeasurement.UNIT_NANOSECONDS, measurementValues) val actual = serializeToString(profileMeasurement) @@ -918,17 +917,14 @@ class JsonSerializerTest { val expected = ProfileMeasurement( ProfileMeasurement.UNIT_HZ, - listOf( - ProfileMeasurementValue(1, 60.1, SentryNanotimeDate(Date(0), 0)), - ProfileMeasurementValue(2, 100, SentryNanotimeDate(Date(1), 1)), - ), + listOf(ProfileMeasurementValue(1, 60.1, 0), ProfileMeasurementValue(2, 100, 1000000)), ) assertEquals(expected, profileMeasurement) } @Test fun `serializes profileMeasurementValue`() { - val profileMeasurementValue = ProfileMeasurementValue(1, 2, SentryNanotimeDate(Date(1), 1)) + val profileMeasurementValue = ProfileMeasurementValue(1, 2, 1000000) val actual = serializeToString(profileMeasurementValue) val expected = "{\"value\":2.0,\"elapsed_since_start_ns\":\"1\",\"timestamp\":0.001000}" assertEquals(expected, actual) @@ -939,7 +935,7 @@ class JsonSerializerTest { val json = """{"value":"60.1","elapsed_since_start_ns":"1"}""" val profileMeasurementValue = fixture.serializer.deserialize(StringReader(json), ProfileMeasurementValue::class.java) - val expected = ProfileMeasurementValue(1, 60.1, mock()) + val expected = ProfileMeasurementValue(1, 60.1, 0) assertEquals(expected, profileMeasurementValue) assertEquals(60.1, profileMeasurementValue?.value) assertEquals("1", profileMeasurementValue?.relativeStartNs) @@ -951,7 +947,7 @@ class JsonSerializerTest { val json = """{"value":"60.1","elapsed_since_start_ns":"1","timestamp":0.001000}""" val profileMeasurementValue = fixture.serializer.deserialize(StringReader(json), ProfileMeasurementValue::class.java) - val expected = ProfileMeasurementValue(1, 60.1, SentryNanotimeDate(Date(1), 1)) + val expected = ProfileMeasurementValue(1, 60.1, 1000000) assertEquals(expected, profileMeasurementValue) assertEquals(60.1, profileMeasurementValue?.value) assertEquals("1", profileMeasurementValue?.relativeStartNs) @@ -967,9 +963,9 @@ class JsonSerializerTest { fixture.options.environment = "environment" val profileChunk = ProfileChunk(profilerId, chunkId, fixture.traceFile, HashMap(), 5.3, fixture.options) - val measurementNow = SentryNanotimeDate() + val measurementNow = SentryNanotimeDate().nanoTimestamp() val measurementNowSeconds = - BigDecimal.valueOf(DateUtils.nanosToSeconds(measurementNow.nanoTimestamp())) + BigDecimal.valueOf(DateUtils.nanosToSeconds(measurementNow)) .setScale(6, RoundingMode.DOWN) .toDouble() profileChunk.sampledProfile = "sampled profile in base 64" @@ -1089,31 +1085,31 @@ class JsonSerializerTest { "screen_frame_rates": { "unit":"hz", "values":[ - {"value":"60.1","elapsed_since_start_ns":"1"} + {"value":"60.1","elapsed_since_start_ns":"1", "timestamp": 0.000000001} ] }, "frozen_frame_renders": { "unit":"nanosecond", "values":[ - {"value":"100","elapsed_since_start_ns":"2"} + {"value":"100","elapsed_since_start_ns":"2", "timestamp": 0.000000002} ] }, "memory_footprint": { "unit":"byte", "values":[ - {"value":"1000","elapsed_since_start_ns":"3"} + {"value":"1000","elapsed_since_start_ns":"3", "timestamp": 0.000000003} ] }, "memory_native_footprint": { "unit":"byte", "values":[ - {"value":"1100","elapsed_since_start_ns":"4"} + {"value":"1100","elapsed_since_start_ns":"4", "timestamp": 0.000000004} ] }, "cpu_usage": { "unit":"percent", "values":[ - {"value":"17.04","elapsed_since_start_ns":"5"} + {"value":"17.04","elapsed_since_start_ns":"5", "timestamp": 0.000000005} ] } } @@ -1134,27 +1130,27 @@ class JsonSerializerTest { ProfileMeasurement.ID_SCREEN_FRAME_RATES to ProfileMeasurement( ProfileMeasurement.UNIT_HZ, - listOf(ProfileMeasurementValue(1, 60.1, mock())), + listOf(ProfileMeasurementValue(1, 60.1, 1)), ), ProfileMeasurement.ID_FROZEN_FRAME_RENDERS to ProfileMeasurement( ProfileMeasurement.UNIT_NANOSECONDS, - listOf(ProfileMeasurementValue(2, 100, mock())), + listOf(ProfileMeasurementValue(2, 100, 2)), ), ProfileMeasurement.ID_MEMORY_FOOTPRINT to ProfileMeasurement( ProfileMeasurement.UNIT_BYTES, - listOf(ProfileMeasurementValue(3, 1000, mock())), + listOf(ProfileMeasurementValue(3, 1000, 3)), ), ProfileMeasurement.ID_MEMORY_NATIVE_FOOTPRINT to ProfileMeasurement( ProfileMeasurement.UNIT_BYTES, - listOf(ProfileMeasurementValue(4, 1100, mock())), + listOf(ProfileMeasurementValue(4, 1100, 4)), ), ProfileMeasurement.ID_CPU_USAGE to ProfileMeasurement( ProfileMeasurement.UNIT_PERCENT, - listOf(ProfileMeasurementValue(5, 17.04, mock())), + listOf(ProfileMeasurementValue(5, 17.04, 5)), ), ) assertEquals(expectedMeasurements, profileChunk.measurements) diff --git a/sentry/src/test/java/io/sentry/PerformanceCollectionDataTest.kt b/sentry/src/test/java/io/sentry/PerformanceCollectionDataTest.kt deleted file mode 100644 index 7f79f0308a..0000000000 --- a/sentry/src/test/java/io/sentry/PerformanceCollectionDataTest.kt +++ /dev/null @@ -1,55 +0,0 @@ -package io.sentry - -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertNotEquals -import kotlin.test.assertNull -import org.mockito.kotlin.mock - -class PerformanceCollectionDataTest { - private val fixture = Fixture() - - private class Fixture { - fun getSut() = PerformanceCollectionData() - } - - @Test - fun `only the last of multiple memory data is saved`() { - val data = fixture.getSut() - val t1 = mock() - val t2 = mock() - val memData1 = MemoryCollectionData(0, 0, t1) - val memData2 = MemoryCollectionData(1, 1, t2) - data.addMemoryData(memData1) - data.addMemoryData(memData2) - val savedMemoryData = data.memoryData - assertNotEquals(memData1, savedMemoryData) - assertEquals(memData2, savedMemoryData) - } - - @Test - fun `only the last of multiple cpu data is saved`() { - val data = fixture.getSut() - val t1 = mock() - val t2 = mock() - val cpuData1 = CpuCollectionData(0.0, t1) - val cpuData2 = CpuCollectionData(1.0, t2) - data.addCpuData(cpuData1) - data.addCpuData(cpuData2) - val savedCpuData = data.cpuData - assertNotEquals(cpuData1, savedCpuData) - assertEquals(cpuData2, savedCpuData) - } - - @Test - fun `null values are ignored`() { - val data = fixture.getSut() - val cpuData1 = CpuCollectionData(0.0, mock()) - data.addCpuData(cpuData1) - data.addCpuData(null) - data.addMemoryData(null) - assertNull(data.memoryData) - val savedCpuData = data.cpuData - assertEquals(cpuData1, savedCpuData) - } -} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy