event_engine/iomgr_engine/time_averaged_stats.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #ifndef GRPC_CORE_LIB_EVENT_ENGINE_IOMGR_ENGINE_TIME_AVERAGED_STATS_H
20 #define GRPC_CORE_LIB_EVENT_ENGINE_IOMGR_ENGINE_TIME_AVERAGED_STATS_H
21 
22 namespace grpc_event_engine {
23 namespace iomgr_engine {
24 
25 /* This tracks a time-decaying weighted average. It works by collecting
26  batches of samples and then mixing their average into a time-decaying
27  weighted mean. It is designed for batch operations where we do many adds
28  before updating the average. */
29 
31  public:
32  TimeAveragedStats(double init_avg, double regress_weight,
33  double persistence_factor);
34 
35  /* Add a sample to the current batch. */
36  void AddSample(double value);
37  /* Complete a batch and compute the new estimate of the average sample
38  value. */
39  double UpdateAverage();
40 
43 
44  private:
45  /* The initial average value. This is the reported average until the first
46  grpc_time_averaged_stats_update_average call. If a positive regress_weight
47  is used, we also regress towards this value on each update. */
48  const double init_avg_;
49  /* The sample weight of "init_avg" that is mixed in with each call to
50  grpc_time_averaged_stats_update_average. If the calls to
51  grpc_time_averaged_stats_add_sample stop, this will cause the average to
52  regress back to the mean. This should be non-negative. Set it to 0 to
53  disable the bias. A value of 1 has the effect of adding in 1 bonus sample
54  with value init_avg to each sample period. */
55  const double regress_weight_;
56  /* This determines the rate of decay of the time-averaging from one period
57  to the next by scaling the aggregate_total_weight of samples from prior
58  periods when combining with the latest period. It should be in the range
59  [0,1]. A higher value adapts more slowly. With a value of 0.5, if the
60  batches each have k samples, the samples_in_avg_ will grow to 2 k, so the
61  weighting of the time average will eventually be 1/3 new batch and 2/3
62  old average. */
63  const double persistence_factor_;
64 
65  /* The total value of samples since the last UpdateAverage(). */
66  double batch_total_value_ = 0;
67  /* The number of samples since the last UpdateAverage(). */
68  double batch_num_samples_ = 0;
69  /* The time-decayed sum of batch_num_samples_ over previous batches. This is
70  the "weight" of the old aggregate_weighted_avg_ when updating the
71  average. */
73  /* A time-decayed average of the (batch_total_value_ / batch_num_samples_),
74  computed by decaying the samples_in_avg_ weight in the weighted average. */
76 };
77 
78 } // namespace iomgr_engine
79 } // namespace grpc_event_engine
80 
81 #endif /* GRPC_CORE_LIB_EVENT_ENGINE_IOMGR_ENGINE_TIME_AVERAGED_STATS_H */
grpc_event_engine::iomgr_engine::TimeAveragedStats::aggregate_total_weight
double aggregate_total_weight() const
Definition: event_engine/iomgr_engine/time_averaged_stats.h:42
grpc_event_engine::iomgr_engine::TimeAveragedStats::AddSample
void AddSample(double value)
Definition: event_engine/iomgr_engine/time_averaged_stats.cc:32
grpc_event_engine::iomgr_engine::TimeAveragedStats::aggregate_weighted_avg
double aggregate_weighted_avg() const
Definition: event_engine/iomgr_engine/time_averaged_stats.h:41
grpc_event_engine::iomgr_engine::TimeAveragedStats
Definition: event_engine/iomgr_engine/time_averaged_stats.h:30
grpc_event_engine::iomgr_engine::TimeAveragedStats::persistence_factor_
const double persistence_factor_
Definition: event_engine/iomgr_engine/time_averaged_stats.h:63
grpc_event_engine::iomgr_engine::TimeAveragedStats::batch_num_samples_
double batch_num_samples_
Definition: event_engine/iomgr_engine/time_averaged_stats.h:68
grpc_event_engine::iomgr_engine::TimeAveragedStats::regress_weight_
const double regress_weight_
Definition: event_engine/iomgr_engine/time_averaged_stats.h:55
grpc_event_engine::iomgr_engine::TimeAveragedStats::aggregate_weighted_avg_
double aggregate_weighted_avg_
Definition: event_engine/iomgr_engine/time_averaged_stats.h:75
grpc_event_engine::iomgr_engine::TimeAveragedStats::UpdateAverage
double UpdateAverage()
Definition: event_engine/iomgr_engine/time_averaged_stats.cc:37
grpc_event_engine::iomgr_engine::TimeAveragedStats::aggregate_total_weight_
double aggregate_total_weight_
Definition: event_engine/iomgr_engine/time_averaged_stats.h:72
value
const char * value
Definition: hpack_parser_table.cc:165
grpc_event_engine::iomgr_engine::TimeAveragedStats::batch_total_value_
double batch_total_value_
Definition: event_engine/iomgr_engine/time_averaged_stats.h:66
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::iomgr_engine::TimeAveragedStats::TimeAveragedStats
TimeAveragedStats(double init_avg, double regress_weight, double persistence_factor)
Definition: event_engine/iomgr_engine/time_averaged_stats.cc:26
grpc_event_engine::iomgr_engine::TimeAveragedStats::init_avg_
const double init_avg_
Definition: event_engine/iomgr_engine/time_averaged_stats.h:48


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:36