blob: b45c769a779309289204a9b766883ff11473ccde [file] [log] [blame]
Craig Tiller6af9ed02015-03-02 22:42:10 -08001/*
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>
vjpaiaa391922015-11-02 14:46:04 -080038#include "test/proto/benchmarks/stats.grpc.pb.h"
Craig Tiller6af9ed02015-03-02 22:42:10 -080039
40namespace grpc {
41namespace testing {
42
43class Histogram {
44 public:
Jan Tattermuschc5ebbd52015-11-18 16:50:47 -080045 // TODO: look into making histogram params not hardcoded for C++
46 Histogram() : impl_(gpr_histogram_create(default_resolution(),
47 default_max_possible())) {}
Craig Tillera182bf12015-03-04 13:54:39 -080048 ~Histogram() {
49 if (impl_) gpr_histogram_destroy(impl_);
Craig Tillerf2825142015-03-03 17:15:36 -080050 }
Craig Tillera182bf12015-03-04 13:54:39 -080051 Histogram(Histogram&& other) : impl_(other.impl_) { other.impl_ = nullptr; }
Craig Tiller6af9ed02015-03-02 22:42:10 -080052
vjpai119c1032015-10-29 01:21:04 -070053 void Merge(const Histogram& h) { gpr_histogram_merge(impl_, h.impl_); }
Craig Tiller6af9ed02015-03-02 22:42:10 -080054 void Add(double value) { gpr_histogram_add(impl_, value); }
Craig Tiller66048f42015-04-10 08:23:52 -070055 double Percentile(double pctile) const {
Craig Tiller10923c22015-03-03 14:24:49 -080056 return gpr_histogram_percentile(impl_, pctile);
57 }
Craig Tiller66048f42015-04-10 08:23:52 -070058 double Count() const { return gpr_histogram_count(impl_); }
Craig Tiller6af9ed02015-03-02 22:42:10 -080059 void Swap(Histogram* other) { std::swap(impl_, other->impl_); }
Craig Tiller76877c32015-03-03 16:04:23 -080060 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 Tillerd701c5f2015-03-03 16:05:04 -080073 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 Tiller76877c32015-03-03 16:04:23 -080076 }
Craig Tiller6af9ed02015-03-02 22:42:10 -080077
Jan Tattermuschc5ebbd52015-11-18 16:50:47 -080078 static double default_resolution() { return 0.01; }
79 static double default_max_possible() { return 60e9; }
80
Craig Tiller6af9ed02015-03-02 22:42:10 -080081 private:
82 Histogram(const Histogram&);
83 Histogram& operator=(const Histogram&);
84
85 gpr_histogram* impl_;
86};
Craig Tiller6af9ed02015-03-02 22:42:10 -080087}
88}
89
Craig Tiller10923c22015-03-03 14:24:49 -080090#endif /* TEST_QPS_HISTOGRAM_H */