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

Unified Diff: cc/scheduler/rolling_time_delta_history.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, 7 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cc/scheduler/rolling_time_delta_history.h ('k') | cc/scheduler/rolling_time_delta_history_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/scheduler/rolling_time_delta_history.cc
diff --git a/cc/scheduler/rolling_time_delta_history.cc b/cc/scheduler/rolling_time_delta_history.cc
new file mode 100644
index 0000000000000000000000000000000000000000..8d8bba5032fe385dee7e0c7540afa79cb931be6c
--- /dev/null
+++ b/cc/scheduler/rolling_time_delta_history.cc
@@ -0,0 +1,65 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <cmath>
+
+#include "cc/scheduler/rolling_time_delta_history.h"
+
+namespace cc {
+
+RollingTimeDeltaHistory::RollingTimeDeltaHistory(size_t max_size)
+ : max_size_(max_size) {
+}
+
+RollingTimeDeltaHistory::~RollingTimeDeltaHistory() {
+}
+
+void RollingTimeDeltaHistory::InsertSample(base::TimeDelta time) {
+ if (max_size_ == 0)
+ return;
+
+ if (sample_set_.size() == max_size_) {
+ sample_set_.erase(chronological_sample_deque_.front());
+ chronological_sample_deque_.pop_front();
+ }
+
+ TimeDeltaMultiset::iterator it = sample_set_.insert(time);
+ chronological_sample_deque_.push_back(it);
+}
+
+void RollingTimeDeltaHistory::Clear() {
+ chronological_sample_deque_.clear();
+ sample_set_.clear();
+}
+
+base::TimeDelta RollingTimeDeltaHistory::Percentile(double percent) const {
+ if (sample_set_.size() == 0)
+ return base::TimeDelta();
+
+ double fraction = percent / 100.0;
+
+ if (fraction <= 0.0)
+ return *(sample_set_.begin());
+
+ if (fraction >= 1.0)
+ return *(sample_set_.rbegin());
+
+ size_t num_smaller_samples =
+ static_cast<size_t>(std::ceil(fraction * sample_set_.size())) - 1;
+
+ if (num_smaller_samples > sample_set_.size() / 2) {
+ size_t num_larger_samples = sample_set_.size() - num_smaller_samples - 1;
+ TimeDeltaMultiset::const_reverse_iterator it = sample_set_.rbegin();
+ for (size_t i = 0; i < num_larger_samples; i++)
+ it++;
+ return *it;
+ }
+
+ TimeDeltaMultiset::const_iterator it = sample_set_.begin();
+ for (size_t i = 0; i < num_smaller_samples; i++)
+ it++;
+ return *it;
+}
+
+} // namespace cc
« no previous file with comments | « cc/scheduler/rolling_time_delta_history.h ('k') | cc/scheduler/rolling_time_delta_history_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698