xref: /aosp_15_r20/external/cronet/base/test/test_trace_processor.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 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 // Use TestTraceProcessor to load a perfetto trace and run queries on the trace.
6 // Documentation on how to use the trace processor and write queries can be
7 // found here: https://perfetto.dev/docs/analysis/trace-processor.
8 // TODO(b/224531105): Implement EXTRACT_ARGS to return multiple args to simplify
9 // queries.
10 
11 #ifndef BASE_TEST_TEST_TRACE_PROCESSOR_H_
12 #define BASE_TEST_TEST_TRACE_PROCESSOR_H_
13 
14 #include <string_view>
15 
16 #include "base/test/test_trace_processor_impl.h"
17 #include "base/test/trace_test_utils.h"
18 #include "base/types/expected.h"
19 #include "build/build_config.h"
20 
21 #if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY) && !BUILDFLAG(IS_WIN)
22 #define TEST_TRACE_PROCESSOR_ENABLED
23 #endif
24 
25 namespace base::test {
26 
27 #if BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
28 
29 using perfetto::protos::gen::TraceConfig;
30 
31 TraceConfig DefaultTraceConfig(std::string_view category_filter_string,
32                                bool privacy_filtering);
33 
34 // Use TestTraceProcessor to record Perfetto traces in unit and browser tests.
35 // This API can be used to start and stop traces, run SQL queries on the trace
36 // and write expectations against the query result.
37 //
38 // Example:
39 //
40 //   TestTraceProcessor test_trace_processor;
41 //   test_trace_processor.StartTrace();
42 //
43 //   /* do stuff */
44 //
45 //   absl::Status status = test_trace_processor.StopAndParseTrace();
46 //   ASSERT_TRUE(status.ok()) << status.message();
47 //
48 //   std::string query = "YOUR QUERY";
49 //   auto result = test_trace_processor.RunQuery(query);
50 //
51 //   ASSERT_TRUE(result.has_value()) << result.message();
52 //   EXPECT_THAT(result.value(), /* your expectations */);
53 
54 class TestTraceProcessor {
55  public:
56   using QueryResult = std::vector<std::vector<std::string>>;
57 
58   TestTraceProcessor();
59   ~TestTraceProcessor();
60 
61   // Privacy filtering removes high entropy and high information fields and
62   // only allows categories, event names, and arguments listed in
63   // `services/tracing/perfetto/privacy_filtered_fields-inl.h`
64   void StartTrace(std::string_view category_filter_string,
65                   bool privacy_filtering = false);
66   void StartTrace(
67       const TraceConfig& config,
68       perfetto::BackendType backend = perfetto::kUnspecifiedBackend);
69 
70   absl::Status StopAndParseTrace();
71 
72   base::expected<QueryResult, std::string> RunQuery(const std::string& query);
73 
74  private:
75   TestTraceProcessorImpl test_trace_processor_;
76   std::unique_ptr<perfetto::TracingSession> session_;
77 };
78 
79 #endif  // BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)
80 
81 }  // namespace base::test
82 
83 #endif  // BASE_TEST_TEST_TRACE_PROCESSOR_H_
84