 Chromium Code Reviews
 Chromium Code Reviews Issue 2412343003:
  Add TaskScheduler.NumTasksBeforeDetach.[worker pool name] histogram.  (Closed)
    
  
    Issue 2412343003:
  Add TaskScheduler.NumTasksBeforeDetach.[worker pool name] histogram.  (Closed) 
  | Index: base/task_scheduler/scheduler_worker_pool_impl_unittest.cc | 
| diff --git a/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc b/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc | 
| index 0f8475a69051fc7f56a1d5d3f3feb1d4a6a75f39..b67847acb7fd86205308abb0bb6b179bfb1fab09 100644 | 
| --- a/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc | 
| +++ b/base/task_scheduler/scheduler_worker_pool_impl_unittest.cc | 
| @@ -593,7 +593,11 @@ class TaskSchedulerWorkerPoolHistogramTest | 
| protected: | 
| void SetUp() override {} | 
| 
gab
2016/10/17 18:24:00
drive-by nit: No-op override?
 
robliao
2016/10/17 23:05:39
This suppresses the common worker_pool_ init and a
 
gab
2016/10/18 14:45:45
Hmmm okay, then how about a comment stating that t
 
fdoray
2016/10/18 19:41:23
Done.
 | 
| - void TearDown() override { worker_pool_->JoinForTesting(); } | 
| + void TearDown() override { | 
| + // |worker_pool_| initialization is done in test body. | 
| + if (worker_pool_) | 
| + worker_pool_->JoinForTesting(); | 
| 
gab
2016/10/17 18:24:00
This code belongs in TaskSchedulerWorkerPoolImplTe
 
fdoray
2016/10/18 19:41:23
Done.
 | 
| + } | 
| private: | 
| std::unique_ptr<StatisticsRecorder> statistics_recorder_ = | 
| @@ -633,15 +637,10 @@ TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaits) { | 
| worker_pool_->WaitForAllWorkersIdleForTesting(); | 
| // Verify that counts were recorded to the histogram as expected. | 
| - EXPECT_EQ(0, worker_pool_->num_tasks_between_waits_histogram_for_testing() | 
| - ->SnapshotSamples() | 
| - ->GetCount(0)); | 
| - EXPECT_EQ(1, worker_pool_->num_tasks_between_waits_histogram_for_testing() | 
| - ->SnapshotSamples() | 
| - ->GetCount(3)); | 
| - EXPECT_EQ(0, worker_pool_->num_tasks_between_waits_histogram_for_testing() | 
| - ->SnapshotSamples() | 
| - ->GetCount(10)); | 
| + const auto* histogram = worker_pool_->num_tasks_between_waits_histogram(); | 
| + EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); | 
| + EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); | 
| + EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 
| } | 
| namespace { | 
| @@ -697,31 +696,64 @@ TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaitsWithDetach) { | 
| for (const auto& task_started_event : task_started_events) | 
| task_started_event->Wait(); | 
| + const auto* histogram = worker_pool_->num_tasks_between_waits_histogram(); | 
| + | 
| // Verify that counts were recorded to the histogram as expected. | 
| // - The "0" bucket has a count of at least 1 because the SchedulerWorker on | 
| // top of the idle stack isn't allowed to detach when its sleep timeout | 
| // expires. Instead, it waits on its WaitableEvent again without running a | 
| // task. The count may be higher than 1 because of spurious wake ups before | 
| // the sleep timeout expires. | 
| - EXPECT_GE(worker_pool_->num_tasks_between_waits_histogram_for_testing() | 
| - ->SnapshotSamples() | 
| - ->GetCount(0), | 
| - 1); | 
| + EXPECT_GE(histogram->SnapshotSamples()->GetCount(0), 1); | 
| // - The "1" bucket has a count of |kNumWorkersInWorkerPool| because each | 
| // SchedulerWorker ran a task before waiting on its WaitableEvent at the | 
| // beginning of the test. | 
| EXPECT_EQ(static_cast<int>(kNumWorkersInWorkerPool), | 
| - worker_pool_->num_tasks_between_waits_histogram_for_testing() | 
| - ->SnapshotSamples() | 
| - ->GetCount(1)); | 
| - EXPECT_EQ(0, worker_pool_->num_tasks_between_waits_histogram_for_testing() | 
| - ->SnapshotSamples() | 
| - ->GetCount(10)); | 
| + histogram->SnapshotSamples()->GetCount(1)); | 
| + EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 
| tasks_can_exit_event.Signal(); | 
| worker_pool_->WaitForAllWorkersIdleForTesting(); | 
| worker_pool_->DisallowWorkerDetachmentForTesting(); | 
| } | 
| +TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBeforeDetach) { | 
| + InitializeWorkerPool(kReclaimTimeForDetachTests, kNumWorkersInWorkerPool); | 
| + auto task_runner = worker_pool_->CreateTaskRunnerWithTraits( | 
| + TaskTraits(), ExecutionMode::SINGLE_THREADED); | 
| + auto other_task_runner = worker_pool_->CreateTaskRunnerWithTraits( | 
| + TaskTraits(), ExecutionMode::SINGLE_THREADED); | 
| 
gab
2016/10/17 18:24:00
This test assumes both of these aren't assigned th
 
fdoray
2016/10/18 19:41:22
Done.
 | 
| + | 
| + // Post 3 tasks and wait until they run. | 
| + task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 
| + task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 
| + task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 
| + worker_pool_->WaitForAllWorkersIdleForTesting(); | 
| + | 
| + // To allow the SchedulerWorker associated with |task_runner| to detach: | 
| + // - Make sure it isn't on top of the idle stack by waking up another | 
| + // SchedulerWorker. | 
| + // - Release |task_runner|. | 
| + other_task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); | 
| + task_runner = nullptr; | 
| 
gab
2016/10/17 18:24:00
nit: I prefer |task_runner.Release()| here to expl
 
fdoray
2016/10/18 19:41:23
Doesn't work. Release() is a private static method
 
gab
2016/10/19 18:35:08
Ah interesting, somehow was convinced it was expos
 | 
| + | 
| + // Allow the SchedulerWorker to detach. | 
| + PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach); | 
| + | 
| + // Join the SchedulerWorkerPool. This forces SchedulerWorkers that detached | 
| + // during the test to record to the histogram. | 
| + worker_pool_->WaitForAllWorkersIdleForTesting(); | 
| + worker_pool_->DisallowWorkerDetachmentForTesting(); | 
| 
gab
2016/10/17 18:24:00
Is this required? If we don't post any more tasks,
 
robliao
2016/10/17 23:05:39
It's a nice to have for test cleanliness for the s
 
gab
2016/10/18 14:45:46
IDK, to me any code that should never do anything
 
fdoray
2016/10/18 19:41:23
DisallowWorkerDetachmentForTesting() must be calle
 | 
| + worker_pool_->JoinForTesting(); | 
| + const auto* histogram = worker_pool_->num_tasks_before_detach_histogram(); | 
| + other_task_runner = nullptr; | 
| 
gab
2016/10/17 18:24:00
.Release()
 
fdoray
2016/10/18 19:41:23
N/A
 | 
| + worker_pool_.reset(); | 
| + | 
| + // Verify that counts were recorded to the histogram as expected. | 
| + EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0)); | 
| + EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3)); | 
| + EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10)); | 
| +} | 
| + | 
| } // namespace internal | 
| } // namespace base |