From 58ba9527dc5fd3343f719b11a4cef73d23215253 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Mon, 29 Apr 2019 19:45:33 +0800 Subject: src: refactor V8ProfilerConnection to be more reusable Now the subclasses only need to override methods to - Get the profile node from the profiler response message object - Return the directory and file path And the V8ProfilerConnection() would handle the rest of profile serialization and message parsing. This makes it easier to add other types of profiles in the future. PR-URL: https://github.com/nodejs/node/pull/27475 Refs: https://github.com/nodejs/node/issues/27421 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis --- src/inspector_profiler.h | 49 +++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) (limited to 'src/inspector_profiler.h') diff --git a/src/inspector_profiler.h b/src/inspector_profiler.h index 7120819c13..cbe053e578 100644 --- a/src/inspector_profiler.h +++ b/src/inspector_profiler.h @@ -24,9 +24,7 @@ class V8ProfilerConnection { : connection_(connection) {} void SendMessageToFrontend( - const v8_inspector::StringView& message) override { - connection_->OnMessage(message); - } + const v8_inspector::StringView& message) override; private: V8ProfilerConnection* connection_; @@ -34,20 +32,30 @@ class V8ProfilerConnection { explicit V8ProfilerConnection(Environment* env); virtual ~V8ProfilerConnection() = default; - Environment* env() { return env_; } + + Environment* env() const { return env_; } + void DispatchMessage(v8::Local message); // Use DispatchMessage() to dispatch necessary inspector messages + // to start and end the profiling. virtual void Start() = 0; virtual void End() = 0; - // Override this to respond to the messages sent from the session. - virtual void OnMessage(const v8_inspector::StringView& message) = 0; - virtual bool ending() const = 0; - void DispatchMessage(v8::Local message); - // Write the result to a path - bool WriteResult(const char* path, v8::Local result); + // Return a descriptive name of the profile for debugging. + virtual const char* type() const = 0; + // Return if the profile is ending and the response can be parsed. + virtual bool ending() const = 0; + // Return the directory where the profile should be placed. + virtual std::string GetDirectory() const = 0; + // Return the filename the profile should be written as. + virtual std::string GetFilename() const = 0; + // Return the profile object parsed from `message.result`, + // which will be then written as a JSON. + virtual v8::MaybeLocal GetProfile( + v8::Local result) = 0; private: + void WriteProfile(v8::Local message); std::unique_ptr session_; Environment* env_ = nullptr; }; @@ -58,14 +66,18 @@ class V8CoverageConnection : public V8ProfilerConnection { void Start() override; void End() override; - void OnMessage(const v8_inspector::StringView& message) override; + + const char* type() const override { return type_.c_str(); } bool ending() const override { return ending_; } + std::string GetDirectory() const override; + std::string GetFilename() const override; + v8::MaybeLocal GetProfile(v8::Local result) override; + private: - bool WriteCoverage(v8::Local message); - v8::MaybeLocal GetResult(v8::Local message); std::unique_ptr session_; bool ending_ = false; + std::string type_ = "coverage"; }; class V8CpuProfilerConnection : public V8ProfilerConnection { @@ -75,15 +87,18 @@ class V8CpuProfilerConnection : public V8ProfilerConnection { void Start() override; void End() override; - void OnMessage(const v8_inspector::StringView& message) override; + + const char* type() const override { return type_.c_str(); } bool ending() const override { return ending_; } - private: - void WriteCpuProfile(v8::Local message); - v8::MaybeLocal GetResult(v8::Local message); + std::string GetDirectory() const override; + std::string GetFilename() const override; + v8::MaybeLocal GetProfile(v8::Local result) override; + private: std::unique_ptr session_; bool ending_ = false; + std::string type_ = "CPU"; }; } // namespace profiler -- cgit v1.2.3