Skip to content

Commit

Permalink
Integrate firebase performance monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
LinZong committed Sep 7, 2024
1 parent 3d00b23 commit 44e6c28
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("PropertyName")

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

@Suppress("DSL_SCOPE_VIOLATION") // TODO: Remove once KTIJ-19369 is fixed
Expand All @@ -6,9 +8,12 @@ plugins {
alias(libs.plugins.kotlinAndroid)
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
// Add the Performance Monitoring Gradle plugin
id("com.google.firebase.firebase-perf")
}
val version = file(rootDir.resolve("VERSION")).readText().trim()
project.logger.warn("version: $version")
val VERSION = file(rootDir.resolve("VERSION")).readText().trim()
val VERSION_CODE = VERSION.replace(".", "").toInt()
project.logger.warn("Using versionName: $VERSION, versionCode: $VERSION_CODE")

android {
signingConfigs {
Expand All @@ -33,8 +38,8 @@ android {
applicationId = "moe.nemesiss.hostman"
minSdk = 29
targetSdk = 34
versionCode = 1
versionName = version
versionCode = VERSION_CODE
versionName = VERSION
multiDexEnabled = true

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -121,6 +126,7 @@ dependencies {
implementation(platform("com.google.firebase:firebase-bom:33.1.2"))
implementation("com.google.firebase:firebase-crashlytics")
implementation("com.google.firebase:firebase-analytics")
implementation("com.google.firebase:firebase-perf")

// Barcode Scanner
implementation(libs.barcode.scanning)
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
android:exported="true"
android:multiprocess="false"
android:permission="android.permission.INTERACT_ACROSS_USERS_FULL"/>

<meta-data
android:name="firebase_performance_logcat_enabled"
android:value="true" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.firebase.Firebase
import com.google.firebase.perf.metrics.AddTrace
import com.google.firebase.perf.performance
import io.netty.resolver.HostsFileParser
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
Expand Down Expand Up @@ -41,13 +44,16 @@ class HostmanViewModel : ViewModel() {
fun loadHostFileEntries(fileProvider: IFileProvider) {
viewModelScope.launch {
loading.value = true
val trace = Firebase.performance.newTrace("load_host_file_entries")
trace.start()
val begin = System.currentTimeMillis()
val hostEntries = withContext(Dispatchers.IO) {
val hostContent = fileProvider.getFileTextContent(HostEntries.HOST_FILE_PATH)
val nettyEntries = HostsFileParser.parse(StringReader(hostContent))
HostEntries.fromNettyHostFileEntries(nettyEntries)
}
val end = System.currentTimeMillis()
trace.stop()
val cost = ((end - begin).toInt()).coerceAtLeast(500).milliseconds
delay(cost)
hostFileEntries.value = hostEntries
Expand Down Expand Up @@ -91,6 +97,8 @@ class HostmanViewModel : ViewModel() {
}
}


@AddTrace(name = "write_host_entries_to_host_file")
private suspend fun writeHostEntriesToHostFile(context: Context,
fileProvider: IFileProvider,
hostsFileEntries: HostEntries): FileOperationResult {
Expand Down
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ plugins {
alias(libs.plugins.kotlinAndroid) apply false
id("com.google.gms.google-services") version "4.4.2" apply false
id("com.google.firebase.crashlytics") version "3.0.2" apply false
id("com.google.firebase.firebase-perf") version "1.4.2" apply false
}
true // Needed to make the Suppress annotation work for the plugins block

0 comments on commit 44e6c28

Please sign in to comment.