• 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 "agent/tracing_impl.h"
17 #include "ecmascript/tests/test_helper.h"
18 #include "protocol_handler.h"
19 
20 using namespace panda::ecmascript;
21 using namespace panda::ecmascript::tooling;
22 
23 namespace panda::test {
24 class TracingImplTest : public testing::Test {
25 public:
SetUpTestCase()26     static void SetUpTestCase()
27     {
28         GTEST_LOG_(INFO) << "SetUpTestCase";
29     }
30 
TearDownTestCase()31     static void TearDownTestCase()
32     {
33         GTEST_LOG_(INFO) << "TearDownCase";
34     }
35 
SetUp()36     void SetUp() override
37     {
38         TestHelper::CreateEcmaVMWithScope(ecmaVm, thread, scope);
39     }
40 
TearDown()41     void TearDown() override
42     {
43         TestHelper::DestroyEcmaVMWithScope(ecmaVm, scope);
44     }
45 
46 protected:
47     EcmaVM *ecmaVm {nullptr};
48     EcmaHandleScope *scope {nullptr};
49     JSThread *thread {nullptr};
50 };
51 
HWTEST_F_L0(TracingImplTest,StartAndEndTest)52 HWTEST_F_L0(TracingImplTest, StartAndEndTest)
53 {
54     ProtocolChannel *channel = nullptr;
55     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
56     auto params = std::make_unique<StartParams>();
57     params->SetCategories("cpu_profiler");
58     DispatchResponse response = tracing->Start(std::move(params));
59     ASSERT_TRUE(response.IsOk());
60 
61     auto profileInfo = tracing->End();
62     ASSERT_TRUE(profileInfo != nullptr);
63 }
64 
HWTEST_F_L0(TracingImplTest,GetCategoriesTest)65 HWTEST_F_L0(TracingImplTest, GetCategoriesTest)
66 {
67     ProtocolChannel *channel = nullptr;
68     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
69     std::vector<std::string> categories = {};
70     DispatchResponse response = tracing->GetCategories(categories);
71     ASSERT_TRUE(response.GetMessage() == "GetCategories not support now.");
72 }
73 
HWTEST_F_L0(TracingImplTest,RecordClockSyncMarkerTest)74 HWTEST_F_L0(TracingImplTest, RecordClockSyncMarkerTest)
75 {
76     ProtocolChannel *channel = nullptr;
77     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
78     std::string syncId;
79     DispatchResponse response = tracing->RecordClockSyncMarker(syncId);
80     ASSERT_TRUE(response.GetMessage() == "RecordClockSyncMarker not support now.");
81 }
82 
HWTEST_F_L0(TracingImplTest,RequestMemoryDumpTest)83 HWTEST_F_L0(TracingImplTest, RequestMemoryDumpTest)
84 {
85     ProtocolChannel *channel = nullptr;
86     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
87     auto params = std::make_unique<RequestMemoryDumpParams>();
88     std::string dumpGuid;
89     bool success = false;
90     DispatchResponse response = tracing->RequestMemoryDump(std::move(params), dumpGuid, success);
91     ASSERT_TRUE(response.GetMessage() == "RequestMemoryDump not support now.");
92 }
93 
HWTEST_F_L0(TracingImplTest,DispatcherImplDispatchTest)94 HWTEST_F_L0(TracingImplTest, DispatcherImplDispatchTest)
95 {
96     std::string result = "";
97     std::function<void(const void*, const std::string &)> callback =
98         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
99     ProtocolChannel *channel =  new ProtocolHandler(callback, ecmaVm);
100     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
101     auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
102     std::string msg = std::string() + R"({"id":0,"method":"Debugger.Test","params":{}})";
103     DispatchRequest request(msg);
104     dispatcherImpl->Dispatch(request);
105     ASSERT_TRUE(result.find("Unknown method: Test") != std::string::npos);
106     msg = std::string() + R"({"id":0,"method":"Debugger.end","params":{}})";
107     DispatchRequest request1 = DispatchRequest(msg);
108     dispatcherImpl->Dispatch(request1);
109     if (channel) {
110         delete channel;
111         channel = nullptr;
112     }
113     ASSERT_TRUE(result.find("\"id\":0,") != std::string::npos);
114 }
115 
HWTEST_F_L0(TracingImplTest,DispatcherImplEndTest)116 HWTEST_F_L0(TracingImplTest, DispatcherImplEndTest)
117 {
118     std::string result = "";
119     std::function<void(const void*, const std::string &)> callback =
120         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
121     ProtocolChannel *channel =  new ProtocolHandler(callback, ecmaVm);
122     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
123     auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
124     std::string msg = std::string() + R"({"id":0,"method":"Debugger.end","params":{}})";
125         DispatchRequest request = DispatchRequest(msg);
126     dispatcherImpl->Dispatch(request);
127     if (channel) {
128         delete channel;
129         channel = nullptr;
130     }
131     ASSERT_TRUE(result.find("\"id\":0,") != std::string::npos);
132 }
133 
HWTEST_F_L0(TracingImplTest,DispatcherImplGetCategoriesTest)134 HWTEST_F_L0(TracingImplTest, DispatcherImplGetCategoriesTest)
135 {
136     std::string result = "";
137     std::function<void(const void*, const std::string &)> callback =
138         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
139     ProtocolChannel *channel =  new ProtocolHandler(callback, ecmaVm);
140     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
141     auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
142     std::string msg = std::string() + R"({"id":0,"method":"Debugger.getCategories","params":{}})";
143         DispatchRequest request = DispatchRequest(msg);
144     dispatcherImpl->Dispatch(request);
145     if (channel) {
146         delete channel;
147         channel = nullptr;
148     }
149     ASSERT_TRUE(result.find("GetCategories not support now") != std::string::npos);
150 }
151 
HWTEST_F_L0(TracingImplTest,DispatcherImplRecordClockSyncMarkerTest)152 HWTEST_F_L0(TracingImplTest, DispatcherImplRecordClockSyncMarkerTest)
153 {
154     std::string result = "";
155     std::function<void(const void*, const std::string &)> callback =
156         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
157     ProtocolChannel *channel =  new ProtocolHandler(callback, ecmaVm);
158     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
159     auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
160     std::string msg = std::string() + R"({"id":0,"method":"Debugger.recordClockSyncMarker","params":{}})";
161         DispatchRequest request = DispatchRequest(msg);
162     dispatcherImpl->Dispatch(request);
163     if (channel) {
164         delete channel;
165         channel = nullptr;
166     }
167     ASSERT_TRUE(result.find("RecordClockSyncMarker not support now.") != std::string::npos);
168 }
169 
HWTEST_F_L0(TracingImplTest,DispatcherImplRequestMemoryDumpTest)170 HWTEST_F_L0(TracingImplTest, DispatcherImplRequestMemoryDumpTest)
171 {
172     std::string result = "";
173     std::function<void(const void*, const std::string &)> callback =
174         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
175     ProtocolChannel *channel =  new ProtocolHandler(callback, ecmaVm);
176     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
177     auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
178     std::string msg = std::string() + R"({"id":0,"method":"Debugger.requestMemoryDump","params":{}})";
179         DispatchRequest request = DispatchRequest(msg);
180     dispatcherImpl->Dispatch(request);
181     if (channel) {
182         delete channel;
183         channel = nullptr;
184     }
185     ASSERT_TRUE(result.find("RequestMemoryDump not support now.") != std::string::npos);
186 }
187 
HWTEST_F_L0(TracingImplTest,DispatcherImplStartDumpTest)188 HWTEST_F_L0(TracingImplTest, DispatcherImplStartDumpTest)
189 {
190     std::string result = "";
191     std::function<void(const void*, const std::string &)> callback =
192         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
193     ProtocolChannel *channel =  new ProtocolHandler(callback, ecmaVm);
194     auto tracing = std::make_unique<TracingImpl>(ecmaVm, channel);
195     auto dispatcherImpl = std::make_unique<TracingImpl::DispatcherImpl>(channel, std::move(tracing));
196     std::string msg = std::string() + R"({"id":0,"method":"Debugger.start","params":{"categories":"cpu_profiler"}})";
197     DispatchRequest request = DispatchRequest(msg);
198     dispatcherImpl->Dispatch(request);
199     if (channel) {
200         delete channel;
201         channel = nullptr;
202     }
203     ASSERT_TRUE(result.find("\"id\":0,") != std::string::npos);
204 }
205 
HWTEST_F_L0(TracingImplTest,FrontendBufferUsageTest)206 HWTEST_F_L0(TracingImplTest, FrontendBufferUsageTest)
207 {
208     std::string result = "";
209     std::function<void(const void*, const std::string &)> callback =
210         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
211     ProtocolChannel *channel = nullptr;
212     auto frontend = std::make_unique<TracingImpl::Frontend>(channel);
213     frontend->BufferUsage(0, 0, 0);
214     ASSERT_TRUE(result == "");
215     if (!channel) {
216         channel =  new ProtocolHandler(callback, ecmaVm);
217     }
218     auto frontend1 = std::make_unique<TracingImpl::Frontend>(channel);
219     frontend1->BufferUsage(0, 0, 0);
220     if (channel) {
221         delete channel;
222         channel = nullptr;
223     }
224     ASSERT_TRUE(result.find("Tracing.bufferUsage") != std::string::npos);
225 }
226 
HWTEST_F_L0(TracingImplTest,FrontendDataCollectedTest)227 HWTEST_F_L0(TracingImplTest, FrontendDataCollectedTest)
228 {
229     int64_t ts = 604898475815;
230     TraceEvent event("timeline", "UpdateCounters", "I", getpid(), 1415);
231     event.SetTs(ts);
232     event.SetTts(ts);
233     event.SetS("t");
234     std::string args = "{\"data\":{\"jsHeapSizeUsed\":" + std::to_string(1024) + "}}";
235     event.SetArgs(args);
236     std::unique_ptr<std::vector<TraceEvent>> traceEvents = std::make_unique<std::vector<TraceEvent>>();
237     traceEvents->emplace_back(event);
238 
239     std::string result = "";
240     std::function<void(const void*, const std::string &)> callback =
241         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
242     ProtocolChannel *channel = nullptr;
243     auto frontend = std::make_unique<TracingImpl::Frontend>(channel);
244     frontend->DataCollected(std::move(traceEvents));
245     ASSERT_TRUE(result == "");
246     if (!channel) {
247         channel =  new ProtocolHandler(callback, ecmaVm);
248     }
249     auto frontend1 = std::make_unique<TracingImpl::Frontend>(channel);
250     std::unique_ptr<std::vector<TraceEvent>> traceEvents1 = std::make_unique<std::vector<TraceEvent>>();
251     traceEvents1->emplace_back(event);
252     frontend1->DataCollected(std::move(traceEvents1));
253     if (channel) {
254         delete channel;
255         channel = nullptr;
256     }
257     ASSERT_TRUE(result.find("Tracing.dataCollected") != std::string::npos);
258 }
259 
HWTEST_F_L0(TracingImplTest,FrontendTracingCompleteTest)260 HWTEST_F_L0(TracingImplTest, FrontendTracingCompleteTest)
261 {
262     std::string result = "";
263     std::function<void(const void*, const std::string &)> callback =
264         [&result]([[maybe_unused]] const void *ptr, const std::string &temp) { result = temp; };
265     ProtocolChannel *channel = nullptr;
266     auto frontend = std::make_unique<TracingImpl::Frontend>(channel);
267     frontend->TracingComplete();
268     ASSERT_TRUE(result == "");
269     if (!channel) {
270         channel =  new ProtocolHandler(callback, ecmaVm);
271     }
272     auto frontend1 = std::make_unique<TracingImpl::Frontend>(channel);
273     frontend1->TracingComplete();
274     if (channel) {
275         delete channel;
276         channel = nullptr;
277     }
278     ASSERT_TRUE(result.find("Tracing.tracingComplete") != std::string::npos);
279 }
280 }  // namespace panda::test