Rasika Navarange | f422f0d | 2023-03-06 11:32:29 | [diff] [blame] | 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 | |
Helmut Januschka | 6c2e1601 | 2024-03-12 03:04:29 | [diff] [blame] | 14 | #include <string_view> |
| 15 | |
Rasika Navarange | b7d71bc | 2023-05-15 14:16:31 | [diff] [blame] | 16 | #include "base/test/test_trace_processor_impl.h" |
| 17 | #include "base/test/trace_test_utils.h" |
| 18 | #include "base/types/expected.h" |
Alexander Timin | 4de8959 | 2023-11-24 18:11:28 | [diff] [blame] | 19 | #include "build/build_config.h" |
| 20 | |
Mikhail Khokhlov | 1931792 | 2024-05-02 09:37:28 | [diff] [blame] | 21 | #if !BUILDFLAG(IS_WIN) |
Alexander Timin | 4de8959 | 2023-11-24 18:11:28 | [diff] [blame] | 22 | #define TEST_TRACE_PROCESSOR_ENABLED |
| 23 | #endif |
Rasika Navarange | f422f0d | 2023-03-06 11:32:29 | [diff] [blame] | 24 | |
Rasika Navarange | f422f0d | 2023-03-06 11:32:29 | [diff] [blame] | 25 | namespace base::test { |
| 26 | |
Rasika Navarange | db9b360 | 2023-05-30 13:18:41 | [diff] [blame] | 27 | |
Mikhail Khokhlov | 1641b80 | 2023-06-27 08:41:12 | [diff] [blame] | 28 | using perfetto::protos::gen::TraceConfig; |
| 29 | |
Helmut Januschka | 6c2e1601 | 2024-03-12 03:04:29 | [diff] [blame] | 30 | TraceConfig DefaultTraceConfig(std::string_view category_filter_string, |
Mikhail Khokhlov | 1641b80 | 2023-06-27 08:41:12 | [diff] [blame] | 31 | bool privacy_filtering); |
| 32 | |
Rasika Navarange | d10c6a1 | 2023-06-23 10:42:26 | [diff] [blame] | 33 | // Use TestTraceProcessor to record Perfetto traces in unit and browser tests. |
| 34 | // This API can be used to start and stop traces, run SQL queries on the trace |
| 35 | // and write expectations against the query result. |
| 36 | // |
| 37 | // Example: |
| 38 | // |
| 39 | // TestTraceProcessor test_trace_processor; |
| 40 | // test_trace_processor.StartTrace(); |
| 41 | // |
| 42 | // /* do stuff */ |
| 43 | // |
| 44 | // absl::Status status = test_trace_processor.StopAndParseTrace(); |
| 45 | // ASSERT_TRUE(status.ok()) << status.message(); |
| 46 | // |
| 47 | // std::string query = "YOUR QUERY"; |
| 48 | // auto result = test_trace_processor.RunQuery(query); |
| 49 | // |
| 50 | // ASSERT_TRUE(result.has_value()) << result.message(); |
| 51 | // EXPECT_THAT(result.value(), /* your expectations */); |
Rasika Navarange | f422f0d | 2023-03-06 11:32:29 | [diff] [blame] | 52 | |
Rasika Navarange | d10c6a1 | 2023-06-23 10:42:26 | [diff] [blame] | 53 | class TestTraceProcessor { |
| 54 | public: |
| 55 | using QueryResult = std::vector<std::vector<std::string>>; |
Rasika Navarange | f422f0d | 2023-03-06 11:32:29 | [diff] [blame] | 56 | |
Rasika Navarange | d10c6a1 | 2023-06-23 10:42:26 | [diff] [blame] | 57 | TestTraceProcessor(); |
| 58 | ~TestTraceProcessor(); |
| 59 | |
Rasika Navarange | 28575ae | 2024-03-28 13:30:57 | [diff] [blame] | 60 | // Privacy filtering removes high entropy and high information fields and |
| 61 | // only allows categories, event names, and arguments listed in |
| 62 | // `services/tracing/perfetto/privacy_filtered_fields-inl.h` |
Helmut Januschka | 6c2e1601 | 2024-03-12 03:04:29 | [diff] [blame] | 63 | void StartTrace(std::string_view category_filter_string, |
Mikhail Khokhlov | 1641b80 | 2023-06-27 08:41:12 | [diff] [blame] | 64 | bool privacy_filtering = false); |
| 65 | void StartTrace( |
| 66 | const TraceConfig& config, |
| 67 | perfetto::BackendType backend = perfetto::kUnspecifiedBackend); |
Rasika Navarange | d10c6a1 | 2023-06-23 10:42:26 | [diff] [blame] | 68 | |
| 69 | absl::Status StopAndParseTrace(); |
| 70 | |
| 71 | base::expected<QueryResult, std::string> RunQuery(const std::string& query); |
| 72 | |
| 73 | private: |
| 74 | TestTraceProcessorImpl test_trace_processor_; |
| 75 | std::unique_ptr<perfetto::TracingSession> session_; |
| 76 | }; |
| 77 | |
Rasika Navarange | f422f0d | 2023-03-06 11:32:29 | [diff] [blame] | 78 | |
| 79 | } // namespace base::test |
| 80 | |
| 81 | #endif // BASE_TEST_TEST_TRACE_PROCESSOR_H_ |