Skip to content

Commit

Permalink
Ensure performance measurement collection is not taken too frequently (
Browse files Browse the repository at this point in the history
…#3221)

* performance measurement collection now waits at least 10ms between collections
* compiled regex in AndroidCpuCollector to parse the cpu file, to improve speed
  • Loading branch information
stefanosiano authored Feb 23, 2024
1 parent 56f2a8a commit bf82eb3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Fixes

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

## 7.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.sentry.util.Objects;
import java.io.File;
import java.io.IOException;
import java.util.regex.Pattern;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

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

public AndroidCpuCollector(
final @NotNull ILogger logger, final @NotNull BuildInfoProvider buildInfoProvider) {
Expand Down Expand Up @@ -102,7 +104,7 @@ private long readTotalCpuNanos() {
}
if (stat != null) {
stat = stat.trim();
String[] stats = stat.split("[\n\t\r ]");
String[] stats = newLinePattern.split(stat);
try {
// Amount of clock ticks this process has been scheduled in user mode
long uTime = Long.parseLong(stats[13]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class DefaultTransactionPerformanceCollector

private final @NotNull SentryOptions options;
private final @NotNull AtomicBoolean isStarted = new AtomicBoolean(false);
private long lastCollectionTimestamp = 0;

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

for (IPerformanceSnapshotCollector collector : snapshotCollectors) {
Expand Down

0 comments on commit bf82eb3

Please sign in to comment.