1 // Copyright 2015 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.
5 #include "components/scheduler/renderer/task_cost_estimator.h"
7 #include "base/time/default_tick_clock.h"
11 TaskCostEstimator::TaskCostEstimator(int sample_count
,
12 double estimation_percentile
)
13 : rolling_time_delta_history_(sample_count
),
14 time_source_(new base::DefaultTickClock
),
15 outstanding_task_count_(0),
16 estimation_percentile_(estimation_percentile
) {}
18 TaskCostEstimator::~TaskCostEstimator() {}
20 void TaskCostEstimator::WillProcessTask(const base::PendingTask
& pending_task
) {
21 // Avoid measuring the duration in nested run loops.
22 if (++outstanding_task_count_
== 1)
23 task_start_time_
= time_source_
->NowTicks();
26 void TaskCostEstimator::DidProcessTask(const base::PendingTask
& pending_task
) {
27 if (--outstanding_task_count_
== 0) {
28 base::TimeDelta duration
= time_source_
->NowTicks() - task_start_time_
;
29 rolling_time_delta_history_
.InsertSample(duration
);
31 // TODO(skyostil): Should we do this less often?
32 expected_task_duration_
=
33 rolling_time_delta_history_
.Percentile(estimation_percentile_
);
37 void TaskCostEstimator::Clear() {
38 rolling_time_delta_history_
.Clear();
39 expected_task_duration_
= base::TimeDelta();
42 void TaskCostEstimator::SetTimeSourceForTesting(
43 scoped_ptr
<base::TickClock
> time_source
) {
44 time_source_
= time_source
.Pass();
47 } // namespace scheduler