Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(531)

Side by Side Diff: base/task_scheduler/scheduler_worker_pool_impl_unittest.cc

Issue 2412343003: Add TaskScheduler.NumTasksBeforeDetach.[worker pool name] histogram. (Closed)
Patch Set: CR robliao #3 Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/task_scheduler/scheduler_worker_pool_impl.h" 5 #include "base/task_scheduler/scheduler_worker_pool_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <unordered_set> 10 #include <unordered_set>
(...skipping 573 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 } 584 }
585 585
586 namespace { 586 namespace {
587 587
588 class TaskSchedulerWorkerPoolHistogramTest 588 class TaskSchedulerWorkerPoolHistogramTest
589 : public TaskSchedulerWorkerPoolImplTest { 589 : public TaskSchedulerWorkerPoolImplTest {
590 public: 590 public:
591 TaskSchedulerWorkerPoolHistogramTest() = default; 591 TaskSchedulerWorkerPoolHistogramTest() = default;
592 592
593 protected: 593 protected:
594 void SetUp() override {} 594 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.
595 595
596 void TearDown() override { worker_pool_->JoinForTesting(); } 596 void TearDown() override {
597 // |worker_pool_| initialization is done in test body.
598 if (worker_pool_)
599 worker_pool_->JoinForTesting();
gab 2016/10/17 18:24:00 This code belongs in TaskSchedulerWorkerPoolImplTe
fdoray 2016/10/18 19:41:23 Done.
600 }
597 601
598 private: 602 private:
599 std::unique_ptr<StatisticsRecorder> statistics_recorder_ = 603 std::unique_ptr<StatisticsRecorder> statistics_recorder_ =
600 StatisticsRecorder::CreateTemporaryForTesting(); 604 StatisticsRecorder::CreateTemporaryForTesting();
601 605
602 private: 606 private:
gab 2016/10/17 18:24:00 drive-by nit: double private
fdoray 2016/10/18 19:41:23 Done.
603 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolHistogramTest); 607 DISALLOW_COPY_AND_ASSIGN(TaskSchedulerWorkerPoolHistogramTest);
604 }; 608 };
605 609
606 } // namespace 610 } // namespace
607 611
608 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaits) { 612 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBetweenWaits) {
609 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, 613 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
610 WaitableEvent::InitialState::NOT_SIGNALED); 614 WaitableEvent::InitialState::NOT_SIGNALED);
611 InitializeWorkerPool(TimeDelta::Max(), kNumWorkersInWorkerPool); 615 InitializeWorkerPool(TimeDelta::Max(), kNumWorkersInWorkerPool);
612 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits( 616 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits(
(...skipping 13 matching lines...) Expand all
626 event.Signal(); 630 event.Signal();
627 worker_pool_->WaitForAllWorkersIdleForTesting(); 631 worker_pool_->WaitForAllWorkersIdleForTesting();
628 632
629 // Wake up the SchedulerWorker that just became idle by posting a task and 633 // Wake up the SchedulerWorker that just became idle by posting a task and
630 // wait until it becomes idle again. The SchedulerWorker should record the 634 // wait until it becomes idle again. The SchedulerWorker should record the
631 // TaskScheduler.NumTasksBetweenWaits.* histogram on wake up. 635 // TaskScheduler.NumTasksBetweenWaits.* histogram on wake up.
632 task_runner->PostTask(FROM_HERE, Bind(&DoNothing)); 636 task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
633 worker_pool_->WaitForAllWorkersIdleForTesting(); 637 worker_pool_->WaitForAllWorkersIdleForTesting();
634 638
635 // Verify that counts were recorded to the histogram as expected. 639 // Verify that counts were recorded to the histogram as expected.
636 EXPECT_EQ(0, worker_pool_->num_tasks_between_waits_histogram_for_testing() 640 const auto* histogram = worker_pool_->num_tasks_between_waits_histogram();
637 ->SnapshotSamples() 641 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0));
638 ->GetCount(0)); 642 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3));
639 EXPECT_EQ(1, worker_pool_->num_tasks_between_waits_histogram_for_testing() 643 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10));
640 ->SnapshotSamples()
641 ->GetCount(3));
642 EXPECT_EQ(0, worker_pool_->num_tasks_between_waits_histogram_for_testing()
643 ->SnapshotSamples()
644 ->GetCount(10));
645 } 644 }
646 645
647 namespace { 646 namespace {
648 647
649 void SignalAndWaitEvent(WaitableEvent* signal_event, 648 void SignalAndWaitEvent(WaitableEvent* signal_event,
650 WaitableEvent* wait_event) { 649 WaitableEvent* wait_event) {
651 signal_event->Signal(); 650 signal_event->Signal();
652 wait_event->Wait(); 651 wait_event->Wait();
653 } 652 }
654 653
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
690 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::MANUAL, 689 MakeUnique<WaitableEvent>(WaitableEvent::ResetPolicy::MANUAL,
691 WaitableEvent::InitialState::NOT_SIGNALED)); 690 WaitableEvent::InitialState::NOT_SIGNALED));
692 task_runner->PostTask( 691 task_runner->PostTask(
693 FROM_HERE, 692 FROM_HERE,
694 Bind(&SignalAndWaitEvent, Unretained(task_started_events.back().get()), 693 Bind(&SignalAndWaitEvent, Unretained(task_started_events.back().get()),
695 Unretained(&tasks_can_exit_event))); 694 Unretained(&tasks_can_exit_event)));
696 } 695 }
697 for (const auto& task_started_event : task_started_events) 696 for (const auto& task_started_event : task_started_events)
698 task_started_event->Wait(); 697 task_started_event->Wait();
699 698
699 const auto* histogram = worker_pool_->num_tasks_between_waits_histogram();
700
700 // Verify that counts were recorded to the histogram as expected. 701 // Verify that counts were recorded to the histogram as expected.
701 // - The "0" bucket has a count of at least 1 because the SchedulerWorker on 702 // - The "0" bucket has a count of at least 1 because the SchedulerWorker on
702 // top of the idle stack isn't allowed to detach when its sleep timeout 703 // top of the idle stack isn't allowed to detach when its sleep timeout
703 // expires. Instead, it waits on its WaitableEvent again without running a 704 // expires. Instead, it waits on its WaitableEvent again without running a
704 // task. The count may be higher than 1 because of spurious wake ups before 705 // task. The count may be higher than 1 because of spurious wake ups before
705 // the sleep timeout expires. 706 // the sleep timeout expires.
706 EXPECT_GE(worker_pool_->num_tasks_between_waits_histogram_for_testing() 707 EXPECT_GE(histogram->SnapshotSamples()->GetCount(0), 1);
707 ->SnapshotSamples()
708 ->GetCount(0),
709 1);
710 // - The "1" bucket has a count of |kNumWorkersInWorkerPool| because each 708 // - The "1" bucket has a count of |kNumWorkersInWorkerPool| because each
711 // SchedulerWorker ran a task before waiting on its WaitableEvent at the 709 // SchedulerWorker ran a task before waiting on its WaitableEvent at the
712 // beginning of the test. 710 // beginning of the test.
713 EXPECT_EQ(static_cast<int>(kNumWorkersInWorkerPool), 711 EXPECT_EQ(static_cast<int>(kNumWorkersInWorkerPool),
714 worker_pool_->num_tasks_between_waits_histogram_for_testing() 712 histogram->SnapshotSamples()->GetCount(1));
715 ->SnapshotSamples() 713 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10));
716 ->GetCount(1));
717 EXPECT_EQ(0, worker_pool_->num_tasks_between_waits_histogram_for_testing()
718 ->SnapshotSamples()
719 ->GetCount(10));
720 714
721 tasks_can_exit_event.Signal(); 715 tasks_can_exit_event.Signal();
722 worker_pool_->WaitForAllWorkersIdleForTesting(); 716 worker_pool_->WaitForAllWorkersIdleForTesting();
723 worker_pool_->DisallowWorkerDetachmentForTesting(); 717 worker_pool_->DisallowWorkerDetachmentForTesting();
724 } 718 }
725 719
720 TEST_F(TaskSchedulerWorkerPoolHistogramTest, NumTasksBeforeDetach) {
721 InitializeWorkerPool(kReclaimTimeForDetachTests, kNumWorkersInWorkerPool);
722 auto task_runner = worker_pool_->CreateTaskRunnerWithTraits(
723 TaskTraits(), ExecutionMode::SINGLE_THREADED);
724 auto other_task_runner = worker_pool_->CreateTaskRunnerWithTraits(
725 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.
726
727 // Post 3 tasks and wait until they run.
728 task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
729 task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
730 task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
731 worker_pool_->WaitForAllWorkersIdleForTesting();
732
733 // To allow the SchedulerWorker associated with |task_runner| to detach:
734 // - Make sure it isn't on top of the idle stack by waking up another
735 // SchedulerWorker.
736 // - Release |task_runner|.
737 other_task_runner->PostTask(FROM_HERE, Bind(&DoNothing));
738 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
739
740 // Allow the SchedulerWorker to detach.
741 PlatformThread::Sleep(kReclaimTimeForDetachTests + kExtraTimeToWaitForDetach);
742
743 // Join the SchedulerWorkerPool. This forces SchedulerWorkers that detached
744 // during the test to record to the histogram.
745 worker_pool_->WaitForAllWorkersIdleForTesting();
746 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
747 worker_pool_->JoinForTesting();
748 const auto* histogram = worker_pool_->num_tasks_before_detach_histogram();
749 other_task_runner = nullptr;
gab 2016/10/17 18:24:00 .Release()
fdoray 2016/10/18 19:41:23 N/A
750 worker_pool_.reset();
751
752 // Verify that counts were recorded to the histogram as expected.
753 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(0));
754 EXPECT_EQ(1, histogram->SnapshotSamples()->GetCount(3));
755 EXPECT_EQ(0, histogram->SnapshotSamples()->GetCount(10));
756 }
757
726 } // namespace internal 758 } // namespace internal
727 } // namespace base 759 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698