histogram.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef TEST_QPS_HISTOGRAM_H
  19. #define TEST_QPS_HISTOGRAM_H
  20. #include "src/proto/grpc/testing/stats.pb.h"
  21. #include "test/core/util/histogram.h"
  22. namespace grpc {
  23. namespace testing {
  24. class Histogram {
  25. public:
  26. // TODO(unknown): look into making histogram params not hardcoded for C++
  27. Histogram()
  28. : impl_(grpc_histogram_create(default_resolution(),
  29. default_max_possible())) {}
  30. ~Histogram() {
  31. if (impl_) grpc_histogram_destroy(impl_);
  32. }
  33. void Reset() {
  34. if (impl_) grpc_histogram_destroy(impl_);
  35. impl_ = grpc_histogram_create(default_resolution(), default_max_possible());
  36. }
  37. Histogram(Histogram&& other) noexcept : impl_(other.impl_) {
  38. other.impl_ = nullptr;
  39. }
  40. void Merge(const Histogram& h) { grpc_histogram_merge(impl_, h.impl_); }
  41. void Add(double value) { grpc_histogram_add(impl_, value); }
  42. double Percentile(double pctile) const {
  43. return grpc_histogram_percentile(impl_, pctile);
  44. }
  45. double Count() const { return grpc_histogram_count(impl_); }
  46. void Swap(Histogram* other) { std::swap(impl_, other->impl_); }
  47. void FillProto(HistogramData* p) {
  48. size_t n;
  49. const auto* data = grpc_histogram_get_contents(impl_, &n);
  50. for (size_t i = 0; i < n; i++) {
  51. p->add_bucket(data[i]);
  52. }
  53. p->set_min_seen(grpc_histogram_minimum(impl_));
  54. p->set_max_seen(grpc_histogram_maximum(impl_));
  55. p->set_sum(grpc_histogram_sum(impl_));
  56. p->set_sum_of_squares(grpc_histogram_sum_of_squares(impl_));
  57. p->set_count(grpc_histogram_count(impl_));
  58. }
  59. void MergeProto(const HistogramData& p) {
  60. grpc_histogram_merge_contents(impl_, &*p.bucket().begin(), p.bucket_size(),
  61. p.min_seen(), p.max_seen(), p.sum(),
  62. p.sum_of_squares(), p.count());
  63. }
  64. static double default_resolution() { return 0.01; }
  65. static double default_max_possible() { return 60e9; }
  66. private:
  67. Histogram(const Histogram&);
  68. Histogram& operator=(const Histogram&);
  69. grpc_histogram* impl_;
  70. };
  71. } // namespace testing
  72. } // namespace grpc
  73. #endif /* TEST_QPS_HISTOGRAM_H */