1/*
2 * Copyright 2018-present Facebook, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <folly/stats/detail/BufferedStatTDigest.h>
20
21namespace folly {
22
23struct QuantileEstimates {
24 public:
25 double sum;
26 double count;
27
28 // vector of {quantile, value}
29 std::vector<std::pair<double, double>> quantiles;
30};
31
32/*
33 * A QuantileEstimator that buffers writes for 1 second.
34 */
35template <typename ClockT = std::chrono::steady_clock>
36class SimpleQuantileEstimator {
37 public:
38 using TimePoint = typename ClockT::time_point;
39
40 SimpleQuantileEstimator();
41
42 QuantileEstimates estimateQuantiles(
43 Range<const double*> quantiles,
44 TimePoint now = ClockT::now());
45
46 void addValue(double value, TimePoint now = ClockT::now());
47
48 /// Flush buffered values
49 void flush() {
50 bufferedDigest_.flush();
51 }
52
53 private:
54 detail::BufferedDigest<TDigest, ClockT> bufferedDigest_;
55};
56
57/*
58 * A QuantileEstimator that keeps values for nWindows * windowDuration (see
59 * constructor). Values are buffered for windowDuration.
60 */
61template <typename ClockT = std::chrono::steady_clock>
62class SlidingWindowQuantileEstimator {
63 public:
64 using TimePoint = typename ClockT::time_point;
65
66 SlidingWindowQuantileEstimator(
67 std::chrono::seconds windowDuration,
68 size_t nWindows = 60);
69
70 QuantileEstimates estimateQuantiles(
71 Range<const double*> quantiles,
72 TimePoint now = ClockT::now());
73
74 void addValue(double value, TimePoint now = ClockT::now());
75
76 /// Flush buffered values
77 void flush() {
78 bufferedSlidingWindow_.flush();
79 }
80
81 private:
82 detail::BufferedSlidingWindow<TDigest, ClockT> bufferedSlidingWindow_;
83};
84
85extern template class SimpleQuantileEstimator<std::chrono::steady_clock>;
86extern template class SlidingWindowQuantileEstimator<std::chrono::steady_clock>;
87
88} // namespace folly
89