Commit b09b775c authored by Xiyuan Xia's avatar Xiyuan Xia Committed by Commit Bot

Add API to collect ui::ThroughputTracker reported data

Add StartDataCollection/StopDataCollection to start/stop gathering
the data reported by ui::ThroughputTracker. The API will be used
by tast tests via AutotestPrivateAPI to calculate animation frame
rate sub score.

Bug: 1098555
Change-Id: I1e871fd6575f54101bbe701d42960d5259b749e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2305013
Commit-Queue: Xiyuan Xia <xiyuan@chromium.org>
Reviewed-by: default avatarSadrul Chowdhury <sadrul@chromium.org>
Reviewed-by: default avatarJun Mukai <mukai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#792749}
parent 916c12f8
......@@ -340,6 +340,7 @@ source_set("unit_tests") {
"android_intent_helper_unittest.cc",
"app_list/app_list_config_provider_unittest.cc",
"default_scale_factor_retriever_unittest.cc",
"metrics_util_unittest.cc",
"pagination/pagination_model_unittest.cc",
"power_utils_unittest.cc",
"rounded_corner_decorator_unittest.cc",
......
......@@ -5,12 +5,33 @@
#include "ash/public/cpp/metrics_util.h"
#include "base/bind.h"
#include "base/check.h"
#include "base/no_destructor.h"
namespace ash {
namespace metrics_util {
namespace {
bool g_data_collection_enabled = false;
std::vector<cc::FrameSequenceMetrics::ThroughputData>& GetDataCollector() {
static base::NoDestructor<
std::vector<cc::FrameSequenceMetrics::ThroughputData>>
data;
return *data;
}
void CollectDataAndForwardReport(
ReportCallback callback,
const cc::FrameSequenceMetrics::ThroughputData throughput) {
// An arbitrary cap on the maximum number of animations being collected.
DCHECK_LT(GetDataCollector().size(), 1000u);
GetDataCollector().push_back(throughput);
std::move(callback).Run(throughput);
}
// Calculates smoothness from |throughput| and sends to |callback|.
void ForwardSmoothness(SmoothnessCallback callback,
cc::FrameSequenceMetrics::ThroughputData throughput) {
......@@ -21,8 +42,29 @@ void ForwardSmoothness(SmoothnessCallback callback,
} // namespace
ReportCallback ForSmoothness(SmoothnessCallback callback) {
return base::BindRepeating(&ForwardSmoothness, std::move(callback));
ReportCallback ForSmoothness(SmoothnessCallback callback,
bool exclude_from_data_collection) {
auto forward_smoothness =
base::BindRepeating(&ForwardSmoothness, std::move(callback));
if (!g_data_collection_enabled || exclude_from_data_collection)
return forward_smoothness;
return base::BindRepeating(&CollectDataAndForwardReport,
std::move(forward_smoothness));
}
void StartDataCollection() {
DCHECK(!g_data_collection_enabled);
g_data_collection_enabled = true;
}
std::vector<cc::FrameSequenceMetrics::ThroughputData> StopDataCollection() {
DCHECK(g_data_collection_enabled);
g_data_collection_enabled = false;
std::vector<cc::FrameSequenceMetrics::ThroughputData> data;
data.swap(GetDataCollector());
return data;
}
} // namespace metrics_util
......
......@@ -5,6 +5,8 @@
#ifndef ASH_PUBLIC_CPP_METRICS_UTIL_H_
#define ASH_PUBLIC_CPP_METRICS_UTIL_H_
#include <vector>
#include "ash/public/cpp/ash_public_export.h"
#include "base/callback.h"
#include "cc/metrics/frame_sequence_metrics.h"
......@@ -20,7 +22,17 @@ using SmoothnessCallback = base::RepeatingCallback<void(int smoothness)>;
// or ui::AnimationThroughputReporter. The returned callback picks up the
// cc::FrameSequenceMetrics::ThroughputData, calculates the smoothness
// out of it and forward it to the smoothness report callback.
ASH_PUBLIC_EXPORT ReportCallback ForSmoothness(SmoothnessCallback callback);
ASH_PUBLIC_EXPORT ReportCallback
ForSmoothness(SmoothnessCallback callback,
bool exclude_from_data_collection = false);
// Starts to collect data reported by all trackers unless they opt out.
// Note this DCHECKs if called again without StopDataCollection().
ASH_PUBLIC_EXPORT void StartDataCollection();
// Stops data collection and returns the data collected since starting.
ASH_PUBLIC_EXPORT std::vector<cc::FrameSequenceMetrics::ThroughputData>
StopDataCollection();
} // namespace metrics_util
} // namespace ash
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/public/cpp/metrics_util.h"
#include "base/optional.h"
#include "base/test/bind_test_util.h"
#include "cc/metrics/frame_sequence_metrics.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace ash {
namespace metrics_util {
TEST(MetricsUtilTest, ReportSmoothness) {
cc::FrameSequenceMetrics::ThroughputData throughput_data;
throughput_data.frames_produced = 30;
throughput_data.frames_expected = 60;
constexpr int kExpectedSmoothes = 50;
base::Optional<int> reported_smoothness;
SmoothnessCallback smoothness_callback = base::BindLambdaForTesting(
[&](int smoothess) { reported_smoothness = smoothess; });
struct {
bool enable_data_collection;
bool exclude_from_collection;
bool expected_has_collected_data;
} kTestCases[] = {
{/*enable_data_collection=*/false, /*exclude_from_collection=*/false,
/*expected_has_collected_data=*/false},
{/*enable_data_collection=*/true, /*exclude_from_collection=*/false,
/*expected_has_collected_data=*/true},
{/*enable_data_collection=*/true, /*exclude_from_collection=*/true,
/*expected_has_collected_data=*/false},
};
for (auto& test : kTestCases) {
SCOPED_TRACE(testing::Message()
<< "enable_data_collection=" << test.enable_data_collection
<< ", exclude_from_collection=" << test.exclude_from_collection
<< ", expected_has_collected_data="
<< test.expected_has_collected_data);
if (test.enable_data_collection)
StartDataCollection();
reported_smoothness.reset();
ReportCallback report_callback =
ForSmoothness(smoothness_callback, test.exclude_from_collection);
report_callback.Run(throughput_data);
ASSERT_TRUE(reported_smoothness.has_value());
EXPECT_EQ(kExpectedSmoothes, reported_smoothness.value());
if (test.enable_data_collection) {
auto collected_data = StopDataCollection();
EXPECT_EQ(test.expected_has_collected_data, !collected_data.empty());
}
}
}
} // namespace metrics_util
} // namespace ash
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment