• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.example.tracing.demo.experiments
17 
18 import com.android.app.tracing.coroutines.launchTraced
19 import com.android.app.tracing.coroutines.traceCoroutine
20 import com.android.app.tracing.coroutines.withContextTraced
21 import com.example.tracing.demo.FixedPool
22 import com.example.tracing.demo.FixedThread1
23 import com.example.tracing.demo.FixedThread2
24 import com.example.tracing.demo.FixedThread3
25 import com.example.tracing.demo.FixedThread4
26 import javax.inject.Inject
27 import javax.inject.Singleton
28 import kotlinx.coroutines.CoroutineDispatcher
29 import kotlinx.coroutines.coroutineScope
30 import kotlinx.coroutines.delay
31 
32 @Singleton
33 class LaunchStressTest
34 @Inject
35 constructor(
36     @FixedThread1 private var dispatcher1: CoroutineDispatcher,
37     @FixedThread2 private var dispatcher2: CoroutineDispatcher,
38     @FixedThread3 private val dispatcher3: CoroutineDispatcher,
39     @FixedThread4 private val dispatcher4: CoroutineDispatcher,
40     @FixedPool private var fixedPoolDispatcher: CoroutineDispatcher,
41 ) : TracedExperiment() {
42 
43     override val description: String = "Simultaneous launch{} calls on different threads"
44 
45     override suspend fun runExperiment(): Unit = coroutineScope {
46         repeat(16) { n ->
47             launchTraced("launch#$n", fixedPoolDispatcher) {
48                 withContextTraced("context-switch-pool", fixedPoolDispatcher) {
49                     withContextTraced("context-switch-1", dispatcher1) {
50                         traceCoroutine("delay#$n:1") { delay(5) }
51                     }
52                     traceCoroutine("delay#$n:2") { delay(5) }
53                     withContextTraced("context-switch-2", dispatcher2) {
54                         traceCoroutine("delay#$n:3") { delay(5) }
55                     }
56                 }
57                 withContextTraced("context-switch-3", dispatcher3) {
58                     traceCoroutine("delay#$n:3") {
59                         traceCoroutine("delay#$n:4") { delay(5) }
60                         withContextTraced("context-switch-4", dispatcher4) {
61                             traceCoroutine("delay#$n:5") { delay(5) }
62                         }
63                     }
64                 }
65             }
66         }
67     }
68 }
69