• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_LOOP_TEST_H
17 #define ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_LOOP_TEST_H
18 
19 #include "tooling/test/client_utils/test_util.h"
20 
21 namespace panda::ecmascript::tooling::test {
22 class JsCpuprofileLoopTest : public TestActions {
23 public:
JsCpuprofileLoopTest()24     JsCpuprofileLoopTest()
25     {
26         testAction = {
27             {SocketAction::SEND, "enable"},
28             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
29             {SocketAction::SEND, "runtime-enable"},
30             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
31             {SocketAction::SEND, "run"},
32             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
33             // load common_func.js
34             {SocketAction::RECV, "Debugger.scriptParsed", ActionRule::STRING_CONTAIN},
35             // break on start
36             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
37             {SocketAction::SEND, "cpuprofile-enable"},
38             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
39 
40             {SocketAction::SEND, "cpuprofile"},
41             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
42 
43             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 64"},
44             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
45             {SocketAction::SEND, "b " DEBUGGER_JS_DIR "common_func.js 65"},
46             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
47 
48             {SocketAction::SEND, "resume"},
49             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
50             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
51             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
52             {SocketAction::SEND, "cpuprofile-stop"},
53             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
54                 [this](auto recv, auto, auto) -> bool { return RecvCpuprofileInfo(recv); }},
55 
56             {SocketAction::SEND, "cpuprofile"},
57             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
58             {SocketAction::SEND, "resume"},
59             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
60             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
61             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
62             {SocketAction::SEND, "cpuprofile-stop"},
63             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE,
64                 [this](auto recv, auto, auto) -> bool { return RecvCpuprofileInfo(recv); }},
65 
66             {SocketAction::SEND, "cpuprofile-disable"},
67             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
68 
69             {SocketAction::SEND, "resume"},
70             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
71             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
72             {SocketAction::RECV, "Debugger.paused", ActionRule::STRING_CONTAIN},
73 
74             // reply success and run
75             {SocketAction::SEND, "success"},
76             {SocketAction::SEND, "resume"},
77             {SocketAction::RECV, "Debugger.resumed", ActionRule::STRING_CONTAIN},
78             {SocketAction::RECV, "", ActionRule::CUSTOM_RULE, MatchRule::replySuccess},
79         };
80     }
81 
RecvCpuprofileInfo(std::string recv)82     bool RecvCpuprofileInfo(std::string recv)
83     {
84         std::unique_ptr<PtJson> json = PtJson::Parse(recv);
85         Result ret;
86         int id = 0;
87         ret = json->GetInt("id", &id);
88         if (ret != Result::SUCCESS) {
89             return false;
90         }
91 
92         std::unique_ptr<PtJson> result = nullptr;
93         ret = json->GetObject("result", &result);
94         if (ret != Result::SUCCESS) {
95             return false;
96         }
97 
98         std::unique_ptr<PtJson> profile = nullptr;
99         ret = result->GetObject("profile", &profile);
100         if (ret != Result::SUCCESS) {
101             return false;
102         }
103         return true;
104     }
105 
GetEntryPoint()106     std::pair<std::string, std::string> GetEntryPoint() override
107     {
108         return {pandaFile_, entryPoint_};
109     }
110     ~JsCpuprofileLoopTest() = default;
111 
112 private:
113     std::string pandaFile_ = DEBUGGER_ABC_DIR "common_func.abc";
114     std::string sourceFile_ = DEBUGGER_JS_DIR "common_func.js";
115     std::string entryPoint_ = "_GLOBAL::func_main_0";
116 };
117 
GetJsCpuprofileLoopTest()118 std::unique_ptr<TestActions> GetJsCpuprofileLoopTest()
119 {
120     return std::make_unique<JsCpuprofileLoopTest>();
121 }
122 }  // namespace panda::ecmascript::tooling::test
123 
124 #endif // ECMASCRIPT_TOOLING_TEST_TESTCASES_JS_CPUPROFILE_LOOP_TEST_H
125