Skip to content

Commit e659596

Browse files
committed
Add a way to create traces
1 parent ba69975 commit e659596

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/session/PerfSession.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public class PerfSession implements Parcelable {
4040
public static PerfSession createWithId(@NonNull String sessionId) {
4141
String prunedSessionId = sessionId.replace("-", "");
4242
PerfSession session = new PerfSession(prunedSessionId, new Clock());
43-
session.setGaugeAndEventCollectionEnabled(shouldCollectGaugesAndEvents());
43+
// session.setGaugeAndEventCollectionEnabled(shouldCollectGaugesAndEvents());
44+
// For testing.
45+
session.setGaugeAndEventCollectionEnabled(true);
4446

4547
return session;
4648
}

firebase-sessions/test-app/src/main/kotlin/com/google/firebase/testing/sessions/FirstFragment.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ import android.view.View
2727
import android.view.ViewGroup
2828
import androidx.fragment.app.Fragment
2929
import com.google.firebase.crashlytics.FirebaseCrashlytics
30+
import com.google.firebase.perf.FirebasePerformance
31+
import com.google.firebase.perf.metrics.Trace as PerfTrace
3032
import com.google.firebase.testing.sessions.databinding.FragmentFirstBinding
3133
import java.util.Date
3234
import java.util.Locale
3335

3436
/** A simple [Fragment] subclass as the default destination in the navigation. */
3537
class FirstFragment : Fragment() {
3638
val crashlytics = FirebaseCrashlytics.getInstance()
39+
val performance = FirebasePerformance.getInstance()
40+
var performanceTrace: PerfTrace? = null
41+
var performanceTraceIdenticalName: PerfTrace? = null
3742

3843
private var _binding: FragmentFirstBinding? = null
3944

@@ -64,6 +69,18 @@ class FirstFragment : Fragment() {
6469
Thread.sleep(1_000)
6570
}
6671
}
72+
binding.createTrace.setOnClickListener {
73+
performanceTrace = performance.newTrace("test_trace")
74+
performanceTraceIdenticalName = performance.newTrace("test_trace")
75+
}
76+
binding.startTrace.setOnClickListener {
77+
performanceTrace?.start()
78+
performanceTraceIdenticalName?.start()
79+
}
80+
binding.stopTrace.setOnClickListener {
81+
performanceTrace?.stop()
82+
performanceTraceIdenticalName?.stop()
83+
}
6784
binding.buttonForegroundProcess.setOnClickListener {
6885
if (binding.buttonForegroundProcess.getText().startsWith("Start")) {
6986
ForegroundService.startService(requireContext(), "Starting service at ${getDateText()}")

firebase-sessions/test-app/src/main/res/layout/fragment_first.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@
4747
app:layout_constraintLeft_toLeftOf="parent"
4848
app:layout_constraintTop_toBottomOf="@id/button_non_fatal" />
4949

50+
<Button
51+
android:id="@+id/create_trace"
52+
android:layout_width="wrap_content"
53+
android:layout_height="wrap_content"
54+
android:text="@string/create_trace"
55+
app:layout_constraintLeft_toLeftOf="parent"
56+
app:layout_constraintTop_toBottomOf="@id/button_anr" />
57+
58+
<Button
59+
android:id="@+id/start_trace"
60+
android:layout_width="wrap_content"
61+
android:layout_height="wrap_content"
62+
android:text="@string/start_trace"
63+
app:layout_constraintLeft_toLeftOf="parent"
64+
app:layout_constraintTop_toBottomOf="@id/button_anr" />
65+
66+
<Button
67+
android:id="@+id/stop_trace"
68+
android:layout_width="wrap_content"
69+
android:layout_height="wrap_content"
70+
android:text="@string/stop_trace"
71+
app:layout_constraintLeft_toLeftOf="parent"
72+
app:layout_constraintTop_toBottomOf="@id/start_trace" />
73+
5074
<Button
5175
android:id="@+id/button_foreground_process"
5276
android:layout_width="wrap_content"

firebase-sessions/test-app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
<string name="crash_button_text">Crash!</string>
2424
<string name="non_fatal_button_text">Non Fatal</string>
2525
<string name="button_anr_text">ANR</string>
26+
<string name="create_trace">Create New Trace: test_trace</string>
27+
<string name="start_trace">Start Trace: test_trace</string>
28+
<string name="stop_trace">Stop Trace: test_trace</string>
2629
<string name="start_foreground_service_text">Start Foreground Service</string>
2730
<string name="start_splitcreen_text">Start splitscreen - Different activity</string>
2831
<string name="start_splitcreen_same_text">Start splitscreen - Same activity</string>

0 commit comments

Comments
 (0)