小组成员:谢瑞阳、徐翔宇
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

139 rindas
4.6 KiB

  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #include <math.h>
  5. #include <stdio.h>
  6. #include "port/port.h"
  7. #include "util/histogram.h"
  8. namespace leveldb {
  9. const double Histogram::kBucketLimit[kNumBuckets] = {
  10. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 25, 30, 35, 40, 45,
  11. 50, 60, 70, 80, 90, 100, 120, 140, 160, 180, 200, 250, 300, 350, 400, 450,
  12. 500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000, 2500, 3000,
  13. 3500, 4000, 4500, 5000, 6000, 7000, 8000, 9000, 10000, 12000, 14000,
  14. 16000, 18000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 60000,
  15. 70000, 80000, 90000, 100000, 120000, 140000, 160000, 180000, 200000,
  16. 250000, 300000, 350000, 400000, 450000, 500000, 600000, 700000, 800000,
  17. 900000, 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2500000,
  18. 3000000, 3500000, 4000000, 4500000, 5000000, 6000000, 7000000, 8000000,
  19. 9000000, 10000000, 12000000, 14000000, 16000000, 18000000, 20000000,
  20. 25000000, 30000000, 35000000, 40000000, 45000000, 50000000, 60000000,
  21. 70000000, 80000000, 90000000, 100000000, 120000000, 140000000, 160000000,
  22. 180000000, 200000000, 250000000, 300000000, 350000000, 400000000,
  23. 450000000, 500000000, 600000000, 700000000, 800000000, 900000000,
  24. 1000000000, 1200000000, 1400000000, 1600000000, 1800000000, 2000000000,
  25. 2500000000.0, 3000000000.0, 3500000000.0, 4000000000.0, 4500000000.0,
  26. 5000000000.0, 6000000000.0, 7000000000.0, 8000000000.0, 9000000000.0,
  27. 1e200,
  28. };
  29. void Histogram::Clear() {
  30. min_ = kBucketLimit[kNumBuckets-1];
  31. max_ = 0;
  32. num_ = 0;
  33. sum_ = 0;
  34. sum_squares_ = 0;
  35. for (int i = 0; i < kNumBuckets; i++) {
  36. buckets_[i] = 0;
  37. }
  38. }
  39. void Histogram::Add(double value) {
  40. // Linear search is fast enough for our usage in db_bench
  41. int b = 0;
  42. while (b < kNumBuckets - 1 && kBucketLimit[b] <= value) {
  43. b++;
  44. }
  45. buckets_[b] += 1.0;
  46. if (min_ > value) min_ = value;
  47. if (max_ < value) max_ = value;
  48. num_++;
  49. sum_ += value;
  50. sum_squares_ += (value * value);
  51. }
  52. void Histogram::Merge(const Histogram& other) {
  53. if (other.min_ < min_) min_ = other.min_;
  54. if (other.max_ > max_) max_ = other.max_;
  55. num_ += other.num_;
  56. sum_ += other.sum_;
  57. sum_squares_ += other.sum_squares_;
  58. for (int b = 0; b < kNumBuckets; b++) {
  59. buckets_[b] += other.buckets_[b];
  60. }
  61. }
  62. double Histogram::Median() const {
  63. return Percentile(50.0);
  64. }
  65. double Histogram::Percentile(double p) const {
  66. double threshold = num_ * (p / 100.0);
  67. double sum = 0;
  68. for (int b = 0; b < kNumBuckets; b++) {
  69. sum += buckets_[b];
  70. if (sum >= threshold) {
  71. // Scale linearly within this bucket
  72. double left_point = (b == 0) ? 0 : kBucketLimit[b-1];
  73. double right_point = kBucketLimit[b];
  74. double left_sum = sum - buckets_[b];
  75. double right_sum = sum;
  76. double pos = (threshold - left_sum) / (right_sum - left_sum);
  77. double r = left_point + (right_point - left_point) * pos;
  78. if (r < min_) r = min_;
  79. if (r > max_) r = max_;
  80. return r;
  81. }
  82. }
  83. return max_;
  84. }
  85. double Histogram::Average() const {
  86. if (num_ == 0.0) return 0;
  87. return sum_ / num_;
  88. }
  89. double Histogram::StandardDeviation() const {
  90. if (num_ == 0.0) return 0;
  91. double variance = (sum_squares_ * num_ - sum_ * sum_) / (num_ * num_);
  92. return sqrt(variance);
  93. }
  94. std::string Histogram::ToString() const {
  95. std::string r;
  96. char buf[200];
  97. snprintf(buf, sizeof(buf),
  98. "Count: %.0f Average: %.4f StdDev: %.2f\n",
  99. num_, Average(), StandardDeviation());
  100. r.append(buf);
  101. snprintf(buf, sizeof(buf),
  102. "Min: %.4f Median: %.4f Max: %.4f\n",
  103. (num_ == 0.0 ? 0.0 : min_), Median(), max_);
  104. r.append(buf);
  105. r.append("------------------------------------------------------\n");
  106. const double mult = 100.0 / num_;
  107. double sum = 0;
  108. for (int b = 0; b < kNumBuckets; b++) {
  109. if (buckets_[b] <= 0.0) continue;
  110. sum += buckets_[b];
  111. snprintf(buf, sizeof(buf),
  112. "[ %7.0f, %7.0f ) %7.0f %7.3f%% %7.3f%% ",
  113. ((b == 0) ? 0.0 : kBucketLimit[b-1]), // left
  114. kBucketLimit[b], // right
  115. buckets_[b], // count
  116. mult * buckets_[b], // percentage
  117. mult * sum); // cumulative percentage
  118. r.append(buf);
  119. // Add hash marks based on percentage; 20 marks for 100%.
  120. int marks = static_cast<int>(20*(buckets_[b] / num_) + 0.5);
  121. r.append(marks, '#');
  122. r.push_back('\n');
  123. }
  124. return r;
  125. }
  126. } // namespace leveldb