Name Date Size #Lines LOC

..--

data/H25-Apr-2025-109

trace_processor/diff_tests/H25-Apr-2025-854726

READMEH A D25-Apr-20253.9 KiB7547

run_perfetto_diff_tests.pyH A D25-Apr-20251.9 KiB5643

test_data.pyH A D25-Apr-20251.6 KiB4835

README

1# PerfettoSQL Chrome Standard Library tests
2
3This directory contains the [Perfetto Diff Tests](https://perfetto.dev/docs/analysis/trace-processor#diff-tests) to test changes to the Chrome standard library.
4
5The diff tests themselves are in `./trace_processor/diff_tests/chrome`. The `./data` directory contains the Perfetto traces that are used by the diff tests. As well as testing the functionality of your metric, the diff tests help to ensure that the stdlib remains backwards compatible with existing traces recorded from older Chrome versions.
6
7## Running Diff Tests
8
9Currently, the diff tests only run on Linux. You can build and run the diff tests with the following.
10
11```
12$ gn gen --args='' out/Linux
13$ autoninja -C out/Linux perfetto_diff_tests
14$ out/Linux/bin/run_perfetto_diff_tests
15```
16
17To run specific diff tests you can specify the `--name-filter` flag on the `run_perfetto_diff_tests` script with regex to filter which tests you want to run.
18
19## Adding a New Diff Test
20
21Your new diff test should go in `base/tracing/test/trace_processor/diff_tests/chrome`. You can either add to an existing TestSuite in one of the files or add a new test in a new file.
22
23If you are adding a **new TestSuite**, be sure to add it to `include_index.py` so the runner knows to run this new TestSuite.
24
25If your test requires modifying or adding new test data i.e. a new trace in `base/tracing/test/data`, you will need to upload this to the GCS bucket. These trace files are too large to be checked-in to the codebase so we check-in only `.sha256` files. You can upload any new traces with this script:
26
27```
28$ base/tracing/test/test_data.py upload --verbose
29```
30
31This script will upload your file and generate the `.sha256` file in the `base/tracing/test/data/` directory. You can then upload this file with your CL.
32
33## Writing TestTraceProcessor Tests
34
35See [test_trace_processor_example_unittests.cc](../../test/test_trace_processor_example_unittest.cc) for examples you can compile and run.
36
37You can write unit or browser tests with the TestTraceProcessor to record a trace, run a query on it and write expectations against the result.
38
39Instructions:
40
411. As of 22nd March 2024 all platforms use the Perfetto client library. However, if you do encounter compiler errors you may need to put your test behind the `BUILDFLAG(USE_PERFETTO_CLIENT_LIBRARY)`. [TODO(crbug/42050015): remove after fully launching Perfetto Client Library]
42
432. You need to add a `base::test::TracingEnvironment` as a member in your test class to handle the setup and teardown between tests. You also need a `base::test::TaskEnvironment` which is needed for starting/stopping tracing.
44
453. Record a trace:
46```
47TestTraceProcessor test_trace_processor;
48test_trace_processor.StartTrace(/* category_filter_string */);
49
50/* do stuff */
51
52absl::Status status = test_trace_processor.StopAndParseTrace();
53ASSERT_TRUE(status.ok()) << status.message();
54```
55
564. Run your query:
57```
58auto result = test_trace_processor.RunQuery(/* your query */);
59ASSERT_TRUE(result.has_value()) << result.message();
60```
61
625. Write expectations against the output:
63```
64EXPECT_THAT(result.value(), /* your expectations */);
65```
66
67The output format is a 2D vector of strings `std::vector<std::vector<std::string>>` where each vector is an SQLite row you would see when querying from the Perfetto UI. The first row will contain the header names for the columns.
68
69#### Best Practices
70
71* Use `ORDER BY` in queries so that the results are deterministic.
72
73* Note that the some data is not stable over long time, in particular ids generated by trace processor, which can change for the same trace is the trace processor under-the-hood parsing logic changes. Slice ids, utids and upids are the most common examples of this.
74
75* In general, it's recommended for tests to focus on the relationships between events, e.g. checking that you find the correct event when filtering by specific id and that its name is as expected, rather than checking specific id values.