const char kCategoryName[] = "my_category"; void StartTracing() { base::trace_event::CategoryFilter category_filter(kCategoryName); base::trace_event::TraceLog::GetInstance()->SetEnabled( category_filter, base::trace_event::TraceLog::RECORDING_MODE, base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); }
float SomeHardcoreTask(int max_num) { TRACE_EVENT1(kCategoryName, "SomeHardcoreTask", "max_num", max_num); float x = 1.5f; for (int i = 0; i < max_num; ++i) x *= sin(x) / atan(x) * tanh(x) * sqrt(x); return x; } void SomeHardcoreAsyncTask( base::WaitableEvent* event, const base::Callback<void(float)>& out_cb) { TRACE_EVENT0(kCategoryName, "SomeHardcoreAsyncTask"); for (int i : {20000000, 5000000, 80000000}) out_cb.Run(SomeHardcoreTask(i)); event->Signal(); } void AsyncTaskCb(float x) { } void DoWork() { TRACE_EVENT0(kCategoryName, "DoWork"); base::Thread thread("HardcoreTaskThread"); thread.Start(); base::WaitableEvent event(false, false); thread.message_loop_proxy()->PostDelayedTask( FROM_HERE, base::Bind(&SomeHardcoreAsyncTask, &event, base::Bind(&AsyncTaskCb)), base::TimeDelta::FromSeconds(2)); for (int i : {10000000, 20000000, 50000000}) std::cout << SomeHardcoreTask(i); event.Wait(); }
const char kTracingJsonPath[] = "D:\\trace.json"; // -, . void WriteTraceDataCollected( base::File* output_file, const scoped_refptr<base::RefCountedString>& events_str, bool has_more_events) { output_file->WriteAtCurrentPos(events_str->data().c_str(), events_str->data().length()); if (has_more_events) output_file->WriteAtCurrentPos(",", 1); } // . void StopTracingAndFlushToDisk() { base::trace_event::TraceLog::GetInstance()->SetDisabled(); base::File output(base::FilePath::FromUTF8Unsafe(kTracingJsonPath), base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); CHECK(output.IsValid()); static const char kStart[] = "{\"traceEvents\":["; static const char kEnd[] = "]}"; output.WriteAtCurrentPos(kStart, strlen(kStart)); base::trace_event::TraceLog::GetInstance()->SetDisabled(); base::trace_event::TraceLog::GetInstance()->Flush( base::Bind(&WriteTraceDataCollected, &output)); output.WriteAtCurrentPos(kEnd, strlen(kEnd)); output.Close(); }
int main() { StartTracing(); DoWork(); StopTracingAndFlushToDisk(); return 0; }
#define NOMINMAX #include <iostream> #include <base/files/file.h> #include <base/synchronization/waitable_event.h> #include <base/trace_event/trace_event.h> namespace { const char kCategoryName[] = "my_category"; const char kTracingJsonPath[] = "D:\\trace.json"; void WriteTraceDataCollected( base::File* output_file, const scoped_refptr<base::RefCountedString>& events_str, bool has_more_events) { output_file->WriteAtCurrentPos(events_str->data().c_str(), events_str->data().length()); if (has_more_events) output_file->WriteAtCurrentPos(",", 1); } void StartTracing() { base::trace_event::CategoryFilter category_filter(kCategoryName); base::trace_event::TraceLog::GetInstance()->SetEnabled( category_filter, base::trace_event::TraceLog::RECORDING_MODE, base::trace_event::TraceOptions(base::trace_event::RECORD_UNTIL_FULL)); } void StopTracingAndFlushToDisk() { base::trace_event::TraceLog::GetInstance()->SetDisabled(); base::File output(base::FilePath::FromUTF8Unsafe(kTracingJsonPath), base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); CHECK(output.IsValid()); static const char kStart[] = "{\"traceEvents\":["; static const char kEnd[] = "]}"; output.WriteAtCurrentPos(kStart, strlen(kStart)); base::trace_event::TraceLog::GetInstance()->SetDisabled(); base::trace_event::TraceLog::GetInstance()->Flush( base::Bind(&WriteTraceDataCollected, &output)); output.WriteAtCurrentPos(kEnd, strlen(kEnd)); output.Close(); } float SomeHardcoreTask(int max_num) { TRACE_EVENT1(kCategoryName, "SomeHardcoreTask", "max_num", max_num); float x = 1.5f; for (int i = 0; i < max_num; ++i) x *= sin(x) / atan(x) * tanh(x) * sqrt(x); return x; } void SomeHardcoreAsyncTask( base::WaitableEvent* event, const base::Callback<void(float)>& out_cb) { TRACE_EVENT0(kCategoryName, "SomeHardcoreAsyncTask"); for (int i : {20000000, 5000000, 80000000}) out_cb.Run(SomeHardcoreTask(i)); event->Signal(); } void AsyncTaskCb(float x) { } void DoWork() { TRACE_EVENT0(kCategoryName, "DoWork"); base::Thread thread("HardcoreTaskThread"); thread.Start(); base::WaitableEvent event(false, false); thread.message_loop_proxy()->PostDelayedTask( FROM_HERE, base::Bind(&SomeHardcoreAsyncTask, &event, base::Bind(&AsyncTaskCb)), base::TimeDelta::FromSeconds(2)); for (int i : {10000000, 20000000, 50000000}) std::cout << SomeHardcoreTask(i); event.Wait(); } } // namespace int main() { StartTracing(); DoWork(); StopTracingAndFlushToDisk(); return 0; }
Source: https://habr.com/ru/post/256907/
All Articles