• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "report_json_file_test.h"
17 
18 #include <bitset>
19 #include <memory>
20 
21 using namespace testing::ext;
22 using namespace std;
23 using namespace OHOS::HiviewDFX;
24 using namespace ::testing;
25 namespace OHOS {
26 namespace Developtools {
27 namespace HiPerf {
28 class ReportJsonFileTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34     std::unique_ptr<ReportJsonFile> PrepairReportJson(const VirtualRuntime &virtualRuntime) const;
35 };
36 
SetUpTestCase()37 void ReportJsonFileTest::SetUpTestCase() {}
38 
TearDownTestCase()39 void ReportJsonFileTest::TearDownTestCase() {}
40 
SetUp()41 void ReportJsonFileTest::SetUp() {}
42 
TearDown()43 void ReportJsonFileTest::TearDown() {}
44 
45 /*
46 this func give a report json object which have
47 1 conifg with name "eventName" and ids is  1,2,3
48     1 process : pid 1
49     2 thread : tid is 3 and 4
50     3 lib : liba , libb and libc
51     5 func :
52         funca1 in liba
53         funca2 in liba
54         funcb1 in libb
55         funcc1 in libc
56 
57     2 callstack
58         in thread id 3:
59             funca1 - liba count 10
60             funca2 - liba count 0
61         in thread id 4:
62             funcc1 - libc count 20
63             funcb1 - libb count 0
64 */
PrepairReportJson(const VirtualRuntime & virtualRuntime) const65 std::unique_ptr<ReportJsonFile> ReportJsonFileTest::PrepairReportJson(
66     const VirtualRuntime &virtualRuntime) const
67 {
68     std::unique_ptr<ReportJsonFile> json =
69         std::make_unique<ReportJsonFile>(nullptr, virtualRuntime);
70     std::vector<DfxFrame> frames = {
71         {0x1u, 0x1u, "liba", "funca1"},
72         {0x2u, 0x1u, "liba", "funca2"},
73         {0x3u, 0x1u, "libb", "funcb1"},
74     };
75     std::vector<DfxFrame> frames2 = {
76         {0x1u, 0x1u, "libc", "funcc1"},
77         {0x2u, 0x1u, "libb", "funcb1"},
78     };
79     std::vector<uint64_t> ids = {1, 2, 3};
80     json->reportConfigItems_.emplace(
81         ids, ReportConfigItem(json->reportConfigItems_.size(), "eventName"));
82     json->libList_ = {"liba", "libb", "libc"};
83     json->AddNewFunction(0, "funca1");
84     json->AddNewFunction(0, "funca2");
85     json->AddNewFunction(1, "funcb1");
86     json->AddNewFunction(2, "funcc1"); // 2: num two
87     // id , pid , tid , event count
88     uint64_t id = 1;
89     uint64_t eventCount = 10;
90     uint64_t eventCount2 = 20;
91     int pid = 2;
92     int tid = 3;
93     int tid2 = 4;
94     json->UpdateReportCallStack(id, pid, tid, eventCount, frames);
95     json->UpdateReportCallStack(id, pid, tid2, eventCount2, frames2);
96 
97     return json;
98 }
99 
100 /**
101  * @tc.name: OutputJsonKey
102  * @tc.desc:
103  * @tc.type: FUNC
104  */
105 HWTEST_F(ReportJsonFileTest, OutputJsonKey, TestSize.Level1)
106 {
107     StdoutRecord output;
108 
109     // no name
110     output.Start();
111     OutputJsonKey(stdout, std::string());
112     EXPECT_STREQ(output.Stop().c_str(), "");
113 
114     output.Start();
115     OutputJsonKey(stdout, "");
116     EXPECT_STREQ(output.Stop().c_str(), "");
117 
118     // have name
119     output.Start();
120     OutputJsonKey(stdout, "keyname");
121     EXPECT_STREQ(output.Stop().c_str(), "\"keyname\":");
122 
123     output.Start();
124     OutputJsonKey(stdout, static_cast<int>(1));
125     EXPECT_STREQ(output.Stop().c_str(), "\"1\":");
126 
127     output.Start();
128     OutputJsonKey(stdout, static_cast<long>(1));
129     EXPECT_STREQ(output.Stop().c_str(), "\"1\":");
130 
131     output.Start();
132     OutputJsonKey(stdout, static_cast<size_t>(2));
133     EXPECT_STREQ(output.Stop().c_str(), "\"2\":");
134 
135     output.Start();
136 
137     OutputJsonKey(stdout, std::string("keyname"));
138     EXPECT_STREQ(output.Stop().c_str(), "\"keyname\":");
139 }
140 
141 /**
142  * @tc.name: OutputJsonValue
143  * @tc.desc:
144  * @tc.type: FUNC
145  */
146 HWTEST_F(ReportJsonFileTest, OutputJsonValue, TestSize.Level1)
147 {
148     StdoutRecord output;
149 
150     output.Start();
151     OutputJsonValue(stdout, std::string("value"));
152     EXPECT_STREQ(output.Stop().c_str(), "\"value\"");
153 
154     output.Start();
155     OutputJsonValue(stdout, int(1));
156     EXPECT_STREQ(output.Stop().c_str(), "1");
157 
158     output.Start();
159     OutputJsonValue(stdout, uint64_t(1));
160     EXPECT_STREQ(output.Stop().c_str(), "1");
161 
162     output.Start();
163     OutputJsonValue(stdout, bool(true));
164     EXPECT_STREQ(output.Stop().c_str(), "1");
165 
166     output.Start();
167     OutputJsonValue(stdout, size_t(1));
168     EXPECT_STREQ(output.Stop().c_str(), "1");
169 
170     output.Start();
171     OutputJsonValue(stdout, "value");
172     EXPECT_STREQ(output.Stop().c_str(), "\"value\"");
173 
174     output.Start();
175     OutputJsonValue(stdout, "value", false);
176     EXPECT_STREQ(output.Stop().c_str(), ",\"value\"");
177 }
178 
179 /**
180  * @tc.name: OutputJsonPair
181  * @tc.desc:
182  * @tc.type: FUNC
183  */
184 HWTEST_F(ReportJsonFileTest, OutputJsonPair, TestSize.Level1)
185 {
186     StdoutRecord output;
187     output.Start();
188     OutputJsonValue(stdout, std::string("value"));
189     EXPECT_STREQ(output.Stop().c_str(), "\"value\"");
190 }
191 
192 /**
193  * @tc.name: OutputJsonVectorList
194  * @tc.desc:
195  * @tc.type: FUNC
196  */
197 HWTEST_F(ReportJsonFileTest, OutputJsonVectorList, TestSize.Level1)
198 {
199     StdoutRecord output;
200 
201     output.Start();
202     OutputJsonVectorList<int>(stdout, "listname", {1, 2, 3}, true);
203     EXPECT_STREQ(output.Stop().c_str(), "\"listname\":[1,2,3]");
204 
205     output.Start();
206     OutputJsonVectorList<std::string>(stdout, "listname", {"1", "2", "3"}, true);
207     EXPECT_STREQ(output.Stop().c_str(), "\"listname\":[\"1\",\"2\",\"3\"]");
208 }
209 
210 /**
211  * @tc.name: OutputJsonMapList
212  * @tc.desc:
213  * @tc.type: FUNC
214  */
215 HWTEST_F(ReportJsonFileTest, OutputJsonMapList, TestSize.Level1)
216 {
217     StdoutRecord output;
218     std::map<int, int> map = {
219         {1, 2},
220         {3, 4},
221         {5, 6},
222     };
223 
224     output.Start();
225     OutputJsonMapList(stdout, "map", map, true);
226     EXPECT_STREQ(output.Stop().c_str(), "\"map\":[2,4,6]");
227 
228     std::map<std::string, std::string> map2 = {
229         {"1", "2"},
230         {"3", "4"},
231         {"5", "6"},
232     };
233 
234     output.Start();
235     OutputJsonMapList(stdout, "map2", map2, true);
236     EXPECT_STREQ(output.Stop().c_str(), "\"map2\":[\"2\",\"4\",\"6\"]");
237 }
238 
239 /**
240  * @tc.name: OutputJsonMap
241  * @tc.desc:
242  * @tc.type: FUNC
243  */
244 HWTEST_F(ReportJsonFileTest, OutputJsonMap, TestSize.Level1)
245 {
246     StdoutRecord output;
247     std::map<int, int> map = {
248         {1, 2},
249         {3, 4},
250         {5, 6},
251     };
252 
253     output.Start();
254     OutputJsonMap(stdout, "map", map, true);
255     EXPECT_STREQ(output.Stop().c_str(), "\"map\":{\"1\":2,\"3\":4,\"5\":6}");
256 
257     std::map<std::string, std::string> map2 = {
258         {"1", "2"},
259         {"3", "4"},
260         {"5", "6"},
261     };
262 
263     output.Start();
264     OutputJsonMap(stdout, "map2", map2, true);
265     EXPECT_STREQ(output.Stop().c_str(), "\"map2\":{\"1\":\"2\",\"3\":\"4\",\"5\":\"6\"}");
266 }
267 
268 /**
269  * @tc.name: GetOrCreateMapItem
270  * @tc.desc:
271  * @tc.type: FUNC
272  */
273 HWTEST_F(ReportJsonFileTest, GetOrCreateMapItem, TestSize.Level1)
274 {
275     std::map<int, int> map = {
276         {1, 2},
277         {3, 4},
278         {5, 6},
279     };
280 
281     EXPECT_EQ(GetOrCreateMapItem(map, 1), 2);
282     EXPECT_EQ(GetOrCreateMapItem(map, 2), 2);
283     EXPECT_EQ(GetOrCreateMapItem(map, 3), 4);
284     EXPECT_EQ(GetOrCreateMapItem(map, 4), 4);
285 }
286 
287 /**
288  * @tc.name: ReportFuncItem
289  * @tc.desc:
290  * @tc.type: FUNC
291  */
292 HWTEST_F(ReportJsonFileTest, ReportFuncItem, TestSize.Level1)
293 {
294     StdoutRecord output;
295     ReportFuncItem func(1);
296     func.sampleCount_ = 2;
297     func.eventCount_ = 3;
298     func.subTreeEventCount_ = 4;
299 
300     output.Start();
301     func.OutputJson(stdout);
302 
303     EXPECT_STREQ(output.Stop().c_str(), "{\"symbol\":1,\"counts\":[2,3,4]}");
304 }
305 
306 /**
307  * @tc.name: ReportCallNodeItem
308  * @tc.desc:
309  * @tc.type: FUNC
310  */
311 HWTEST_F(ReportJsonFileTest, ReportCallNodeItem, TestSize.Level1)
312 {
313     StdoutRecord output;
314     /*
315     2 / 12
316         4 / 10
317             6 / 6
318     */
319 
320     ReportCallNodeItem callnode(1);
321     callnode.selfEventCount_ = 2;
322     callnode.subTreeEventCount_ = 12;
323 
324     output.Start();
325     callnode.OutputJson(stdout);
326 
327     EXPECT_STREQ(output.Stop().c_str(),
328                  "{\"selfEvents\":2,\"subEvents\":12,\"symbol\":1,\"callStack\":[]}");
329 
330     ReportCallNodeItem &callnode2 = GetOrCreateMapItem(callnode.childrenMap, 2);
331     callnode2.selfEventCount_ = 4;
332     callnode2.subTreeEventCount_ = 10;
333     output.Start();
334     callnode.OutputJson(stdout);
335     EXPECT_STREQ(output.Stop().c_str(),
336                  "{\"selfEvents\":2,\"subEvents\":12,\"symbol\":1,\"callStack\":[{\"selfEvents\":4,"
337                  "\"subEvents\":10,\"symbol\":2,\"callStack\":[]}]}");
338 
339     ReportCallNodeItem &callnode3 = GetOrCreateMapItem(callnode2.childrenMap, 3);
340     callnode3.selfEventCount_ = 6;
341     callnode3.subTreeEventCount_ = 6;
342     output.Start();
343     callnode.OutputJson(stdout);
344 
345     EXPECT_STREQ(output.Stop().c_str(),
346                  "{\"selfEvents\":2,\"subEvents\":12,\"symbol\":1,\"callStack\":[{\"selfEvents\":4,"
347                  "\"subEvents\":10,\"symbol\":2,\"callStack\":[{\"selfEvents\":6,\"subEvents\":6,"
348                  "\"symbol\":3,\"callStack\":[]}]}]}");
349 }
350 
351 /**
352  * @tc.name: ReportCallNodeItem
353  * @tc.desc:
354  * @tc.type: FUNC
355  */
356 HWTEST_F(ReportJsonFileTest, UpdateChildrenEventCount, TestSize.Level1)
357 {
358     StdoutRecord output;
359     /*
360     2 / 12
361         4 / 10
362             6 / 6
363     */
364 
365     ReportCallNodeItem callnode(1);
366     callnode.selfEventCount_ = 2;
367     callnode.subTreeEventCount_ = 0;
368 
369     ReportCallNodeItem &callnode2 = GetOrCreateMapItem(callnode.childrenMap, 2);
370     callnode2.selfEventCount_ = 4;
371     callnode2.subTreeEventCount_ = 0;
372 
373     ReportCallNodeItem &callnode3 = GetOrCreateMapItem(callnode2.childrenMap, 3);
374     callnode3.selfEventCount_ = 6;
375     callnode3.subTreeEventCount_ = 0;
376 
377     output.Start();
378     callnode.UpdateChildrenEventCount();
379     callnode.OutputJson(stdout);
380 
381     EXPECT_STREQ(output.Stop().c_str(),
382                  "{\"selfEvents\":2,\"subEvents\":12,\"symbol\":1,\"callStack\":[{\"selfEvents\":4,"
383                  "\"subEvents\":10,\"symbol\":2,\"callStack\":[{\"selfEvents\":6,\"subEvents\":6,"
384                  "\"symbol\":3,\"callStack\":[]}]}]}");
385 }
386 
387 /**
388  * @tc.name: ReportLibItem
389  * @tc.desc:
390  * @tc.type: FUNC
391  */
392 HWTEST_F(ReportJsonFileTest, ReportLibItem, TestSize.Level1)
393 {
394     StdoutRecord output;
395     ReportLibItem lib;
396     lib.libId_ = 1;
397     lib.eventCount_ = 2;
398 
399     ReportFuncItem &func = GetOrCreateMapItem(lib.funcs_, 1);
400     func.sampleCount_ = 2;
401     func.eventCount_ = 3;
402     func.subTreeEventCount_ = 4;
403 
404     output.Start();
405     lib.OutputJson(stdout);
406 
407     EXPECT_STREQ(
408         output.Stop().c_str(),
409         "{\"fileId\":1,\"eventCount\":2,\"functions\":[{\"symbol\":1,\"counts\":[2,3,4]}]}");
410 }
411 
412 /**
413  * @tc.name: ReportThreadItem
414  * @tc.desc:
415  * @tc.type: FUNC
416  */
417 HWTEST_F(ReportJsonFileTest, ReportThreadItem, TestSize.Level1)
418 {
419     StdoutRecord output;
420     ReportThreadItem thread(1);
421     thread.tid_ = 2;
422     thread.eventCount_ = 3;
423     thread.sampleCount_ = 4;
424 
425     output.Start();
426     thread.OutputJson(stdout);
427 
428     EXPECT_STREQ(output.Stop().c_str(),
429                  "{\"tid\":2,\"eventCount\":3,\"sampleCount\":4,\"libs\":[],\"CallOrder\":{"
430                  "\"selfEvents\":0,\"subEvents\":0,\"symbol\":-1,\"callStack\":[]},\"CalledOrder\":"
431                  "{\"selfEvents\":0,\"subEvents\":0,\"symbol\":-1,\"callStack\":[]}}");
432 }
433 
434 /**
435  * @tc.name: ReportProcessItem
436  * @tc.desc:
437  * @tc.type: FUNC
438  */
439 HWTEST_F(ReportJsonFileTest, ReportProcessItem, TestSize.Level1)
440 {
441     StdoutRecord output;
442     ReportProcessItem process(1);
443     process.pid_ = 2;
444     process.eventCount_ = 3;
445 
446     output.Start();
447     process.OutputJson(stdout);
448 
449     EXPECT_STREQ(output.Stop().c_str(), "{\"pid\":2,\"eventCount\":3,\"threads\":[]}");
450 }
451 
452 /**
453  * @tc.name: ReportConfigItem
454  * @tc.desc:
455  * @tc.type: FUNC
456  */
457 HWTEST_F(ReportJsonFileTest, ReportConfigItem, TestSize.Level1)
458 {
459     StdoutRecord output;
460     ReportConfigItem config(1, "configname");
461     config.eventCount_ = 3;
462 
463     output.Start();
464     config.OutputJson(stdout);
465 
466     EXPECT_STREQ(output.Stop().c_str(),
467                  "{\"eventConfigName\":\"configname\",\"eventCount\":3,\"processes\":[]}");
468 }
469 
470 /**
471  * @tc.name: UpdateReportSample
472  * @tc.desc:
473  * @tc.type: FUNC
474  */
475 HWTEST_F(ReportJsonFileTest, UpdateReportSample, TestSize.Level1)
476 {
477     StdoutRecord output;
478     VirtualRuntime virtualRuntime;
479     ReportJsonFile json(nullptr, virtualRuntime);
480     std::vector<uint64_t> ids = {1, 2, 3};
481     json.reportConfigItems_.emplace(ids, ReportConfigItem(1, "eventName"));
482     // id , pid , tid , event count
483     json.UpdateReportSample(1, 2, 3, 4);
484     auto configIt = json.reportConfigItems_.begin();
485 
486     EXPECT_EQ(configIt->second.eventCount_, 4u);
487     ASSERT_EQ(configIt->second.processes_.size(), 1u);
488     EXPECT_EQ(configIt->second.processes_.at(2).pid_, 2);
489     EXPECT_EQ(configIt->second.processes_.at(2).eventCount_, 4u);
490     ASSERT_EQ(configIt->second.processes_.at(2).threads_.size(), 1u);
491     EXPECT_EQ(configIt->second.processes_.at(2).threads_.at(3).tid_, 3);
492     EXPECT_EQ(configIt->second.processes_.at(2).threads_.at(3).eventCount_, 4u);
493     EXPECT_EQ(configIt->second.processes_.at(2).threads_.at(3).sampleCount_, 1u);
494     EXPECT_EQ(json.sampleCount_, 1u);
495 }
496 
497 /**
498  * @tc.name: UpdateReportCallStack
499  * @tc.desc:
500  * @tc.type: FUNC
501  */
502 HWTEST_F(ReportJsonFileTest, UpdateReportCallStack, TestSize.Level1)
503 {
504     StdoutRecord output;
505     VirtualRuntime virtualRuntime;
506     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
507 
508     // check
509     auto configIt = json->reportConfigItems_.begin();
510 
511     // this should not update here
512     EXPECT_EQ(configIt->second.eventCount_, 0u);
513     ASSERT_EQ(configIt->second.processes_.size(), 1u);
514     ASSERT_EQ(configIt->second.processes_.begin()->second.threads_.size(), 2u);
515     ReportProcessItem &process = configIt->second.processes_.begin()->second;
516     ReportThreadItem &thread = process.threads_.begin()->second;
517     // only liba and libb
518     ASSERT_EQ(thread.libs_.size(), 2u);
519     ReportLibItem &lib = thread.libs_.begin()->second;
520     // we have to function , a1 and a2 in liba
521     ASSERT_EQ(lib.libId_, 0);
522     ASSERT_EQ(lib.funcs_.size(), 2u);
523 
524     // chec func count
525     ReportFuncItem &funca1 = lib.funcs_.at(0);
526     ReportFuncItem &funca2 = lib.funcs_.at(1);
527 
528     // only fist one have count
529     ASSERT_EQ(funca1.eventCount_, 10u);
530     ASSERT_EQ(funca1.subTreeEventCount_, 10u);
531     ASSERT_EQ(funca2.eventCount_, 0u);
532     ASSERT_EQ(funca2.subTreeEventCount_, 10u);
533 }
534 
535 /**
536  * @tc.name: UpdateCallNodeEventCount
537  * @tc.desc:
538  * @tc.type: FUNC
539  */
540 HWTEST_F(ReportJsonFileTest, UpdateCallNodeEventCount, TestSize.Level1)
541 {
542     StdoutRecord output;
543     VirtualRuntime virtualRuntime;
544     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
545 
546     auto configIt = json->reportConfigItems_.begin();
547     ReportProcessItem &process = configIt->second.processes_.begin()->second;
548     ReportThreadItem &thread = process.threads_.begin()->second;
549     ReportLibItem &lib = thread.libs_.begin()->second;
550 
551     ReportFuncItem &funca1 = lib.funcs_.at(0);
552     ReportFuncItem &funca2 = lib.funcs_.at(1);
553 
554     // only fist one have count
555     EXPECT_EQ(funca1.eventCount_, 10u);
556     EXPECT_EQ(funca1.subTreeEventCount_, 10u);
557     EXPECT_EQ(funca2.eventCount_, 0u);
558     EXPECT_EQ(funca2.subTreeEventCount_, 10u);
559     EXPECT_EQ(thread.callNode.subTreeEventCount_, 0u);
560     EXPECT_EQ(thread.callNodeReverse.subTreeEventCount_, 0u);
561 
562     json->UpdateCallNodeEventCount();
563     EXPECT_EQ(thread.callNode.subTreeEventCount_, 10u);
564     EXPECT_EQ(thread.callNodeReverse.subTreeEventCount_, 10u);
565 }
566 
567 /**
568  * @tc.name: ProcessSymbolsFiles
569  * @tc.desc:
570  * @tc.type: FUNC
571  */
572 HWTEST_F(ReportJsonFileTest, ProcessSymbolsFiles, TestSize.Level1)
573 {
574     VirtualRuntime virtualRuntime;
575     std::unique_ptr<ReportJsonFile> json =
576         std::make_unique<ReportJsonFile>(nullptr, virtualRuntime);
577     std::vector<std::unique_ptr<SymbolsFile>> symbolsFiles;
578 
579     std::string userSymbol = "user_symbol";
580     std::unique_ptr<SymbolsFile> user = SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE);
581     user->symbols_.emplace_back(0x1, 1u, "first_user_func", user->filePath_);
582     user->symbols_.emplace_back(0x2, 1u, "second_user_func", user->filePath_);
583     user->filePath_ = userSymbol;
584     symbolsFiles.emplace_back(std::move(user));
585 
586     std::string userSymbol2 = "user_symbol2";
587     std::unique_ptr<SymbolsFile> user2 = SymbolsFile::CreateSymbolsFile(SYMBOL_ELF_FILE);
588     user2->symbols_.emplace_back(0x1, 1u, "first_user2_func", user2->filePath_);
589     user2->symbols_.emplace_back(0x2, 1u, "second_user2_func", user2->filePath_);
590     user2->symbols_.emplace_back(0x3, 1u, "third_user2_func", user2->filePath_);
591     user2->filePath_ = userSymbol2;
592     symbolsFiles.emplace_back(std::move(user2));
593 
594     json->ProcessSymbolsFiles(symbolsFiles);
595     EXPECT_EQ(json->libList_.size(), 2u);
596     ASSERT_EQ(json->functionMap_.size(), 2u);
597     EXPECT_EQ(json->functionMap_[0].size(), 2u);
598     EXPECT_EQ(json->functionMap_[1].size(), 3u);
599 }
600 
601 /**
602  * @tc.name: GetFunctionID
603  * @tc.desc:
604  * @tc.type: FUNC
605  */
606 HWTEST_F(ReportJsonFileTest, GetFunctionID, TestSize.Level1)
607 {
608     StdoutRecord output;
609     VirtualRuntime virtualRuntime;
610     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
611     /*
612         3 lib : liba , libb and libc
613         5 func :
614             funca1 in liba
615             funca2 in liba
616             funcb1 in libb
617             funcc1 in libc
618     */
619     EXPECT_EQ(json->GetFunctionID(0, "funca1"), 0);
620     EXPECT_EQ(json->GetFunctionID(0, "funca2"), 1);
621     EXPECT_EQ(json->GetFunctionID(0, "funca3"), 4);
622     EXPECT_EQ(json->GetFunctionID(0, "funcb1"), 5);
623     EXPECT_EQ(json->GetFunctionID(1, "funcb1"), 2);
624     EXPECT_EQ(json->GetFunctionID(2, "funcc1"), 3);
625 }
626 
627 /**
628  * @tc.name: GetLibID
629  * @tc.desc:
630  * @tc.type: FUNC
631  */
632 HWTEST_F(ReportJsonFileTest, GetLibID, TestSize.Level1)
633 {
634     StdoutRecord output;
635     VirtualRuntime virtualRuntime;
636     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
637     /*
638         3 lib : liba , libb and libc
639         5 func :
640             funca1 in liba
641             funca2 in liba
642             funcb1 in libb
643             funcc1 in libc
644     */
645     EXPECT_EQ(json->GetLibID("liba"), 0);
646     EXPECT_EQ(json->GetLibID("libb"), 1);
647     EXPECT_EQ(json->GetLibID("libc"), 2);
648     EXPECT_EQ(json->GetLibID("libd"), -1);
649     EXPECT_EQ(json->GetLibID(""), -1);
650 }
651 
652 /**
653  * @tc.name: GetConfigIndex
654  * @tc.desc:
655  * @tc.type: FUNC
656  */
657 HWTEST_F(ReportJsonFileTest, GetConfigIndex, TestSize.Level1)
658 {
659     StdoutRecord output;
660     VirtualRuntime virtualRuntime;
661     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
662 
663     std::vector<uint64_t> ids = {4, 5};
664     json->reportConfigItems_.emplace(
665         ids, ReportConfigItem(json->reportConfigItems_.size(), "eventName2"));
666 
667     EXPECT_EQ(json->GetConfigIndex(1), 0u);
668     EXPECT_EQ(json->GetConfigIndex(2), 0u);
669     EXPECT_EQ(json->GetConfigIndex(3), 0u);
670     EXPECT_EQ(json->GetConfigIndex(4), 1u);
671     EXPECT_EQ(json->GetConfigIndex(5), 1u);
672     EXPECT_EQ(json->GetConfigIndex(6), 0u);
673 }
674 
675 /**
676  * @tc.name: GetConfigName
677  * @tc.desc:
678  * @tc.type: FUNC
679  */
680 HWTEST_F(ReportJsonFileTest, GetConfigName, TestSize.Level1)
681 {
682     StdoutRecord output;
683     VirtualRuntime virtualRuntime;
684     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
685 
686     std::vector<uint64_t> ids = {4, 5};
687     json->reportConfigItems_.emplace(
688         ids, ReportConfigItem(json->reportConfigItems_.size(), "eventName2"));
689 
690     EXPECT_STREQ(json->GetConfigName(1).c_str(), "eventName");
691     EXPECT_STREQ(json->GetConfigName(2).c_str(), "eventName");
692     EXPECT_STREQ(json->GetConfigName(3).c_str(), "eventName");
693     EXPECT_STREQ(json->GetConfigName(4).c_str(), "eventName2");
694     EXPECT_STREQ(json->GetConfigName(5).c_str(), "eventName2");
695     EXPECT_STREQ(json->GetConfigName(6).c_str(), "eventName");
696 }
697 
698 /**
699  * @tc.name: GetConfig
700  * @tc.desc:
701  * @tc.type: FUNC
702  */
703 HWTEST_F(ReportJsonFileTest, GetConfig, TestSize.Level1)
704 {
705     StdoutRecord output;
706     VirtualRuntime virtualRuntime;
707     std::unique_ptr<ReportJsonFile> json = PrepairReportJson(virtualRuntime);
708 
709     std::vector<uint64_t> ids = {4, 5};
710     json->reportConfigItems_.emplace(
711         ids, ReportConfigItem(json->reportConfigItems_.size(), "eventName2"));
712 
713     EXPECT_EQ(json->GetConfig(1).index_, 0);
714     EXPECT_EQ(json->GetConfig(2).index_, 0);
715     EXPECT_EQ(json->GetConfig(3).index_, 0);
716     EXPECT_EQ(json->GetConfig(4).index_, 1);
717     EXPECT_EQ(json->GetConfig(5).index_, 1);
718     EXPECT_EQ(json->GetConfig(6).index_, 0);
719 }
720 } // namespace HiPerf
721 } // namespace Developtools
722 } // namespace OHOS
723