Apache Mesos
statistics.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_STATISTICS_HPP__
14 #define __PROCESS_STATISTICS_HPP__
15 
16 #include <glog/logging.h>
17 
18 #include <algorithm>
19 #include <vector>
20 
21 #include <process/timeseries.hpp>
22 
23 #include <stout/foreach.hpp>
24 #include <stout/option.hpp>
25 
26 namespace process {
27 
28 // Represents statistics for a TimeSeries of data.
29 template <typename T>
30 struct Statistics
31 {
32  // Returns Statistics for the given TimeSeries, or None() if the
33  // TimeSeries is empty.
34  // TODO(dhamon): Consider adding a histogram abstraction for better
35  // performance.
36  static Option<Statistics<T>> from(const TimeSeries<T>& timeseries)
37  {
38  std::vector<typename TimeSeries<T>::Value> values_ = timeseries.get();
39 
40  // We need at least 2 values to compute aggregates.
41  if (values_.size() < 2) {
42  return None();
43  }
44 
45  std::vector<T> values;
46  values.reserve(values_.size());
47 
48  foreach (const typename TimeSeries<T>::Value& value, values_) {
49  values.push_back(value.data);
50  }
51 
52  std::sort(values.begin(), values.end());
53 
55 
56  statistics.count = values.size();
57 
58  statistics.min = values.front();
59  statistics.max = values.back();
60 
61  statistics.p50 = percentile(values, 0.5);
62  statistics.p90 = percentile(values, 0.90);
63  statistics.p95 = percentile(values, 0.95);
64  statistics.p99 = percentile(values, 0.99);
65  statistics.p999 = percentile(values, 0.999);
66  statistics.p9999 = percentile(values, 0.9999);
67 
68  return statistics;
69  }
70 
71  size_t count;
72 
73  T min;
74  T max;
75 
76  // TODO(dhamon): Consider making the percentiles we store dynamic.
77  T p50;
78  T p90;
79  T p95;
80  T p99;
81  T p999;
82  T p9999;
83 
84 private:
85  // Returns the requested percentile from the sorted values.
86  // Note that we need at least two values to compute percentiles!
87  // TODO(dhamon): Use a 'Percentage' abstraction.
88  static T percentile(const std::vector<T>& values, double percentile)
89  {
90  CHECK_GE(values.size(), 2u);
91 
92  if (percentile <= 0.0) {
93  return values.front();
94  }
95 
96  if (percentile >= 1.0) {
97  return values.back();
98  }
99 
100  // Use linear interpolation.
101  const double position = percentile * (values.size() - 1);
102  const size_t index = static_cast<size_t>(floor(position));
103  const double delta = position - index;
104 
105  CHECK_GE(index, 0u);
106  CHECK_LT(index, values.size() - 1);
107 
108  return values[index] + delta * (values[index + 1] - values[index]);
109  }
110 };
111 
112 } // namespace process {
113 
114 #endif // __PROCESS_STATISTICS_HPP__
size_t count
Definition: statistics.hpp:71
T max
Definition: statistics.hpp:74
Definition: option.hpp:28
T p95
Definition: statistics.hpp:79
std::vector< Value > get(const Option< Time > &start=None(), const Option< Time > &stop=None()) const
Definition: timeseries.hpp:91
T p9999
Definition: statistics.hpp:82
static Option< Statistics< T > > from(const TimeSeries< T > &timeseries)
Definition: statistics.hpp:36
T min
Definition: statistics.hpp:73
T p50
Definition: statistics.hpp:77
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: none.hpp:27
Definition: timeseries.hpp:65
T data
Definition: timeseries.hpp:71
T p99
Definition: statistics.hpp:80
T p90
Definition: statistics.hpp:78
T p999
Definition: statistics.hpp:81
Definition: statistics.hpp:30
Definition: timeseries.hpp:57