• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "gtest/gtest.h"
17 
18 #include "ecmascript/ecma_vm.h"
19 #include "ecmascript/jspandafile/method_literal.h"
20 #include "ecmascript/js_thread.h"
21 #include "ecmascript/napi/include/jsnapi.h"
22 #include "ecmascript/pgo_profiler/pgo_profiler_info.h"
23 #include "ecmascript/pgo_profiler/pgo_profiler_loader.h"
24 #include "ecmascript/pgo_profiler/pgo_profiler_manager.h"
25 #include "ecmascript/tests/test_helper.h"
26 
27 using namespace panda;
28 using namespace panda::ecmascript;
29 
30 namespace panda::test {
31 class PGOProfilerTest : public testing::Test {
32 public:
SetUpTestCase()33     static void SetUpTestCase()
34     {
35         GTEST_LOG_(INFO) << "SetUpTestCase";
36     }
37 
TearDownTestCase()38     static void TearDownTestCase()
39     {
40         PGOProfilerManager::GetInstance()->Destroy();
41         GTEST_LOG_(INFO) << "TearDownCase";
42     }
43 
44 protected:
45     EcmaVM *vm_ = nullptr;
46 };
47 
HWTEST_F_L0(PGOProfilerTest,Sample)48 HWTEST_F_L0(PGOProfilerTest, Sample)
49 {
50     mkdir("ark-profiler/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
51     RuntimeOption option;
52     option.SetEnableProfile(true);
53     option.SetProfileDir("ark-profiler/");
54     vm_ = JSNApi::CreateJSVM(option);
55     uint32_t checksum = 304293;
56     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
57     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
58 
59     MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(10));
60     JSHandle<Method> method = vm_->GetFactory()->NewMethod(methodLiteral);
61     JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
62     JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test"));
63     func->SetModule(vm_->GetJSThread(), recordName);
64     vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
65     JSNApi::DestroyJSVM(vm_);
66     // Loader
67     PGOProfilerLoader loader("ark-profiler/modules.ap", 2);
68     CString expectRecordName = "test";
69 #if defined(SUPPORT_ENABLE_ASM_INTERP)
70     ASSERT_TRUE(loader.LoadAndVerify(checksum));
71     ASSERT_TRUE(!loader.Match(expectRecordName, EntityId(10)));
72 #else
73     ASSERT_TRUE(!loader.LoadAndVerify(checksum));
74     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(10)));
75 #endif
76     unlink("ark-profiler/modules.ap");
77     rmdir("ark-profiler/");
78 }
79 
HWTEST_F_L0(PGOProfilerTest,Sample1)80 HWTEST_F_L0(PGOProfilerTest, Sample1)
81 {
82     mkdir("ark-profiler1/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
83     RuntimeOption option;
84     option.SetEnableProfile(true);
85     option.SetProfileDir("ark-profiler1/");
86     vm_ = JSNApi::CreateJSVM(option);
87     uint32_t checksum = 304293;
88     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
89     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
90 
91     MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(10));
92     MethodLiteral *methodLiteral1 = new MethodLiteral(nullptr, EntityId(15));
93     MethodLiteral *methodLiteral2 = new MethodLiteral(nullptr, EntityId(20));
94     JSHandle<Method> method = vm_->GetFactory()->NewMethod(methodLiteral);
95     JSHandle<Method> method1 = vm_->GetFactory()->NewMethod(methodLiteral1);
96     JSHandle<Method> method2 = vm_->GetFactory()->NewMethod(methodLiteral2);
97     JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
98     JSHandle<JSFunction> func1 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method1);
99     JSHandle<JSFunction> func2 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method2);
100     JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test"));
101     func->SetModule(vm_->GetJSThread(), recordName);
102     func1->SetModule(vm_->GetJSThread(), recordName);
103     func2->SetModule(vm_->GetJSThread(), recordName);
104     for (int i = 0; i < 5; i++) {
105         vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
106     }
107     for (int i = 0; i < 50; i++) {
108         vm_->GetPGOProfiler()->Sample(func2.GetTaggedType());
109     }
110     vm_->GetPGOProfiler()->Sample(func1.GetTaggedType());
111     JSNApi::DestroyJSVM(vm_);
112 
113     // Loader
114     PGOProfilerLoader loader("ark-profiler1/modules.ap", 2);
115     CString expectRecordName = "test";
116 #if defined(SUPPORT_ENABLE_ASM_INTERP)
117     ASSERT_TRUE(loader.LoadAndVerify(checksum));
118     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(10)));
119     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(20)));
120     ASSERT_TRUE(!loader.Match(expectRecordName, EntityId(15)));
121 #else
122     ASSERT_TRUE(!loader.LoadAndVerify(checksum));
123     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(15)));
124 #endif
125     unlink("ark-profiler1/modules.ap");
126     rmdir("ark-profiler1/");
127 }
128 
HWTEST_F_L0(PGOProfilerTest,Sample2)129 HWTEST_F_L0(PGOProfilerTest, Sample2)
130 {
131     mkdir("ark-profiler2/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
132     RuntimeOption option;
133     option.SetEnableProfile(true);
134     option.SetProfileDir("ark-profiler2/");
135     vm_ = JSNApi::CreateJSVM(option);
136     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
137     uint32_t checksum = 304293;
138     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
139 
140     MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(10));
141     MethodLiteral *methodLiteral1 = new MethodLiteral(nullptr, EntityId(15));
142     JSHandle<Method> method = vm_->GetFactory()->NewMethod(methodLiteral);
143     JSHandle<Method> method1 = vm_->GetFactory()->NewMethod(methodLiteral1);
144     JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
145     JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test"));
146     func->SetModule(vm_->GetJSThread(), recordName);
147     JSHandle<JSFunction> func1 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method1);
148     JSHandle<JSTaggedValue> recordName1(vm_->GetFactory()->NewFromStdString("test1"));
149     func1->SetModule(vm_->GetJSThread(), recordName1);
150     vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
151     for (int i = 0; i < 5; i++) {
152         vm_->GetPGOProfiler()->Sample(func1.GetTaggedType());
153     }
154     JSNApi::DestroyJSVM(vm_);
155 
156     // Loader
157     PGOProfilerLoader loader("ark-profiler2/modules.ap", 2);
158     CString expectRecordName = "test";
159     CString expectRecordName1 = "test1";
160 #if defined(SUPPORT_ENABLE_ASM_INTERP)
161     ASSERT_TRUE(loader.LoadAndVerify(checksum));
162     ASSERT_TRUE(!loader.Match(expectRecordName, EntityId(10)));
163 #else
164     ASSERT_TRUE(!loader.LoadAndVerify(checksum));
165     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(10)));
166 #endif
167     ASSERT_TRUE(loader.Match(expectRecordName1, EntityId(15)));
168     unlink("ark-profiler2/modules.ap");
169     rmdir("ark-profiler2/");
170 }
171 
HWTEST_F_L0(PGOProfilerTest,DisEnableSample)172 HWTEST_F_L0(PGOProfilerTest, DisEnableSample)
173 {
174     mkdir("ark-profiler3/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
175     RuntimeOption option;
176     option.SetEnableProfile(false);
177     option.SetProfileDir("ark-profiler3/");
178     vm_ = JSNApi::CreateJSVM(option);
179     uint32_t checksum = 304293;
180     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
181     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
182 
183     MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(10));
184     JSHandle<Method> method = vm_->GetFactory()->NewMethod(methodLiteral);
185     JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
186     JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test"));
187     func->SetModule(vm_->GetJSThread(), recordName);
188     vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
189     JSNApi::DestroyJSVM(vm_);
190 
191     // Loader
192     PGOProfilerLoader loader("ark-profiler3/modules.ap", 2);
193     // path is empty()
194     ASSERT_TRUE(!loader.LoadAndVerify(checksum));
195     CString expectRecordName = "test";
196     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(10)));
197     rmdir("ark-profiler3/");
198 }
199 
HWTEST_F_L0(PGOProfilerTest,PGOProfilerManagerInvalidPath)200 HWTEST_F_L0(PGOProfilerTest, PGOProfilerManagerInvalidPath)
201 {
202     RuntimeOption option;
203     option.SetEnableProfile(true);
204     option.SetProfileDir("ark-profiler4");
205     vm_ = JSNApi::CreateJSVM(option);
206     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
207     JSNApi::DestroyJSVM(vm_);
208 }
209 
HWTEST_F_L0(PGOProfilerTest,PGOProfilerManagerInitialize)210 HWTEST_F_L0(PGOProfilerTest, PGOProfilerManagerInitialize)
211 {
212     RuntimeOption option;
213     option.SetEnableProfile(true);
214     // outDir is empty
215     option.SetProfileDir("");
216     vm_ = JSNApi::CreateJSVM(option);
217     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
218 
219     JSNApi::DestroyJSVM(vm_);
220 }
221 
HWTEST_F_L0(PGOProfilerTest,PGOProfilerManagerSample)222 HWTEST_F_L0(PGOProfilerTest, PGOProfilerManagerSample)
223 {
224     RuntimeOption option;
225     option.SetEnableProfile(true);
226     char currentPath[PATH_MAX + 2];
227     if (memset_s(currentPath, PATH_MAX, 1, PATH_MAX) != EOK) {
228         ASSERT_TRUE(false);
229     }
230     currentPath[PATH_MAX + 1] = '\0';
231     option.SetProfileDir(currentPath);
232     vm_ = JSNApi::CreateJSVM(option);
233     uint32_t checksum = 304293;
234     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
235     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
236 
237     JSHandle<JSArray> array = vm_->GetFactory()->NewJSArray();
238     vm_->GetPGOProfiler()->Sample(array.GetTaggedType());
239 
240     // RecordName is hole
241     MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(10));
242     JSHandle<Method> method = vm_->GetFactory()->NewMethod(methodLiteral);
243     JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
244     func->SetModule(vm_->GetJSThread(), JSTaggedValue::Hole());
245     vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
246     JSNApi::DestroyJSVM(vm_);
247 
248     PGOProfilerLoader loader("", 2);
249     // path is empty()
250     ASSERT_TRUE(loader.LoadAndVerify(checksum));
251     // path size greater than PATH_MAX
252     char path[PATH_MAX + 1] = {'0'};
253     PGOProfilerLoader loader1(path, 4);
254     ASSERT_TRUE(!loader1.LoadAndVerify(checksum));
255 }
256 
HWTEST_F_L0(PGOProfilerTest,PGOProfilerDoubleVM)257 HWTEST_F_L0(PGOProfilerTest, PGOProfilerDoubleVM)
258 {
259     mkdir("ark-profiler5/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
260     RuntimeOption option;
261     option.SetEnableProfile(true);
262     // outDir is empty
263     option.SetProfileDir("ark-profiler5/");
264     vm_ = JSNApi::CreateJSVM(option);
265     uint32_t checksum = 304293;
266     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
267     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
268     // worker vm read profile enable from PGOProfilerManager singleton
269     option.SetEnableProfile(false);
270     auto vm2 = JSNApi::CreateJSVM(option);
271     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
272     ASSERT_TRUE(vm2 != nullptr) << "Cannot create Runtime";
273 
274     MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(10));
275     MethodLiteral *methodLiteral1 = new MethodLiteral(nullptr, EntityId(15));
276     JSHandle<Method> method = vm2->GetFactory()->NewMethod(methodLiteral);
277     JSHandle<JSFunction> func = vm2->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
278     JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test"));
279     func->SetModule(vm2->GetJSThread(), recordName);
280     vm2->GetPGOProfiler()->Sample(func.GetTaggedType());
281 
282     JSHandle<Method> method1 = vm_->GetFactory()->NewMethod(methodLiteral);
283     JSHandle<Method> method2 = vm_->GetFactory()->NewMethod(methodLiteral1);
284     JSHandle<JSFunction> func1 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method1);
285     JSHandle<JSFunction> func2 = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method2);
286     JSHandle<JSTaggedValue> recordName1(vm_->GetFactory()->NewFromStdString("test"));
287     func1->SetModule(vm_->GetJSThread(), recordName);
288     func2->SetModule(vm_->GetJSThread(), recordName);
289     vm_->GetPGOProfiler()->Sample(func1.GetTaggedType());
290     vm_->GetPGOProfiler()->Sample(func2.GetTaggedType());
291 
292     JSNApi::DestroyJSVM(vm2);
293     JSNApi::DestroyJSVM(vm_);
294 
295     PGOProfilerLoader loader("ark-profiler5/profiler", 2);
296     mkdir("ark-profiler5/profiler", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
297     ASSERT_TRUE(!loader.LoadAndVerify(checksum));
298     CString expectRecordName = "test";
299     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(15)));
300 
301     PGOProfilerLoader loader1("ark-profiler5/modules.ap", 2);
302 #if defined(SUPPORT_ENABLE_ASM_INTERP)
303     ASSERT_TRUE(loader1.LoadAndVerify(checksum));
304     ASSERT_TRUE(!loader1.Match(expectRecordName, EntityId(15)));
305 #else
306     ASSERT_TRUE(!loader1.LoadAndVerify(checksum));
307     ASSERT_TRUE(loader1.Match(expectRecordName, EntityId(15)));
308 #endif
309 
310     unlink("ark-profiler5/modules.ap");
311     rmdir("ark-profiler5/profiler");
312     rmdir("ark-profiler5/");
313 }
314 
HWTEST_F_L0(PGOProfilerTest,PGOProfilerLoaderNoHotMethod)315 HWTEST_F_L0(PGOProfilerTest, PGOProfilerLoaderNoHotMethod)
316 {
317     mkdir("ark-profiler8/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
318     RuntimeOption option;
319     option.SetEnableProfile(true);
320     option.SetProfileDir("ark-profiler8/");
321     vm_ = JSNApi::CreateJSVM(option);
322     uint32_t checksum = 304293;
323     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
324 
325     MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(10));
326 
327     JSHandle<Method> method = vm_->GetFactory()->NewMethod(methodLiteral);
328     JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
329     JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test"));
330     func->SetModule(vm_->GetJSThread(), recordName);
331     vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
332     JSNApi::DestroyJSVM(vm_);
333 
334     PGOProfilerLoader loader("ark-profiler8/modules.ap", 2);
335     CString expectRecordName = "test";
336 #if defined(SUPPORT_ENABLE_ASM_INTERP)
337     ASSERT_TRUE(loader.LoadAndVerify(checksum));
338     ASSERT_TRUE(!loader.Match(expectRecordName, EntityId(10)));
339 #else
340     ASSERT_TRUE(!loader.LoadAndVerify(checksum));
341     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(10)));
342 #endif
343 
344     unlink("ark-profiler8/modules.ap");
345     rmdir("ark-profiler8/");
346 }
347 
HWTEST_F_L0(PGOProfilerTest,PGOProfilerPostTask)348 HWTEST_F_L0(PGOProfilerTest, PGOProfilerPostTask)
349 {
350     mkdir("ark-profiler9/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
351     RuntimeOption option;
352     option.SetEnableProfile(true);
353     option.SetProfileDir("ark-profiler9/");
354     vm_ = JSNApi::CreateJSVM(option);
355     uint32_t checksum = 304293;
356     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
357 
358     JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test"));
359     for (int i = 0; i < 31; i++) {
360         MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(i));
361         JSHandle<Method> method = vm_->GetFactory()->NewMethod(methodLiteral);
362         JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
363         func->SetModule(vm_->GetJSThread(), recordName);
364         vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
365         if (i % 3 == 0) {
366             vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
367         }
368     }
369 
370     JSNApi::DestroyJSVM(vm_);
371 
372     PGOProfilerLoader loader("ark-profiler9/modules.ap", 2);
373 #if defined(SUPPORT_ENABLE_ASM_INTERP)
374     ASSERT_TRUE(loader.LoadAndVerify(checksum));
375 #else
376     ASSERT_TRUE(!loader.LoadAndVerify(checksum));
377 #endif
378     CString expectRecordName = "test";
379     for (int i = 0; i < 31; i++) {
380         if (i % 3 == 0) {
381             ASSERT_TRUE(loader.Match(expectRecordName, EntityId(i)));
382         } else {
383 #if defined(SUPPORT_ENABLE_ASM_INTERP)
384             ASSERT_TRUE(!loader.Match(expectRecordName, EntityId(i)));
385 #else
386             ASSERT_TRUE(loader.Match(expectRecordName, EntityId(i)));
387 #endif
388         }
389     }
390 
391     unlink("ark-profiler9/modules.ap");
392     rmdir("ark-profiler9/");
393 }
394 
HWTEST_F_L0(PGOProfilerTest,BinaryToText)395 HWTEST_F_L0(PGOProfilerTest, BinaryToText)
396 {
397     mkdir("ark-profiler7/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
398 
399     std::ofstream file("ark-profiler7/modules.ap");
400 
401     PGOProfilerHeader *header = nullptr;
402     PGOProfilerHeader::Build(&header, PGOProfilerHeader::LastSize());
403     std::unique_ptr<PGOPandaFileInfos> pandaFileInfos = std::make_unique<PGOPandaFileInfos>();
404     std::unique_ptr<PGORecordDetailInfos> recordInfos = std::make_unique<PGORecordDetailInfos>(2);
405     pandaFileInfos->Sample(0x34556738);
406     ASSERT_TRUE(recordInfos->AddMethod("test", EntityId(23), "test", SampleMode::CALL_MODE));
407     ASSERT_FALSE(recordInfos->AddMethod("test", EntityId(23), "test", SampleMode::CALL_MODE));
408     ASSERT_FALSE(recordInfos->AddMethod("test", EntityId(23), "test", SampleMode::CALL_MODE));
409 
410     pandaFileInfos->ProcessToBinary(file, header->GetPandaInfoSection());
411     recordInfos->ProcessToBinary(nullptr, file, header->GetRecordInfoSection());
412     header->ProcessToBinary(file);
413     file.close();
414 
415     ASSERT_TRUE(PGOProfilerManager::GetInstance()->BinaryToText(
416         "ark-profiler7/modules.ap", "ark-profiler7/modules.text", 2));
417 
418     unlink("ark-profiler7/modules.ap");
419     unlink("ark-profiler7/modules.text");
420     rmdir("ark-profiler7");
421 }
422 
HWTEST_F_L0(PGOProfilerTest,TextToBinary)423 HWTEST_F_L0(PGOProfilerTest, TextToBinary)
424 {
425     mkdir("ark-profiler10/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
426 
427     std::ofstream file("ark-profiler10/modules.text");
428     std::string result = "Profiler Version: 0.0.0.1\n";
429     file.write(result.c_str(), result.size());
430     result = "\nPanda file sumcheck list: [ 413775942 ]\n";
431     file.write(result.c_str(), result.size());
432     result = "\nrecordName: [ 1232/3/CALL_MODE/hello, 234/100/HOTNESS_MODE/h#ello1 ]\n";
433     file.write(result.c_str(), result.size());
434     file.close();
435 
436     ASSERT_TRUE(PGOProfilerManager::GetInstance()->TextToBinary("ark-profiler10/modules.text", "ark-profiler10/", 2));
437 
438     PGOProfilerLoader loader("ark-profiler10/modules.ap", 2);
439     ASSERT_TRUE(loader.LoadAndVerify(413775942));
440 
441     unlink("ark-profiler10/modules.ap");
442     unlink("ark-profiler10/modules.text");
443     rmdir("ark-profiler10");
444 }
445 
HWTEST_F_L0(PGOProfilerTest,TextRecover)446 HWTEST_F_L0(PGOProfilerTest, TextRecover)
447 {
448     mkdir("ark-profiler11/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
449 
450     std::ofstream file("ark-profiler11/modules.text");
451     std::string result = "Profiler Version: 0.0.0.1\n";
452     file.write(result.c_str(), result.size());
453     result = "\nPanda file sumcheck list: [ 413775942 ]\n";
454     file.write(result.c_str(), result.size());
455     result = "\n_GLOBAL::funct_main_0: [ 1232/3/CALL_MODE/hello ]\n";
456     file.write(result.c_str(), result.size());
457     result = "\nrecordName: [ 234/100/HOTNESS_MODE/h#ello1 ]\n";
458     file.write(result.c_str(), result.size());
459     file.close();
460 
461     ASSERT_TRUE(PGOProfilerManager::GetInstance()->TextToBinary("ark-profiler11/modules.text", "ark-profiler11/", 2));
462 
463     ASSERT_TRUE(PGOProfilerManager::GetInstance()->BinaryToText(
464         "ark-profiler11/modules.ap", "ark-profiler11/modules_recover.text", 2));
465 
466     std::ifstream fileOrigin("ark-profiler11/modules.text");
467     std::ifstream fileRecover("ark-profiler11/modules_recover.text");
468 
469     std::string lineOrigin;
470     std::string lineRecover;
471     // check content from origin and recovered profile line by line.
472     while (std::getline(fileOrigin, lineOrigin)) {
473         std::getline(fileRecover, lineRecover);
474         ASSERT_EQ(lineOrigin, lineRecover);
475     }
476 
477     fileOrigin.close();
478     fileRecover.close();
479     unlink("ark-profiler11/modules.ap");
480     unlink("ark-profiler11/modules.text");
481     unlink("ark-profiler11/modules_recover.text");
482     rmdir("ark-profiler11");
483 }
484 
HWTEST_F_L0(PGOProfilerTest,FailResetProfilerInWorker)485 HWTEST_F_L0(PGOProfilerTest, FailResetProfilerInWorker)
486 {
487     mkdir("ark-profiler12/", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
488     RuntimeOption option;
489     // Although enableProfle is set in option, but it will not work when isWorker is set.
490     option.SetEnableProfile(true);
491     option.SetIsWorker();
492     option.SetProfileDir("ark-profiler12/");
493     // PgoProfiler is disabled as default.
494     vm_ = JSNApi::CreateJSVM(option);
495     uint32_t checksum = 304293;
496     PGOProfilerManager::GetInstance()->SamplePandaFileInfo(checksum);
497     ASSERT_TRUE(vm_ != nullptr) << "Cannot create Runtime";
498 
499     MethodLiteral *methodLiteral = new MethodLiteral(nullptr, EntityId(61));
500     JSHandle<Method> method = vm_->GetFactory()->NewMethod(methodLiteral);
501     JSHandle<JSFunction> func = vm_->GetFactory()->NewJSFunction(vm_->GetGlobalEnv(), method);
502     JSHandle<JSTaggedValue> recordName(vm_->GetFactory()->NewFromStdString("test"));
503     func->SetModule(vm_->GetJSThread(), recordName);
504     vm_->GetPGOProfiler()->Sample(func.GetTaggedType());
505     JSNApi::DestroyJSVM(vm_);
506 
507     // Loader
508     PGOProfilerLoader loader("ark-profiler12/modules.ap", 2);
509     // path is empty()
510     ASSERT_TRUE(!loader.LoadAndVerify(checksum));
511     CString expectRecordName = "test";
512     ASSERT_TRUE(loader.Match(expectRecordName, EntityId(61)));
513     rmdir("ark-profiler12/");
514 }
515 }  // namespace panda::test
516