1 // Copyright 2016 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/test/launcher/test_launcher_tracer.h" 6 7 #include "base/json/json_file_value_serializer.h" 8 #include "base/strings/stringprintf.h" 9 #include "base/values.h" 10 11 namespace base { 12 TestLauncherTracer()13TestLauncherTracer::TestLauncherTracer() 14 : trace_start_time_(TimeTicks::Now()) {} 15 16 TestLauncherTracer::~TestLauncherTracer() = default; 17 RecordProcessExecution(TimeTicks start_time,TimeDelta duration)18void TestLauncherTracer::RecordProcessExecution(TimeTicks start_time, 19 TimeDelta duration) { 20 AutoLock lock(lock_); 21 22 Event event; 23 event.name = StringPrintf("process #%zu", events_.size()); 24 event.timestamp = start_time; 25 event.duration = duration; 26 event.thread_id = PlatformThread::CurrentId(); 27 events_.push_back(event); 28 } 29 Dump(const FilePath & path)30bool TestLauncherTracer::Dump(const FilePath& path) { 31 AutoLock lock(lock_); 32 33 std::unique_ptr<ListValue> json_events(new ListValue); 34 for (const Event& event : events_) { 35 std::unique_ptr<DictionaryValue> json_event(new DictionaryValue); 36 json_event->SetString("name", event.name); 37 json_event->SetString("ph", "X"); 38 json_event->SetInteger( 39 "ts", (event.timestamp - trace_start_time_).InMicroseconds()); 40 json_event->SetInteger("dur", event.duration.InMicroseconds()); 41 json_event->SetInteger("tid", event.thread_id); 42 43 // Add fake values required by the trace viewer. 44 json_event->SetInteger("pid", 0); 45 46 json_events->Append(std::move(json_event)); 47 } 48 49 JSONFileValueSerializer serializer(path); 50 return serializer.Serialize(*json_events); 51 } 52 53 } // namespace base 54