Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 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> |
vjpai | aa39192 | 2015-11-02 14:46:04 -0800 | [diff] [blame] | 38 | #include "test/proto/benchmarks/stats.grpc.pb.h" |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 39 | |
| 40 | namespace grpc { |
| 41 | namespace testing { |
| 42 | |
| 43 | class Histogram { |
| 44 | public: |
Jan Tattermusch | c5ebbd5 | 2015-11-18 16:50:47 -0800 | [diff] [blame] | 45 | // TODO: look into making histogram params not hardcoded for C++ |
| 46 | Histogram() : impl_(gpr_histogram_create(default_resolution(), |
| 47 | default_max_possible())) {} |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 48 | ~Histogram() { |
| 49 | if (impl_) gpr_histogram_destroy(impl_); |
Craig Tiller | f282514 | 2015-03-03 17:15:36 -0800 | [diff] [blame] | 50 | } |
Craig Tiller | a182bf1 | 2015-03-04 13:54:39 -0800 | [diff] [blame] | 51 | Histogram(Histogram&& other) : impl_(other.impl_) { other.impl_ = nullptr; } |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 52 | |
vjpai | 119c103 | 2015-10-29 01:21:04 -0700 | [diff] [blame] | 53 | void Merge(const Histogram& h) { gpr_histogram_merge(impl_, h.impl_); } |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 54 | void Add(double value) { gpr_histogram_add(impl_, value); } |
Craig Tiller | 66048f4 | 2015-04-10 08:23:52 -0700 | [diff] [blame] | 55 | double Percentile(double pctile) const { |
Craig Tiller | 10923c2 | 2015-03-03 14:24:49 -0800 | [diff] [blame] | 56 | return gpr_histogram_percentile(impl_, pctile); |
| 57 | } |
Craig Tiller | 66048f4 | 2015-04-10 08:23:52 -0700 | [diff] [blame] | 58 | double Count() const { return gpr_histogram_count(impl_); } |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 59 | void Swap(Histogram* other) { std::swap(impl_, other->impl_); } |
Craig Tiller | 76877c3 | 2015-03-03 16:04:23 -0800 | [diff] [blame] | 60 | void FillProto(HistogramData* p) { |
| 61 | size_t n; |
| 62 | const auto* data = gpr_histogram_get_contents(impl_, &n); |
| 63 | for (size_t i = 0; i < n; i++) { |
| 64 | p->add_bucket(data[i]); |
| 65 | } |
| 66 | p->set_min_seen(gpr_histogram_minimum(impl_)); |
| 67 | p->set_max_seen(gpr_histogram_maximum(impl_)); |
| 68 | p->set_sum(gpr_histogram_sum(impl_)); |
| 69 | p->set_sum_of_squares(gpr_histogram_sum_of_squares(impl_)); |
| 70 | p->set_count(gpr_histogram_count(impl_)); |
| 71 | } |
| 72 | void MergeProto(const HistogramData& p) { |
Craig Tiller | d701c5f | 2015-03-03 16:05:04 -0800 | [diff] [blame] | 73 | gpr_histogram_merge_contents(impl_, &*p.bucket().begin(), p.bucket_size(), |
| 74 | p.min_seen(), p.max_seen(), p.sum(), |
| 75 | p.sum_of_squares(), p.count()); |
Craig Tiller | 76877c3 | 2015-03-03 16:04:23 -0800 | [diff] [blame] | 76 | } |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 77 | |
Jan Tattermusch | c5ebbd5 | 2015-11-18 16:50:47 -0800 | [diff] [blame] | 78 | static double default_resolution() { return 0.01; } |
| 79 | static double default_max_possible() { return 60e9; } |
| 80 | |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 81 | private: |
| 82 | Histogram(const Histogram&); |
| 83 | Histogram& operator=(const Histogram&); |
| 84 | |
| 85 | gpr_histogram* impl_; |
| 86 | }; |
Craig Tiller | 6af9ed0 | 2015-03-02 22:42:10 -0800 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Craig Tiller | 10923c2 | 2015-03-03 14:24:49 -0800 | [diff] [blame] | 90 | #endif /* TEST_QPS_HISTOGRAM_H */ |