From 050e38f7c43b5d17cd3f34d735368eb08df39472 Mon Sep 17 00:00:00 2001 From: Yves Gerey Date: Wed, 28 Aug 2019 13:24:00 +0200 Subject: [PATCH] Add --trace_event option to capture events in unit tests. Usage example: % out/head/modules_unittests --gtest_filter="MyTest" --trace_event=trace_event.json The resulting file can be uploaded into chrome for nice visualization (chrome://tracing). Bug: webrtc:10926 Change-Id: I420b9dff0626126f25e993fd31c3f2622329f858 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/150647 Reviewed-by: Artem Titov Commit-Queue: Yves Gerey Cr-Commit-Position: refs/heads/master@{#28982} --- test/test_main_lib.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_main_lib.cc b/test/test_main_lib.cc index 02e0703116..a276eb6d03 100644 --- a/test/test_main_lib.cc +++ b/test/test_main_lib.cc @@ -17,6 +17,7 @@ #include "absl/flags/parse.h" #include "absl/memory/memory.h" #include "rtc_base/checks.h" +#include "rtc_base/event_tracer.h" #include "rtc_base/logging.h" #include "rtc_base/ssl_adapter.h" #include "rtc_base/ssl_stream_adapter.h" @@ -74,6 +75,12 @@ ABSL_FLAG( ABSL_FLAG(bool, logs, true, "print logs to stderr"); ABSL_FLAG(bool, verbose, false, "verbose logs to stderr"); +ABSL_FLAG(std::string, + trace_event, + "", + "Path to collect trace events (json file) for chrome://tracing. " + "If not set, events aren't captured."); + ABSL_FLAG(std::string, force_fieldtrials, "", @@ -102,6 +109,13 @@ class TestMainImpl : public TestMain { rtc::LogMessage::SetLogToStderr(absl::GetFlag(FLAGS_logs) || absl::GetFlag(FLAGS_verbose)); + std::string trace_event_path = absl::GetFlag(FLAGS_trace_event); + const bool capture_events = !trace_event_path.empty(); + if (capture_events) { + rtc::tracing::SetupInternalTracer(); + rtc::tracing::StartInternalCapture(trace_event_path.c_str()); + } + // TODO(bugs.webrtc.org/9792): we need to reference something from // fileutils.h so that our downstream hack where we replace fileutils.cc // works. Otherwise the downstream flag implementation will take over and @@ -131,6 +145,10 @@ class TestMainImpl : public TestMain { // automatically wrapped. rtc::ThreadManager::Instance()->WrapCurrentThread(); RTC_CHECK(rtc::Thread::Current()); + + if (capture_events) { + rtc::tracing::StopInternalCapture(); + } return 0; }