1 /*
2  * Copyright 2022 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 
17 package androidx.navigation
18 
19 import androidx.benchmark.junit4.BenchmarkRule
20 import androidx.benchmark.junit4.measureRepeated
21 import androidx.test.ext.junit.runners.AndroidJUnit4
22 import org.junit.Rule
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 
26 @RunWith(AndroidJUnit4::class)
27 class NavDeepLinkBenchmark {
28 
29     @get:Rule val benchmarkRule = BenchmarkRule()
30 
navGraphDestinations_withRoutesnull31     @Test fun navGraphDestinations_withRoutes() = inflateNavGraph_withRoutes(1)
32 
33     @Test fun navGraphDestinations_withRoutes10() = inflateNavGraph_withRoutes(10)
34 
35     @Test fun navGraphDestinations_withRoutes50() = inflateNavGraph_withRoutes(50)
36 
37     @Test fun navGraphDestinations_withRoutes100() = inflateNavGraph_withRoutes(100)
38 
39     private fun inflateNavGraph_withRoutes(count: Int) {
40         val navigatorProvider =
41             NavigatorProvider().apply {
42                 addNavigator(NavGraphNavigator(this))
43                 addNavigator(NoOpNavigator())
44             }
45         val navigator = navigatorProvider.getNavigator(NoOpNavigator::class.java)
46         benchmarkRule.measureRepeated {
47             navigatorProvider
48                 .getNavigator(NavGraphNavigator::class.java)
49                 .createDestination()
50                 .apply {
51                     id = GRAPH_ID
52                     setStartDestination(START_DESTINATION_ID)
53                     for (i in 0 until count) {
54                         addDestination(
55                             navigator.createDestination().apply {
56                                 route = URI_PATH + i + URI_EXTRAS
57                                 id = i
58                             }
59                         )
60                     }
61                 }
62         }
63     }
64 
65     companion object {
66         const val URI_PATH = "example.com/"
67         const val URI_EXTRAS = "test/{test}?param1={param}#fragment"
68         const val START_DESTINATION_ID = 0
69         const val GRAPH_ID = 111
70     }
71 }
72