• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <benchmark/benchmark.h>
17 #include "benchmark_fwk.h"
18 #include "init_param.h"
19 #include "parameter.h"
20 #include "sys_param.h"
21 
22 using namespace std;
23 using namespace init_benchmark_test;
24 namespace {
25 static int g_maxCount = 512;
26 }
27 
TestRandom(void)28 static inline int TestRandom(void)
29 {
30     return random();
31 }
32 
33 namespace init_benchmark_param {
34 struct LocalParameterTestState {
LocalParameterTestStateinit_benchmark_param::LocalParameterTestState35     explicit LocalParameterTestState(int nprops) noexcept : nprops(nprops), valid(false)
36     {
37         static const char paramNameChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_.";
38         if (g_maxCount < nprops) {
39             fprintf(stderr, "Invalid nprops %d\n", nprops);
40             return;
41         }
42         names = new char *[nprops];
43         nameLens = new int[nprops];
44         values = new char *[nprops];
45         valueLens = new int[nprops];
46 
47         srandom(nprops);
48         int count = 0;
49         for (int i = 0; i < nprops; i++) {
50             // Make sure the name has at least 10 characters to make
51             // it very unlikely to generate the same TestRandom name.
52             nameLens[i] = (TestRandom() % (PARAM_NAME_LEN_MAX - 10)) + 10; // 10 name len
53             names[i] = new char[PARAM_NAME_LEN_MAX + 1];
54             size_t paramNameLen = sizeof(paramNameChars) - 1;
55             for (int j = 0; j < nameLens[i]; j++) {
56                 if (j == 0 || names[i][j - 1] == '.' || j == nameLens[i] - 1) {
57                     // Certain values are not allowed:
58                     // - Don't start name with '.'
59                     // - Don't allow '.' to appear twice in a row
60                     // - Don't allow the name to end with '.'
61                     // This assumes that '.' is the last character in the
62                     // array so that decrementing the length by one removes
63                     // the value from the possible values.
64                     paramNameLen--;
65                 }
66                 names[i][j] = paramNameChars[TestRandom() % paramNameLen];
67             }
68             names[i][nameLens[i]] = 0;
69 
70             // Make sure the value contains at least 1 character.
71             valueLens[i] = (TestRandom() % (PARAM_VALUE_LEN_MAX - 1)) + 1;
72             values[i] = new char[PARAM_VALUE_LEN_MAX];
73             for (int j = 0; j < valueLens[i]; j++) {
74                 values[i][j] = paramNameChars[TestRandom() % (sizeof(paramNameChars) - 1)];
75             }
76 
77             if (SystemSetParameter(names[i], values[i]) != 0) {
78                 count++;
79             }
80         }
81         if (count > 0) {
82             fprintf(stderr, "Failed to add a property, count %d\n", count);
83         }
84         valid = true;
85     }
86 
87     LocalParameterTestState(const LocalParameterTestState&) = delete;
88     LocalParameterTestState & operator=(const LocalParameterTestState&) = delete;
89 
~LocalParameterTestStateinit_benchmark_param::LocalParameterTestState90     ~LocalParameterTestState() noexcept
91     {
92         for (int i = 0; i < nprops; i++) {
93             delete names[i];
94             delete values[i];
95         }
96         delete[] names;
97         delete[] nameLens;
98         delete[] values;
99         delete[] valueLens;
100     }
101 
102 public:
103     const int nprops;
104     char **names;
105     int *nameLens;
106     char **values;
107     int *valueLens;
108     bool valid;
109 };
110 }
111 
112 static init_benchmark_param::LocalParameterTestState *g_localParamTester = nullptr;
113 
CreateLocalParameterTest(int max)114 void CreateLocalParameterTest(int max)
115 {
116     g_maxCount = max > 0 ? max : g_maxCount;
117     g_localParamTester = new init_benchmark_param::LocalParameterTestState(g_maxCount);
118     if (g_localParamTester == nullptr) {
119         exit(0);
120     }
121 }
122 
123 /**
124  * @brief for parameter get
125  *
126  * @param state
127  */
BMCachedParameterGet(benchmark::State & state)128 static void BMCachedParameterGet(benchmark::State &state)
129 {
130     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
131         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
132         return;
133     }
134 
135     CachedHandle handle = CachedParameterCreate(g_localParamTester->names[TestRandom() % g_maxCount], "4444444");
136     for (auto _ : state) {
137         benchmark::DoNotOptimize(CachedParameterGet(handle));
138     }
139     state.SetItemsProcessed(state.iterations());
140 }
141 
142 /**
143  * @brief for parameter get, static handle
144  *
145  * @param state
146  */
BMCachedParameterGetChangedStatic(benchmark::State & state)147 static void BMCachedParameterGetChangedStatic(benchmark::State &state)
148 {
149     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
150         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
151         return;
152     }
153 
154     for (auto _ : state) {
155         static CachedHandle handle = CachedParameterCreate(
156             g_localParamTester->names[TestRandom() % g_maxCount], "xxxxxx");
157         int changed = 0;
158         benchmark::DoNotOptimize(CachedParameterGetChanged(handle, &changed));
159     }
160     state.SetItemsProcessed(state.iterations());
161 }
162 
163 /**
164  * @brief for parameter get, global handle
165  *
166  * @param state
167  */
BMCachedParameterGetChangedGlobal(benchmark::State & state)168 static void BMCachedParameterGetChangedGlobal(benchmark::State &state)
169 {
170     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
171         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
172         return;
173     }
174 
175     CachedHandle handle = CachedParameterCreate(g_localParamTester->names[TestRandom() % g_maxCount], "xxxxxxxxx");
176     for (auto _ : state) {
177         int changed = 0;
178         benchmark::DoNotOptimize(CachedParameterGetChanged(handle, &changed));
179     }
180     state.SetItemsProcessed(state.iterations());
181 }
182 
183 /**
184  * @brief for parameter get, global handle
185  *
186  * @param state
187  */
BMCachedParameterGetChangedGlobal2(benchmark::State & state)188 static void BMCachedParameterGetChangedGlobal2(benchmark::State &state)
189 {
190     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
191         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
192         return;
193     }
194 
195     CachedHandle handle = nullptr;
196     for (auto _ : state) {
197         if (handle == nullptr) {
198             handle = CachedParameterCreate(g_localParamTester->names[TestRandom() % g_maxCount], "xxxxxxxxx");
199         }
200         int changed = 0;
201         benchmark::DoNotOptimize(CachedParameterGetChanged(handle, &changed));
202     }
203     state.SetItemsProcessed(state.iterations());
204 }
205 
206 /**
207  * @brief for get
208  * data exist
209  *
210  * @param state
211  */
BMSystemReadParam(benchmark::State & state)212 static void BMSystemReadParam(benchmark::State &state)
213 {
214     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
215         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
216         return;
217     }
218     {
219         char value[PARAM_VALUE_LEN_MAX] = {0};
220         uint32_t len = PARAM_VALUE_LEN_MAX;
221         SystemReadParam(g_localParamTester->names[TestRandom() % g_maxCount], value, &len);
222     }
223     for (auto _ : state) {
224         char value[PARAM_VALUE_LEN_MAX] = {0};
225         uint32_t len = PARAM_VALUE_LEN_MAX;
226         SystemReadParam(g_localParamTester->names[TestRandom() % g_maxCount], value, &len);
227     }
228     state.SetItemsProcessed(state.iterations());
229 }
230 
231 /**
232  * @brief for get
233  * data not exist
234  *
235  * @param state
236  */
BMSystemReadParam_none(benchmark::State & state)237 static void BMSystemReadParam_none(benchmark::State &state)
238 {
239     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
240         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
241         return;
242     }
243     {
244         char value[PARAM_VALUE_LEN_MAX] = {0};
245         uint32_t len = PARAM_VALUE_LEN_MAX;
246         SystemReadParam("test.aaa.aaa.aaa", value, &len);
247     }
248     for (auto _ : state) {
249         char value[PARAM_VALUE_LEN_MAX] = {0};
250         uint32_t len = PARAM_VALUE_LEN_MAX;
251         SystemReadParam("test.aaa.aaa.aaa", value, &len);
252     }
253     state.SetItemsProcessed(state.iterations());
254 }
255 
256 /**
257  * @brief for find
258  *
259  * @param state
260  */
BMSystemFindParameter(benchmark::State & state)261 static void BMSystemFindParameter(benchmark::State &state)
262 {
263     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
264         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
265         return;
266     }
267 
268     for (auto _ : state) {
269         ParamHandle handle = 0;
270         SystemFindParameter(g_localParamTester->names[TestRandom() % g_maxCount], &handle);
271     }
272     state.SetItemsProcessed(state.iterations());
273 }
274 
275 /**
276  * @brief for find, and read value
277  *
278  * @param state
279  */
BMSystemGetParameterValue(benchmark::State & state)280 static void BMSystemGetParameterValue(benchmark::State &state)
281 {
282     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
283         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
284         return;
285     }
286 
287     ParamHandle *handle = new ParamHandle[g_maxCount];
288     for (int i = 0; i < g_maxCount; ++i) {
289         SystemFindParameter(g_localParamTester->names[TestRandom() % g_maxCount], &handle[i]);
290     }
291 
292     int i = 0;
293     char value[PARAM_VALUE_LEN_MAX];
294     for (auto _ : state) {
295         uint32_t len = PARAM_VALUE_LEN_MAX;
296         SystemGetParameterValue(handle[i], value, &len);
297         i = (i + 1) % g_maxCount;
298     }
299     state.SetItemsProcessed(state.iterations());
300     delete[] handle;
301 }
302 
303 /**
304  * @brief for find, and read commit id
305  *
306  * @param state
307  */
BMSystemGetParameterCommitId(benchmark::State & state)308 static void BMSystemGetParameterCommitId(benchmark::State &state)
309 {
310     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
311         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
312         return;
313     }
314 
315     ParamHandle *handle = new ParamHandle[g_maxCount];
316     for (int i = 0; i < g_maxCount; ++i) {
317         SystemFindParameter(g_localParamTester->names[TestRandom() % g_maxCount], &handle[i]);
318     }
319 
320     int i = 0;
321     for (auto _ : state) {
322         uint32_t commitId = 0;
323         SystemGetParameterCommitId(handle[i], &commitId);
324         i = (i + 1) % g_maxCount;
325     }
326     state.SetItemsProcessed(state.iterations());
327     delete[] handle;
328 }
329 
BMTestRandom(benchmark::State & state)330 static void BMTestRandom(benchmark::State &state)
331 {
332     if (g_localParamTester == nullptr || !g_localParamTester->valid) {
333         fprintf(stderr, "Invalid nprops %d \n", g_maxCount);
334         return;
335     }
336 
337     for (auto _ : state) {
338         benchmark::DoNotOptimize(TestRandom());
339     }
340     state.SetItemsProcessed(state.iterations());
341 }
342 
343 INIT_BENCHMARK(BMCachedParameterGet);
344 INIT_BENCHMARK(BMCachedParameterGetChangedStatic);
345 INIT_BENCHMARK(BMCachedParameterGetChangedGlobal);
346 INIT_BENCHMARK(BMCachedParameterGetChangedGlobal2);
347 
348 INIT_BENCHMARK(BMSystemReadParam);
349 INIT_BENCHMARK(BMSystemReadParam_none);
350 INIT_BENCHMARK(BMSystemFindParameter);
351 INIT_BENCHMARK(BMSystemGetParameterValue);
352 INIT_BENCHMARK(BMSystemGetParameterCommitId);
353 INIT_BENCHMARK(BMTestRandom);
354