Skip to content

Commit

Permalink
feat: optimize log system and time statistics system
Browse files Browse the repository at this point in the history
  • Loading branch information
idisfkj committed Dec 27, 2021
1 parent 0c4ce07 commit 785ac5d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class StartupManager private constructor(
mAwaitCountDownLatch = CountDownLatch(needAwaitCount.get())

if (startupList.isNullOrEmpty()) {
StartupLogUtils.e("startupList is empty in the current process.")
StartupLogUtils.e { "startupList is empty in the current process." }
return@apply
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ internal class StartupManagerDispatcher(
}

override fun dispatch(startup: Startup<*>, sortStore: StartupSortStore) {
StartupLogUtils.d("${startup::class.java.simpleName} being dispatching, onMainThread ${startup.callCreateOnMainThread()}.")
StartupLogUtils.d { "${startup::class.java.simpleName} being dispatching, onMainThread ${startup.callCreateOnMainThread()}." }

if (StartupCacheManager.instance.hadInitialized(startup::class.java)) {
val result = StartupCacheManager.instance.obtainInitializedResult<Any>(startup::class.java)

StartupLogUtils.d("${startup::class.java.simpleName} was completed, result from cache.")
StartupLogUtils.d { "${startup::class.java.simpleName} was completed, result from cache." }

notifyChildren(startup, result, sortStore)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ internal class StartupRunnable(
override fun run() {
Process.setThreadPriority(startup::class.java.getAnnotation(ThreadPriority::class.java)?.priority ?: Process.THREAD_PRIORITY_DEFAULT)
startup.toWait()
StartupLogUtils.d("${startup::class.java.simpleName} being create.")
StartupLogUtils.d { "${startup::class.java.simpleName} being create." }

TraceCompat.beginSection(startup::class.java.simpleName)
StartupCostTimesUtils.recordStart(startup::class.java, startup.callCreateOnMainThread(), startup.waitOnMainThread())
StartupCostTimesUtils.recordStart { Triple(startup::class.java, startup.callCreateOnMainThread(), startup.waitOnMainThread()) }
val result = startup.create(context)
StartupCostTimesUtils.recordEnd(startup::class.java)
StartupCostTimesUtils.recordEnd { startup::class.java }
TraceCompat.endSection()

// To save result of initialized component.
StartupCacheManager.instance.saveInitializedComponent(startup::class.java, ResultModel(result))
StartupLogUtils.d("${startup::class.java.simpleName} was completed.")
StartupLogUtils.d { "${startup::class.java.simpleName} was completed." }

dispatcher.notifyChildren(startup, result, sortStore)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ internal object TopologySort {
append("|================================================================")
}
}
StartupLogUtils.d(printBuilder)
StartupLogUtils.d { printBuilder }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ internal object StartupCostTimesUtils {
val mainThreadTimes
get() = (endTime ?: System.nanoTime()) - startTime

fun recordStart(startup: Class<out Startup<*>>, callOnMainThread: Boolean, waitOnMainThread: Boolean) {
fun recordStart(block: () -> Triple<Class<out Startup<*>>, Boolean, Boolean>) {
if (checkOpenStatistics()) {
costTimesMap[startup.getUniqueKey()] = CostTimesModel(
startup.simpleName,
callOnMainThread,
waitOnMainThread,
System.nanoTime() / ACCURACY
)
block().run {
costTimesMap[first.getUniqueKey()] = CostTimesModel(
first.simpleName,
second,
third,
System.nanoTime() / ACCURACY
)
}
}
}

fun recordEnd(startup: Class<out Startup<*>>) {
fun recordEnd(block: () -> Class<out Startup<*>>) {
if (checkOpenStatistics()) {
costTimesMap[startup.getUniqueKey()]?.let {
costTimesMap[block().getUniqueKey()]?.let {
it.endTime = System.nanoTime() / ACCURACY
}
}
Expand All @@ -49,7 +51,7 @@ internal object StartupCostTimesUtils {
}

fun printAll() {
StartupLogUtils.d(buildString {
StartupLogUtils.d { buildString {
append("startup cost times detail:")
append("\n")
append("|=================================================================")
Expand All @@ -75,7 +77,7 @@ internal object StartupCostTimesUtils {
append("| Total Main Thread Times | ${mainThreadTimes / ACCURACY} ms")
append("\n")
append("|=================================================================")
})
} }
}

private fun checkOpenStatistics() = StartupCacheManager.instance.initializedConfig?.openStatistic == true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ internal object StartupLogUtils {
private const val TAG = "StartupTrack"
var level: LoggerLevel = LoggerLevel.NONE

fun e(message: String) {
if (level >= LoggerLevel.ERROR) print(Log.ERROR, TAG, message)
fun e(block: () -> String) {
if (level >= LoggerLevel.ERROR) print(Log.ERROR, TAG, block())
}

fun d(message: String) {
if (level >= LoggerLevel.DEBUG) print(Log.DEBUG, TAG, message)
fun d(block: () -> String) {
if (level >= LoggerLevel.DEBUG) print(Log.DEBUG, TAG, block())
}

/**
Expand Down

0 comments on commit 785ac5d

Please sign in to comment.