Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 <limits> | |
| 6 #include <map> | |
| 7 #include <string> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "base/timer/mock_timer.h" | |
| 12 #include "chrome/browser/chromeos/resource_reporter/resource_reporter.h" | |
| 13 #include "chrome/browser/task_management/task_manager_test_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 using task_management::TaskId; | |
| 17 | |
| 18 namespace chromeos { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 const int64_t k1KB = 1024; | |
| 23 const int64_t k1MB = 1024 * 1024; | |
| 24 const int64_t k1GB = 1024 * 1024 * 1024; | |
| 25 | |
| 26 // A list of task records that we'll use to fill the task manager. | |
| 27 const ResourceReporter::TaskRecord kTestTasks[] = { | |
| 28 { 0, "0", 30.0, 43 * k1KB, false }, | |
| 29 { 1, "1", 9.0, 20 * k1MB, false }, | |
| 30 { 2, "2", 35.0, 3 * k1GB, false }, | |
| 31 { 3, "3", 21.0, 300 * k1MB, false }, | |
| 32 { 4, "4", 85.0, 400 * k1KB, false }, | |
| 33 { 5, "5", 30.1, 500 * k1MB, false }, | |
| 34 { 6, "6", 60.0, 900 * k1MB, false }, | |
| 35 { 7, "7", 4.0, 1 * k1GB, false }, | |
| 36 { 8, "8", 40.0, 64 * k1KB, false }, | |
| 37 { 9, "9", 93.0, 64 * k1MB, false }, | |
| 38 { 10, "10", 2.23, 2 * k1KB, false }, | |
| 39 { 11, "11", 55.0, 40 * k1MB, false }, | |
| 40 { 12, "12", 87.0, 30 * k1KB, false }, | |
| 41 }; | |
| 42 | |
| 43 // A list of task IDs that will be removed from the task manager later after all | |
| 44 // the above tasks have been added. | |
| 45 const task_management::TaskId kIdsOfTasksToRemove[] = { | |
| 46 4, 7, 12, 8, 5, | |
| 47 }; | |
| 48 | |
| 49 // Must be larger than |ResourceReporter::kTopConsumerCount|. | |
|
ncarter (slow)
2015/11/10 23:29:56
Instead of commenting about this, you can static_a
afakhry
2015/11/12 00:21:28
Actually we're already testing these assumptions a
| |
| 50 const size_t kTasksSize = arraysize(kTestTasks); | |
| 51 | |
| 52 // Must be smaller than |ResourceReporter::kTopConsumerCount|. | |
| 53 const size_t kInitiallyAddedTasks = kTasksSize - 6; | |
| 54 | |
| 55 // Must be such that |kTasksSize| - |kTasksToBeRemovedSize| is less than | |
| 56 // |ResourceReporter::kTopConsumerCount|. | |
| 57 const size_t kTasksToBeRemovedSize = arraysize(kIdsOfTasksToRemove); | |
| 58 | |
| 59 // A test implementation of the task manager that can be used to collect CPU and | |
| 60 // memory usage so that they can be tested with the resource reporter. | |
| 61 class DummyTaskManager : public task_management::TestTaskManager { | |
| 62 public: | |
| 63 DummyTaskManager() { | |
| 64 set_timer_for_testing( | |
| 65 scoped_ptr<base::Timer>(new base::MockTimer(false, false))); | |
| 66 } | |
| 67 ~DummyTaskManager() override {} | |
| 68 | |
| 69 // task_management::TestTaskManager: | |
| 70 double GetCpuUsage(TaskId task_id) const override { | |
| 71 return tasks_.at(task_id)->cpu; | |
| 72 } | |
| 73 int64 GetPhysicalMemoryUsage(TaskId task_id) const override { | |
| 74 return tasks_.at(task_id)->memory; | |
| 75 } | |
| 76 const std::string& GetRapporSampleName(TaskId task_id) const override { | |
| 77 return tasks_.at(task_id)->rappor_sample; | |
| 78 } | |
| 79 | |
| 80 void AddTaskFromIndex(size_t index) { | |
| 81 tasks_[kTestTasks[index].id] = &kTestTasks[index]; | |
| 82 } | |
| 83 | |
| 84 void RemoveTask(TaskId id) { | |
| 85 NotifyObserversOnTaskToBeRemoved(id); | |
| 86 | |
| 87 tasks_.erase(id); | |
| 88 } | |
| 89 | |
| 90 void ManualRefresh() { | |
| 91 ids_.clear(); | |
| 92 for (const auto& pair : tasks_) { | |
| 93 ids_.push_back(pair.first); | |
| 94 } | |
| 95 | |
| 96 NotifyObserversOnRefresh(ids_); | |
| 97 } | |
| 98 | |
| 99 private: | |
| 100 std::map<TaskId, const ResourceReporter::TaskRecord*> tasks_; | |
| 101 | |
| 102 DISALLOW_COPY_AND_ASSIGN(DummyTaskManager); | |
| 103 }; | |
| 104 | |
| 105 } // namespace | |
| 106 | |
| 107 class ResourceReporterTest : public testing::Test { | |
| 108 public: | |
| 109 ResourceReporterTest() {} | |
| 110 ~ResourceReporterTest() override {} | |
| 111 | |
| 112 // Start / Stop observing the task manager. | |
| 113 void Start() { | |
| 114 task_manager_.AddObserver(resource_reporter()); | |
| 115 } | |
| 116 void Stop() { | |
| 117 task_manager_.RemoveObserver(resource_reporter()); | |
| 118 } | |
| 119 | |
| 120 // Adds a number of tasks less than |kTopConsumersCount| to the task manager. | |
| 121 void AddInitialTasks() { | |
| 122 for (size_t i = 0; i < kInitiallyAddedTasks; ++i) | |
| 123 task_manager_.AddTaskFromIndex(i); | |
| 124 } | |
| 125 | |
| 126 // Adds all the remaining tasks to the task manager so that we have more than | |
| 127 // |kTopConsumersCount|. | |
| 128 void AddRemainingTasks() { | |
| 129 for (size_t i = kInitiallyAddedTasks; i < kTasksSize; ++i) | |
| 130 task_manager_.AddTaskFromIndex(i); | |
| 131 } | |
| 132 | |
| 133 // Remove the task with |id| from the task manager. | |
| 134 void RemoveTask(TaskId id) { | |
| 135 task_manager_.RemoveTask(id); | |
| 136 } | |
| 137 | |
| 138 // Manually refresh the task manager. | |
| 139 void RefreshTaskManager() { | |
| 140 task_manager_.ManualRefresh(); | |
| 141 } | |
| 142 | |
| 143 // Tests that the task records in |ResourceReporter::task_records_by_cpu_| are | |
| 144 // properly sorted by the CPU usage in a descending order. | |
| 145 bool IsCpuRecordsSetSorted() const { | |
| 146 double current_cpu = std::numeric_limits<double>::max(); | |
| 147 for (const auto& record : resource_reporter()->task_records_by_cpu_) { | |
| 148 if (record->cpu > current_cpu) | |
| 149 return false; | |
| 150 current_cpu = record->cpu; | |
| 151 } | |
| 152 | |
| 153 return true; | |
| 154 } | |
| 155 | |
| 156 // Tests that the task records in |ResourceReporter::task_records_by_memory_| | |
| 157 // are properly sorted by the memory usage in a descending order. | |
| 158 bool IsMemoryRecordsSetSorted() const { | |
| 159 int64_t current_memory = std::numeric_limits<int64_t>::max(); | |
| 160 for (const auto& record : resource_reporter()->task_records_by_memory_) { | |
| 161 if (record->memory > current_memory) | |
| 162 return false; | |
| 163 current_memory = record->memory; | |
| 164 } | |
| 165 | |
| 166 return true; | |
| 167 } | |
| 168 | |
| 169 ResourceReporter* resource_reporter() const { | |
| 170 return ResourceReporter::GetInstance(); | |
| 171 } | |
| 172 | |
| 173 private: | |
| 174 DummyTaskManager task_manager_; | |
| 175 | |
| 176 DISALLOW_COPY_AND_ASSIGN(ResourceReporterTest); | |
| 177 }; | |
| 178 | |
| 179 // Tests that ResourceReporter::GetCpuRapporMetricName() returns the correct | |
| 180 // metric name that corresponds to the given CPU usage. | |
| 181 TEST_F(ResourceReporterTest, TestGetCpuRapporMetricName) { | |
| 182 EXPECT_EQ(ResourceReporter::RANGE_0_TO_10_PERCENT, | |
| 183 ResourceReporter::GetCpuUsageRange(0.3)); | |
| 184 EXPECT_EQ(ResourceReporter::RANGE_0_TO_10_PERCENT, | |
| 185 ResourceReporter::GetCpuUsageRange(5.7)); | |
| 186 EXPECT_EQ(ResourceReporter::RANGE_0_TO_10_PERCENT, | |
| 187 ResourceReporter::GetCpuUsageRange(9.99)); | |
| 188 EXPECT_EQ(ResourceReporter::RANGE_0_TO_10_PERCENT, | |
| 189 ResourceReporter::GetCpuUsageRange(10.0)); | |
| 190 | |
| 191 EXPECT_EQ(ResourceReporter::RANGE_10_TO_30_PERCENT, | |
| 192 ResourceReporter::GetCpuUsageRange(10.1)); | |
| 193 EXPECT_EQ(ResourceReporter::RANGE_10_TO_30_PERCENT, | |
| 194 ResourceReporter::GetCpuUsageRange(29.99)); | |
| 195 EXPECT_EQ(ResourceReporter::RANGE_10_TO_30_PERCENT, | |
| 196 ResourceReporter::GetCpuUsageRange(30.0)); | |
| 197 | |
| 198 EXPECT_EQ(ResourceReporter::RANGE_30_TO_60_PERCENT, | |
| 199 ResourceReporter::GetCpuUsageRange(30.1)); | |
| 200 EXPECT_EQ(ResourceReporter::RANGE_30_TO_60_PERCENT, | |
| 201 ResourceReporter::GetCpuUsageRange(59.99)); | |
| 202 EXPECT_EQ(ResourceReporter::RANGE_30_TO_60_PERCENT, | |
| 203 ResourceReporter::GetCpuUsageRange(60.0)); | |
| 204 | |
| 205 EXPECT_EQ(ResourceReporter::RANGE_ABOVE_60_PERCENT, | |
| 206 ResourceReporter::GetCpuUsageRange(60.1)); | |
| 207 EXPECT_EQ(ResourceReporter::RANGE_ABOVE_60_PERCENT, | |
| 208 ResourceReporter::GetCpuUsageRange(100.0)); | |
| 209 } | |
| 210 | |
| 211 // Tests that ResourceReporter::GetMemoryRapporMetricName() returns the correct | |
| 212 // metric names for the given memory usage. | |
| 213 TEST_F(ResourceReporterTest, TestGetMemoryRapporMetricName) { | |
| 214 EXPECT_EQ(ResourceReporter::RANGE_0_TO_200_MB, | |
| 215 ResourceReporter::GetMemoryUsageRange(2 * k1KB)); | |
| 216 EXPECT_EQ(ResourceReporter::RANGE_0_TO_200_MB, | |
| 217 ResourceReporter::GetMemoryUsageRange(20 * k1MB)); | |
| 218 EXPECT_EQ(ResourceReporter::RANGE_0_TO_200_MB, | |
| 219 ResourceReporter::GetMemoryUsageRange(200 * k1MB)); | |
| 220 | |
| 221 EXPECT_EQ(ResourceReporter::RANGE_200_TO_400_MB, | |
| 222 ResourceReporter::GetMemoryUsageRange(201 * k1MB)); | |
| 223 EXPECT_EQ(ResourceReporter::RANGE_200_TO_400_MB, | |
| 224 ResourceReporter::GetMemoryUsageRange(400 * k1MB)); | |
| 225 | |
| 226 EXPECT_EQ(ResourceReporter::RANGE_400_TO_600_MB, | |
| 227 ResourceReporter::GetMemoryUsageRange(401 * k1MB)); | |
| 228 EXPECT_EQ(ResourceReporter::RANGE_400_TO_600_MB, | |
| 229 ResourceReporter::GetMemoryUsageRange(600 * k1MB)); | |
| 230 | |
| 231 EXPECT_EQ(ResourceReporter::RANGE_600_TO_800_MB, | |
| 232 ResourceReporter::GetMemoryUsageRange(601 * k1MB)); | |
| 233 EXPECT_EQ(ResourceReporter::RANGE_600_TO_800_MB, | |
| 234 ResourceReporter::GetMemoryUsageRange(800 * k1MB)); | |
| 235 | |
| 236 EXPECT_EQ(ResourceReporter::RANGE_800_TO_1_GB, | |
| 237 ResourceReporter::GetMemoryUsageRange(801 * k1MB)); | |
| 238 EXPECT_EQ(ResourceReporter::RANGE_800_TO_1_GB, | |
| 239 ResourceReporter::GetMemoryUsageRange(1 * k1GB)); | |
| 240 | |
| 241 EXPECT_EQ(ResourceReporter::RANGE_ABOVE_1_GB, | |
| 242 ResourceReporter::GetMemoryUsageRange(1 * k1GB + 1 * k1KB)); | |
| 243 } | |
| 244 | |
| 245 // Tests all the interactions between the resource reporter and the task | |
| 246 // manager. | |
| 247 TEST_F(ResourceReporterTest, TestAll) { | |
| 248 // First start by making sure that our assumptions are correct. | |
| 249 ASSERT_LT(kInitiallyAddedTasks, ResourceReporter::kTopConsumersCount); | |
| 250 ASSERT_LT(ResourceReporter::kTopConsumersCount, kTasksSize); | |
| 251 ASSERT_LT(kTasksSize - kTasksToBeRemovedSize, | |
| 252 ResourceReporter::kTopConsumersCount); | |
| 253 | |
| 254 Start(); | |
| 255 | |
| 256 // Add the initial tasks to the task manager, and expect that after a refresh | |
| 257 // that the resource reporter is already tracking them, and the records are | |
| 258 // sorted properly. | |
| 259 AddInitialTasks(); | |
| 260 RefreshTaskManager(); | |
| 261 EXPECT_EQ(kInitiallyAddedTasks, resource_reporter()->task_records_.size()); | |
| 262 EXPECT_LE(resource_reporter()->task_records_by_cpu_.size(), | |
| 263 ResourceReporter::kTopConsumersCount); | |
| 264 EXPECT_LE(resource_reporter()->task_records_by_memory_.size(), | |
| 265 ResourceReporter::kTopConsumersCount); | |
| 266 EXPECT_TRUE(IsCpuRecordsSetSorted()); | |
| 267 EXPECT_TRUE(IsMemoryRecordsSetSorted()); | |
| 268 | |
| 269 // After adding the remaining tasks which are more than |kTopConsumerCount|, | |
| 270 // we must expect that the records used for recording the samples are EQUAL to | |
| 271 // |kTopConsumerCount|, and the records are still properly sorted. | |
| 272 AddRemainingTasks(); | |
| 273 RefreshTaskManager(); | |
| 274 EXPECT_EQ(kTasksSize, resource_reporter()->task_records_.size()); | |
| 275 EXPECT_EQ(ResourceReporter::kTopConsumersCount, | |
| 276 resource_reporter()->task_records_by_cpu_.size()); | |
| 277 EXPECT_EQ(ResourceReporter::kTopConsumersCount, | |
| 278 resource_reporter()->task_records_by_memory_.size()); | |
| 279 EXPECT_TRUE(IsCpuRecordsSetSorted()); | |
| 280 EXPECT_TRUE(IsMemoryRecordsSetSorted()); | |
| 281 | |
| 282 // Removing tasks from the task manager must result in their removal from the | |
| 283 // resource reporter immediately without having to wait until the next | |
| 284 // refresh. | |
| 285 for (const auto& id : kIdsOfTasksToRemove) | |
| 286 RemoveTask(id); | |
| 287 const size_t kExpectedTasksCount = kTasksSize - kTasksToBeRemovedSize; | |
| 288 EXPECT_EQ(kExpectedTasksCount, resource_reporter()->task_records_.size()); | |
| 289 EXPECT_LE(resource_reporter()->task_records_by_cpu_.size(), | |
| 290 ResourceReporter::kTopConsumersCount); | |
| 291 EXPECT_LE(resource_reporter()->task_records_by_memory_.size(), | |
| 292 ResourceReporter::kTopConsumersCount); | |
| 293 EXPECT_TRUE(IsCpuRecordsSetSorted()); | |
| 294 EXPECT_TRUE(IsMemoryRecordsSetSorted()); | |
| 295 | |
| 296 // After refresh nothing changes. | |
| 297 RefreshTaskManager(); | |
| 298 EXPECT_EQ(kExpectedTasksCount, resource_reporter()->task_records_.size()); | |
| 299 EXPECT_LE(resource_reporter()->task_records_by_cpu_.size(), | |
| 300 ResourceReporter::kTopConsumersCount); | |
| 301 EXPECT_LE(resource_reporter()->task_records_by_memory_.size(), | |
| 302 ResourceReporter::kTopConsumersCount); | |
| 303 EXPECT_TRUE(IsCpuRecordsSetSorted()); | |
| 304 EXPECT_TRUE(IsMemoryRecordsSetSorted()); | |
| 305 | |
| 306 Stop(); | |
| 307 } | |
| 308 | |
| 309 } // namespace chromeos | |
| OLD | NEW |