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