23652c5f4cd07dd6304f78b407541c6a02caf60e diff --git a/include/libplatform/v8-tracing.h b/include/libplatform/v8-tracing.h index 9dcf3d7bca..e430e7c350 100644 --- a/include/libplatform/v8-tracing.h +++ b/include/libplatform/v8-tracing.h @@ -112,6 +112,8 @@ class V8_PLATFORM_EXPORT TraceWriter { virtual void Flush() = 0; static TraceWriter* CreateJSONTraceWriter(std::ostream& stream); + static TraceWriter* CreateJSONTraceWriter(std::ostream& stream, + const std::string& tag); private: // Disallow copy and assign diff --git a/src/libplatform/tracing/trace-writer.cc b/src/libplatform/tracing/trace-writer.cc index 36a8783499..7bfc766469 100644 --- a/src/libplatform/tracing/trace-writer.cc +++ b/src/libplatform/tracing/trace-writer.cc @@ -119,8 +119,12 @@ void JSONTraceWriter::AppendArgValue(ConvertableToTraceFormat* value) { stream_ << arg_stringified; } -JSONTraceWriter::JSONTraceWriter(std::ostream& stream) : stream_(stream) { - stream_ << "{\"traceEvents\":["; +JSONTraceWriter::JSONTraceWriter(std::ostream& stream) + : JSONTraceWriter(stream, "traceEvents") {} + +JSONTraceWriter::JSONTraceWriter(std::ostream& stream, const std::string& tag) + : stream_(stream) { + stream_ << "{\"" << tag << "\":["; } JSONTraceWriter::~JSONTraceWriter() { stream_ << "]}"; } @@ -171,6 +175,11 @@ TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream) { return new JSONTraceWriter(stream); } +TraceWriter* TraceWriter::CreateJSONTraceWriter(std::ostream& stream, + const std::string& tag) { + return new JSONTraceWriter(stream, tag); +} + } // namespace tracing } // namespace platform } // namespace v8 diff --git a/src/libplatform/tracing/trace-writer.h b/src/libplatform/tracing/trace-writer.h index 7e1bdc24d6..d811351389 100644 --- a/src/libplatform/tracing/trace-writer.h +++ b/src/libplatform/tracing/trace-writer.h @@ -14,6 +14,7 @@ namespace tracing { class JSONTraceWriter : public TraceWriter { public: explicit JSONTraceWriter(std::ostream& stream); + JSONTraceWriter(std::ostream& stream, const std::string& tag); ~JSONTraceWriter(); void AppendTraceEvent(TraceObject* trace_event) override; void Flush() override; diff --git a/test/cctest/libplatform/test-tracing.cc b/test/cctest/libplatform/test-tracing.cc index da202057de..b949785bcf 100644 --- a/test/cctest/libplatform/test-tracing.cc +++ b/test/cctest/libplatform/test-tracing.cc @@ -128,44 +128,42 @@ TEST(TestTraceBufferRingBuffer) { delete ring_buffer; } -TEST(TestJSONTraceWriter) { - std::ostringstream stream; - // Create a scope for the tracing controller to terminate the trace writer. - { - v8::Platform* old_platform = i::V8::GetCurrentPlatform(); - std::unique_ptr default_platform( - v8::platform::NewDefaultPlatform()); - i::V8::SetPlatformForTesting(default_platform.get()); - auto tracing = - base::make_unique(); - v8::platform::tracing::TracingController* tracing_controller = - tracing.get(); - static_cast(default_platform.get()) - ->SetTracingController(std::move(tracing)); - TraceWriter* writer = TraceWriter::CreateJSONTraceWriter(stream); +void PopulateJSONWriter(TraceWriter* writer) { + v8::Platform* old_platform = i::V8::GetCurrentPlatform(); + std::unique_ptr default_platform( + v8::platform::NewDefaultPlatform()); + i::V8::SetPlatformForTesting(default_platform.get()); + auto tracing = base::make_unique(); + v8::platform::tracing::TracingController* tracing_controller = tracing.get(); + static_cast(default_platform.get()) + ->SetTracingController(std::move(tracing)); - TraceBuffer* ring_buffer = - TraceBuffer::CreateTraceBufferRingBuffer(1, writer); - tracing_controller->Initialize(ring_buffer); - TraceConfig* trace_config = new TraceConfig(); - trace_config->AddIncludedCategory("v8-cat"); - tracing_controller->StartTracing(trace_config); + TraceBuffer* ring_buffer = + TraceBuffer::CreateTraceBufferRingBuffer(1, writer); + tracing_controller->Initialize(ring_buffer); + TraceConfig* trace_config = new TraceConfig(); + trace_config->AddIncludedCategory("v8-cat"); + tracing_controller->StartTracing(trace_config); - TraceObject trace_object; - trace_object.InitializeForTesting( - 'X', tracing_controller->GetCategoryGroupEnabled("v8-cat"), "Test0", - v8::internal::tracing::kGlobalScope, 42, 123, 0, nullptr, nullptr, - nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID, 11, 22, 100, 50, 33, 44); - writer->AppendTraceEvent(&trace_object); - trace_object.InitializeForTesting( - 'Y', tracing_controller->GetCategoryGroupEnabled("v8-cat"), "Test1", - v8::internal::tracing::kGlobalScope, 43, 456, 0, nullptr, nullptr, - nullptr, nullptr, 0, 55, 66, 110, 55, 77, 88); - writer->AppendTraceEvent(&trace_object); - tracing_controller->StopTracing(); - i::V8::SetPlatformForTesting(old_platform); - } + TraceObject trace_object; + trace_object.InitializeForTesting( + 'X', tracing_controller->GetCategoryGroupEnabled("v8-cat"), "Test0", + v8::internal::tracing::kGlobalScope, 42, 123, 0, nullptr, nullptr, + nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID, 11, 22, 100, 50, 33, 44); + writer->AppendTraceEvent(&trace_object); + trace_object.InitializeForTesting( + 'Y', tracing_controller->GetCategoryGroupEnabled("v8-cat"), "Test1", + v8::internal::tracing::kGlobalScope, 43, 456, 0, nullptr, nullptr, + nullptr, nullptr, 0, 55, 66, 110, 55, 77, 88); + writer->AppendTraceEvent(&trace_object); + tracing_controller->StopTracing(); + i::V8::SetPlatformForTesting(old_platform); +} +TEST(TestJSONTraceWriter) { + std::ostringstream stream; + TraceWriter* writer = TraceWriter::CreateJSONTraceWriter(stream); + PopulateJSONWriter(writer); std::string trace_str = stream.str(); std::string expected_trace_str = "{\"traceEvents\":[{\"pid\":11,\"tid\":22,\"ts\":100,\"tts\":50," @@ -177,6 +175,21 @@ TEST(TestJSONTraceWriter) { CHECK_EQ(expected_trace_str, trace_str); } +TEST(TestJSONTraceWriterWithCustomtag) { + std::ostringstream stream; + TraceWriter* writer = TraceWriter::CreateJSONTraceWriter(stream, "customTag"); + PopulateJSONWriter(writer); + std::string trace_str = stream.str(); + std::string expected_trace_str = + "{\"customTag\":[{\"pid\":11,\"tid\":22,\"ts\":100,\"tts\":50," + "\"ph\":\"X\",\"cat\":\"v8-cat\",\"name\":\"Test0\",\"dur\":33," + "\"tdur\":44,\"id\":\"0x2a\",\"args\":{}},{\"pid\":55,\"tid\":66," + "\"ts\":110,\"tts\":55,\"ph\":\"Y\",\"cat\":\"v8-cat\",\"name\":" + "\"Test1\",\"dur\":77,\"tdur\":88,\"args\":{}}]}"; + + CHECK_EQ(expected_trace_str, trace_str); +} + TEST(TestTracingController) { v8::Platform* old_platform = i::V8::GetCurrentPlatform(); std::unique_ptr default_platform(