Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/task_manager/sampling/task_group.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/macros.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "base/run_loop.h" | |
| 16 #include "base/strings/string16.h" | |
| 17 #include "chrome/browser/task_manager/sampling/shared_sampler.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/test/test_browser_thread_bundle.h" | |
| 20 #include "gpu/ipc/common/memory_stats.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 namespace task_manager { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class FakeTask : public Task { | |
| 28 public: | |
| 29 FakeTask(base::ProcessId process_id, | |
| 30 Type type) | |
| 31 : Task(base::string16(), | |
| 32 "FakeTask", | |
| 33 nullptr, | |
| 34 base::kNullProcessHandle, | |
| 35 process_id), | |
| 36 type_(type) { | |
| 37 } | |
| 38 | |
| 39 Type GetType() const override { return type_; } | |
| 40 | |
| 41 int GetChildProcessUniqueID() const override { return 0; } | |
| 42 | |
| 43 const Task* GetParentTask() const override { return nullptr; } | |
| 44 | |
| 45 int GetTabId() const override { return 0; } | |
| 46 | |
| 47 private: | |
| 48 Type type_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(FakeTask); | |
| 51 }; | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 55 class TaskGroupTest : public testing::Test { | |
| 56 public: | |
| 57 TaskGroupTest() | |
| 58 : io_task_runner_(content::BrowserThread::GetTaskRunnerForThread( | |
| 59 content::BrowserThread::IO)), | |
| 60 run_loop_(base::MakeUnique<base::RunLoop>()), | |
| 61 task_group_(process_handle(), | |
| 62 process_id(), | |
|
afakhry
2017/02/10 20:08:52
I think you should make the above two accessors st
Wez
2017/02/13 01:22:19
Removed them in favour of calling the base::Proces
afakhry
2017/02/15 02:58:19
Acknowledged.
| |
| 63 base::Bind(&TaskGroupTest::OnBackgroundCalculationsDone, | |
| 64 base::Unretained(this)), | |
| 65 new SharedSampler(io_task_runner_), | |
| 66 io_task_runner_) {} | |
| 67 | |
| 68 protected: | |
| 69 void OnBackgroundCalculationsDone() { | |
| 70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 71 background_refresh_complete_ = true; | |
| 72 run_loop_->QuitWhenIdle(); | |
| 73 } | |
| 74 | |
| 75 base::ProcessHandle process_handle() const { | |
| 76 return base::Process::Current().Handle(); | |
| 77 } | |
| 78 base::ProcessId process_id() const { return base::Process::Current().Pid(); } | |
| 79 | |
| 80 content::TestBrowserThreadBundle browser_threads_; | |
| 81 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | |
| 82 std::unique_ptr<base::RunLoop> run_loop_; | |
| 83 TaskGroup task_group_; | |
| 84 bool background_refresh_complete_ = false; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(TaskGroupTest); | |
| 87 }; | |
| 88 | |
| 89 TEST_F(TaskGroupTest, NullRefresh) { | |
|
afakhry
2017/02/10 20:08:52
It would be really nice to add a comment above eac
Wez
2017/02/13 01:22:19
Done.
| |
| 90 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), 0); | |
| 91 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 92 EXPECT_FALSE(background_refresh_complete_); | |
| 93 } | |
| 94 | |
| 95 TEST_F(TaskGroupTest, SyncRefresh) { | |
| 96 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 97 REFRESH_TYPE_NETWORK_USAGE); | |
| 98 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 99 EXPECT_FALSE(background_refresh_complete_); | |
| 100 } | |
| 101 | |
| 102 TEST_F(TaskGroupTest, AsyncRefresh) { | |
| 103 // Memory is measured asynchronously, but not via SharedSampler (see below). | |
| 104 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 105 REFRESH_TYPE_MEMORY); | |
| 106 EXPECT_FALSE(task_group_.AreBackgroundCalculationsDone()); | |
| 107 | |
| 108 ASSERT_FALSE(background_refresh_complete_); | |
| 109 run_loop_->Run(); | |
| 110 | |
| 111 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 112 EXPECT_TRUE(background_refresh_complete_); | |
| 113 } | |
| 114 | |
| 115 TEST_F(TaskGroupTest, SharedAsyncRefresh) { | |
| 116 // Idle wakeups are measured via SharedSampler under Windows. | |
| 117 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 118 REFRESH_TYPE_IDLE_WAKEUPS); | |
|
afakhry
2017/02/10 20:08:52
IDLE_WAKEUPS is a background-calculated value and
Wez
2017/02/13 01:22:19
I've reworked the comment; I'd prefer to keep the
afakhry
2017/02/15 02:58:19
Acknowledged.
| |
| 119 EXPECT_FALSE(task_group_.AreBackgroundCalculationsDone()); | |
| 120 | |
| 121 ASSERT_FALSE(background_refresh_complete_); | |
| 122 run_loop_->Run(); | |
| 123 | |
| 124 EXPECT_TRUE(background_refresh_complete_); | |
| 125 | |
| 126 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 127 } | |
| 128 | |
| 129 TEST_F(TaskGroupTest, NaclRefreshZeroTasks) { | |
| 130 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 131 REFRESH_TYPE_NACL); | |
| 132 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 133 EXPECT_FALSE(background_refresh_complete_); | |
| 134 } | |
| 135 | |
| 136 TEST_F(TaskGroupTest, NaclRefreshWithTask) { | |
| 137 FakeTask fake_task(process_id(), Task::NACL); | |
| 138 task_group_.AddTask(&fake_task); | |
| 139 | |
| 140 task_group_.Refresh(gpu::VideoMemoryUsageStats(), base::TimeDelta(), | |
| 141 REFRESH_TYPE_NACL); | |
| 142 #if !defined(DISABLE_NACL) | |
| 143 EXPECT_FALSE(task_group_.AreBackgroundCalculationsDone()); | |
| 144 | |
| 145 ASSERT_FALSE(background_refresh_complete_); | |
| 146 run_loop_->Run(); | |
| 147 | |
| 148 EXPECT_TRUE(background_refresh_complete_); | |
| 149 #endif // !defined(DISABLE_NACL) | |
| 150 | |
| 151 EXPECT_TRUE(task_group_.AreBackgroundCalculationsDone()); | |
| 152 } | |
| 153 | |
| 154 } // namespace task_manager | |
| OLD | NEW |