blob: 30abcc4627f815ee8fd5def476228e1ef7453f40 [file] [log] [blame]
[email protected]666d7cf2013-10-12 01:30:291// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]666d7cf2013-10-12 01:30:295#include "cc/debug/micro_benchmark_controller.h"
danakj60bc3bc2016-04-09 00:24:486
7#include <memory>
8
9#include "base/callback.h"
10#include "base/memory/ptr_util.h"
fdoray56f645c2016-06-10 17:55:4911#include "base/run_loop.h"
danakj60bc3bc2016-04-09 00:24:4812#include "cc/debug/micro_benchmark.h"
[email protected]666d7cf2013-10-12 01:30:2913#include "cc/layers/layer.h"
khushalsagarb64b360d2015-10-21 19:25:1614#include "cc/test/fake_impl_task_runner_provider.h"
[email protected]666d7cf2013-10-12 01:30:2915#include "cc/test/fake_layer_tree_host.h"
[email protected]5e5648a2013-11-18 00:39:3316#include "cc/test/fake_layer_tree_host_impl.h"
[email protected]666d7cf2013-10-12 01:30:2917#include "cc/test/fake_proxy.h"
danakjcf610582015-06-16 22:48:5618#include "cc/test/test_task_graph_runner.h"
[email protected]666d7cf2013-10-12 01:30:2919#include "testing/gtest/include/gtest/gtest.h"
20
21namespace cc {
22namespace {
23
24class MicroBenchmarkControllerTest : public testing::Test {
25 public:
danakjaeb95062014-11-14 01:35:3626 void SetUp() override {
khushalsagarb64b360d2015-10-21 19:25:1627 impl_task_runner_provider_ =
danakj60bc3bc2016-04-09 00:24:4828 base::WrapUnique(new FakeImplTaskRunnerProvider);
29 layer_tree_host_impl_ = base::WrapUnique(new FakeLayerTreeHostImpl(
khushalsagarb64b360d2015-10-21 19:25:1630 impl_task_runner_provider_.get(), &shared_bitmap_manager_,
31 &task_graph_runner_));
[email protected]5e5648a2013-11-18 00:39:3332
danakjcf610582015-06-16 22:48:5633 layer_tree_host_ = FakeLayerTreeHost::Create(&layer_tree_host_client_,
34 &task_graph_runner_);
loyso0940d412016-03-14 01:30:3135 layer_tree_host_->SetRootLayer(Layer::Create());
khushalsagarb7db1fe2015-11-12 00:51:2736 layer_tree_host_->InitializeForTesting(
37 TaskRunnerProvider::Create(nullptr, nullptr),
danakj60bc3bc2016-04-09 00:24:4838 std::unique_ptr<Proxy>(new FakeProxy), nullptr);
[email protected]5e5648a2013-11-18 00:39:3339 }
40
danakjaeb95062014-11-14 01:35:3641 void TearDown() override {
danakjf446a072014-09-27 21:55:4842 layer_tree_host_impl_ = nullptr;
43 layer_tree_host_ = nullptr;
khushalsagarb64b360d2015-10-21 19:25:1644 impl_task_runner_provider_ = nullptr;
[email protected]666d7cf2013-10-12 01:30:2945 }
46
enne2097cab2014-09-25 20:16:3147 FakeLayerTreeHostClient layer_tree_host_client_;
danakjcf610582015-06-16 22:48:5648 TestTaskGraphRunner task_graph_runner_;
49 TestSharedBitmapManager shared_bitmap_manager_;
danakj60bc3bc2016-04-09 00:24:4850 std::unique_ptr<FakeLayerTreeHost> layer_tree_host_;
51 std::unique_ptr<FakeLayerTreeHostImpl> layer_tree_host_impl_;
52 std::unique_ptr<FakeImplTaskRunnerProvider> impl_task_runner_provider_;
[email protected]666d7cf2013-10-12 01:30:2953};
54
danakj60bc3bc2016-04-09 00:24:4855void Noop(std::unique_ptr<base::Value> value) {}
[email protected]666d7cf2013-10-12 01:30:2956
danakj60bc3bc2016-04-09 00:24:4857void IncrementCallCount(int* count, std::unique_ptr<base::Value> value) {
[email protected]666d7cf2013-10-12 01:30:2958 ++(*count);
59}
60
61TEST_F(MicroBenchmarkControllerTest, ScheduleFail) {
[email protected]67e703e12014-05-30 05:31:5162 int id = layer_tree_host_->ScheduleMicroBenchmark(
danakj968153f32014-10-15 22:52:1663 "non_existant_benchmark", nullptr, base::Bind(&Noop));
[email protected]67e703e12014-05-30 05:31:5164 EXPECT_EQ(id, 0);
[email protected]666d7cf2013-10-12 01:30:2965}
66
67TEST_F(MicroBenchmarkControllerTest, CommitScheduled) {
68 EXPECT_FALSE(layer_tree_host_->needs_commit());
[email protected]67e703e12014-05-30 05:31:5169 int id = layer_tree_host_->ScheduleMicroBenchmark(
danakj968153f32014-10-15 22:52:1670 "unittest_only_benchmark", nullptr, base::Bind(&Noop));
[email protected]67e703e12014-05-30 05:31:5171 EXPECT_GT(id, 0);
[email protected]666d7cf2013-10-12 01:30:2972 EXPECT_TRUE(layer_tree_host_->needs_commit());
73}
74
75TEST_F(MicroBenchmarkControllerTest, BenchmarkRan) {
76 int run_count = 0;
[email protected]67e703e12014-05-30 05:31:5177 int id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:2978 "unittest_only_benchmark",
danakj968153f32014-10-15 22:52:1679 nullptr,
[email protected]666d7cf2013-10-12 01:30:2980 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:5181 EXPECT_GT(id, 0);
[email protected]666d7cf2013-10-12 01:30:2982
[email protected]666d7cf2013-10-12 01:30:2983 layer_tree_host_->SetOutputSurfaceLostForTesting(false);
danakj5f46636a2015-06-19 00:01:4084 layer_tree_host_->UpdateLayers();
[email protected]666d7cf2013-10-12 01:30:2985
86 EXPECT_EQ(1, run_count);
87}
88
89TEST_F(MicroBenchmarkControllerTest, MultipleBenchmarkRan) {
90 int run_count = 0;
[email protected]67e703e12014-05-30 05:31:5191 int id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:2992 "unittest_only_benchmark",
danakj968153f32014-10-15 22:52:1693 nullptr,
[email protected]666d7cf2013-10-12 01:30:2994 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:5195 EXPECT_GT(id, 0);
96 id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:2997 "unittest_only_benchmark",
danakj968153f32014-10-15 22:52:1698 nullptr,
[email protected]666d7cf2013-10-12 01:30:2999 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:51100 EXPECT_GT(id, 0);
[email protected]666d7cf2013-10-12 01:30:29101
[email protected]666d7cf2013-10-12 01:30:29102 layer_tree_host_->SetOutputSurfaceLostForTesting(false);
danakj5f46636a2015-06-19 00:01:40103 layer_tree_host_->UpdateLayers();
[email protected]666d7cf2013-10-12 01:30:29104
105 EXPECT_EQ(2, run_count);
106
[email protected]67e703e12014-05-30 05:31:51107 id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:29108 "unittest_only_benchmark",
danakj968153f32014-10-15 22:52:16109 nullptr,
[email protected]666d7cf2013-10-12 01:30:29110 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:51111 EXPECT_GT(id, 0);
112 id = layer_tree_host_->ScheduleMicroBenchmark(
[email protected]666d7cf2013-10-12 01:30:29113 "unittest_only_benchmark",
danakj968153f32014-10-15 22:52:16114 nullptr,
[email protected]666d7cf2013-10-12 01:30:29115 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:51116 EXPECT_GT(id, 0);
[email protected]666d7cf2013-10-12 01:30:29117
danakj5f46636a2015-06-19 00:01:40118 layer_tree_host_->UpdateLayers();
[email protected]666d7cf2013-10-12 01:30:29119 EXPECT_EQ(4, run_count);
120
danakj5f46636a2015-06-19 00:01:40121 layer_tree_host_->UpdateLayers();
[email protected]666d7cf2013-10-12 01:30:29122 EXPECT_EQ(4, run_count);
123}
124
[email protected]5e5648a2013-11-18 00:39:33125TEST_F(MicroBenchmarkControllerTest, BenchmarkImplRan) {
126 int run_count = 0;
danakj60bc3bc2016-04-09 00:24:48127 std::unique_ptr<base::DictionaryValue> settings(new base::DictionaryValue);
[email protected]5e5648a2013-11-18 00:39:33128 settings->SetBoolean("run_benchmark_impl", true);
129
130 // Schedule a main thread benchmark.
[email protected]67e703e12014-05-30 05:31:51131 int id = layer_tree_host_->ScheduleMicroBenchmark(
danakj73187bb72015-11-18 20:29:16132 "unittest_only_benchmark", std::move(settings),
[email protected]5e5648a2013-11-18 00:39:33133 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
[email protected]67e703e12014-05-30 05:31:51134 EXPECT_GT(id, 0);
[email protected]5e5648a2013-11-18 00:39:33135
136 // Schedule impl benchmarks. In production code, this is run in commit.
137 layer_tree_host_->GetMicroBenchmarkController()->ScheduleImplBenchmarks(
138 layer_tree_host_impl_.get());
139
140 // Now complete the commit (as if on the impl thread).
141 layer_tree_host_impl_->CommitComplete();
142
143 // Make sure all posted messages run.
fdoray56f645c2016-06-10 17:55:49144 base::RunLoop().RunUntilIdle();
[email protected]5e5648a2013-11-18 00:39:33145
146 EXPECT_EQ(1, run_count);
147}
148
[email protected]67e703e12014-05-30 05:31:51149TEST_F(MicroBenchmarkControllerTest, SendMessage) {
150 // Send valid message to invalid benchmark (id = 0)
danakj60bc3bc2016-04-09 00:24:48151 std::unique_ptr<base::DictionaryValue> message(new base::DictionaryValue);
[email protected]67e703e12014-05-30 05:31:51152 message->SetBoolean("can_handle", true);
danakjf446a072014-09-27 21:55:48153 bool message_handled =
danakj73187bb72015-11-18 20:29:16154 layer_tree_host_->SendMessageToMicroBenchmark(0, std::move(message));
[email protected]67e703e12014-05-30 05:31:51155 EXPECT_FALSE(message_handled);
156
157 // Schedule a benchmark
158 int run_count = 0;
159 int id = layer_tree_host_->ScheduleMicroBenchmark(
160 "unittest_only_benchmark",
danakj968153f32014-10-15 22:52:16161 nullptr,
[email protected]67e703e12014-05-30 05:31:51162 base::Bind(&IncrementCallCount, base::Unretained(&run_count)));
163 EXPECT_GT(id, 0);
164
165 // Send valid message to valid benchmark
danakj60bc3bc2016-04-09 00:24:48166 message = base::WrapUnique(new base::DictionaryValue);
[email protected]67e703e12014-05-30 05:31:51167 message->SetBoolean("can_handle", true);
danakjf446a072014-09-27 21:55:48168 message_handled =
danakj73187bb72015-11-18 20:29:16169 layer_tree_host_->SendMessageToMicroBenchmark(id, std::move(message));
[email protected]67e703e12014-05-30 05:31:51170 EXPECT_TRUE(message_handled);
171
172 // Send invalid message to valid benchmark
danakj60bc3bc2016-04-09 00:24:48173 message = base::WrapUnique(new base::DictionaryValue);
[email protected]67e703e12014-05-30 05:31:51174 message->SetBoolean("can_handle", false);
danakjf446a072014-09-27 21:55:48175 message_handled =
danakj73187bb72015-11-18 20:29:16176 layer_tree_host_->SendMessageToMicroBenchmark(id, std::move(message));
[email protected]67e703e12014-05-30 05:31:51177 EXPECT_FALSE(message_handled);
178}
179
[email protected]666d7cf2013-10-12 01:30:29180} // namespace
181} // namespace cc