Skip to content

Commit 94aad20

Browse files
authored
Merge pull request #45 from sergiosentry/add-profiling
Enable profiling
2 parents 8c9d690 + a14dc17 commit 94aad20

File tree

3 files changed

+87
-6
lines changed

3 files changed

+87
-6
lines changed

app/build.gradle

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ android {
5757
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
5858
signingConfig = signingConfigs.debug // to be able to run release mode
5959
buildConfigField("String", "SE", "\"tda\"")
60+
buildConfigField("boolean", "SLOW_PROFILING", "true")
6061
}
6162
debug {
6263
buildConfigField("String", "SE", "\"tda\"")
64+
buildConfigField("boolean", "SLOW_PROFILING", "true")
6365
}
6466
}
6567

@@ -78,9 +80,9 @@ dependencies {
7880
implementation fileTree(dir: 'libs', include: ['*.jar'])
7981
implementation 'androidx.appcompat:appcompat:1.1.0'
8082
implementation group: 'androidx.constraintlayout', name: 'constraintlayout', version: '1.1.3'
81-
implementation 'io.sentry:sentry-android:5.6.0'
82-
implementation 'io.sentry:sentry-android-okhttp:5.6.0'
83-
implementation 'io.sentry:sentry-android-fragment:5.6.0'
83+
implementation 'io.sentry:sentry-android:6.4.2'
84+
implementation 'io.sentry:sentry-android-okhttp:6.4.2'
85+
implementation 'io.sentry:sentry-android-fragment:6.4.2'
8486
implementation 'com.google.android.material:material:1.0.0'
8587
implementation 'androidx.navigation:navigation-fragment:2.1.0'
8688
implementation 'androidx.navigation:navigation-ui:2.1.0'

app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<!-- lowering this number below 5000ms overrides the 5000ms, we check the state of the processor faster. doesn't mean we're detecting more ANR's or not -->
5656
<!-- Default is 5seconds (is from the OperatingSystem, to define the state of Not Responding) and defined by the SDK -->
5757
<!-- we can check the state of the property ourselves, using this -->
58-
<!-- <meta-data android:name="io.sentry.anr.timeout-interval-mills" android:value="3000" /> -->
58+
<!-- <meta-data android:name="io.sentry.anr.timeout-interval-mills" android:value="3000" /> -->
5959

6060
<!-- Easy way to get a new release if you're testing, to separate from past releases with lots of crashes/sessions. overrides what's in app/build.gradle -->
6161
<!-- <meta-data android:name="io.sentry.release" android:value="[email protected]+1" /> -->
@@ -67,7 +67,12 @@
6767
<meta-data
6868
android:name="io.sentry.traces.sample-rate"
6969
android:value="1.0" />
70-
70+
<meta-data
71+
android:name="io.sentry.traces.profiling.enable"
72+
android:value="true" />
73+
<meta-data
74+
android:name="io.sentry.traces.profiling.sample-rate"
75+
android:value="1.0" />
7176
<meta-data
7277
android:name="empowerplant.domain"
7378
android:value="https://application-monitoring-flask-dot-sales-engineering-sf.appspot.com" />

app/src/main/java/com/example/vu/android/MyBaseActivity.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.app.Activity;
44
import android.content.Context;
55
import android.os.Bundle;
6+
import android.util.Log;
67

78
import androidx.appcompat.app.AppCompatActivity;
89

@@ -13,7 +14,11 @@
1314
import java.io.File;
1415
import java.io.FileOutputStream;
1516
import java.text.SimpleDateFormat;
17+
import java.util.ArrayList;
1618
import java.util.Date;
19+
import java.util.List;
20+
import java.util.Random;
21+
import java.util.UUID;
1722

1823
import io.sentry.Attachment;
1924
import io.sentry.Sentry;
@@ -62,10 +67,19 @@ private void clearReferences () {
6267

6368
protected Boolean addAttachment() {
6469
File f = null;
70+
String fileName = "tmp" + UUID.randomUUID();
71+
boolean slowProfiling = BuildConfig.SLOW_PROFILING;
72+
6573
try {
6674
Context c = getApplicationContext();
6775
File cacheDirectory = c.getCacheDir();
68-
f = File.createTempFile("tmp", ".txt", cacheDirectory);
76+
77+
if (slowProfiling) {
78+
f = generateSlowProfile(cacheDirectory, fileName);
79+
} else {
80+
f = File.createTempFile(fileName, ".txt", cacheDirectory);
81+
}
82+
6983
System.out.println("File path: "+f.getAbsolutePath());
7084
f.deleteOnExit();
7185
try (FileOutputStream fos = new FileOutputStream(f)) {
@@ -88,4 +102,64 @@ protected Boolean addAttachment() {
88102
}
89103
return true;
90104
}
105+
106+
protected void generateCacheFiles(int filesToGenerate, File cacheDirectory) {
107+
for (int x = 0; x < filesToGenerate; x++) {
108+
try {
109+
File.createTempFile("tmp" + x, ".txt", cacheDirectory);
110+
} catch (Exception e) {
111+
Sentry.captureException(e);
112+
e.printStackTrace();
113+
}
114+
}
115+
}
116+
117+
protected File generateSlowProfile(File cacheDirectory, String fileName){
118+
int maxTries = 1000000;
119+
boolean cacheFileExists = false;
120+
boolean outOfBounds = false;
121+
List<Integer> indexes = new ArrayList<>();
122+
int count = 0;
123+
Random rand = new Random();
124+
File[] cacheFiles = cacheDirectory.listFiles();
125+
File f = null;
126+
127+
// If this is the first time the app is running or the cache has been cleared, the cacheFile length will be 1
128+
if (cacheFiles == null || cacheFiles.length <= 1) {
129+
generateCacheFiles(50, cacheDirectory);
130+
cacheFiles = cacheDirectory.listFiles();
131+
}
132+
133+
// Loop through cache dir and check that tmp file does not exist already
134+
while (!outOfBounds && cacheFiles != null) {
135+
int index = rand.nextInt();
136+
int iteration = 0;
137+
138+
// Play a guessing game and try to find the index for an existing file in the cache dir
139+
while (indexes.contains(index) || index > cacheFiles.length || index < 0) {
140+
index = rand.nextInt();
141+
iteration++;
142+
if (iteration > maxTries) {
143+
index = rand.nextInt(cacheFiles.length);
144+
}
145+
}
146+
147+
if (cacheFiles[index].getName().equals(fileName)) {
148+
cacheFileExists = true;
149+
}
150+
151+
if (count == cacheFiles.length - 1) {
152+
outOfBounds = true;
153+
}
154+
155+
indexes.add(index);
156+
count = count + 1;
157+
}
158+
159+
if (!cacheFileExists) {
160+
f = new File(cacheDirectory + fileName);
161+
}
162+
163+
return f;
164+
}
91165
}

0 commit comments

Comments
 (0)