1 /*
2 * Copyright (C) 2017 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 specic language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <android-base/file.h>
18 #include <android-base/properties.h>
19 #include <android-base/stringprintf.h>
20 #include <gtest/gtest.h>
21
22 #include <algorithm>
23 #include <thread>
24
25 #include "perfmgr/PropertyNode.h"
26
27 namespace android {
28 namespace perfmgr {
29
30 using std::literals::chrono_literals::operator""ms;
31
32 constexpr double kTIMING_TOLERANCE_MS = std::chrono::milliseconds(25).count();
33 constexpr auto kSLEEP_TOLERANCE_MS = 2ms;
34
_VerifyPropertyValue(const std::string & path,const std::string & value)35 static inline void _VerifyPropertyValue(const std::string& path,
36 const std::string& value) {
37 std::string s = android::base::GetProperty(path, "");
38 EXPECT_EQ(value, s);
39 }
40
_InitProperty(const std::string & path)41 static inline const std::string _InitProperty(const std::string& path) {
42 EXPECT_TRUE(android::base::SetProperty(path, ""))
43 << "failed to clear property";
44 return path;
45 }
46
47 // Test init with no default value
TEST(PropertyNodeTest,NoInitDefaultTest)48 TEST(PropertyNodeTest, NoInitDefaultTest) {
49 std::string key = _InitProperty("test.libperfmgr.key");
50 PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
51 t.Update(false);
52 _VerifyPropertyValue(key, "");
53 }
54
55 // Test init with default value
TEST(PropertyNodeTest,InitDefaultTest)56 TEST(PropertyNodeTest, InitDefaultTest) {
57 std::string key = _InitProperty("test.libperfmgr.key");
58 PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 1, true);
59 t.Update(false);
60 _VerifyPropertyValue(key, "value1");
61 std::string key2 = _InitProperty("test.libperfmgr.key2");
62 PropertyNode t2("t2", key2, {{"value0"}, {"value1"}, {"value2"}}, 0, true);
63 t2.Update(false);
64 _VerifyPropertyValue(key2, "value0");
65 }
66
67 // Test DumpToFd
TEST(PropertyNodeTest,DumpToFdTest)68 TEST(PropertyNodeTest, DumpToFdTest) {
69 std::string key = _InitProperty("test.libperfmgr.key");
70 PropertyNode t("test_dump", key, {{"value0"}, {"value1"}, {"value2"}}, 1,
71 true);
72 t.Update(false);
73 TemporaryFile dumptf;
74 t.DumpToFd(dumptf.fd);
75 fsync(dumptf.fd);
76 std::string buf(
77 android::base::StringPrintf("test_dump\t%s\t1\tvalue1\n", key.c_str()));
78 std::string s;
79 EXPECT_TRUE(android::base::ReadFileToString(dumptf.path, &s))
80 << strerror(errno);
81 EXPECT_EQ(buf, s);
82 }
83
84 // Test GetValueIndex
TEST(PropertyNodeTest,GetValueIndexTest)85 TEST(PropertyNodeTest, GetValueIndexTest) {
86 std::string key = _InitProperty("test.libperfmgr.key");
87 PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
88 std::size_t index = 0;
89 EXPECT_TRUE(t.GetValueIndex("value2", &index));
90 EXPECT_EQ(2u, index);
91 index = 1234;
92 EXPECT_FALSE(t.GetValueIndex("NON_EXIST", &index));
93 EXPECT_EQ(1234u, index);
94 }
95
96 // Test GetValues
TEST(PropertyNodeTest,GetValuesTest)97 TEST(PropertyNodeTest, GetValuesTest) {
98 std::string key = _InitProperty("test.libperfmgr.key");
99 PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 1, false);
100 std::vector values = t.GetValues();
101 EXPECT_EQ(3u, values.size());
102 EXPECT_EQ("value0", values[0]);
103 EXPECT_EQ("value1", values[1]);
104 EXPECT_EQ("value2", values[2]);
105 }
106
107 // Test get more properties
TEST(PropertyNodeTest,GetPropertiesTest)108 TEST(PropertyNodeTest, GetPropertiesTest) {
109 std::string test_name = "TESTREQ_1";
110 std::string test_path = "TEST_PATH";
111 PropertyNode t(test_name, test_path, {}, 0, false);
112 EXPECT_EQ(test_name, t.GetName());
113 EXPECT_EQ(test_path, t.GetPath());
114 EXPECT_EQ(0u, t.GetValues().size());
115 EXPECT_EQ(0u, t.GetDefaultIndex());
116 EXPECT_FALSE(t.GetResetOnInit());
117 }
118
119 // Test add request
TEST(PropertyNodeTest,AddRequestTest)120 TEST(PropertyNodeTest, AddRequestTest) {
121 std::string key = _InitProperty("test.libperfmgr.key");
122 PropertyNode t("t", key, {{"value0"}, {"value1"}, {""}}, 2, true);
123 auto start = std::chrono::steady_clock::now();
124 EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
125 std::chrono::milliseconds expire_time = t.Update(true);
126 // Add request @ value1
127 _VerifyPropertyValue(key, "value1");
128 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
129 kTIMING_TOLERANCE_MS);
130 // Add request @ value0 higher prio than value1
131 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
132 expire_time = t.Update(true);
133 _VerifyPropertyValue(key, "value0");
134 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
135 kTIMING_TOLERANCE_MS);
136 // Let high prio request timeout, now only request @ value1 active
137 std::this_thread::sleep_for(expire_time + kSLEEP_TOLERANCE_MS);
138 expire_time = t.Update(true);
139 _VerifyPropertyValue(key, "value1");
140 EXPECT_NEAR(std::chrono::milliseconds(300).count(), expire_time.count(),
141 kTIMING_TOLERANCE_MS);
142 // Let all requests timeout, now default value2
143 std::this_thread::sleep_for(expire_time + kSLEEP_TOLERANCE_MS);
144 expire_time = t.Update(true);
145 _VerifyPropertyValue(key, "");
146 EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
147 }
148
149 // Test remove request
TEST(PropertyNodeTest,RemoveRequestTest)150 TEST(PropertyNodeTest, RemoveRequestTest) {
151 std::string key = _InitProperty("test.libperfmgr.key");
152 PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 2, true);
153 auto start = std::chrono::steady_clock::now();
154 EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
155 std::chrono::milliseconds expire_time = t.Update(true);
156 // Add request @ value1
157 _VerifyPropertyValue(key, "value1");
158 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
159 kTIMING_TOLERANCE_MS);
160 // Add request @ value0 higher prio than value1
161 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
162 expire_time = t.Update(true);
163 _VerifyPropertyValue(key, "value0");
164 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
165 kTIMING_TOLERANCE_MS);
166 // Remove high prio request, now only request @ value1 active
167 t.RemoveRequest("LAUNCH");
168 expire_time = t.Update(true);
169 _VerifyPropertyValue(key, "value1");
170 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
171 kTIMING_TOLERANCE_MS);
172 // Remove request, now default value2
173 t.RemoveRequest("INTERACTION");
174 expire_time = t.Update(true);
175 _VerifyPropertyValue(key, "value2");
176 EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
177 }
178
179 // Test add request
TEST(PropertyNodeTest,AddRequestTestOverride)180 TEST(PropertyNodeTest, AddRequestTestOverride) {
181 std::string key = _InitProperty("test.libperfmgr.key");
182 PropertyNode t("t", key, {{"value0"}, {"value1"}, {"value2"}}, 2, true);
183 auto start = std::chrono::steady_clock::now();
184 EXPECT_TRUE(t.AddRequest(1, "INTERACTION", start + 500ms));
185 std::chrono::milliseconds expire_time = t.Update(true);
186 // Add request @ value1
187 _VerifyPropertyValue(key, "value1");
188 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
189 kTIMING_TOLERANCE_MS);
190 // Add request @ value0 higher prio than value1
191 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 200ms));
192 expire_time = t.Update(true);
193 _VerifyPropertyValue(key, "value0");
194 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
195 kTIMING_TOLERANCE_MS);
196 // Add request @ value0 shorter
197 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 100ms));
198 expire_time = t.Update(true);
199 _VerifyPropertyValue(key, "value0");
200 EXPECT_NEAR(std::chrono::milliseconds(200).count(), expire_time.count(),
201 kTIMING_TOLERANCE_MS);
202 // Add request @ value0 longer
203 EXPECT_TRUE(t.AddRequest(0, "LAUNCH", start + 300ms));
204 expire_time = t.Update(true);
205 _VerifyPropertyValue(key, "value0");
206 EXPECT_NEAR(std::chrono::milliseconds(300).count(), expire_time.count(),
207 kTIMING_TOLERANCE_MS);
208 // Remove high prio request, now only request @ value1 active
209 t.RemoveRequest("LAUNCH");
210 expire_time = t.Update(true);
211 _VerifyPropertyValue(key, "value1");
212 EXPECT_NEAR(std::chrono::milliseconds(500).count(), expire_time.count(),
213 kTIMING_TOLERANCE_MS);
214 // Remove request, now default value2
215 t.RemoveRequest("INTERACTION");
216 expire_time = t.Update(true);
217 _VerifyPropertyValue(key, "value2");
218 EXPECT_EQ(std::chrono::milliseconds::max(), expire_time);
219 }
220
221 } // namespace perfmgr
222 } // namespace android
223