OLD | NEW |
(Empty) | |
| 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 "src/proto/grpc/testing/stats.grpc.pb.h" |
| 39 |
| 40 namespace grpc { |
| 41 namespace testing { |
| 42 |
| 43 class Histogram { |
| 44 public: |
| 45 // TODO: look into making histogram params not hardcoded for C++ |
| 46 Histogram() |
| 47 : impl_(gpr_histogram_create(default_resolution(), |
| 48 default_max_possible())) {} |
| 49 ~Histogram() { |
| 50 if (impl_) gpr_histogram_destroy(impl_); |
| 51 } |
| 52 Histogram(Histogram&& other) : impl_(other.impl_) { other.impl_ = nullptr; } |
| 53 |
| 54 void Merge(const Histogram& h) { gpr_histogram_merge(impl_, h.impl_); } |
| 55 void Add(double value) { gpr_histogram_add(impl_, value); } |
| 56 double Percentile(double pctile) const { |
| 57 return gpr_histogram_percentile(impl_, pctile); |
| 58 } |
| 59 double Count() const { return gpr_histogram_count(impl_); } |
| 60 void Swap(Histogram* other) { std::swap(impl_, other->impl_); } |
| 61 void FillProto(HistogramData* p) { |
| 62 size_t n; |
| 63 const auto* data = gpr_histogram_get_contents(impl_, &n); |
| 64 for (size_t i = 0; i < n; i++) { |
| 65 p->add_bucket(data[i]); |
| 66 } |
| 67 p->set_min_seen(gpr_histogram_minimum(impl_)); |
| 68 p->set_max_seen(gpr_histogram_maximum(impl_)); |
| 69 p->set_sum(gpr_histogram_sum(impl_)); |
| 70 p->set_sum_of_squares(gpr_histogram_sum_of_squares(impl_)); |
| 71 p->set_count(gpr_histogram_count(impl_)); |
| 72 } |
| 73 void MergeProto(const HistogramData& p) { |
| 74 gpr_histogram_merge_contents(impl_, &*p.bucket().begin(), p.bucket_size(), |
| 75 p.min_seen(), p.max_seen(), p.sum(), |
| 76 p.sum_of_squares(), p.count()); |
| 77 } |
| 78 |
| 79 static double default_resolution() { return 0.01; } |
| 80 static double default_max_possible() { return 60e9; } |
| 81 |
| 82 private: |
| 83 Histogram(const Histogram&); |
| 84 Histogram& operator=(const Histogram&); |
| 85 |
| 86 gpr_histogram* impl_; |
| 87 }; |
| 88 } |
| 89 } |
| 90 |
| 91 #endif /* TEST_QPS_HISTOGRAM_H */ |
OLD | NEW |