Skip to content

Commit bf82eb3

Browse files
authored
Ensure performance measurement collection is not taken too frequently (#3221)
* performance measurement collection now waits at least 10ms between collections * compiled regex in AndroidCpuCollector to parse the cpu file, to improve speed
1 parent 56f2a8a commit bf82eb3

3 files changed

Lines changed: 13 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
### Fixes
1111

12+
- Ensure performance measurement collection is not taken too frequently ([#3221](https://github.com/getsentry/sentry-java/pull/3221))
1213
- Fix old profiles deletion on SDK init ([#3216](https://github.com/getsentry/sentry-java/pull/3216))
1314

1415
## 7.4.0

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.sentry.util.Objects;
1515
import java.io.File;
1616
import java.io.IOException;
17+
import java.util.regex.Pattern;
1718
import org.jetbrains.annotations.ApiStatus;
1819
import org.jetbrains.annotations.NotNull;
1920

@@ -42,6 +43,7 @@ public final class AndroidCpuCollector implements IPerformanceSnapshotCollector
4243
private final @NotNull ILogger logger;
4344
private final @NotNull BuildInfoProvider buildInfoProvider;
4445
private boolean isEnabled = false;
46+
private final @NotNull Pattern newLinePattern = Pattern.compile("[\n\t\r ]");
4547

4648
public AndroidCpuCollector(
4749
final @NotNull ILogger logger, final @NotNull BuildInfoProvider buildInfoProvider) {
@@ -102,7 +104,7 @@ private long readTotalCpuNanos() {
102104
}
103105
if (stat != null) {
104106
stat = stat.trim();
105-
String[] stats = stat.split("[\n\t\r ]");
107+
String[] stats = newLinePattern.split(stat);
106108
try {
107109
// Amount of clock ticks this process has been scheduled in user mode
108110
long uTime = Long.parseLong(stats[13]);

sentry/src/main/java/io/sentry/DefaultTransactionPerformanceCollector.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class DefaultTransactionPerformanceCollector
2828

2929
private final @NotNull SentryOptions options;
3030
private final @NotNull AtomicBoolean isStarted = new AtomicBoolean(false);
31+
private long lastCollectionTimestamp = 0;
3132

3233
public DefaultTransactionPerformanceCollector(final @NotNull SentryOptions options) {
3334
this.options = Objects.requireNonNull(options, "The options object is required.");
@@ -104,6 +105,14 @@ public void run() {
104105
new TimerTask() {
105106
@Override
106107
public void run() {
108+
long now = System.currentTimeMillis();
109+
// The timer is scheduled to run every 100ms on average. In case it takes longer,
110+
// subsequent tasks are executed more quickly. If two tasks are scheduled to run in
111+
// less than 10ms, the measurement that we collect is not meaningful, so we skip it
112+
if (now - lastCollectionTimestamp < 10) {
113+
return;
114+
}
115+
lastCollectionTimestamp = now;
107116
final @NotNull PerformanceCollectionData tempData = new PerformanceCollectionData();
108117

109118
for (IPerformanceSnapshotCollector collector : snapshotCollectors) {

0 commit comments

Comments
 (0)