# Graphic metrics in Tast CUJ
Tast is an end-to-end testing framework on ChromeOS written in Go language.
Tast runs on real devices with a certain scenario specified by the test author. Since it runs on real devices, it can also be utilized to investigate performance.
CUJ tests such as [DesksCUJ](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/tast-tests/src/go.chromium.org/tast-tests/cros/local/bundles/cros/ui/desks_cuj.go), [DocsCUJ](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/tast-tests/src/go.chromium.org/tast-tests/cros/local/bundles/cros/ui/docs_cuj.go), [MeetCUJ](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/tast-tests/src/go.chromium.org/tast-tests/cros/local/bundles/cros/ui/meet_cuj.go) are run to obtain performance results from various aspects.
One of the most common metrics are graphic feature.
Let's see where the results coming from.
## DisplayFrameData on tast
From various metrics, we focus on JankCount.
JankCount is a total number of dropped frame.
[DisplayFrameData](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/tast-tests/src/go.chromium.org/tast-tests/cros/local/perf/display_smoothness_tracker.go;l=22-29;drc=d98fa067ee99d562c7240e8632ce316693149dc8) is a container of the collected frame data including JankCount.
On starting [FrameDataTracker](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/tast-tests/src/go.chromium.org/tast-tests/cros/local/perf/frame_data_tracker.go;l=54;drc=d98fa067ee99d562c7240e8632ce316693149dc8), it starts collecting the animation data.
Tast test communicates via autotest private API [chrome.autotestPrivate.startThroughputTrackerDataCollection](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/tast-tests/src/go.chromium.org/tast-tests/cros/local/perf/frame_data_tracker.go;l=68;drc=d98fa067ee99d562c7240e8632ce316693149dc8) to trigger the data collection.
Then, it collects the data for each frameDataFetchInterval to [`data`](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/tast-tests/src/go.chromium.org/tast-tests/cros/local/perf/frame_data_tracker.go;l=94;drc=d98fa067ee99d562c7240e8632ce316693149dc8).
At the end of the test, [FrameDataTracker::Stop](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/tast-tests/src/go.chromium.org/tast-tests/cros/local/perf/frame_data_tracker.go;l=135;drc=d98fa067ee99d562c7240e8632ce316693149dc8) will stop data collection via chrome.autotestPrivate.stopThroughputTrackerDataCollection.
As you can see, the detailed impelmentation on how the data is collected is on Chromium side, so now we move on to Chromium code.
## autotest private API
The private api called [AutotestPrivateAPI](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/extensions/autotest_private/autotest_private_api.h;l=1562;drc=3a9b15c81d04feb7f9e926137eabcd921243a91e) can be used from Tast to interact with Chromium implementations.
[AutotestPrivateStartThroughputTrackerDataCollectionFunction](https://source.chromium.org/chromium/chromium/src/+/main:chrome/browser/ash/extensions/autotest_private/autotest_private_api.h;l=1562;drc=3a9b15c81d04feb7f9e926137eabcd921243a91e) is an implementation to trigger data tracker for DisplayFrameData.
In this function, [StartDataCollection](https://source.chromium.org/chromium/chromium/src/+/main:ash/public/cpp/metrics_util.cc;l=116;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2) is called to flip [`g_data_collection_enabled`](https://source.chromium.org/chromium/chromium/src/+/main:ash/public/cpp/metrics_util.cc;l=21;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2) flag.
If this flag is true, it collects [AnimationData](https://source.chromium.org/chromium/chromium/src/+/main:ash/public/cpp/metrics_util.h;l=23;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2) via [StopDataCollection](https://source.chromium.org/chromium/chromium/src/+/main:ash/public/cpp/metrics_util.cc;l=121;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2).
The collected data is stored as a singleton object accessable from [GeteDataCollector](https://source.chromium.org/chromium/chromium/src/+/main:ash/public/cpp/metrics_util.cc;l=23;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2) using base::NoDestructor + static technique.
[AnimationData](https://source.chromium.org/chromium/chromium/src/+/main:ash/public/cpp/metrics_util.h;l=23;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2) stores 3 values.
2 are to store when it started and ended.
The last one is important which is [FrameSequenceMetrics:::CustomReportData](https://source.chromium.org/chromium/chromium/src/+/main:cc/metrics/frame_sequence_metrics.h;l=105;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2).
From here, they are on cc side.
## cc metrics
Here are the contents of [FrameSequenceMetrics:::CustomReportData](https://source.chromium.org/chromium/chromium/src/+/main:cc/metrics/frame_sequence_metrics.h;l=105;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2).
```cpp=
struct CustomReportData {
uint32_t frames_expected = 0;
uint32_t frames_produced = 0;
int jank_count = 0;
uint32_t frames_expected_v3 = 0;
uint32_t frames_dropped_v3 = 0;
uint32_t jank_count_v3 = 0;
};
```
As for the jank results, [`jank_reporter_`](https://source.chromium.org/chromium/chromium/src/+/main:cc/metrics/frame_sequence_metrics.h;l=222;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2) is responsible for reporting the results.
[JankMetrics](https://source.chromium.org/chromium/chromium/src/+/main:cc/metrics/jank_metrics.h;l=26;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2) reports 3 sets of metrics related to janks:
- Graphics.Smoothness.Jank.*
- Graphics.Smoothness.Stale.*
- Graphics.Smoothness.MaxStale*
On [DestroyTrackers](https://source.chromium.org/chromium/chromium/src/+/main:cc/metrics/frame_sequence_tracker_collection.cc;l=301;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2), it [Merge](https://source.chromium.org/chromium/chromium/src/+/main:cc/metrics/frame_sequence_tracker_collection.cc;l=321;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2) the results obtained from trackers.
For FrameDequenceMetrics::Merge, it collects data by adds results for multiple metrics and stored them to `v3_` including [`jank_count`](https://source.chromium.org/chromium/chromium/src/+/main:cc/metrics/frame_sequence_metrics.cc;l=214;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2).
These histogram names are defined in [here](https://source.chromium.org/chromium/chromium/src/+/main:cc/metrics/frame_sequence_metrics.cc;l=102-128;drc=d4a7d3fb6f5100019d6153d5cf00c60f06b1d0a2).
The parameter JankCount is [converted from `jank_count`](https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:gen/arm-generic/chroot/var/cache/chromeos-chrome/chrome-src/src/out_arm-generic/Release/gen/chrome/common/extensions/api/autotest_private.cc;l=4657;drc=436aba9b8bbbff597ab40541d67af1528f158b33).