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 TEST_QPS_HISTOGRAM_H 20 #define TEST_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: 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) : impl_(other.impl_) { other.impl_ = nullptr; } 43 Merge(const Histogram & h)44 void Merge(const Histogram& h) { grpc_histogram_merge(impl_, h.impl_); } Add(double value)45 void Add(double value) { grpc_histogram_add(impl_, value); } Percentile(double pctile)46 double Percentile(double pctile) const { 47 return grpc_histogram_percentile(impl_, pctile); 48 } Count()49 double Count() const { return grpc_histogram_count(impl_); } Swap(Histogram * other)50 void Swap(Histogram* other) { std::swap(impl_, other->impl_); } FillProto(HistogramData * p)51 void FillProto(HistogramData* p) { 52 size_t n; 53 const auto* data = grpc_histogram_get_contents(impl_, &n); 54 for (size_t i = 0; i < n; i++) { 55 p->add_bucket(data[i]); 56 } 57 p->set_min_seen(grpc_histogram_minimum(impl_)); 58 p->set_max_seen(grpc_histogram_maximum(impl_)); 59 p->set_sum(grpc_histogram_sum(impl_)); 60 p->set_sum_of_squares(grpc_histogram_sum_of_squares(impl_)); 61 p->set_count(grpc_histogram_count(impl_)); 62 } MergeProto(const HistogramData & p)63 void MergeProto(const HistogramData& p) { 64 grpc_histogram_merge_contents(impl_, &*p.bucket().begin(), p.bucket_size(), 65 p.min_seen(), p.max_seen(), p.sum(), 66 p.sum_of_squares(), p.count()); 67 } 68 default_resolution()69 static double default_resolution() { return 0.01; } default_max_possible()70 static double default_max_possible() { return 60e9; } 71 72 private: 73 Histogram(const Histogram&); 74 Histogram& operator=(const Histogram&); 75 76 grpc_histogram* impl_; 77 }; 78 } // namespace testing 79 } // namespace grpc 80 81 #endif /* TEST_QPS_HISTOGRAM_H */ 82