xref: /aosp_15_r20/external/grpc-grpc/test/cpp/qps/histogram.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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_TEST_CPP_QPS_HISTOGRAM_H
20 #define GRPC_TEST_CPP_QPS_HISTOGRAM_H
21 
22 #include "src/proto/grpc/testing/stats.pb.h"
23 #include "test/core/util/histogram.h"
24 
25 namespace grpc {
26 namespace testing {
27 
28 class Histogram {
29  public:
30   // TODO(unknown): look into making histogram params not hardcoded for C++
Histogram()31   Histogram()
32       : impl_(grpc_histogram_create(default_resolution(),
33                                     default_max_possible())) {}
~Histogram()34   ~Histogram() {
35     if (impl_) grpc_histogram_destroy(impl_);
36   }
Reset()37   void Reset() {
38     if (impl_) grpc_histogram_destroy(impl_);
39     impl_ = grpc_histogram_create(default_resolution(), default_max_possible());
40   }
41 
Histogram(Histogram && other)42   Histogram(Histogram&& other) noexcept : impl_(other.impl_) {
43     other.impl_ = nullptr;
44   }
45 
Merge(const Histogram & h)46   void Merge(const Histogram& h) { grpc_histogram_merge(impl_, h.impl_); }
Add(double value)47   void Add(double value) { grpc_histogram_add(impl_, value); }
Percentile(double pctile)48   double Percentile(double pctile) const {
49     return grpc_histogram_percentile(impl_, pctile);
50   }
Count()51   double Count() const { return grpc_histogram_count(impl_); }
Swap(Histogram * other)52   void Swap(Histogram* other) { std::swap(impl_, other->impl_); }
FillProto(HistogramData * p)53   void FillProto(HistogramData* p) {
54     size_t n;
55     const auto* data = grpc_histogram_get_contents(impl_, &n);
56     for (size_t i = 0; i < n; i++) {
57       p->add_bucket(data[i]);
58     }
59     p->set_min_seen(grpc_histogram_minimum(impl_));
60     p->set_max_seen(grpc_histogram_maximum(impl_));
61     p->set_sum(grpc_histogram_sum(impl_));
62     p->set_sum_of_squares(grpc_histogram_sum_of_squares(impl_));
63     p->set_count(grpc_histogram_count(impl_));
64   }
MergeProto(const HistogramData & p)65   void MergeProto(const HistogramData& p) {
66     grpc_histogram_merge_contents(impl_, &*p.bucket().begin(), p.bucket_size(),
67                                   p.min_seen(), p.max_seen(), p.sum(),
68                                   p.sum_of_squares(), p.count());
69   }
70 
default_resolution()71   static double default_resolution() { return 0.01; }
default_max_possible()72   static double default_max_possible() { return 60e9; }
73 
74  private:
75   Histogram(const Histogram&);
76   Histogram& operator=(const Histogram&);
77 
78   grpc_histogram* impl_;
79 };
80 }  // namespace testing
81 }  // namespace grpc
82 
83 #endif  // GRPC_TEST_CPP_QPS_HISTOGRAM_H
84