Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #ifndef TEST_QPS_HISTOGRAM_H
35 : #define TEST_QPS_HISTOGRAM_H
36 :
37 : #include <grpc/support/histogram.h>
38 : #include "test/cpp/qps/qpstest.grpc.pb.h"
39 :
40 : namespace grpc {
41 : namespace testing {
42 :
43 : class Histogram {
44 : public:
45 78 : Histogram() : impl_(gpr_histogram_create(0.01, 60e9)) {}
46 78 : ~Histogram() {
47 78 : if (impl_) gpr_histogram_destroy(impl_);
48 78 : }
49 : Histogram(Histogram&& other) : impl_(other.impl_) { other.impl_ = nullptr; }
50 :
51 40 : void Merge(Histogram* h) { gpr_histogram_merge(impl_, h->impl_); }
52 1261860 : void Add(double value) { gpr_histogram_add(impl_, value); }
53 30 : double Percentile(double pctile) const {
54 30 : return gpr_histogram_percentile(impl_, pctile);
55 : }
56 6 : double Count() const { return gpr_histogram_count(impl_); }
57 40 : void Swap(Histogram* other) { std::swap(impl_, other->impl_); }
58 12 : void FillProto(HistogramData* p) {
59 : size_t n;
60 12 : const auto* data = gpr_histogram_get_contents(impl_, &n);
61 29952 : for (size_t i = 0; i < n; i++) {
62 29940 : p->add_bucket(data[i]);
63 : }
64 12 : p->set_min_seen(gpr_histogram_minimum(impl_));
65 12 : p->set_max_seen(gpr_histogram_maximum(impl_));
66 12 : p->set_sum(gpr_histogram_sum(impl_));
67 12 : p->set_sum_of_squares(gpr_histogram_sum_of_squares(impl_));
68 12 : p->set_count(gpr_histogram_count(impl_));
69 12 : }
70 6 : void MergeProto(const HistogramData& p) {
71 12 : gpr_histogram_merge_contents(impl_, &*p.bucket().begin(), p.bucket_size(),
72 : p.min_seen(), p.max_seen(), p.sum(),
73 18 : p.sum_of_squares(), p.count());
74 6 : }
75 :
76 : private:
77 : Histogram(const Histogram&);
78 : Histogram& operator=(const Histogram&);
79 :
80 : gpr_histogram* impl_;
81 : };
82 : }
83 : }
84 :
85 : #endif /* TEST_QPS_HISTOGRAM_H */
|