Skip to content

Commit a39c5da

Browse files
Merge pull request #2629 from DataDog/aleksandr-gringauz/RUM-9508/logs-scenarios-heavy-4
RUM-9508: LogsHeavyTraffic scenario for android benchmarks
2 parents dd4ee16 + 8162d64 commit a39c5da

23 files changed

+857
-149
lines changed

sample/benchmark/src/main/java/com/datadog/benchmark/sample/MainActivity.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import android.app.Activity
1010
import android.os.Bundle
1111
import androidx.activity.compose.setContent
1212
import androidx.appcompat.app.AppCompatActivity
13+
import androidx.lifecycle.ViewModelProvider
1314
import androidx.navigation.findNavController
1415
import com.datadog.benchmark.DatadogBaseMeter
1516
import com.datadog.benchmark.sample.config.BenchmarkConfig
1617
import com.datadog.benchmark.sample.di.activity.BenchmarkActivityComponent
17-
import com.datadog.benchmark.sample.di.activity.DaggerBenchmarkActivityComponent
1818
import com.datadog.benchmark.sample.navigation.FragmentsNavigationManager
1919
import com.datadog.benchmark.sample.ui.sessionreplaycompose.MainView
2020
import com.datadog.sample.benchmark.R
@@ -34,20 +34,17 @@ class MainActivity : AppCompatActivity() {
3434
@Inject
3535
internal lateinit var datadogFeaturesInitializer: DatadogFeaturesInitializer
3636

37-
private lateinit var config: BenchmarkConfig
37+
@Inject
38+
internal lateinit var config: BenchmarkConfig
3839

39-
internal lateinit var benchmarkActivityComponent: BenchmarkActivityComponent
40+
internal lateinit var viewModel: MainActivityViewModel
4041

4142
override fun onCreate(savedInstanceState: Bundle?) {
4243
super.onCreate(savedInstanceState)
4344

44-
config = BenchmarkConfig.resolveSyntheticsBundle(intent.extras)
45+
val factory = MainActivityViewModelFactory(application, this)
4546

46-
benchmarkActivityComponent = DaggerBenchmarkActivityComponent.factory().create(
47-
deps = application.benchmarkAppComponent,
48-
config = config,
49-
mainActivity = this
50-
)
47+
viewModel = ViewModelProvider(this, factory)[MainActivityViewModel::class]
5148

5249
benchmarkActivityComponent.inject(this)
5350

@@ -82,4 +79,4 @@ class MainActivity : AppCompatActivity() {
8279
}
8380

8481
internal val Activity.benchmarkActivityComponent: BenchmarkActivityComponent
85-
get() = (this as MainActivity).benchmarkActivityComponent
82+
get() = (this as MainActivity).viewModel.component
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2016-Present Datadog, Inc.
5+
*/
6+
7+
package com.datadog.benchmark.sample
8+
9+
import android.app.Application
10+
import androidx.lifecycle.ViewModel
11+
import androidx.lifecycle.ViewModelProvider
12+
import com.datadog.benchmark.sample.config.BenchmarkConfig
13+
import com.datadog.benchmark.sample.di.activity.BenchmarkActivityComponent
14+
import com.datadog.benchmark.sample.di.activity.DaggerBenchmarkActivityComponent
15+
16+
internal class MainActivityViewModel(
17+
val component: BenchmarkActivityComponent
18+
) : ViewModel()
19+
20+
internal class MainActivityViewModelFactory(
21+
private val application: Application,
22+
private val activity: MainActivity
23+
) : ViewModelProvider.Factory {
24+
@Suppress("UNCHECKED_CAST")
25+
override fun <T : ViewModel> create(modelClass: Class<T>): T {
26+
val config = BenchmarkConfig.resolveSyntheticsBundle(activity.intent.extras)
27+
28+
val benchmarkActivityComponent = DaggerBenchmarkActivityComponent.factory().create(
29+
deps = application.benchmarkAppComponent,
30+
config = config,
31+
mainActivity = activity
32+
)
33+
34+
return MainActivityViewModel(benchmarkActivityComponent) as T
35+
}
36+
}

sample/benchmark/src/main/java/com/datadog/benchmark/sample/di/activity/BenchmarkActivityComponent.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import com.datadog.android.api.SdkCore
1010
import com.datadog.benchmark.sample.DatadogFeaturesInitializer
1111
import com.datadog.benchmark.sample.MainActivity
1212
import com.datadog.benchmark.sample.config.BenchmarkConfig
13+
import com.datadog.benchmark.sample.di.common.DispatchersModule
1314
import com.datadog.benchmark.sample.ui.logscustom.LogsFragment
15+
import com.datadog.benchmark.sample.ui.logsheavytraffic.di.LogsHeavyTrafficComponentDependencies
1416
import com.datadog.benchmark.sample.ui.sessionreplay.SessionReplayAppcompatFragment
1517
import com.datadog.benchmark.sample.ui.sessionreplay.SessionReplayMaterialFragment
1618
import dagger.BindsInstance
@@ -32,11 +34,12 @@ internal interface BenchmarkActivityComponentDependencies {
3234
modules = [
3335
BenchmarkActivityModule::class,
3436
ViewModelsModule::class,
35-
DatadogActivityModule::class
37+
DatadogActivityModule::class,
38+
DispatchersModule::class
3639
]
3740
)
3841
@BenchmarkActivityScope
39-
internal interface BenchmarkActivityComponent {
42+
internal interface BenchmarkActivityComponent : LogsHeavyTrafficComponentDependencies {
4043
@Component.Factory
4144
interface Factory {
4245
fun create(

sample/benchmark/src/main/java/com/datadog/benchmark/sample/di/activity/DatadogActivityModule.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
package com.datadog.benchmark.sample.di.activity
88

9+
import com.datadog.android.api.SdkCore
10+
import com.datadog.android.log.Logger
911
import com.datadog.benchmark.DatadogBaseMeter
1012
import com.datadog.benchmark.DatadogExporterConfiguration
1113
import com.datadog.benchmark.DatadogSdkMeter
@@ -37,6 +39,15 @@ internal interface DatadogActivityModule {
3739
DatadogVitalsMeter.create(exporterConfig)
3840
}
3941
}
42+
43+
@Provides
44+
@BenchmarkActivityScope
45+
fun provideLogger(sdkCore: SdkCore): Logger {
46+
return Logger.Builder(sdkCore)
47+
.setName("benchmarkLogger")
48+
.setLogcatLogsEnabled(true)
49+
.build()
50+
}
4051
}
4152
}
4253

sample/benchmark/src/main/java/com/datadog/benchmark/sample/di/activity/ViewModelsModule.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ package com.datadog.benchmark.sample.di.activity
99
import androidx.lifecycle.ViewModelProvider
1010
import androidx.lifecycle.viewmodel.initializer
1111
import androidx.lifecycle.viewmodel.viewModelFactory
12-
import com.datadog.android.api.SdkCore
1312
import com.datadog.android.log.Logger
13+
import com.datadog.benchmark.sample.di.common.CoroutineDispatcherQualifier
14+
import com.datadog.benchmark.sample.di.common.CoroutineDispatcherType
1415
import com.datadog.benchmark.sample.ui.logscustom.LogsScreenViewModel
1516
import dagger.Module
1617
import dagger.Provides
17-
import kotlinx.coroutines.Dispatchers
18+
import kotlinx.coroutines.CoroutineDispatcher
1819
import javax.inject.Qualifier
1920
import kotlin.reflect.KClass
2021

@@ -27,17 +28,14 @@ internal interface ViewModelsModule {
2728
@Provides
2829
@ViewModel(LogsScreenViewModel::class)
2930
fun provideLogsScreenViewModelFactory(
30-
sdkCore: SdkCore
31+
logger: Logger,
32+
@CoroutineDispatcherQualifier(CoroutineDispatcherType.Default)
33+
defaultDispatcher: CoroutineDispatcher
3134
): ViewModelProvider.Factory = viewModelFactory {
3235
initializer {
33-
val logger = Logger.Builder(sdkCore)
34-
.setName("benchmarkLogger")
35-
.setLogcatLogsEnabled(true)
36-
.build()
37-
3836
LogsScreenViewModel(
3937
logger = logger,
40-
defaultDispatcher = Dispatchers.Default
38+
defaultDispatcher = defaultDispatcher
4139
)
4240
}
4341
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2016-Present Datadog, Inc.
5+
*/
6+
7+
package com.datadog.benchmark.sample.di.common
8+
9+
import dagger.Module
10+
import dagger.Provides
11+
import kotlinx.coroutines.CoroutineDispatcher
12+
import kotlinx.coroutines.Dispatchers
13+
import javax.inject.Qualifier
14+
15+
internal enum class CoroutineDispatcherType {
16+
IO, Default, Main
17+
}
18+
19+
@Qualifier
20+
internal annotation class CoroutineDispatcherQualifier(val type: CoroutineDispatcherType)
21+
22+
@Module
23+
internal object DispatchersModule {
24+
@Provides
25+
@CoroutineDispatcherQualifier(CoroutineDispatcherType.Default)
26+
fun provideDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
27+
28+
@Provides
29+
@CoroutineDispatcherQualifier(CoroutineDispatcherType.Main)
30+
fun provideMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
31+
32+
@Provides
33+
@CoroutineDispatcherQualifier(CoroutineDispatcherType.IO)
34+
fun provideIODispatcher(): CoroutineDispatcher = Dispatchers.IO
35+
}

sample/benchmark/src/main/java/com/datadog/benchmark/sample/navigation/FragmentsNavigationManagerImpl.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import androidx.navigation.fragment.fragment
1414
import com.datadog.benchmark.sample.config.BenchmarkConfig
1515
import com.datadog.benchmark.sample.config.SyntheticsScenario
1616
import com.datadog.benchmark.sample.ui.logscustom.LogsFragment
17+
import com.datadog.benchmark.sample.ui.logsheavytraffic.LogsHeavyTrafficHostFragment
1718
import com.datadog.benchmark.sample.ui.sessionreplay.SessionReplayAppcompatFragment
1819
import com.datadog.benchmark.sample.ui.sessionreplay.SessionReplayMaterialFragment
1920
import javax.inject.Inject
@@ -52,9 +53,9 @@ private fun createStartDestination(scenario: SyntheticsScenario?): String {
5253
SyntheticsScenario.Upload -> SESSION_REPLAY_METERIAL_FRAGMENT_KEY
5354
SyntheticsScenario.SessionReplayCompose -> error("Using fragments for SessionReplayCompose scenario")
5455
SyntheticsScenario.LogsCustom -> LOGS_FRAGMENT_KEY
56+
SyntheticsScenario.LogsHeavyTraffic -> LOGS_HEAVY_TRAFFIC_FRAGMENT_KEY
5557
SyntheticsScenario.Rum, // TODO RUM-9510
56-
SyntheticsScenario.Trace, // TODO RUM-9509
57-
SyntheticsScenario.LogsHeavyTraffic -> SESSION_REPLAY_METERIAL_FRAGMENT_KEY // TODO RUM-9508
58+
SyntheticsScenario.Trace -> SESSION_REPLAY_METERIAL_FRAGMENT_KEY // TODO RUM-9509
5859
}
5960
}
6061

@@ -65,9 +66,15 @@ private fun NavGraphBuilder.navGraph(scenario: SyntheticsScenario?) {
6566
SyntheticsScenario.Upload -> navGraphSessionReplay()
6667
SyntheticsScenario.SessionReplayCompose -> error("Using fragments for SessionReplayCompose scenario")
6768
SyntheticsScenario.LogsCustom -> navGraphLogs()
69+
SyntheticsScenario.LogsHeavyTraffic -> navGraphLogsHeavyTraffic()
6870
SyntheticsScenario.Rum, // TODO RUM-9510
69-
SyntheticsScenario.Trace, // TODO RUM-9509
70-
SyntheticsScenario.LogsHeavyTraffic -> navGraphSessionReplay() // TODO RUM-9508
71+
SyntheticsScenario.Trace -> navGraphSessionReplay() // TODO RUM-9509
72+
}
73+
}
74+
75+
private fun NavGraphBuilder.navGraphLogsHeavyTraffic() {
76+
fragment<LogsHeavyTrafficHostFragment>(route = LOGS_HEAVY_TRAFFIC_FRAGMENT_KEY) {
77+
label = "logs heavy traffic fragment"
7178
}
7279
}
7380

@@ -88,5 +95,6 @@ private fun NavGraphBuilder.navGraphSessionReplay() {
8895
}
8996

9097
private const val LOGS_FRAGMENT_KEY = "logs_fragment"
98+
private const val LOGS_HEAVY_TRAFFIC_FRAGMENT_KEY = "logs_heavy_traffic_fragment"
9199
private const val SESSION_REPLAY_METERIAL_FRAGMENT_KEY = "fragment_session_replay_material"
92100
private const val SESSION_REPLAY_APPCOMPAT_FRAGMENT_KEY = "fragment_session_replay_appcompat"
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
* Copyright 2016-Present Datadog, Inc.
55
*/
66

7-
@file:Suppress("StringLiteralDuplication")
7+
@file:Suppress("StringLiteralDuplication", "MatchingDeclarationName")
88

9-
package com.datadog.benchmark.sample.ui.logscustom
9+
package com.datadog.benchmark.sample.ui
1010

11+
import android.util.Log
1112
import java.util.UUID
1213

14+
internal enum class LogPayloadSize {
15+
Small,
16+
Medium,
17+
Large
18+
}
19+
1320
internal fun LogPayloadSize.createLogAttributes(): Map<String, Any?> {
1421
return when (this) {
1522
LogPayloadSize.Small -> SMALL_ATTRIBUTES_PAYLOAD
@@ -18,6 +25,27 @@ internal fun LogPayloadSize.createLogAttributes(): Map<String, Any?> {
1825
}
1926
}
2027

28+
internal val ALL_LOG_LEVELS = listOf(
29+
Log.ERROR,
30+
Log.VERBOSE,
31+
Log.ASSERT,
32+
Log.WARN,
33+
Log.INFO,
34+
Log.DEBUG
35+
)
36+
37+
internal fun Int.logStringRepresentation(): String {
38+
return when (this) {
39+
Log.ERROR -> "ERROR"
40+
Log.VERBOSE -> "VERBOSE"
41+
Log.ASSERT -> "ASSERT"
42+
Log.WARN -> "WARN"
43+
Log.INFO -> "INFO"
44+
Log.DEBUG -> "DEBUG"
45+
else -> "UNKNOWN"
46+
}
47+
}
48+
2149
private val MEDIUM_ATTRIBUTES_PAYLOAD = mapOf(
2250
"benchmark_user" to mapOf(
2351
"id" to UUID.randomUUID().toString(),
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2016-Present Datadog, Inc.
5+
*/
6+
7+
package com.datadog.benchmark.sample.ui.common
8+
9+
import androidx.compose.foundation.layout.Box
10+
import androidx.compose.material3.Button
11+
import androidx.compose.material3.DropdownMenu
12+
import androidx.compose.material3.DropdownMenuItem
13+
import androidx.compose.material3.Text
14+
import androidx.compose.runtime.Composable
15+
import androidx.compose.runtime.getValue
16+
import androidx.compose.runtime.mutableStateOf
17+
import androidx.compose.runtime.remember
18+
import androidx.compose.runtime.setValue
19+
import androidx.compose.ui.Modifier
20+
21+
@Composable
22+
internal fun <T> DropDownMenuView(
23+
headerText: String,
24+
items: List<T>,
25+
textFactory: @Composable (T) -> Unit,
26+
onClickAction: (T) -> Unit
27+
) {
28+
Box {
29+
var expanded by remember { mutableStateOf(false) }
30+
31+
Button(
32+
modifier = Modifier,
33+
onClick = { expanded = !expanded }
34+
) {
35+
Text(headerText)
36+
}
37+
DropdownMenu(
38+
expanded = expanded,
39+
onDismissRequest = { expanded = false }
40+
) {
41+
items.forEach {
42+
DropdownMenuItem(
43+
text = { textFactory(it) },
44+
onClick = {
45+
onClickAction(it)
46+
expanded = false
47+
}
48+
)
49+
}
50+
}
51+
}
52+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0.
3+
* This product includes software developed at Datadog (https://www.datadoghq.com/).
4+
* Copyright 2016-Present Datadog, Inc.
5+
*/
6+
7+
package com.datadog.benchmark.sample.ui.common
8+
9+
import androidx.compose.foundation.layout.Row
10+
import androidx.compose.material3.Text
11+
import androidx.compose.runtime.Composable
12+
import androidx.compose.ui.Alignment
13+
import androidx.compose.ui.Modifier
14+
15+
@Composable
16+
internal fun <T> ExpandedItemView(
17+
titleText: String,
18+
items: List<T>,
19+
headerText: String,
20+
itemTextFactory: (T) -> String,
21+
onClick: (T) -> Unit
22+
) {
23+
Row(verticalAlignment = Alignment.CenterVertically) {
24+
Text(text = titleText, modifier = Modifier.weight(1f))
25+
26+
DropDownMenuView(
27+
headerText = headerText,
28+
items = items,
29+
textFactory = { Text(text = itemTextFactory(it)) },
30+
onClickAction = { onClick(it) }
31+
)
32+
}
33+
}

0 commit comments

Comments
 (0)