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

Side by Side Diff: cc/scheduler/rolling_sample_window.cc

Issue 15647015: Support computing percentiles on a rolling window of samples (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 6 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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.
4
5 #include <cmath>
6
7 #include "cc/scheduler/rolling_sample_window.h"
8
9 namespace cc {
10
11 RollingSampleWindow::RollingSampleWindow(size_t max_size)
12 : max_size_(max_size) {
13 }
14
15 RollingSampleWindow::~RollingSampleWindow() {
16 }
17
18 void RollingSampleWindow::InsertSample(base::TimeDelta time) {
19 if (max_size_ == 0)
20 return;
21
22 if (sample_set_.size() == max_size_) {
23 sample_set_.erase(chronological_sample_list_.front());
24 chronological_sample_list_.pop_front();
25 }
26
27 TimeDeltaMultiset::iterator it = sample_set_.insert(time);
28 chronological_sample_list_.push_back(it);
29 }
30
31 void RollingSampleWindow::Clear() {
32 chronological_sample_list_.clear();
33 sample_set_.clear();
34 }
35
36 base::TimeDelta RollingSampleWindow::Percentile(double percent) const {
37 double fraction = percent / 100.0;
38
39 if (sample_set_.size() == 0 || fraction <= 0.0)
40 return base::TimeDelta();
41
42 if (fraction >= 1.0)
43 return *(sample_set_.rbegin());
44
45 size_t num_smaller_samples =
46 static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1;
47
48 if (num_smaller_samples > sample_set_.size() / 2) {
49 size_t num_larger_samples = sample_set_.size() - num_smaller_samples - 1;
50 TimeDeltaMultiset::const_reverse_iterator it = sample_set_.rbegin();
51 for (size_t i = 0; i < num_larger_samples; i++)
52 it++;
53 return *it;
54 }
55
56 TimeDeltaMultiset::const_iterator it = sample_set_.begin();
57 for (size_t i = 0; i < num_smaller_samples; i++)
58 it++;
59 return *it;
60 }
61
62 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698