10225501448 李度 10225101546 陈胤遒 10215501422 高宇菲
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

128 lignes
4.3 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. double Histogram::Median() const {
  53. return Percentile(50.0);
  54. }
  55. double Histogram::Percentile(double p) const {
  56. double threshold = num_ * (p / 100.0);
  57. double sum = 0;
  58. for (int b = 0; b < kNumBuckets; b++) {
  59. sum += buckets_[b];
  60. if (sum >= threshold) {
  61. // Scale linearly within this bucket
  62. double left_point = (b == 0) ? 0 : kBucketLimit[b-1];
  63. double right_point = kBucketLimit[b];
  64. double left_sum = sum - buckets_[b];
  65. double right_sum = sum;
  66. double pos = (threshold - left_sum) / (right_sum - left_sum);
  67. double r = left_point + (right_point - left_point) * pos;
  68. if (r < min_) r = min_;
  69. if (r > max_) r = max_;
  70. return r;
  71. }
  72. }
  73. return max_;
  74. }
  75. double Histogram::Average() const {
  76. if (num_ == 0.0) return 0;
  77. return sum_ / num_;
  78. }
  79. double Histogram::StandardDeviation() const {
  80. if (num_ == 0.0) return 0;
  81. double variance = (sum_squares_ * num_ - sum_ * sum_) / (num_ * num_);
  82. return sqrt(variance);
  83. }
  84. std::string Histogram::ToString() const {
  85. std::string r;
  86. char buf[200];
  87. snprintf(buf, sizeof(buf),
  88. "Count: %.0f Average: %.4f StdDev: %.2f\n",
  89. num_, Average(), StandardDeviation());
  90. r.append(buf);
  91. snprintf(buf, sizeof(buf),
  92. "Min: %.4f Median: %.4f Max: %.4f\n",
  93. (num_ == 0.0 ? 0.0 : min_), Median(), max_);
  94. r.append(buf);
  95. r.append("------------------------------------------------------\n");
  96. const double mult = 100.0 / num_;
  97. double sum = 0;
  98. for (int b = 0; b < kNumBuckets; b++) {
  99. if (buckets_[b] <= 0.0) continue;
  100. sum += buckets_[b];
  101. snprintf(buf, sizeof(buf),
  102. "[ %7.0f, %7.0f ) %7.0f %7.3f%% %7.3f%% ",
  103. ((b == 0) ? 0.0 : kBucketLimit[b-1]), // left
  104. kBucketLimit[b], // right
  105. buckets_[b], // count
  106. mult * buckets_[b], // percentage
  107. mult * sum); // cumulative percentage
  108. r.append(buf);
  109. // Add hash marks based on percentage; 20 marks for 100%.
  110. int marks = static_cast<int>(20*(buckets_[b] / num_) + 0.5);
  111. r.append(marks, '#');
  112. r.push_back('\n');
  113. }
  114. return r;
  115. }
  116. }