1 /*
2 * Copyright (c) 2021-2022 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 "resource_manager_performance_test.h"
17
18 #include <chrono>
19 #include <climits>
20 #include <cstring>
21 #include <ctime>
22 #include <fstream>
23 #include <gtest/gtest.h>
24 #include <iostream>
25
26 #define private public
27
28 #include "hap_parser.h"
29 #include "hap_resource.h"
30 #include "resource_manager.h"
31 #include "resource_manager_impl.h"
32 #include "test_common.h"
33 #include "utils/errors.h"
34
35 using namespace OHOS::Global::Resource;
36 using namespace testing::ext;
37 using namespace std;
38 namespace {
39 class ResourceManagerPerformanceTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42
43 static void TearDownTestCase(void);
44
45 void SetUp();
46
47 void TearDown();
48
ResourceManagerPerformanceTest()49 ResourceManagerPerformanceTest() : rm(nullptr)
50 {}
51
~ResourceManagerPerformanceTest()52 ~ResourceManagerPerformanceTest()
53 {}
54
55 public:
56 ResourceManager *rm;
57
58 int GetResId(std::string name, ResType resType) const;
59 };
60
GetResId(std::string name,ResType resType) const61 int ResourceManagerPerformanceTest::GetResId(std::string name, ResType resType) const
62 {
63 auto idv = ((ResourceManagerImpl *)rm)->hapManager_->GetResourceListByName(name.c_str(), resType);
64 if (idv == nullptr) {
65 return -1;
66 }
67
68 if (idv->GetLimitPathsConst().size() > 0) {
69 return idv->GetLimitPathsConst()[0]->GetIdItem()->id_;
70 }
71 return OBJ_NOT_FOUND;
72 }
73
SetUpTestCase(void)74 void ResourceManagerPerformanceTest::SetUpTestCase(void)
75 {
76 // step 1: input testsuit setup step
77 }
78
TearDownTestCase(void)79 void ResourceManagerPerformanceTest::TearDownTestCase(void)
80 {
81 // step 2: input testsuit teardown step
82 }
83
SetUp(void)84 void ResourceManagerPerformanceTest::SetUp(void)
85 {
86 // PerformanceTest need higher log level
87 g_logLevel = LOG_INFO;
88 this->rm = CreateResourceManager();
89 if (rm == nullptr) {
90 return;
91 }
92 auto rc = CreateResConfig();
93 if (rc == nullptr) {
94 return;
95 }
96 rc->SetLocaleInfo("zh", nullptr, nullptr);
97 rm->UpdateResConfig(*rc);
98 delete rc;
99 bool ret = rm->AddResource(FormatFullPath(g_resFilePath).c_str());
100 if (!ret) {
101 HILOG_ERROR("AddResource failed. test would fail.");
102 }
103 }
104
TearDown(void)105 void ResourceManagerPerformanceTest::TearDown(void)
106 {
107 if (this->rm != nullptr) {
108 delete this->rm;
109 this->rm = nullptr;
110 }
111 }
112
113 // test HapResource::LoadFromIndex(), spilt to two parts: 1. read from file, 2. parse buf to HapResource
TestLoadFromIndex(const char * filePath)114 int TestLoadFromIndex(const char *filePath)
115 {
116 unsigned long long total = 0;
117 double average = 0;
118 std::string pstr = FormatFullPath(filePath);
119
120 auto start = std::chrono::high_resolution_clock::now();
121 std::ifstream inFile(pstr.c_str(), std::ios::binary | std::ios::in);
122 if (!inFile.good()) {
123 return -1;
124 }
125 inFile.seekg(0, std::ios::end);
126 size_t bufLen = inFile.tellg();
127 if (bufLen <= 0) {
128 HILOG_ERROR("file size is zero");
129 inFile.close();
130 return -1;
131 }
132 void *buf = malloc(bufLen);
133 if (buf == nullptr) {
134 HILOG_ERROR("Error allocating memory");
135 inFile.close();
136 return -1;
137 }
138 inFile.seekg(0, std::ios::beg);
139 inFile.read((char *)buf, bufLen);
140 inFile.close();
141
142 auto end = std::chrono::high_resolution_clock::now();
143 auto readFilecost = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
144
145 g_logLevel = LOG_DEBUG;
146 HILOG_DEBUG("read index file cost 001: %lld us", readFilecost);
147 g_logLevel = LOG_INFO;
148
149 for (int k = 0; k < 1000; ++k) {
150 auto t1 = std::chrono::high_resolution_clock::now();
151 ResDesc *resDesc = new(std::nothrow) ResDesc();
152 if (resDesc == nullptr) {
153 HILOG_ERROR("new ResDesc failed when LoadFromIndex");
154 free(buf);
155 return -1;
156 }
157 int32_t out = HapParser::ParseResHex((char *)buf, bufLen, *resDesc, nullptr);
158 if (out != OK) {
159 delete (resDesc);
160 free(buf);
161 HILOG_ERROR("ParseResHex failed! retcode:%d", out);
162 return -1;
163 } else {
164 HILOG_DEBUG("ParseResHex success:\n%s", resDesc->ToString().c_str());
165 }
166
167 HapResource *pResource = new(std::nothrow) HapResource(pstr, 0, nullptr, resDesc);
168 if (pResource == nullptr) {
169 HILOG_ERROR("new HapResource failed when LoadFromIndex");
170 delete (resDesc);
171 free(buf);
172 return -1;
173 }
174 if (!pResource->Init()) {
175 delete (pResource);
176 free(buf);
177 return -1;
178 }
179 auto t2 = std::chrono::high_resolution_clock::now();
180 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
181 delete pResource;
182 }
183 free(buf);
184 average = total / 1000.0;
185 g_logLevel = LOG_DEBUG;
186 HILOG_DEBUG("parse index avg cost 001: %f us", average);
187 EXPECT_LT(average, 4000);
188 return OK;
189 }
190
191 /*
192 * @tc.name: ResourceManagerPerformanceFuncTest001
193 * @tc.desc: Test AddResource
194 * @tc.type: FUNC
195 */
196 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest001, TestSize.Level1)
197 {
198 int ret = TestLoadFromIndex(g_resFilePath);
199 EXPECT_EQ(OK, ret);
200 };
201
202 /*
203 * @tc.name: ResourceManagerPerformanceFuncTest002
204 * @tc.desc: Test UpdateResConfig
205 * @tc.type: FUNC
206 */
207 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest002, TestSize.Level1)
208 {
209 unsigned long long total = 0;
210 double average = 0;
211 auto tmpRm = CreateResourceManager();
212 if (tmpRm == nullptr) {
213 EXPECT_TRUE(false);
214 return;
215 }
216 ResConfig *rc = CreateResConfig();
217 if (rc == nullptr) {
218 EXPECT_TRUE(false);
219 delete tmpRm;
220 return;
221 }
222 rc->SetLocaleInfo("en", nullptr, "US");
223 rc->SetDeviceType(DeviceType::DEVICE_CAR);
224 for (int k = 0; k < 1000; ++k) {
225 auto t1 = std::chrono::high_resolution_clock::now();
226 tmpRm->UpdateResConfig(*rc);
227 auto t2 = std::chrono::high_resolution_clock::now();
228 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
229 }
230 delete tmpRm;
231 delete rc;
232 average = total / 1000.0;
233 g_logLevel = LOG_DEBUG;
234 HILOG_DEBUG("avg cost 002: %f us", average);
235 EXPECT_LT(average, 100);
236 };
237
238 /*
239 * @tc.name: ResourceManagerPerformanceFuncTest003
240 * @tc.desc: Test GetResConfig
241 * @tc.type: FUNC
242 */
243 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest003, TestSize.Level1)
244 {
245 if (rm == nullptr) {
246 EXPECT_TRUE(false);
247 return;
248 }
249 unsigned long long total = 0;
250 double average = 0;
251 ResConfig *rc = CreateResConfig();
252 if (rc == nullptr) {
253 EXPECT_TRUE(false);
254 return;
255 }
256 rc->SetLocaleInfo("en", nullptr, "US");
257 rc->SetDeviceType(DeviceType::DEVICE_CAR);
258 ResConfigImpl rci;
259 for (int k = 0; k < 1000; ++k) {
260 auto t1 = std::chrono::high_resolution_clock::now();
261 rm->GetResConfig(rci);
262 auto t2 = std::chrono::high_resolution_clock::now();
263 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
264 }
265 delete rc;
266 average = total / 1000.0;
267 g_logLevel = LOG_DEBUG;
268 HILOG_DEBUG("avg cost 003: %f us", average);
269 EXPECT_LT(average, 100);
270 };
271
272 /*
273 * @tc.name: ResourceManagerPerformanceFuncTest004
274 * @tc.desc: Test GetStringByID
275 * @tc.type: FUNC
276 */
277 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest004, TestSize.Level1)
278 {
279 if (rm == nullptr) {
280 EXPECT_TRUE(false);
281 return;
282 }
283 unsigned long long total = 0;
284 double average = 0;
285 string name[] = {"app_name", "title"};
286 vector<uint32_t> ids;
287 int count = 2;
288 for (int i = 0; i < count; ++i) {
289 int id = GetResId(name[i], ResType::STRING);
290 ASSERT_TRUE(id > 0);
291 ids.push_back(static_cast<uint32_t>(id));
292 }
293
294 std::string outValue;
295 for (int k = 0; k < 1000; ++k) {
296 for (int i = 0; i < count; ++i) {
297 auto t1 = std::chrono::high_resolution_clock::now();
298 rm->GetStringById(ids[i], outValue);
299 auto t2 = std::chrono::high_resolution_clock::now();
300 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
301 }
302 }
303 average = total / (1000.0 * count);
304 g_logLevel = LOG_DEBUG;
305 HILOG_DEBUG("avg cost 004: %f us", average);
306 EXPECT_LT(average, 100);
307 };
308
309 /*
310 * @tc.name: ResourceManagerPerformanceFuncTest005
311 * @tc.desc: Test GetStringByName
312 * @tc.type: FUNC
313 */
314 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest005, TestSize.Level1)
315 {
316 if (rm == nullptr) {
317 EXPECT_TRUE(false);
318 return;
319 }
320 unsigned long long total = 0;
321 double average = 0;
322 string name[] = {"app_name", "title"};
323 int count = 2;
324 std::string outValue;
325 for (int k = 0; k < 1000; ++k) {
326 for (int i = 0; i < count; ++i) {
327 auto t1 = std::chrono::high_resolution_clock::now();
328 rm->GetStringByName(name[i].c_str(), outValue);
329 auto t2 = std::chrono::high_resolution_clock::now();
330 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
331 }
332 }
333 average = total / (1000.0 * count);
334 g_logLevel = LOG_DEBUG;
335 HILOG_DEBUG("avg cost 005: %f us", average);
336 EXPECT_LT(average, 100);
337 };
338
339 /*
340 * @tc.name: ResourceManagerPerformanceFuncTest006
341 * @tc.desc: Test GetStringByName
342 * @tc.type: FUNC
343 */
344 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest006, TestSize.Level1)
345 {
346 if (rm == nullptr) {
347 EXPECT_TRUE(false);
348 return;
349 }
350 unsigned long long total = 0;
351 double average = 0;
352 string name[] = {"string_ref", "string_ref2"};
353 int count = 2;
354 std::string outValue;
355 for (int k = 0; k < 1000; ++k) {
356 for (int i = 0; i < count; ++i) {
357 auto t1 = std::chrono::high_resolution_clock::now();
358 rm->GetStringByName(name[i].c_str(), outValue);
359 auto t2 = std::chrono::high_resolution_clock::now();
360 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
361 }
362 }
363 average = total / (1000.0 * count);
364 g_logLevel = LOG_DEBUG;
365 HILOG_DEBUG("avg cost 006: %f us", average);
366 EXPECT_LT(average, 100);
367 };
368
369 /*
370 * @tc.name: ResourceManagerPerformanceFuncTest007
371 * @tc.desc: Test GetStringFormatById
372 * @tc.type: FUNC
373 */
374 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest007, TestSize.Level1)
375 {
376 if (rm == nullptr) {
377 EXPECT_TRUE(false);
378 return;
379 }
380 unsigned long long total = 0;
381 double average = 0;
382 string name[] = {"app_name", "title"};
383 vector<uint32_t> ids;
384 int count = 2;
385 for (int i = 0; i < count; ++i) {
386 int id = GetResId(name[i], ResType::STRING);
387 ASSERT_TRUE(id > 0);
388 ids.push_back(static_cast<uint32_t>(id));
389 }
390
391 std::string outValue;
392 for (int k = 0; k < 1000; ++k) {
393 for (int i = 0; i < count; ++i) {
394 auto t1 = std::chrono::high_resolution_clock::now();
395 rm->GetStringFormatById(outValue, ids[i], 12);
396 auto t2 = std::chrono::high_resolution_clock::now();
397 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
398 }
399 }
400 average = total / (1000.0 * count);
401 g_logLevel = LOG_DEBUG;
402 HILOG_DEBUG("avg cost 007: %f us", average);
403 EXPECT_LT(average, 100);
404 };
405
406 /*
407 * @tc.name: ResourceManagerPerformanceFuncTest008
408 * @tc.desc: Test GetStringFormatByName
409 * @tc.type: FUNC
410 */
411 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest008, TestSize.Level1)
412 {
413 if (rm == nullptr) {
414 EXPECT_TRUE(false);
415 return;
416 }
417 unsigned long long total = 0;
418 double average = 0;
419 string name[] = {"app_name", "title"};
420 int count = 2;
421 std::string outValue;
422 for (int k = 0; k < 1000; ++k) {
423 for (int i = 0; i < count; ++i) {
424 auto t1 = std::chrono::high_resolution_clock::now();
425 rm->GetStringFormatByName(outValue, name[i].c_str(), 123);
426 auto t2 = std::chrono::high_resolution_clock::now();
427 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
428 }
429 }
430 average = total / (1000.0 * count);
431 g_logLevel = LOG_DEBUG;
432 HILOG_DEBUG("avg cost 008: %f us", average);
433 EXPECT_LT(average, 100);
434 };
435
436 /*
437 * @tc.name: ResourceManagerPerformanceFuncTest009
438 * @tc.desc: Test GetStringArrayById
439 * @tc.type: FUNC
440 */
441 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest009, TestSize.Level1)
442 {
443 if (rm == nullptr) {
444 EXPECT_TRUE(false);
445 return;
446 }
447 unsigned long long total = 0;
448 double average = 0;
449 int id = GetResId("size", ResType::STRINGARRAY);
450 ASSERT_TRUE(id > 0);
451
452 std::vector<std::string> outValue;
453 for (int k = 0; k < 1000; ++k) {
454 auto t1 = std::chrono::high_resolution_clock::now();
455 rm->GetStringArrayById(id, outValue);
456 auto t2 = std::chrono::high_resolution_clock::now();
457 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
458 }
459 average = total / 1000.0;
460 g_logLevel = LOG_DEBUG;
461 HILOG_DEBUG("avg cost 009: %f us", average);
462 EXPECT_LT(average, 100);
463 };
464
465 /*
466 * @tc.name: ResourceManagerPerformanceFuncTest010
467 * @tc.desc: Test GetStringArrayByName
468 * @tc.type: FUNC
469 */
470 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest010, TestSize.Level1)
471 {
472 if (rm == nullptr) {
473 EXPECT_TRUE(false);
474 return;
475 }
476 unsigned long long total = 0;
477 double average = 0;
478 std::vector<std::string> outValue;
479 for (int k = 0; k < 1000; ++k) {
480 auto t1 = std::chrono::high_resolution_clock::now();
481 rm->GetStringArrayByName("size", outValue);
482 auto t2 = std::chrono::high_resolution_clock::now();
483 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
484 }
485 average = total / 1000.0;
486 g_logLevel = LOG_DEBUG;
487 HILOG_DEBUG("avg cost 010: %f us", average);
488 EXPECT_LT(average, 100);
489 };
490
491 /*
492 * @tc.name: ResourceManagerPerformanceFuncTest011
493 * @tc.desc: Test GetPatternById
494 * @tc.type: FUNC
495 */
496 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest011, TestSize.Level1)
497 {
498 if (rm == nullptr) {
499 EXPECT_TRUE(false);
500 return;
501 }
502 unsigned long long total = 0;
503 double average = 0;
504 string name[] = {"base", "child"};
505 vector<uint32_t> ids;
506 int count = 2;
507 for (int i = 0; i < count; ++i) {
508 int id = GetResId(name[i], ResType::PATTERN);
509 ASSERT_TRUE(id > 0);
510 ids.push_back(static_cast<uint32_t>(id));
511 }
512 std::map<std::string, std::string> outValue;
513 for (int k = 0; k < 1000; ++k) {
514 for (int i = 0; i < count; ++i) {
515 auto t1 = std::chrono::high_resolution_clock::now();
516 rm->GetPatternById(ids[i], outValue);
517 auto t2 = std::chrono::high_resolution_clock::now();
518 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
519 }
520 }
521 average = total / (1000.0 * count);
522 g_logLevel = LOG_DEBUG;
523 HILOG_DEBUG("avg cost 011: %f us", average);
524 EXPECT_LT(average, 100);
525 };
526
527 /*
528 * @tc.name: ResourceManagerPerformanceFuncTest012
529 * @tc.desc: Test GetPatternByName
530 * @tc.type: FUNC
531 */
532 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest012, TestSize.Level1)
533 {
534 if (rm == nullptr) {
535 EXPECT_TRUE(false);
536 return;
537 }
538 unsigned long long total = 0;
539 double average = 0;
540 string name[] = {"base", "child"};
541 int count = 2;
542 std::map<std::string, std::string> outValue;
543 for (int k = 0; k < 1000; ++k) {
544 for (int i = 0; i < count; ++i) {
545 auto t1 = std::chrono::high_resolution_clock::now();
546 rm->GetPatternByName(name[i].c_str(), outValue);
547 auto t2 = std::chrono::high_resolution_clock::now();
548 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
549 }
550 }
551 average = total / (1000.0 * count);
552 g_logLevel = LOG_DEBUG;
553 HILOG_DEBUG("avg cost 012: %f us", average);
554 EXPECT_LT(average, 100);
555 };
556
557 /*
558 * @tc.name: ResourceManagerPerformanceFuncTest013
559 * @tc.desc: Test GetPluralStringById
560 * @tc.type: FUNC
561 */
562 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest013, TestSize.Level1)
563 {
564 if (rm == nullptr) {
565 EXPECT_TRUE(false);
566 return;
567 }
568 unsigned long long total = 0;
569 double average = 0;
570 int quantity[] = {1, 100};
571 int count = 2;
572 int id = GetResId("eat_apple", ResType::PLURALS);
573 ASSERT_TRUE(id > 0);
574
575 string outValue;
576 for (int k = 0; k < 1000; ++k) {
577 for (int i = 0; i < count; ++i) {
578 auto t1 = std::chrono::high_resolution_clock::now();
579 rm->GetPluralStringById(id, quantity[i], outValue);
580 auto t2 = std::chrono::high_resolution_clock::now();
581 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
582 }
583 }
584 average = total / (1000.0 * count);
585 g_logLevel = LOG_DEBUG;
586 HILOG_DEBUG("avg cost 013: %f us", average);
587 EXPECT_LT(average, 100);
588 };
589
590 /*
591 * @tc.name: ResourceManagerPerformanceFuncTest014
592 * @tc.desc: Test GetPluralStringByName
593 * @tc.type: FUNC
594 */
595 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest014, TestSize.Level1)
596 {
597 if (rm == nullptr) {
598 EXPECT_TRUE(false);
599 return;
600 }
601 unsigned long long total = 0;
602 double average = 0;
603 int quantity[] = {1, 100};
604 int count = 2;
605 string outValue;
606 for (int k = 0; k < 1000; ++k) {
607 for (int i = 0; i < count; ++i) {
608 auto t1 = std::chrono::high_resolution_clock::now();
609 rm->GetPluralStringByName("eat_apple", quantity[i], outValue);
610 auto t2 = std::chrono::high_resolution_clock::now();
611 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
612 }
613 }
614 average = total / (1000.0 * count);
615 g_logLevel = LOG_DEBUG;
616 HILOG_DEBUG("avg cost 014: %f us", average);
617 EXPECT_LT(average, 100);
618 };
619
620 /*
621 * @tc.name: ResourceManagerPerformanceFuncTest015
622 * @tc.desc: Test GetPluralStringByIdFormat
623 * @tc.type: FUNC
624 */
625 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest015, TestSize.Level1)
626 {
627 if (rm == nullptr) {
628 EXPECT_TRUE(false);
629 return;
630 }
631 unsigned long long total = 0;
632 double average = 0;
633 int quantity[] = {1, 100};
634 int count = 2;
635 int id = GetResId("eat_apple", ResType::PLURALS);
636 ASSERT_TRUE(id > 0);
637
638 string outValue;
639 for (int k = 0; k < 1000; ++k) {
640 for (int i = 0; i < count; ++i) {
641 auto t1 = std::chrono::high_resolution_clock::now();
642 rm->GetPluralStringByIdFormat(outValue, id, quantity[i], quantity[i]);
643 auto t2 = std::chrono::high_resolution_clock::now();
644 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
645 }
646 }
647 average = total / (1000.0 * count);
648 g_logLevel = LOG_DEBUG;
649 HILOG_DEBUG("avg cost 015: %f us", average);
650 EXPECT_LT(average, 100);
651 };
652
653 /*
654 * @tc.name: ResourceManagerPerformanceFuncTest016
655 * @tc.desc: Test GetPluralStringByNameFormat
656 * @tc.type: FUNC
657 */
658 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest016, TestSize.Level1)
659 {
660 if (rm == nullptr) {
661 EXPECT_TRUE(false);
662 return;
663 }
664 unsigned long long total = 0;
665 double average = 0;
666 int quantity[] = {1, 100};
667 int count = 2;
668 string outValue;
669 for (int k = 0; k < 1000; ++k) {
670 for (int i = 0; i < count; ++i) {
671 auto t1 = std::chrono::high_resolution_clock::now();
672 rm->GetPluralStringByNameFormat(outValue, "eat_apple", quantity[i], quantity[i]);
673 auto t2 = std::chrono::high_resolution_clock::now();
674 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
675 }
676 }
677 average = total / (1000.0 * count);
678 g_logLevel = LOG_DEBUG;
679 HILOG_DEBUG("avg cost 016: %f us", average);
680 EXPECT_LT(average, 100);
681 };
682
683 /*
684 * @tc.name: ResourceManagerPerformanceFuncTest017
685 * @tc.desc: Test GetThemeById
686 * @tc.type: FUNC
687 */
688 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest017, TestSize.Level1)
689 {
690 if (rm == nullptr) {
691 EXPECT_TRUE(false);
692 return;
693 }
694 unsigned long long total = 0;
695 double average = 0;
696 int id = GetResId("app_theme", ResType::THEME);
697 ASSERT_TRUE(id > 0);
698
699 std::map<std::string, std::string> outValue;
700 for (int k = 0; k < 1000; ++k) {
701 auto t1 = std::chrono::high_resolution_clock::now();
702 rm->GetThemeById(id, outValue);
703 auto t2 = std::chrono::high_resolution_clock::now();
704 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
705 }
706 average = total / 1000.0;
707 g_logLevel = LOG_DEBUG;
708 HILOG_DEBUG("avg cost 017: %f us", average);
709 EXPECT_LT(average, 100);
710 };
711
712 /*
713 * @tc.name: ResourceManagerPerformanceFuncTest018
714 * @tc.desc: Test GetThemeByName
715 * @tc.type: FUNC
716 */
717 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest018, TestSize.Level1)
718 {
719 if (rm == nullptr) {
720 EXPECT_TRUE(false);
721 return;
722 }
723 unsigned long long total = 0;
724 double average = 0;
725 std::map<std::string, std::string> outValue;
726 for (int k = 0; k < 1000; ++k) {
727 auto t1 = std::chrono::high_resolution_clock::now();
728 rm->GetThemeByName("app_theme", outValue);
729 auto t2 = std::chrono::high_resolution_clock::now();
730 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
731 }
732 average = total / 1000.0;
733 g_logLevel = LOG_DEBUG;
734 HILOG_DEBUG("avg cost 018: %f us", average);
735 EXPECT_LT(average, 100);
736 };
737
738 /*
739 * @tc.name: ResourceManagerPerformanceFuncTest019
740 * @tc.desc: Test GetBooleanById
741 * @tc.type: FUNC
742 */
743 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest019, TestSize.Level1)
744 {
745 if (rm == nullptr) {
746 EXPECT_TRUE(false);
747 return;
748 }
749 unsigned long long total = 0;
750 double average = 0;
751 int id = GetResId("boolean_1", ResType::BOOLEAN);
752 ASSERT_TRUE(id > 0);
753
754 bool outValue = true;
755 for (int k = 0; k < 1000; ++k) {
756 auto t1 = std::chrono::high_resolution_clock::now();
757 rm->GetBooleanById(id, outValue);
758 auto t2 = std::chrono::high_resolution_clock::now();
759 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
760 }
761 average = total / 1000.0;
762 g_logLevel = LOG_DEBUG;
763 HILOG_DEBUG("avg cost 019: %f us", average);
764 EXPECT_LT(average, 100);
765 };
766
767 /*
768 * @tc.name: ResourceManagerPerformanceFuncTest020
769 * @tc.desc: Test GetBooleanByName
770 * @tc.type: FUNC
771 */
772 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest020, TestSize.Level1)
773 {
774 if (rm == nullptr) {
775 EXPECT_TRUE(false);
776 return;
777 }
778 unsigned long long total = 0;
779 double average = 0;
780 bool outValue = true;
781 for (int k = 0; k < 1000; ++k) {
782 auto t1 = std::chrono::high_resolution_clock::now();
783 rm->GetBooleanByName("boolean_1", outValue);
784 auto t2 = std::chrono::high_resolution_clock::now();
785 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
786 }
787 average = total / 1000.0;
788 g_logLevel = LOG_DEBUG;
789 HILOG_DEBUG("avg cost 020: %f us", average);
790 EXPECT_LT(average, 100);
791 };
792
793 /*
794 * @tc.name: ResourceManagerPerformanceFuncTest021
795 * @tc.desc: Test GetIntegerById
796 * @tc.type: FUNC
797 */
798 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest021, TestSize.Level1)
799 {
800 if (rm == nullptr) {
801 EXPECT_TRUE(false);
802 return;
803 }
804 unsigned long long total = 0;
805 double average = 0;
806 int id = GetResId("integer_1", ResType::INTEGER);
807 ASSERT_TRUE(id > 0);
808
809 int outValue = 0;
810 for (int k = 0; k < 1000; ++k) {
811 auto t1 = std::chrono::high_resolution_clock::now();
812 rm->GetIntegerById(id, outValue);
813 auto t2 = std::chrono::high_resolution_clock::now();
814 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
815 }
816 average = total / 1000.0;
817 g_logLevel = LOG_DEBUG;
818 HILOG_DEBUG("avg cost 021: %f us", average);
819 EXPECT_LT(average, 100);
820 };
821
822 /*
823 * @tc.name: ResourceManagerPerformanceFuncTest022
824 * @tc.desc: Test GetIntegerByName
825 * @tc.type: FUNC
826 */
827 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest022, TestSize.Level1)
828 {
829 if (rm == nullptr) {
830 EXPECT_TRUE(false);
831 return;
832 }
833 unsigned long long total = 0;
834 double average = 0;
835 int outValue = 0;
836 for (int k = 0; k < 1000; ++k) {
837 auto t1 = std::chrono::high_resolution_clock::now();
838 rm->GetIntegerByName("integer_ref", outValue);
839 auto t2 = std::chrono::high_resolution_clock::now();
840 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
841 }
842 average = total / 1000.0;
843 g_logLevel = LOG_DEBUG;
844 HILOG_DEBUG("avg cost 022: %f us", average);
845 EXPECT_LT(average, 100);
846 };
847
848 /*
849 * @tc.name: ResourceManagerPerformanceFuncTest023
850 * @tc.desc: Test GetFloatById
851 * @tc.type: FUNC
852 */
853 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest023, TestSize.Level1)
854 {
855 if (rm == nullptr) {
856 EXPECT_TRUE(false);
857 return;
858 }
859 unsigned long long total = 0;
860 double average = 0;
861 int id = GetResId("width_appBar_backButton_touchTarget", ResType::FLOAT);
862 ASSERT_TRUE(id > 0);
863
864 float outValue = 0.0;
865 for (int k = 0; k < 1000; ++k) {
866 auto t1 = std::chrono::high_resolution_clock::now();
867 rm->GetFloatById(id, outValue);
868 auto t2 = std::chrono::high_resolution_clock::now();
869 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
870 }
871 average = total / 1000.0;
872 g_logLevel = LOG_DEBUG;
873 HILOG_DEBUG("avg cost 023: %f us", average);
874 EXPECT_LT(average, 100);
875 };
876
877 /*
878 * @tc.name: ResourceManagerPerformanceFuncTest024
879 * @tc.desc: Test GetFloatByName
880 * @tc.type: FUNC
881 */
882 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest024, TestSize.Level1)
883 {
884 if (rm == nullptr) {
885 EXPECT_TRUE(false);
886 return;
887 }
888 unsigned long long total = 0;
889 double average = 0;
890 float outValue = 0;
891 for (int k = 0; k < 1000; ++k) {
892 auto t1 = std::chrono::high_resolution_clock::now();
893 rm->GetFloatByName("width_appBar_backButton_touchTarget", outValue);
894 auto t2 = std::chrono::high_resolution_clock::now();
895 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
896 }
897 average = total / 1000.0;
898 g_logLevel = LOG_DEBUG;
899 HILOG_DEBUG("avg cost 024: %f us", average);
900 EXPECT_LT(average, 100);
901 };
902
903 /*
904 * @tc.name: ResourceManagerPerformanceFuncTest025
905 * @tc.desc: Test GetIntArrayById
906 * @tc.type: FUNC
907 */
908 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest025, TestSize.Level1)
909 {
910 if (rm == nullptr) {
911 EXPECT_TRUE(false);
912 return;
913 }
914 unsigned long long total = 0;
915 double average = 0;
916 int id = GetResId("intarray_1", ResType::INTARRAY);
917 ASSERT_TRUE(id > 0);
918
919 std::vector<int> outValue;
920 for (int k = 0; k < 1000; ++k) {
921 auto t1 = std::chrono::high_resolution_clock::now();
922 rm->GetIntArrayById(id, outValue);
923 auto t2 = std::chrono::high_resolution_clock::now();
924 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
925 }
926 average = total / 1000.0;
927 g_logLevel = LOG_DEBUG;
928 HILOG_DEBUG("avg cost 025: %f us", average);
929 EXPECT_LT(average, 100);
930 };
931
932 /*
933 * @tc.name: ResourceManagerPerformanceFuncTest026
934 * @tc.desc: Test GetIntArrayByName
935 * @tc.type: FUNC
936 */
937 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest026, TestSize.Level1)
938 {
939 if (rm == nullptr) {
940 EXPECT_TRUE(false);
941 return;
942 }
943 unsigned long long total = 0;
944 double average = 0;
945 std::vector<int> outValue;
946 for (int k = 0; k < 1000; ++k) {
947 auto t1 = std::chrono::high_resolution_clock::now();
948 rm->GetIntArrayByName("intarray_1", outValue);
949 auto t2 = std::chrono::high_resolution_clock::now();
950 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
951 }
952 average = total / 1000.0;
953 g_logLevel = LOG_DEBUG;
954 HILOG_DEBUG("avg cost 026: %f us", average);
955 EXPECT_LT(average, 100);
956 };
957
958 /*
959 * @tc.name: ResourceManagerPerformanceFuncTest027
960 * @tc.desc: Test GetColorById
961 * @tc.type: FUNC
962 */
963 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest027, TestSize.Level1)
964 {
965 if (rm == nullptr) {
966 EXPECT_TRUE(false);
967 return;
968 }
969 unsigned long long total = 0;
970 double average = 0;
971 int id = GetResId("divider_color", ResType::COLOR);
972 ASSERT_TRUE(id > 0);
973
974 uint32_t outValue;
975 for (int k = 0; k < 1000; ++k) {
976 auto t1 = std::chrono::high_resolution_clock::now();
977 rm->GetColorById(id, outValue);
978 auto t2 = std::chrono::high_resolution_clock::now();
979 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
980 }
981 average = total / 1000.0;
982 g_logLevel = LOG_DEBUG;
983 HILOG_DEBUG("avg cost 027: %f us", average);
984 EXPECT_LT(average, 100);
985 };
986
987 /*
988 * @tc.name: ResourceManagerPerformanceFuncTest028
989 * @tc.desc: Test GetColorByName
990 * @tc.type: FUNC
991 */
992 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest028, TestSize.Level1)
993 {
994 if (rm == nullptr) {
995 EXPECT_TRUE(false);
996 return;
997 }
998 unsigned long long total = 0;
999 double average = 0;
1000 uint32_t outValue;
1001 for (int k = 0; k < 1000; ++k) {
1002 auto t1 = std::chrono::high_resolution_clock::now();
1003 rm->GetColorByName("divider_color", outValue);
1004 auto t2 = std::chrono::high_resolution_clock::now();
1005 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
1006 }
1007 average = total / 1000.0;
1008 g_logLevel = LOG_DEBUG;
1009 HILOG_DEBUG("avg cost 028: %f us", average);
1010 EXPECT_LT(average, 100);
1011 };
1012
1013 /*
1014 * @tc.name: ResourceManagerPerformanceFuncTest029
1015 * @tc.desc: Test GetProfileById
1016 * @tc.type: FUNC
1017 */
1018 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest029, TestSize.Level1)
1019 {
1020 if (rm == nullptr) {
1021 EXPECT_TRUE(false);
1022 return;
1023 }
1024 unsigned long long total = 0;
1025 double average = 0;
1026 int id = GetResId("test_common", ResType::PROF);
1027 ASSERT_TRUE(id > 0);
1028
1029 string outValue;
1030 for (int k = 0; k < 1000; ++k) {
1031 auto t1 = std::chrono::high_resolution_clock::now();
1032 rm->GetProfileById(id, outValue);
1033 auto t2 = std::chrono::high_resolution_clock::now();
1034 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
1035 }
1036 average = total / 1000.0;
1037 g_logLevel = LOG_DEBUG;
1038 HILOG_DEBUG("avg cost 029: %f us", average);
1039 EXPECT_LT(average, 100);
1040 };
1041
1042 /*
1043 * @tc.name: ResourceManagerPerformanceFuncTest030
1044 * @tc.desc: Test GetProfileByName
1045 * @tc.type: FUNC
1046 */
1047 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest030, TestSize.Level1)
1048 {
1049 if (rm == nullptr) {
1050 EXPECT_TRUE(false);
1051 return;
1052 }
1053 unsigned long long total = 0;
1054 double average = 0;
1055 string outValue;
1056 for (int k = 0; k < 1000; ++k) {
1057 auto t1 = std::chrono::high_resolution_clock::now();
1058 rm->GetProfileByName("test_common", outValue);
1059 auto t2 = std::chrono::high_resolution_clock::now();
1060 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
1061 }
1062 average = total / 1000.0;
1063 g_logLevel = LOG_DEBUG;
1064 HILOG_DEBUG("avg cost 030: %f us", average);
1065 EXPECT_LT(average, 100);
1066 };
1067
1068 /*
1069 * @tc.name: ResourceManagerPerformanceFuncTest031
1070 * @tc.desc: Test GetMediaById
1071 * @tc.type: FUNC
1072 */
1073 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest031, TestSize.Level1)
1074 {
1075 if (rm == nullptr) {
1076 EXPECT_TRUE(false);
1077 return;
1078 }
1079 unsigned long long total = 0;
1080 double average = 0;
1081 int id = GetResId("icon", ResType::MEDIA);
1082 ASSERT_TRUE(id > 0);
1083
1084 string outValue;
1085 for (int k = 0; k < 1000; ++k) {
1086 auto t1 = std::chrono::high_resolution_clock::now();
1087 rm->GetMediaById(id, outValue);
1088 auto t2 = std::chrono::high_resolution_clock::now();
1089 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
1090 }
1091 average = total / 1000.0;
1092 g_logLevel = LOG_DEBUG;
1093 HILOG_DEBUG("avg cost 031: %f us", average);
1094 EXPECT_LT(average, 100);
1095 };
1096
1097 /*
1098 * @tc.name: ResourceManagerPerformanceFuncTest032
1099 * @tc.desc: Test GetMediaByName
1100 * @tc.type: FUNC
1101 */
1102 HWTEST_F(ResourceManagerPerformanceTest, ResourceManagerPerformanceFuncTest032, TestSize.Level1)
1103 {
1104 if (rm == nullptr) {
1105 EXPECT_TRUE(false);
1106 return;
1107 }
1108 unsigned long long total = 0;
1109 double average = 0;
1110 string outValue;
1111 for (int k = 0; k < 1000; ++k) {
1112 auto t1 = std::chrono::high_resolution_clock::now();
1113 rm->GetMediaByName("icon", outValue);
1114 auto t2 = std::chrono::high_resolution_clock::now();
1115 total += std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
1116 }
1117 average = total / 1000.0;
1118 g_logLevel = LOG_DEBUG;
1119 HILOG_DEBUG("avg cost 032: %f us", average);
1120 EXPECT_LT(average, 100);
1121 };
1122 }
1123