Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 1 | // 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 | |
| 5 | #include "base/time/time.h" |
| 6 | #include "cc/resources/tile.h" |
| 7 | #include "cc/resources/tile_priority.h" |
| 8 | #include "cc/test/fake_output_surface.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 9 | #include "cc/test/fake_picture_pile_impl.h" |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 10 | #include "cc/test/fake_tile_manager.h" |
| 11 | #include "cc/test/fake_tile_manager_client.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 12 | #include "cc/test/test_tile_priorities.h" |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 13 | |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace cc { |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | static const int kTimeLimitMillis = 2000; |
| 21 | static const int kWarmupRuns = 5; |
| 22 | static const int kTimeCheckInterval = 10; |
| 23 | |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 24 | class TileManagerPerfTest : public testing::Test { |
| 25 | public: |
| 26 | typedef std::vector<scoped_refptr<Tile> > TileVector; |
| 27 | |
| 28 | TileManagerPerfTest() : num_runs_(0) {} |
| 29 | |
| 30 | // Overridden from testing::Test: |
| 31 | virtual void SetUp() OVERRIDE { |
| 32 | output_surface_ = FakeOutputSurface::Create3d(); |
| 33 | resource_provider_ = ResourceProvider::Create(output_surface_.get(), 0); |
| 34 | tile_manager_ = make_scoped_ptr( |
| 35 | new FakeTileManager(&tile_manager_client_, resource_provider_.get())); |
| 36 | |
| 37 | GlobalStateThatImpactsTilePriority state; |
| 38 | gfx::Size tile_size = settings_.default_tile_size; |
| 39 | state.memory_limit_in_bytes = |
| 40 | 10000 * 4 * tile_size.width() * tile_size.height(); |
| 41 | state.memory_limit_policy = ALLOW_ANYTHING; |
| 42 | state.tree_priority = SMOOTHNESS_TAKES_PRIORITY; |
| 43 | |
| 44 | tile_manager_->SetGlobalState(state); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 45 | picture_pile_ = FakePicturePileImpl::CreatePile(); |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | virtual void TearDown() OVERRIDE { |
| 49 | tile_manager_.reset(NULL); |
| 50 | picture_pile_ = NULL; |
| 51 | } |
| 52 | |
| 53 | void EndTest() { |
| 54 | elapsed_ = base::TimeTicks::HighResNow() - start_time_; |
| 55 | } |
| 56 | |
| 57 | void AfterTest(const std::string test_name) { |
| 58 | // Format matches chrome/test/perf/perf_test.h:PrintResult |
| 59 | printf("*RESULT %s: %.2f runs/s\n", |
| 60 | test_name.c_str(), |
| 61 | num_runs_ / elapsed_.InSecondsF()); |
| 62 | } |
| 63 | |
| 64 | bool DidRun() { |
| 65 | ++num_runs_; |
| 66 | if (num_runs_ == kWarmupRuns) |
| 67 | start_time_ = base::TimeTicks::HighResNow(); |
| 68 | |
| 69 | if (!start_time_.is_null() && (num_runs_ % kTimeCheckInterval) == 0) { |
| 70 | base::TimeDelta elapsed = base::TimeTicks::HighResNow() - start_time_; |
| 71 | if (elapsed >= base::TimeDelta::FromMilliseconds(kTimeLimitMillis)) { |
| 72 | elapsed_ = elapsed; |
| 73 | return false; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void CreateBinTiles(int count, TilePriority priority, TileVector* tiles) { |
| 81 | for (int i = 0; i < count; ++i) { |
| 82 | scoped_refptr<Tile> tile = |
| 83 | make_scoped_refptr(new Tile(tile_manager_.get(), |
| 84 | picture_pile_.get(), |
| 85 | settings_.default_tile_size, |
| 86 | gfx::Rect(), |
| 87 | gfx::Rect(), |
| 88 | 1.0, |
| 89 | 0, |
| 90 | 0, |
| 91 | true)); |
| 92 | tile->SetPriority(ACTIVE_TREE, priority); |
| 93 | tile->SetPriority(PENDING_TREE, priority); |
| 94 | tiles->push_back(tile); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void CreateTiles(int count, TileVector* tiles) { |
| 99 | // Roughly an equal amount of all bins. |
| 100 | int count_per_bin = count / NUM_BINS; |
| 101 | CreateBinTiles(count_per_bin, TilePriorityForNowBin(), tiles); |
| 102 | CreateBinTiles(count_per_bin, TilePriorityForSoonBin(), tiles); |
| 103 | CreateBinTiles(count_per_bin, TilePriorityForEventualBin(), tiles); |
| 104 | CreateBinTiles(count - 3 * count_per_bin, TilePriority(), tiles); |
| 105 | } |
| 106 | |
| 107 | void RunManageTilesTest(const std::string test_name, |
| 108 | unsigned tile_count) { |
| 109 | start_time_ = base::TimeTicks(); |
| 110 | num_runs_ = 0; |
| 111 | TileVector tiles; |
| 112 | CreateTiles(tile_count, &tiles); |
| 113 | do { |
| 114 | tile_manager_->ManageTiles(); |
| 115 | } while (DidRun()); |
| 116 | |
| 117 | AfterTest(test_name); |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | FakeTileManagerClient tile_manager_client_; |
| 122 | LayerTreeSettings settings_; |
| 123 | scoped_ptr<FakeTileManager> tile_manager_; |
| 124 | scoped_refptr<FakePicturePileImpl> picture_pile_; |
| 125 | scoped_ptr<FakeOutputSurface> output_surface_; |
| 126 | scoped_ptr<ResourceProvider> resource_provider_; |
| 127 | |
| 128 | base::TimeTicks start_time_; |
| 129 | base::TimeDelta elapsed_; |
| 130 | int num_runs_; |
| 131 | }; |
| 132 | |
| 133 | TEST_F(TileManagerPerfTest, ManageTiles) { |
| 134 | RunManageTilesTest("manage_tiles_100", 100); |
| 135 | RunManageTilesTest("manage_tiles_1000", 1000); |
| 136 | RunManageTilesTest("manage_tiles_10000", 10000); |
| 137 | } |
| 138 | |
| 139 | } // namespace |
| 140 | |
| 141 | } // namespace cc |