OLD | NEW |
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/task_tracker.h" | 5 #include "base/task_scheduler/task_tracker.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
| 19 #include "base/metrics/histogram_base.h" |
| 20 #include "base/metrics/histogram_samples.h" |
| 21 #include "base/metrics/statistics_recorder.h" |
19 #include "base/sequence_token.h" | 22 #include "base/sequence_token.h" |
20 #include "base/sequenced_task_runner.h" | 23 #include "base/sequenced_task_runner.h" |
21 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
22 #include "base/synchronization/atomic_flag.h" | 25 #include "base/synchronization/atomic_flag.h" |
23 #include "base/synchronization/waitable_event.h" | 26 #include "base/synchronization/waitable_event.h" |
24 #include "base/task_scheduler/scheduler_lock.h" | 27 #include "base/task_scheduler/scheduler_lock.h" |
25 #include "base/task_scheduler/task.h" | 28 #include "base/task_scheduler/task.h" |
26 #include "base/task_scheduler/task_traits.h" | 29 #include "base/task_scheduler/task_traits.h" |
27 #include "base/test/gtest_util.h" | 30 #include "base/test/gtest_util.h" |
| 31 #include "base/test/histogram_tester.h" |
28 #include "base/test/test_simple_task_runner.h" | 32 #include "base/test/test_simple_task_runner.h" |
29 #include "base/test/test_timeouts.h" | 33 #include "base/test/test_timeouts.h" |
30 #include "base/threading/platform_thread.h" | 34 #include "base/threading/platform_thread.h" |
31 #include "base/threading/sequenced_task_runner_handle.h" | 35 #include "base/threading/sequenced_task_runner_handle.h" |
32 #include "base/threading/simple_thread.h" | 36 #include "base/threading/simple_thread.h" |
33 #include "base/threading/thread_restrictions.h" | 37 #include "base/threading/thread_restrictions.h" |
34 #include "base/threading/thread_task_runner_handle.h" | 38 #include "base/threading/thread_task_runner_handle.h" |
35 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
36 | 40 |
37 namespace base { | 41 namespace base { |
(...skipping 840 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
878 // task. | 882 // task. |
879 TEST(TaskSchedulerTaskTrackerWaitAllowedTest, WaitAllowed) { | 883 TEST(TaskSchedulerTaskTrackerWaitAllowedTest, WaitAllowed) { |
880 // Run the test on the separate thread since it is not possible to reset the | 884 // Run the test on the separate thread since it is not possible to reset the |
881 // "wait allowed" bit of a thread without being a friend of | 885 // "wait allowed" bit of a thread without being a friend of |
882 // ThreadRestrictions. | 886 // ThreadRestrictions. |
883 WaitAllowedTestThread wait_allowed_test_thread; | 887 WaitAllowedTestThread wait_allowed_test_thread; |
884 wait_allowed_test_thread.Start(); | 888 wait_allowed_test_thread.Start(); |
885 wait_allowed_test_thread.Join(); | 889 wait_allowed_test_thread.Join(); |
886 } | 890 } |
887 | 891 |
| 892 // Verify that TaskScheduler.TaskLatency.* histograms are correctly recorded |
| 893 // when a task runs. |
| 894 TEST(TaskSchedulerTaskTrackerHistogramTest, TaskLatency) { |
| 895 auto statistics_recorder = StatisticsRecorder::CreateTemporaryForTesting(); |
| 896 |
| 897 TaskTracker tracker; |
| 898 |
| 899 struct { |
| 900 const TaskTraits traits; |
| 901 const char* const expected_histogram; |
| 902 } tests[] = { |
| 903 {TaskTraits().WithPriority(TaskPriority::BACKGROUND), |
| 904 "TaskScheduler.TaskLatency.BackgroundTaskPriority"}, |
| 905 {TaskTraits().WithPriority(TaskPriority::BACKGROUND).MayBlock(), |
| 906 "TaskScheduler.TaskLatency.BackgroundTaskPriority.MayBlock"}, |
| 907 {TaskTraits() |
| 908 .WithPriority(TaskPriority::BACKGROUND) |
| 909 .WithBaseSyncPrimitives(), |
| 910 "TaskScheduler.TaskLatency.BackgroundTaskPriority.MayBlock"}, |
| 911 {TaskTraits().WithPriority(TaskPriority::USER_VISIBLE), |
| 912 "TaskScheduler.TaskLatency.UserVisibleTaskPriority"}, |
| 913 {TaskTraits().WithPriority(TaskPriority::USER_VISIBLE).MayBlock(), |
| 914 "TaskScheduler.TaskLatency.UserVisibleTaskPriority.MayBlock"}, |
| 915 {TaskTraits() |
| 916 .WithPriority(TaskPriority::USER_VISIBLE) |
| 917 .WithBaseSyncPrimitives(), |
| 918 "TaskScheduler.TaskLatency.UserVisibleTaskPriority.MayBlock"}, |
| 919 {TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), |
| 920 "TaskScheduler.TaskLatency.UserBlockingTaskPriority"}, |
| 921 {TaskTraits().WithPriority(TaskPriority::USER_BLOCKING).MayBlock(), |
| 922 "TaskScheduler.TaskLatency.UserBlockingTaskPriority.MayBlock"}, |
| 923 {TaskTraits() |
| 924 .WithPriority(TaskPriority::USER_BLOCKING) |
| 925 .WithBaseSyncPrimitives(), |
| 926 "TaskScheduler.TaskLatency.UserBlockingTaskPriority.MayBlock"}}; |
| 927 |
| 928 for (const auto& test : tests) { |
| 929 auto task = |
| 930 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), test.traits, TimeDelta()); |
| 931 ASSERT_TRUE(tracker.WillPostTask(task.get())); |
| 932 |
| 933 HistogramTester tester; |
| 934 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); |
| 935 tester.ExpectTotalCount(test.expected_histogram, 1); |
| 936 } |
| 937 } |
| 938 |
888 } // namespace internal | 939 } // namespace internal |
889 } // namespace base | 940 } // namespace base |
OLD | NEW |