xref: /aosp_15_r20/external/cronet/base/test/launcher/test_launcher_tracer.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_TEST_LAUNCHER_TEST_LAUNCHER_TRACER_H_
6 #define BASE_TEST_LAUNCHER_TEST_LAUNCHER_TRACER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/synchronization/lock.h"
12 #include "base/threading/platform_thread.h"
13 #include "base/time/time.h"
14 
15 namespace base {
16 
17 class FilePath;
18 
19 // Records traces of test execution, e.g. to analyze performance.
20 // Thread safe.
21 class TestLauncherTracer {
22  public:
23   TestLauncherTracer();
24 
25   TestLauncherTracer(const TestLauncherTracer&) = delete;
26   TestLauncherTracer& operator=(const TestLauncherTracer&) = delete;
27 
28   ~TestLauncherTracer();
29 
30   // Records an event corresponding to test process execution.
31   // Return the sequence num of the process executed. The sequence num is also
32   // used as part of the event name been recorded.
33   int RecordProcessExecution(TimeTicks start_time, TimeDelta duration);
34 
35   // Dumps trace data as JSON. Returns true on success.
36   [[nodiscard]] bool Dump(const FilePath& path);
37 
38  private:
39   // Simplified version of base::TraceEvent.
40   struct Event {
41     std::string name;            // Displayed name.
42     TimeTicks timestamp;         // Timestamp when this event began.
43     TimeDelta duration;          // How long was this event.
44     PlatformThreadId thread_id;  // Thread ID where event was reported.
45   };
46 
47   // Timestamp when tracing started.
48   TimeTicks trace_start_time_;
49 
50   // Log of trace events.
51   std::vector<Event> events_;
52 
53   // Lock to protect all member variables.
54   Lock lock_;
55 };
56 
57 }  // namespace base
58 
59 #endif  // BASE_TEST_LAUNCHER_TEST_LAUNCHER_TRACER_H_
60