Skip to content

Commit 2299b50

Browse files
committed
Changes in the Session Test App to verify behaviour with Fireperf #no-changelog (#6809)
This change adds a way to repeatedly log identical performance traces in different processes. - Currently traces on different activities are logged on different Fireperf Sessions. - Adds additional logging to help identify different Firebase instances in different processes. - Adds Fireperf custom attributes to identify processes.
1 parent e26be15 commit 2299b50

File tree

8 files changed

+93
-2
lines changed

8 files changed

+93
-2
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionLifecycleService.kt

+1
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ internal class SessionLifecycleService : Service() {
193193
handlerThread.start()
194194
messageHandler = MessageHandler(handlerThread.looper)
195195
messenger = Messenger(messageHandler)
196+
Log.d(TAG, "Service created on process ${android.os.Process.myPid()}")
196197
}
197198

198199
/** Called when a new [SessionLifecycleClient] binds to this service. */

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

+27-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,16 @@ import android.os.Bundle
2424
import android.util.Log
2525
import androidx.appcompat.app.AppCompatActivity
2626
import com.google.firebase.FirebaseApp
27+
import com.google.firebase.perf.FirebasePerformance
2728

2829
open class BaseActivity : AppCompatActivity() {
2930

3031
override fun onCreate(savedInstanceState: Bundle?) {
3132
super.onCreate(savedInstanceState)
3233
FirebaseApp.initializeApp(this)
34+
setProcessAttribute()
35+
logProcessDetails()
36+
logFirebaseDetails()
3337
Log.i(TAG, "onCreate - ${getProcessName()} - ${getImportance()}")
3438
}
3539

@@ -64,9 +68,31 @@ open class BaseActivity : AppCompatActivity() {
6468
return processInfo.importance
6569
}
6670

67-
private fun getProcessName(): String =
71+
protected fun getProcessName(): String =
6872
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) Application.getProcessName() else "unknown"
6973

74+
private fun logProcessDetails() {
75+
val pid = android.os.Process.myPid()
76+
val uid = android.os.Process.myUid()
77+
val activity = javaClass.name
78+
val process = getProcessName()
79+
Log.i(TAG, "activity: $activity process: $process, pid: $pid, uid: $uid")
80+
}
81+
82+
private fun logFirebaseDetails() {
83+
val activity = javaClass.name
84+
val firebaseApps = FirebaseApp.getApps(this)
85+
val defaultFirebaseApp = FirebaseApp.getInstance()
86+
Log.i(
87+
TAG,
88+
"activity: $activity firebase: ${defaultFirebaseApp.name} appsCount: ${firebaseApps.count()}"
89+
)
90+
}
91+
92+
private fun setProcessAttribute() {
93+
FirebasePerformance.getInstance().putAttribute("process_name", getProcessName())
94+
}
95+
7096
companion object {
7197
val TAG = "BaseActivity"
7298
}

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

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.firebase.testing.sessions
1818

19+
import android.app.Application
1920
import android.content.Intent
2021
import android.content.Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
2122
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
@@ -26,14 +27,20 @@ import android.view.LayoutInflater
2627
import android.view.View
2728
import android.view.ViewGroup
2829
import androidx.fragment.app.Fragment
30+
import androidx.lifecycle.lifecycleScope
2931
import com.google.firebase.crashlytics.FirebaseCrashlytics
32+
import com.google.firebase.perf.FirebasePerformance
3033
import com.google.firebase.testing.sessions.databinding.FragmentFirstBinding
3134
import java.util.Date
3235
import java.util.Locale
36+
import kotlinx.coroutines.Dispatchers
37+
import kotlinx.coroutines.delay
38+
import kotlinx.coroutines.launch
3339

3440
/** A simple [Fragment] subclass as the default destination in the navigation. */
3541
class FirstFragment : Fragment() {
3642
val crashlytics = FirebaseCrashlytics.getInstance()
43+
val performance = FirebasePerformance.getInstance()
3744

3845
private var _binding: FragmentFirstBinding? = null
3946

@@ -64,6 +71,14 @@ class FirstFragment : Fragment() {
6471
Thread.sleep(1_000)
6572
}
6673
}
74+
binding.createTrace.setOnClickListener {
75+
lifecycleScope.launch(Dispatchers.IO) {
76+
val performanceTrace = performance.newTrace("test_trace")
77+
performanceTrace.start()
78+
delay(1000)
79+
performanceTrace.stop()
80+
}
81+
}
6782
binding.buttonForegroundProcess.setOnClickListener {
6883
if (binding.buttonForegroundProcess.getText().startsWith("Start")) {
6984
ForegroundService.startService(requireContext(), "Starting service at ${getDateText()}")
@@ -89,6 +104,7 @@ class FirstFragment : Fragment() {
89104
intent.addFlags(FLAG_ACTIVITY_NEW_TASK)
90105
startActivity(intent)
91106
}
107+
binding.processName.text = getProcessName()
92108
}
93109

94110
override fun onResume() {
@@ -111,5 +127,9 @@ class FirstFragment : Fragment() {
111127
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
112128
SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())
113129
else "unknown"
130+
131+
fun getProcessName(): String =
132+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) Application.getProcessName()
133+
else "unknown"
114134
}
115135
}

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

+14-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
2222
import android.os.Build
2323
import android.os.Bundle
2424
import android.widget.Button
25+
import android.widget.TextView
26+
import androidx.lifecycle.lifecycleScope
27+
import com.google.firebase.perf.FirebasePerformance
28+
import kotlinx.coroutines.delay
29+
import kotlinx.coroutines.launch
2530

2631
/** Second activity from the MainActivity that runs on a different process. */
2732
class SecondActivity : BaseActivity() {
28-
2933
override fun onCreate(savedInstanceState: Bundle?) {
3034
super.onCreate(savedInstanceState)
3135
setContentView(R.layout.activity_second)
@@ -38,12 +42,21 @@ class SecondActivity : BaseActivity() {
3842
findViewById<Button>(R.id.second_crash_button).setOnClickListener {
3943
throw IllegalStateException("SecondActivity has crashed")
4044
}
45+
findViewById<Button>(R.id.second_create_trace).setOnClickListener {
46+
lifecycleScope.launch {
47+
val performanceTrace = FirebasePerformance.getInstance().newTrace("test_trace")
48+
performanceTrace.start()
49+
delay(1000)
50+
performanceTrace.stop()
51+
}
52+
}
4153
findViewById<Button>(R.id.kill_background_processes).setOnClickListener {
4254
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
4355
getSystemService(ActivityManager::class.java)
4456
.killBackgroundProcesses("com.google.firebase.testing.sessions")
4557
}
4658
}
59+
findViewById<TextView>(R.id.process_name_second).text = getProcessName()
4760
}
4861

4962
override fun onResume() {

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

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838
app:layout_constraintTop_toBottomOf="@id/prev_activity_button"
3939
app:layout_constraintLeft_toLeftOf="parent" />
4040

41+
<Button
42+
android:id="@+id/second_create_trace"
43+
android:layout_width="wrap_content"
44+
android:layout_height="wrap_content"
45+
android:text="@string/create_trace"
46+
app:layout_constraintTop_toBottomOf="@id/second_crash_button"
47+
app:layout_constraintLeft_toLeftOf="parent" />
48+
4149
<Button
4250
android:id="@+id/kill_background_processes"
4351
android:layout_width="wrap_content"
@@ -46,6 +54,12 @@
4654
app:layout_constraintTop_toBottomOf="@id/second_crash_button"
4755
app:layout_constraintLeft_toLeftOf="parent" />
4856

57+
<TextView
58+
android:id="@+id/process_name_second"
59+
android:layout_width="match_parent"
60+
android:layout_height="wrap_content"
61+
android:text=""/>
62+
4963
<LinearLayout
5064
android:layout_width="match_parent"
5165
android:layout_height="match_parent"

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

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
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+
5058
<Button
5159
android:id="@+id/button_foreground_process"
5260
android:layout_width="wrap_content"
@@ -79,6 +87,12 @@
7987
app:layout_constraintLeft_toLeftOf="parent"
8088
app:layout_constraintTop_toBottomOf="@id/start_splitscreen_same" />
8189

90+
<TextView
91+
android:id="@+id/process_name"
92+
android:layout_width="match_parent"
93+
android:layout_height="wrap_content"
94+
android:text=""/>
95+
8296
<LinearLayout
8397
android:layout_width="match_parent"
8498
android:layout_height="match_parent"

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

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
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>
2627
<string name="start_foreground_service_text">Start Foreground Service</string>
2728
<string name="start_splitcreen_text">Start splitscreen - Different activity</string>
2829
<string name="start_splitcreen_same_text">Start splitscreen - Same activity</string>

firebase-sessions/test-app/test-app.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ dependencies {
8484
implementation("androidx.navigation:navigation-fragment-ktx:2.4.1")
8585
implementation("androidx.navigation:navigation-ui-ktx:2.4.1")
8686
implementation("com.google.android.material:material:1.9.0")
87+
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9")
88+
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0")
8789
implementation(libs.androidx.core)
8890

8991
androidTestImplementation("com.google.firebase:firebase-common:21.0.0")

0 commit comments

Comments
 (0)