| 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> |
| (...skipping 871 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 882 // task. | 882 // task. |
| 883 TEST(TaskSchedulerTaskTrackerWaitAllowedTest, WaitAllowed) { | 883 TEST(TaskSchedulerTaskTrackerWaitAllowedTest, WaitAllowed) { |
| 884 // 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 |
| 885 // "wait allowed" bit of a thread without being a friend of | 885 // "wait allowed" bit of a thread without being a friend of |
| 886 // ThreadRestrictions. | 886 // ThreadRestrictions. |
| 887 WaitAllowedTestThread wait_allowed_test_thread; | 887 WaitAllowedTestThread wait_allowed_test_thread; |
| 888 wait_allowed_test_thread.Start(); | 888 wait_allowed_test_thread.Start(); |
| 889 wait_allowed_test_thread.Join(); | 889 wait_allowed_test_thread.Join(); |
| 890 } | 890 } |
| 891 | 891 |
| 892 namespace { |
| 893 |
| 894 struct TaskTraitHistogramPair { |
| 895 TaskTraits traits; |
| 896 const char* expected_histogram; |
| 897 |
| 898 TaskTraitHistogramPair(const TaskTraits& traits_in, |
| 899 const char* expected_histogram_in) |
| 900 : traits(traits_in), expected_histogram(expected_histogram_in) {} |
| 901 }; |
| 902 |
| 903 std::vector<TaskTraitHistogramPair> GenerateTaskTraitHistogramPairs() { |
| 904 std::vector<TaskTraitHistogramPair> pairs; |
| 905 pairs.emplace_back(TaskTraits().WithPriority(TaskPriority::BACKGROUND), |
| 906 "TaskScheduler.TaskLatency.BackgroundTaskPriority"); |
| 907 pairs.emplace_back( |
| 908 TaskTraits().WithPriority(TaskPriority::BACKGROUND).MayBlock(), |
| 909 "TaskScheduler.TaskLatency.BackgroundTaskPriority.MayBlock"); |
| 910 pairs.emplace_back( |
| 911 TaskTraits() |
| 912 .WithPriority(TaskPriority::BACKGROUND) |
| 913 .WithBaseSyncPrimitives(), |
| 914 "TaskScheduler.TaskLatency.BackgroundTaskPriority.MayBlock"); |
| 915 pairs.emplace_back(TaskTraits().WithPriority(TaskPriority::USER_VISIBLE), |
| 916 "TaskScheduler.TaskLatency.UserVisibleTaskPriority"); |
| 917 pairs.emplace_back( |
| 918 TaskTraits().WithPriority(TaskPriority::USER_VISIBLE).MayBlock(), |
| 919 "TaskScheduler.TaskLatency.UserVisibleTaskPriority.MayBlock"); |
| 920 pairs.emplace_back( |
| 921 TaskTraits() |
| 922 .WithPriority(TaskPriority::USER_VISIBLE) |
| 923 .WithBaseSyncPrimitives(), |
| 924 "TaskScheduler.TaskLatency.UserVisibleTaskPriority.MayBlock"); |
| 925 pairs.emplace_back(TaskTraits().WithPriority(TaskPriority::USER_BLOCKING), |
| 926 "TaskScheduler.TaskLatency.UserBlockingTaskPriority"); |
| 927 pairs.emplace_back( |
| 928 TaskTraits().WithPriority(TaskPriority::USER_BLOCKING).MayBlock(), |
| 929 "TaskScheduler.TaskLatency.UserBlockingTaskPriority.MayBlock"); |
| 930 pairs.emplace_back( |
| 931 TaskTraits() |
| 932 .WithPriority(TaskPriority::USER_BLOCKING) |
| 933 .WithBaseSyncPrimitives(), |
| 934 "TaskScheduler.TaskLatency.UserBlockingTaskPriority.MayBlock"); |
| 935 return pairs; |
| 936 } |
| 937 |
| 938 } // namespace |
| 939 |
| 892 // Verify that TaskScheduler.TaskLatency.* histograms are correctly recorded | 940 // Verify that TaskScheduler.TaskLatency.* histograms are correctly recorded |
| 893 // when a task runs. | 941 // when a task runs. |
| 894 TEST(TaskSchedulerTaskTrackerHistogramTest, TaskLatency) { | 942 TEST(TaskSchedulerTaskTrackerHistogramTest, TaskLatency) { |
| 895 auto statistics_recorder = StatisticsRecorder::CreateTemporaryForTesting(); | 943 auto statistics_recorder = StatisticsRecorder::CreateTemporaryForTesting(); |
| 896 | 944 |
| 897 TaskTracker tracker; | 945 TaskTracker tracker; |
| 898 | 946 |
| 899 struct { | 947 for (const auto& test : GenerateTaskTraitHistogramPairs()) { |
| 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 = | 948 auto task = |
| 930 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), test.traits, TimeDelta()); | 949 MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), test.traits, TimeDelta()); |
| 931 ASSERT_TRUE(tracker.WillPostTask(task.get())); | 950 ASSERT_TRUE(tracker.WillPostTask(task.get())); |
| 932 | 951 |
| 933 HistogramTester tester; | 952 HistogramTester tester; |
| 934 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); | 953 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); |
| 935 tester.ExpectTotalCount(test.expected_histogram, 1); | 954 tester.ExpectTotalCount(test.expected_histogram, 1); |
| 936 } | 955 } |
| 937 } | 956 } |
| 938 | 957 |
| 958 TEST(TaskSchedulerTaskTrackerHistogramTest, TaskLatencyInternalOnly) { |
| 959 auto statistics_recorder = StatisticsRecorder::CreateTemporaryForTesting(); |
| 960 |
| 961 TaskTracker tracker; |
| 962 |
| 963 for (const auto& test : GenerateTaskTraitHistogramPairs()) { |
| 964 auto task = MakeUnique<Task>(FROM_HERE, Bind(&DoNothing), test.traits, |
| 965 TimeDelta(), Task::TaskType::INTERNAL); |
| 966 ASSERT_TRUE(tracker.WillPostTask(task.get())); |
| 967 |
| 968 HistogramTester tester; |
| 969 EXPECT_TRUE(tracker.RunTask(std::move(task), SequenceToken::Create())); |
| 970 tester.ExpectTotalCount(test.expected_histogram, 0); |
| 971 } |
| 972 } |
| 973 |
| 939 } // namespace internal | 974 } // namespace internal |
| 940 } // namespace base | 975 } // namespace base |
| OLD | NEW |