Skip to content

Commit

Permalink
Fix logging performance issues (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdsantos authored Nov 14, 2024
1 parent 4d2a0d8 commit 55b5b69
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 38 deletions.
1 change: 1 addition & 0 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ kotlin {
optIn("kotlinx.cinterop.BetaInteropApi")
optIn("kotlinx.cinterop.ExperimentalForeignApi")
optIn("kotlinx.coroutines.ExperimentalCoroutinesApi")
optIn("kotlinx.coroutines.FlowPreview")
optIn("org.jetbrains.compose.resources.ExperimentalResourceApi")
optIn("androidx.compose.foundation.ExperimentalFoundationApi")
optIn("androidx.compose.material3.ExperimentalMaterial3Api")
Expand Down
1 change: 1 addition & 0 deletions composeApp/src/commonMain/kotlin/org/ooni/probe/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fun App(
Logger.addLogWriter(dependencies.crashMonitoring.logWriter)
Logger.addLogWriter(dependencies.appLogger.logWriter)
logAppStart(dependencies.platformInfo)
dependencies.appLogger.writeLogsToFile()
}
LaunchedEffect(Unit) {
if (dependencies.flavorConfig.isCrashReportingEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.withContext
import okio.FileNotFoundException
import okio.FileSystem
import okio.Path.Companion.toPath
import kotlin.coroutines.CoroutineContext
Expand All @@ -24,13 +25,16 @@ class GetStorageUsed(

suspend fun update(): Long {
return withContext(backgroundContext) {
val usedStorage =
(
fileSystem.listRecursively(baseFileDir.toPath()).toList() + fileSystem.listRecursively(
cacheDir.toPath(),
)
).filter { fileSystem.metadata(it).isRegularFile }
.sumOf { (fileSystem.metadata(it).size ?: 0) }
val allFiles = listOf(baseFileDir, cacheDir)
.flatMap { fileSystem.listRecursively(baseFileDir.toPath()) }
val usedStorage = allFiles.sumOf {
val metadata = try {
fileSystem.metadata(it)
} catch (e: FileNotFoundException) {
return@sumOf 0L
}
if (metadata.isRegularFile) metadata.size ?: 0 else 0
}
storageUsed.update { usedStorage }
return@withContext usedStorage
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ class RunNetTest(
if (it !is TestRunState.Running) return@setCurrentTestState it
it.copy(log = event.message)
}

writeToLogFile(event.message + "\n")
}

is TaskEvent.Progress -> {
Expand All @@ -139,8 +137,6 @@ class RunNetTest(
log = event.message,
)
}

writeToLogFile(event.message + "\n")
}

is TaskEvent.Measurement -> {
Expand Down Expand Up @@ -259,9 +255,7 @@ class RunNetTest(
Logger.w("BugJsonDump", Failure(event.value))
}

is TaskEvent.TaskTerminated -> {
deleteLogFile()
}
is TaskEvent.TaskTerminated -> Unit
}
}

Expand All @@ -287,20 +281,6 @@ class RunNetTest(
storeMeasurement(updatedMeasurement)
}

private suspend fun writeToLogFile(text: String) {
writeFile(
path = getLogFilePath(),
contents = text,
append = true,
)
}

private suspend fun deleteLogFile() {
deleteFiles(getLogFilePath())
}

private fun getLogFilePath() = MeasurementModel.logFilePath(spec.resultId, spec.netTest.test)

private suspend fun writeToReportFile(
measurement: MeasurementModel,
text: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import co.touchlab.kermit.Severity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.update
Expand All @@ -18,6 +20,7 @@ import org.ooni.probe.data.disk.WriteFile
import org.ooni.probe.shared.now
import org.ooni.probe.ui.shared.logFormat
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration.Companion.seconds

class AppLogger(
private val readFile: ReadFile,
Expand Down Expand Up @@ -53,6 +56,17 @@ class AppLogger(

fun getLogFilePath() = FILE_PATH

// Persist the log into the log file after a certain period without changes
suspend fun writeLogsToFile() {
withContext(backgroundContext) {
log
.debounce(5.seconds)
.collectLatest { lines ->
writeFile(FILE_PATH, lines.joinToString("\n"), append = false)
}
}
}

val logWriter = object : LogWriter() {
override fun isLoggable(
tag: String,
Expand All @@ -69,9 +83,7 @@ class AppLogger(
val logMessage =
"${LocalDateTime.now().logFormat()} : ${severity.name.uppercase()} : $message"
log.update { lines ->
val newLines = (lines + logMessage).takeLast(MAX_LINES)
writeFile(FILE_PATH, newLines.joinToString("\n"), append = false)
newLines
(lines + logMessage).takeLast(MAX_LINES)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
package org.ooni.probe.ui.settings.about

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import ooniprobe.composeapp.generated.resources.Res
import ooniprobe.composeapp.generated.resources.logo_probe
import ooniprobe.composeapp.generated.resources.onboarding
import org.jetbrains.compose.resources.painterResource
import org.ooni.probe.ui.shared.isHeightCompact

Expand Down

0 comments on commit 55b5b69

Please sign in to comment.