• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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.android.app.tracing.benchmark
17 
18 import android.os.Trace
19 import android.perftests.utils.PerfStatusReporter
20 import android.platform.test.annotations.EnableFlags
21 import android.platform.test.flag.junit.SetFlagsRule
22 import android.platform.test.rule.EnsureDeviceSettingsRule
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import androidx.test.filters.SmallTest
25 import com.android.systemui.Flags.FLAG_COROUTINE_TRACING
26 import java.util.concurrent.atomic.AtomicInteger
27 import org.junit.After
28 import org.junit.Assert
29 import org.junit.Before
30 import org.junit.Rule
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 
34 @SmallTest
35 @RunWith(AndroidJUnit4::class)
36 @EnableFlags(FLAG_COROUTINE_TRACING)
37 class ThreadLocalMicroBenchmark {
38 
39     @get:Rule val setFlagsRule = SetFlagsRule()
40 
41     @get:Rule val ensureDeviceSettingsRule = EnsureDeviceSettingsRule()
42 
43     @get:Rule val perfStatusReporter = PerfStatusReporter()
44 
45     @Before
beforenull46     fun before() {
47         Assert.assertTrue(Trace.isEnabled())
48     }
49 
50     @After
afternull51     fun after() {
52         Assert.assertTrue(Trace.isEnabled())
53     }
54 
55     @Test
testIntegerIncrementnull56     fun testIntegerIncrement() {
57         val state = perfStatusReporter.benchmarkState
58         val count: ThreadLocal<Int> = ThreadLocal()
59         count.set(0)
60         while (state.keepRunning()) {
61             count.set(count.get()!! + 1)
62         }
63     }
64 
65     @Test
testAtomicIntegerIncrementnull66     fun testAtomicIntegerIncrement() {
67         val state = perfStatusReporter.benchmarkState
68         val count: ThreadLocal<AtomicInteger> = ThreadLocal()
69         count.set(AtomicInteger(0))
70         while (state.keepRunning()) {
71             count.get()!!.getAndIncrement()
72         }
73     }
74 
75     @Test
testIntArrayIncrementnull76     fun testIntArrayIncrement() {
77         val state = perfStatusReporter.benchmarkState
78         val count: ThreadLocal<Array<Int>> = ThreadLocal()
79         count.set(arrayOf(0))
80         while (state.keepRunning()) {
81             val arr = count.get()!!
82             arr[0]++
83         }
84     }
85 
86     @Test
testMutableIntIncrementnull87     fun testMutableIntIncrement() {
88         val state = perfStatusReporter.benchmarkState
89         class MutableInt(var value: Int)
90         val count: ThreadLocal<MutableInt> = ThreadLocal()
91         count.set(MutableInt(0))
92         while (state.keepRunning()) {
93             count.get()!!.value++
94         }
95     }
96 }
97