1*6777b538SAndroid Build Coastguard Worker# PerfettoSQL Chrome Standard Library tests 2*6777b538SAndroid Build Coastguard Worker 3*6777b538SAndroid Build Coastguard WorkerThis directory contains the [Perfetto Diff Tests](https://perfetto.dev/docs/analysis/trace-processor#diff-tests) to test changes to the Chrome standard library. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard WorkerThe 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*6777b538SAndroid Build Coastguard Worker 7*6777b538SAndroid Build Coastguard Worker## Running Diff Tests 8*6777b538SAndroid Build Coastguard Worker 9*6777b538SAndroid Build Coastguard WorkerCurrently, the diff tests only run on Linux. You can build and run the diff tests with the following. 10*6777b538SAndroid Build Coastguard Worker 11*6777b538SAndroid Build Coastguard Worker``` 12*6777b538SAndroid Build Coastguard Worker$ gn gen --args='' out/Linux 13*6777b538SAndroid Build Coastguard Worker$ autoninja -C out/Linux perfetto_diff_tests 14*6777b538SAndroid Build Coastguard Worker$ out/Linux/bin/run_perfetto_diff_tests 15*6777b538SAndroid Build Coastguard Worker``` 16*6777b538SAndroid Build Coastguard Worker 17*6777b538SAndroid Build Coastguard WorkerTo 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*6777b538SAndroid Build Coastguard Worker 19*6777b538SAndroid Build Coastguard Worker## Adding a New Diff Test 20*6777b538SAndroid Build Coastguard Worker 21*6777b538SAndroid Build Coastguard WorkerYour 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*6777b538SAndroid Build Coastguard Worker 23*6777b538SAndroid Build Coastguard WorkerIf 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*6777b538SAndroid Build Coastguard Worker 25*6777b538SAndroid Build Coastguard WorkerIf 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*6777b538SAndroid Build Coastguard Worker 27*6777b538SAndroid Build Coastguard Worker``` 28*6777b538SAndroid Build Coastguard Worker$ base/tracing/test/test_data.py upload --verbose 29*6777b538SAndroid Build Coastguard Worker``` 30*6777b538SAndroid Build Coastguard Worker 31*6777b538SAndroid Build Coastguard WorkerThis 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*6777b538SAndroid Build Coastguard Worker 33*6777b538SAndroid Build Coastguard Worker## Writing TestTraceProcessor Tests 34*6777b538SAndroid Build Coastguard Worker 35*6777b538SAndroid Build Coastguard WorkerSee [test_trace_processor_example_unittests.cc](../../test/test_trace_processor_example_unittest.cc) for examples you can compile and run. 36*6777b538SAndroid Build Coastguard Worker 37*6777b538SAndroid Build Coastguard WorkerYou 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*6777b538SAndroid Build Coastguard Worker 39*6777b538SAndroid Build Coastguard WorkerInstructions: 40*6777b538SAndroid Build Coastguard Worker 41*6777b538SAndroid Build Coastguard Worker1. 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*6777b538SAndroid Build Coastguard Worker 43*6777b538SAndroid Build Coastguard Worker2. 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*6777b538SAndroid Build Coastguard Worker 45*6777b538SAndroid Build Coastguard Worker3. Record a trace: 46*6777b538SAndroid Build Coastguard Worker``` 47*6777b538SAndroid Build Coastguard WorkerTestTraceProcessor test_trace_processor; 48*6777b538SAndroid Build Coastguard Workertest_trace_processor.StartTrace(/* category_filter_string */); 49*6777b538SAndroid Build Coastguard Worker 50*6777b538SAndroid Build Coastguard Worker/* do stuff */ 51*6777b538SAndroid Build Coastguard Worker 52*6777b538SAndroid Build Coastguard Workerabsl::Status status = test_trace_processor.StopAndParseTrace(); 53*6777b538SAndroid Build Coastguard WorkerASSERT_TRUE(status.ok()) << status.message(); 54*6777b538SAndroid Build Coastguard Worker``` 55*6777b538SAndroid Build Coastguard Worker 56*6777b538SAndroid Build Coastguard Worker4. Run your query: 57*6777b538SAndroid Build Coastguard Worker``` 58*6777b538SAndroid Build Coastguard Workerauto result = test_trace_processor.RunQuery(/* your query */); 59*6777b538SAndroid Build Coastguard WorkerASSERT_TRUE(result.has_value()) << result.message(); 60*6777b538SAndroid Build Coastguard Worker``` 61*6777b538SAndroid Build Coastguard Worker 62*6777b538SAndroid Build Coastguard Worker5. Write expectations against the output: 63*6777b538SAndroid Build Coastguard Worker``` 64*6777b538SAndroid Build Coastguard WorkerEXPECT_THAT(result.value(), /* your expectations */); 65*6777b538SAndroid Build Coastguard Worker``` 66*6777b538SAndroid Build Coastguard Worker 67*6777b538SAndroid Build Coastguard WorkerThe 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*6777b538SAndroid Build Coastguard Worker 69*6777b538SAndroid Build Coastguard Worker#### Best Practices 70*6777b538SAndroid Build Coastguard Worker 71*6777b538SAndroid Build Coastguard Worker* Use `ORDER BY` in queries so that the results are deterministic. 72*6777b538SAndroid Build Coastguard Worker 73*6777b538SAndroid Build Coastguard Worker* 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*6777b538SAndroid Build Coastguard Worker 75*6777b538SAndroid Build Coastguard Worker* 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.