1 /*
2  * Copyright (C) 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 
18 package android.provider;
19 
20 import android.content.ContentResolver;
21 import android.content.Context;
22 import android.perftests.utils.BenchmarkState;
23 import android.perftests.utils.PerfStatusReporter;
24 
25 import androidx.test.ext.junit.runners.AndroidJUnit4;
26 import androidx.test.platform.app.InstrumentationRegistry;
27 
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 import java.util.ArrayList;
35 import java.util.List;
36 
37 @RunWith(AndroidJUnit4.class)
38 public final class SettingsProviderPerfTest {
39     private static final String NAMESPACE = "test@namespace";
40     private static final String SETTING_NAME1 = "test:setting1";
41     private static final String SETTING_NAME2 = "test-setting2";
42     private static final String UNSET_SETTING = "test_unset_setting";
43 
44     private final ContentResolver mContentResolver;
45 
46     @Rule
47     public final PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
48 
SettingsProviderPerfTest()49     public SettingsProviderPerfTest() {
50         final Context context = InstrumentationRegistry.getInstrumentation().getContext();
51         mContentResolver = context.getContentResolver();
52     }
53 
54     @Before
setUp()55     public void setUp() {
56         Settings.Secure.putString(mContentResolver, SETTING_NAME1, "1");
57         Settings.Config.putString(NAMESPACE, SETTING_NAME1, "1", true);
58         Settings.Config.putString(NAMESPACE, SETTING_NAME2, "2", true);
59     }
60 
61     @After
destroy()62     public void destroy() {
63         Settings.Secure.putString(mContentResolver, SETTING_NAME1, "null");
64         Settings.Config.deleteString(NAMESPACE, SETTING_NAME1);
65         Settings.Config.deleteString(NAMESPACE, SETTING_NAME2);
66     }
67 
68     @Test
testSettingsValueConsecutiveRead()69     public void testSettingsValueConsecutiveRead() {
70         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
71         int i = 0;
72         while (state.keepRunning()) {
73             state.pauseTiming();
74             // Writing to setting2 should not invalidate setting1's cache
75             Settings.Secure.putString(mContentResolver, SETTING_NAME2, Integer.toString(i));
76             i++;
77             state.resumeTiming();
78             Settings.Secure.getString(mContentResolver, SETTING_NAME1);
79         }
80     }
81 
82     @Test
testSettingsValueConsecutiveReadAfterWrite()83     public void testSettingsValueConsecutiveReadAfterWrite() {
84         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
85         int i = 0;
86         while (state.keepRunning()) {
87             state.pauseTiming();
88             // Triggering the invalidation of setting1's cache
89             Settings.Secure.putString(mContentResolver, SETTING_NAME1, Integer.toString(i));
90             i++;
91             state.resumeTiming();
92             Settings.Secure.getString(mContentResolver, SETTING_NAME1);
93         }
94     }
95 
96     @Test
testSettingsValueConsecutiveReadUnset()97     public void testSettingsValueConsecutiveReadUnset() {
98         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
99         while (state.keepRunning()) {
100             Settings.Secure.getString(mContentResolver, UNSET_SETTING);
101         }
102     }
103 
104     @Test
testSettingsNamespaceConsecutiveRead()105     public void testSettingsNamespaceConsecutiveRead() {
106         final List<String> names = new ArrayList<>();
107         names.add(SETTING_NAME1);
108         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
109         while (state.keepRunning()) {
110             Settings.Config.getStrings(mContentResolver, NAMESPACE, names);
111         }
112     }
113 
114     @Test
testSettingsNamespaceConsecutiveReadAfterWrite()115     public void testSettingsNamespaceConsecutiveReadAfterWrite() {
116         final List<String> names = new ArrayList<>();
117         names.add(SETTING_NAME1);
118         final BenchmarkState state = mPerfStatusReporter.getBenchmarkState();
119         int i = 0;
120         while (state.keepRunning()) {
121             state.pauseTiming();
122             // Triggering the invalidation of the list's cache
123             Settings.Config.putString(NAMESPACE, SETTING_NAME2, Integer.toString(i), true);
124             i++;
125             state.resumeTiming();
126             Settings.Config.getStrings(mContentResolver, NAMESPACE, names);
127         }
128     }
129 }
130