|
1 | 1 | #include "inspector_profiler.h"
|
| 2 | +#include <sstream> |
2 | 3 | #include "base_object-inl.h"
|
3 | 4 | #include "debug_utils.h"
|
4 | 5 | #include "node_file.h"
|
@@ -33,21 +34,34 @@ const char* const kPathSeparator = "/";
|
33 | 34 | #define CWD_BUFSIZE (PATH_MAX)
|
34 | 35 | #endif
|
35 | 36 |
|
36 |
| -std::unique_ptr<StringBuffer> ToProtocolString(Isolate* isolate, |
37 |
| - Local<Value> value) { |
38 |
| - TwoByteValue buffer(isolate, value); |
39 |
| - return StringBuffer::create(StringView(*buffer, buffer.length())); |
40 |
| -} |
41 |
| - |
42 | 37 | V8ProfilerConnection::V8ProfilerConnection(Environment* env)
|
43 | 38 | : session_(env->inspector_agent()->Connect(
|
44 | 39 | std::make_unique<V8ProfilerConnection::V8ProfilerSessionDelegate>(
|
45 | 40 | this),
|
46 | 41 | false)),
|
47 | 42 | env_(env) {}
|
48 | 43 |
|
49 |
| -void V8ProfilerConnection::DispatchMessage(Local<String> message) { |
50 |
| - session_->Dispatch(ToProtocolString(env()->isolate(), message)->string()); |
| 44 | +size_t V8ProfilerConnection::DispatchMessage(const char* method, |
| 45 | + const char* params) { |
| 46 | + std::stringstream ss; |
| 47 | + size_t id = next_id(); |
| 48 | + ss << R"({ "id": )" << id; |
| 49 | + DCHECK(method != nullptr); |
| 50 | + ss << R"(, "method": ")" << method << '"'; |
| 51 | + if (params != nullptr) { |
| 52 | + ss << R"(, "params": )" << params; |
| 53 | + } |
| 54 | + ss << " }"; |
| 55 | + std::string message = ss.str(); |
| 56 | + const uint8_t* message_data = |
| 57 | + reinterpret_cast<const uint8_t*>(message.c_str()); |
| 58 | + Debug(env(), |
| 59 | + DebugCategory::INSPECTOR_PROFILER, |
| 60 | + "Dispatching message %s\n", |
| 61 | + message.c_str()); |
| 62 | + session_->Dispatch(StringView(message_data, message.length())); |
| 63 | + // TODO(joyeecheung): use this to identify the ending message. |
| 64 | + return id; |
51 | 65 | }
|
52 | 66 |
|
53 | 67 | static void WriteResult(Environment* env,
|
@@ -202,34 +216,15 @@ std::string V8CoverageConnection::GetDirectory() const {
|
202 | 216 | }
|
203 | 217 |
|
204 | 218 | void V8CoverageConnection::Start() {
|
205 |
| - Debug(env(), |
206 |
| - DebugCategory::INSPECTOR_PROFILER, |
207 |
| - "Sending Profiler.startPreciseCoverage\n"); |
208 |
| - Isolate* isolate = env()->isolate(); |
209 |
| - Local<String> enable = FIXED_ONE_BYTE_STRING( |
210 |
| - isolate, R"({"id": 1, "method": "Profiler.enable"})"); |
211 |
| - Local<String> start = FIXED_ONE_BYTE_STRING(isolate, R"({ |
212 |
| - "id": 2, |
213 |
| - "method": "Profiler.startPreciseCoverage", |
214 |
| - "params": { "callCount": true, "detailed": true } |
215 |
| - })"); |
216 |
| - DispatchMessage(enable); |
217 |
| - DispatchMessage(start); |
| 219 | + DispatchMessage("Profiler.enable"); |
| 220 | + DispatchMessage("Profiler.startPreciseCoverage", |
| 221 | + R"({ "callCount": true, "detailed": true })"); |
218 | 222 | }
|
219 | 223 |
|
220 | 224 | void V8CoverageConnection::End() {
|
221 | 225 | CHECK_EQ(ending_, false);
|
222 | 226 | ending_ = true;
|
223 |
| - Debug(env(), |
224 |
| - DebugCategory::INSPECTOR_PROFILER, |
225 |
| - "Sending Profiler.takePreciseCoverage\n"); |
226 |
| - Isolate* isolate = env()->isolate(); |
227 |
| - HandleScope scope(isolate); |
228 |
| - Local<String> end = FIXED_ONE_BYTE_STRING(isolate, R"({ |
229 |
| - "id": 3, |
230 |
| - "method": "Profiler.takePreciseCoverage" |
231 |
| - })"); |
232 |
| - DispatchMessage(end); |
| 227 | + DispatchMessage("Profiler.takePreciseCoverage"); |
233 | 228 | }
|
234 | 229 |
|
235 | 230 | std::string V8CpuProfilerConnection::GetDirectory() const {
|
@@ -257,25 +252,14 @@ MaybeLocal<Object> V8CpuProfilerConnection::GetProfile(Local<Object> result) {
|
257 | 252 | }
|
258 | 253 |
|
259 | 254 | void V8CpuProfilerConnection::Start() {
|
260 |
| - Debug(env(), DebugCategory::INSPECTOR_PROFILER, "Sending Profiler.start\n"); |
261 |
| - Isolate* isolate = env()->isolate(); |
262 |
| - Local<String> enable = FIXED_ONE_BYTE_STRING( |
263 |
| - isolate, R"({"id": 1, "method": "Profiler.enable"})"); |
264 |
| - Local<String> start = FIXED_ONE_BYTE_STRING( |
265 |
| - isolate, R"({"id": 2, "method": "Profiler.start"})"); |
266 |
| - DispatchMessage(enable); |
267 |
| - DispatchMessage(start); |
| 255 | + DispatchMessage("Profiler.enable"); |
| 256 | + DispatchMessage("Profiler.start"); |
268 | 257 | }
|
269 | 258 |
|
270 | 259 | void V8CpuProfilerConnection::End() {
|
271 | 260 | CHECK_EQ(ending_, false);
|
272 | 261 | ending_ = true;
|
273 |
| - Debug(env(), DebugCategory::INSPECTOR_PROFILER, "Sending Profiler.stop\n"); |
274 |
| - Isolate* isolate = env()->isolate(); |
275 |
| - HandleScope scope(isolate); |
276 |
| - Local<String> end = |
277 |
| - FIXED_ONE_BYTE_STRING(isolate, R"({"id": 3, "method": "Profiler.stop"})"); |
278 |
| - DispatchMessage(end); |
| 262 | + DispatchMessage("Profiler.stop"); |
279 | 263 | }
|
280 | 264 |
|
281 | 265 | // For now, we only support coverage profiling, but we may add more
|
|
0 commit comments