1# Running tests 2 3The testing strategy for Perfetto is rather complex due to the wide variety 4of build configurations and embedding targets. 5 6Common test targets (all platforms / checkouts): 7 8`perfetto_unittests`: 9Platform-agnostic unit-tests. 10 11`perfetto_integrationtests`: 12End-to-end tests, involving the protobuf-based IPC transport and ftrace 13integration (Linux/Android only). 14 15`perfetto_benchmarks`: 16Benchmarks tracking the performance of: (i) trace writing, (ii) trace readback 17and (iii) ftrace raw pipe -> protobuf translation. 18 19Running tests on Linux / MacOS 20------------------------------ 21 22```bash 23tools/ninja -C out/default perfetto_{unittests,integrationtests,benchmarks} 24out/default/perfetto_unittests --gtest_help 25``` 26 27`perfetto_integrationtests` requires that the ftrace debugfs directory is 28is readable/writable by the current user on Linux: 29```bash 30sudo chown -R $USER /sys/kernel/debug/tracing 31``` 32 33Running tests on Android 34------------------------ 351A) Connect a device through `adb` 361B) Start the build-in emulator (supported on Linux and MacOS): 37 38```bash 39tools/install-build-deps --android 40tools/run_android_emulator & 41``` 42 432) Run the tests (either on the emulator or physical device): 44 45```bash 46tools/run_android_test out/default perfetto_unittests 47``` 48 49Continuous testing 50------------------ 51Perfetto is tested in a variety of locations: 52 53**Perfetto CI**: https://ci.perfetto.dev/ 54Builds and runs perfetto_{unittests,integrationtests,benchmarks} from the 55standalone checkout. Benchmarks are ran in a reduced form for smoke testing. 56See [this doc](/docs/design-docs/continuous-integration.md) for more details. 57 58**Android CI** (see go/apct and go/apct-guide): 59runs only `perfetto_integrationtests` 60 61**Android presubmits (TreeHugger)**: 62Runs before submission of every AOSP CL of `external/perfetto`. 63 64**Android CTS** (Android test suite used run to ensure API compatibility): 65Rolling runs internally. 66 67Note that Perfetto CI uses the standalone build system and the others build as 68part of the Android tree. 69 70Unit tests 71---------- 72Unit tests exist for most of the code in Perfetto on the class level. They 73ensure that each class broadly works as expected. 74 75Unit tests are currently ran on ci.perfetto.dev and build.chromium.org. 76Running unit tests on APCT and Treehugger is WIP. 77 78Integration tests 79----------------- 80Integration tests ensure that subsystems (importantly ftrace and the IPC layer) 81and Perfetto as a whole is working correctly end-to-end. 82 83There are two configurations in which integration tests can be run: 84 85**1. Production mode** (Android-only) 86This mode assumes that both the tracing service (`traced`) and the OS probes 87service (`traced_probes`) are already running. In this mode the test enables 88only the consumer endpoint and tests the interaction with the production 89services. This is the way our Android CTS and APCT tests work. 90 91**2. Standalone mode**: 92Starting up the daemons in the test itself and then testing against them. 93This is how standalone builds are tested. This is the only supported way to 94run integration tests on Linux and MacOS. 95 96Trace Processor diff tests 97----------------- 98Trace processor is mainly tested using so called "diff tests". 99 100For these tests, trace processor parses a known trace and executes a query 101file. The output of these queries is then compared (i.e. "diff"ed) against 102an expected output file and discrepencies are highlighted. 103 104Similar diff tests are also available when writing metrics - instead of a 105query file, the metric name is used and the expected output file contains 106the expected result of computing the metric. 107 108These tests (for both queries and metrics) can be run as follows: 109```bash 110tools/ninja -C <out directory> 111tools/diff_test_trace_processor.py <out directory>/trace_processor_shell 112``` 113 114To add a new diff test (for query tests only), the `tools/add_tp_diff_test.py` 115script is useful. It will automatically create the query and expected output 116files and adds them both to the index. For metrics, this has to be done 117manually for now. 118 119TIP: Query diff tests are expected to only have a single query which produces 120output in the whole file (usually at the end). Calling 121`SELECT RUN_METRIC('metric file')` can trip up this check as this query 122generates some hidden output. To address this issue, if a query only has 123column is named `supress_query_output`, even if it has output, this will 124be ignored (for example, 125`SELECT RUN_METRIC('metric file') as surpress_query_output`) 126 127Android CTS tests 128----------------- 129CTS tests ensure that any vendors who modify Android remain compliant with the 130platform API. 131 132These tests include a subset of the integration tests above as well as adding 133more complex tests which ensure interaction between platform (e.g. Android apps 134etc.) and Perfetto is not broken. 135 136The relevant targets are `CtsPerfettoProducerApp` and `CtsPerfettoTestCases`. Once these are built, the following commands should be run: 137 138```bash 139adb push $ANDROID_HOST_OUT/cts/android-cts/testcases/CtsPerfettoTestCases64 /data/local/tmp/ 140adb install -r $ANDROID_HOST_OUT/cts/android-cts/testcases/CtsPerfettoProducerApp.apk 141``` 142 143Next, the app named `android.perfetto.producer` should be run on the device. 144 145Finally, the following command should be run: 146 147```bash 148adb shell /data/local/tmp/CtsPerfettoTestCases64 149``` 150 151Chromium waterfall 152------------------ 153Perfetto is constantly rolled into chromium's //third_party/perfetto via 154[this autoroller](https://autoroll.skia.org/r/perfetto-chromium-autoroll). 155 156The [Chromium CI](https://build.chromium.org) runs the `perfetto_unittests` 157target, as defined in the [buildbot config][chromium_buildbot]. 158 159[chromium_buildbot]: https://cs.chromium.org/search/?q=perfetto_.*tests+f:%5Esrc/testing.*json$&sq=package:chromium&type=cs 160