• 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 "perf_file_writer_test.h"
17 
18 #include "virtual_runtime.h"
19 
20 using namespace testing::ext;
21 using namespace std;
22 using namespace OHOS::HiviewDFX;
23 
24 namespace OHOS {
25 namespace Developtools {
26 namespace HiPerf {
27 const uint TESTRECORDCOUNT = 10;
28 
29 class PerfFileWriterTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35 
36     static void TestEventDescInit(std::vector<AttrWithId> &eventDesc);
37     static const int TESTNUMBER1 = 1;
38     static const int TESTNUMBER2 = 2;
39     static const int TESTNUMBER3 = 3;
40     static const int TESTNUMBER4 = 4;
41     static const int TESTNUMBER5 = 5;
42 };
43 
SetUpTestCase()44 void PerfFileWriterTest::SetUpTestCase() {}
45 
TearDownTestCase()46 void PerfFileWriterTest::TearDownTestCase() {}
47 
SetUp()48 void PerfFileWriterTest::SetUp() {}
49 
TearDown()50 void PerfFileWriterTest::TearDown() {}
51 
52 /**
53  * @tc.name: Test
54  * @tc.desc: push several records of all type into the buffer
55  * @tc.type: FUNC
56  */
TestEventDescInit(std::vector<AttrWithId> & eventDesc)57 void PerfFileWriterTest::TestEventDescInit(std::vector<AttrWithId> &eventDesc)
58 {
59     for (uint32_t nr = TESTNUMBER2; nr > 0; nr--) {
60         auto &item = eventDesc.emplace_back();
61         item.attr = {};
62         item.attr.size = 0;
63         item.attr.type = TESTNUMBER1 * nr;
64         item.attr.config = TESTNUMBER2 * nr;
65         item.attr.__reserved_2 = TESTNUMBER3 * nr;
66         item.ids = {TESTNUMBER4 * nr, TESTNUMBER5 * nr};
67         item.name = "test event desc";
68     }
69 }
70 
71 HWTEST_F(PerfFileWriterTest, TestFileWriter_Normal, TestSize.Level1)
72 {
73     std::string filename = "./TestFileWriter_Normal";
74     PerfFileWriter fileWriter;
75     ASSERT_TRUE(fileWriter.Open(filename)) << "current path no write permission?";
76 
77     std::vector<AttrWithId> attrIds;
78     AttrWithId attrId;
79     perf_event_attr attr;
80     attrId.attr = attr;
81     attrId.ids.emplace_back(0);
82     attrIds.emplace_back(attrId);
83     ASSERT_TRUE(fileWriter.WriteAttrAndId(attrIds));
84 
85     uint64_t dataSize = 0;
86     for (uint i = 0; i < TESTRECORDCOUNT; i++) {
87         std::string testStr = "testFeature " + std::to_string(i);
88         PerfRecordMmap recordmmap(true, i, i, i, i, i, testStr);
89         dataSize += recordmmap.GetSize();
90         ASSERT_TRUE(fileWriter.WriteRecord(recordmmap));
91         ASSERT_TRUE(fileWriter.AddStringFeature(FEATURE::RESERVED, testStr));
92         ASSERT_TRUE(fileWriter.AddNrCpusFeature(FEATURE::RESERVED, 0, TESTNUMBER1));
93         ASSERT_TRUE(fileWriter.AddU64Feature(FEATURE::RESERVED, TESTNUMBER2));
94         std::vector<AttrWithId> eventDesces;
95         TestEventDescInit(eventDesces);
96         ASSERT_TRUE(fileWriter.AddEventDescFeature(FEATURE::RESERVED, eventDesces));
97         VirtualRuntime vr;
98         ASSERT_TRUE(fileWriter.AddSymbolsFeature(vr.GetSymbolsFiles()));
99     }
100     ASSERT_EQ(fileWriter.GetDataSize(), dataSize);
101     ASSERT_EQ(fileWriter.GetRecordCount(), TESTRECORDCOUNT);
102     ASSERT_TRUE(fileWriter.Close());
103     // check file
104     ASSERT_TRUE((access(filename.c_str(), F_OK) == 0));
105 }
106 
107 HWTEST_F(PerfFileWriterTest, TestFileWriter_Compress, TestSize.Level1)
108 {
109     std::string filename = "./TestFileWriter_Compress";
110     PerfFileWriter fileWriter;
111     ASSERT_TRUE(fileWriter.Open(filename, true)) << "current path no write permission?";
112 
113     std::vector<AttrWithId> attrIds;
114     AttrWithId attrId;
115     perf_event_attr attr;
116     attrId.attr = attr;
117     attrId.ids.emplace_back(0);
118     attrIds.emplace_back(attrId);
119     ASSERT_TRUE(fileWriter.WriteAttrAndId(attrIds));
120 
121     uint64_t dataSize = 0;
122     for (uint i = 0; i < TESTRECORDCOUNT; i++) {
123         std::string testStr = "testFeature " + std::to_string(i);
124         PerfRecordMmap recordmmap(true, i, i, i, i, i, testStr);
125         dataSize += recordmmap.GetSize();
126         ASSERT_TRUE(fileWriter.WriteRecord(recordmmap));
127         ASSERT_TRUE(fileWriter.AddStringFeature(FEATURE::RESERVED, testStr));
128         ASSERT_TRUE(fileWriter.AddNrCpusFeature(FEATURE::RESERVED, 0, TESTNUMBER1));
129         ASSERT_TRUE(fileWriter.AddU64Feature(FEATURE::RESERVED, TESTNUMBER2));
130         VirtualRuntime vr;
131         ASSERT_TRUE(fileWriter.AddSymbolsFeature(vr.GetSymbolsFiles()));
132     }
133     ASSERT_EQ(fileWriter.GetDataSize(), dataSize);
134     ASSERT_EQ(fileWriter.GetRecordCount(), TESTRECORDCOUNT);
135     ASSERT_TRUE(fileWriter.Close());
136     // check file
137     ASSERT_TRUE((access(filename.c_str(), F_OK) == 0));
138     filename += ".gz";
139     ASSERT_TRUE((access(filename.c_str(), F_OK) != 0));
140 }
141 
142 HWTEST_F(PerfFileWriterTest, TestFileWriter_NoWriteAttrAndId, TestSize.Level1)
143 {
144     std::string filename = "./TestFileWriter_NoWriteAttrAndId";
145     PerfFileWriter fileWriter;
146     ASSERT_TRUE(fileWriter.Open(filename)) << "current path no write permission?";
147 
148     for (uint i = 0; i < TESTRECORDCOUNT; i++) {
149         std::string testStr = "test " + std::to_string(i);
150         ASSERT_TRUE(fileWriter.AddStringFeature(FEATURE::RESERVED, testStr));
151         VirtualRuntime vr;
152         ASSERT_TRUE(fileWriter.AddSymbolsFeature(vr.GetSymbolsFiles()));
153     }
154 
155     std::string testStr = "testFeature ";
156     PerfRecordMmap recordmmap(true, 0, 0, 0, 0, 0, testStr);
157     ASSERT_FALSE(fileWriter.WriteRecord(recordmmap));
158     ASSERT_TRUE(fileWriter.Close());
159     // check file
160     ASSERT_TRUE((access(filename.c_str(), F_OK) == 0));
161 }
162 } // namespace HiPerf
163 } // namespace Developtools
164 } // namespace OHOS
165