File: test_trace_processor.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (86 lines) | stat: -rw-r--r-- 2,789 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Use TestTraceProcessor to load a perfetto trace and run queries on the trace.
// Documentation on how to use the trace processor and write queries can be
// found here: https://perfetto.dev/docs/analysis/trace-processor.
// TODO(b/224531105): Implement EXTRACT_ARGS to return multiple args to simplify
// queries.

#ifndef BASE_TEST_TEST_TRACE_PROCESSOR_H_
#define BASE_TEST_TEST_TRACE_PROCESSOR_H_

#include <ostream>
#include <string_view>

#include "base/run_loop.h"
#include "base/test/test_trace_processor_impl.h"
#include "base/test/trace_test_utils.h"
#include "base/types/expected.h"
#include "build/build_config.h"
#include "third_party/perfetto/include/perfetto/tracing/tracing.h"

#if !BUILDFLAG(IS_WIN)
#define TEST_TRACE_PROCESSOR_ENABLED
#endif

namespace base::test {

using perfetto::protos::gen::TraceConfig;

TraceConfig DefaultTraceConfig(std::string_view category_filter_string,
                               bool privacy_filtering);

// Use TestTraceProcessor to record Perfetto traces in unit and browser tests.
// This API can be used to start and stop traces, run SQL queries on the trace
// and write expectations against the query result.
//
// Example:
//
//   TestTraceProcessor test_trace_processor;
//   test_trace_processor.StartTrace();
//
//   /* do stuff */
//
//   absl::Status status = test_trace_processor.StopAndParseTrace();
//   ASSERT_TRUE(status.ok()) << status.message();
//
//   std::string query = "YOUR QUERY";
//   auto result = test_trace_processor.RunQuery(query);
//
//   ASSERT_TRUE(result.has_value()) << result.message();
//   EXPECT_THAT(result.value(), /* your expectations */);

class TestTraceProcessor {
 public:
  using QueryResult = std::vector<std::vector<std::string>>;

  TestTraceProcessor();
  ~TestTraceProcessor();

  // Privacy filtering removes high entropy and high information fields and
  // only allows categories, event names, and arguments listed in
  // `services/tracing/perfetto/privacy_filtered_fields-inl.h`
  void StartTrace(std::string_view category_filter_string,
                  bool privacy_filtering = false);
  void StartTrace(
      const TraceConfig& config,
      perfetto::BackendType backend = perfetto::kUnspecifiedBackend);

  absl::Status StopAndParseTrace();

  base::expected<QueryResult, std::string> RunQuery(const std::string& query);

 private:
  TestTraceProcessorImpl test_trace_processor_;
  std::unique_ptr<perfetto::TracingSession> session_;
};

}  // namespace base::test

std::ostream& operator<<(
    std::ostream& out,
    const base::test::TestTraceProcessor::QueryResult& result);

#endif  // BASE_TEST_TEST_TRACE_PROCESSOR_H_