histogram_snapshot.cpp 2.9 KB
Newer Older
羽飞's avatar
羽飞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved.
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2021/4/20.
//

#include "histogram_snapshot.h"

#include <math.h>
#include <stdio.h>

#include <stdexcept>
#include <algorithm>
#include <numeric>
#include <sstream>

namespace common {

HistogramSnapShot::HistogramSnapShot()
28
{}
羽飞's avatar
羽飞 已提交
29

30
HistogramSnapShot::HistogramSnapShot(const std::vector<double> &collection)
羽飞's avatar
羽飞 已提交
31 32 33 34 35
{
  set_collection(collection);
}

HistogramSnapShot::~HistogramSnapShot()
36
{}
羽飞's avatar
羽飞 已提交
37

38
void HistogramSnapShot::set_collection(const std::vector<double> &collection)
羽飞's avatar
羽飞 已提交
39
{
40 41 42 43 44 45
  if (collection.empty()) {
    return;
  }

  data_ = collection;
  std::sort(data_.begin(), data_.end());
羽飞's avatar
羽飞 已提交
46 47 48 49
}

size_t HistogramSnapShot::size() const
{
50
  return data_.size();
羽飞's avatar
羽飞 已提交
51 52 53 54
}

double HistogramSnapShot::get_value(double quantile)
{
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  if (quantile > 1.0f) {
    quantile = 1.0f;
  }

  if (quantile < 0.0f) {
    quantile = 0.0f;
  }

  if (data_.empty()) {
    return 0.0f;
  }

  double pos = quantile * (data_.size() + 1);

  if (pos < 1) {
    return data_[0];
  }

  if (pos >= data_.size()) {
    return data_[data_.size() - 1];
  }

  double lower = data_[(int)pos - 1];
  double upper = data_[(int)pos];

  return lower + (pos - floor(pos)) * (upper - lower);
羽飞's avatar
羽飞 已提交
81 82 83 84
}

double HistogramSnapShot::get_median()
{
85
  return get_value(0.5f);
羽飞's avatar
羽飞 已提交
86 87 88 89
}

double HistogramSnapShot::get_75th()
{
90
  return get_value(0.75f);
羽飞's avatar
羽飞 已提交
91 92 93 94
}

double HistogramSnapShot::get_90th()
{
95
  return get_value(0.90f);
羽飞's avatar
羽飞 已提交
96 97 98 99
}

double HistogramSnapShot::get_95th()
{
100
  return get_value(0.95f);
羽飞's avatar
羽飞 已提交
101 102 103
}
double HistogramSnapShot::get_99th()
{
104
  return get_value(0.99f);
羽飞's avatar
羽飞 已提交
105 106 107
}
double HistogramSnapShot::get_999th()
{
108
  return get_value(0.999f);
羽飞's avatar
羽飞 已提交
109 110 111 112
}

double HistogramSnapShot::get_max()
{
113 114 115 116 117
  if (data_.empty()) {
    return 0.0f;
  }

  return static_cast<double>(*data_.rbegin());
羽飞's avatar
羽飞 已提交
118 119 120 121
}

double HistogramSnapShot::get_min()
{
122 123 124 125 126
  if (data_.empty()) {
    return 0.0f;
  }

  return static_cast<double>(*data_.begin());
羽飞's avatar
羽飞 已提交
127 128 129 130
}

double HistogramSnapShot::get_mean()
{
131 132 133
  if (data_.empty()) {
    return 0.0f;
  }
羽飞's avatar
羽飞 已提交
134

135 136
  return std::accumulate(data_.begin(), data_.end(), (double)0) * 1.0f / data_.size();
}
羽飞's avatar
羽飞 已提交
137

138
const std::vector<double> &HistogramSnapShot::get_values()
羽飞's avatar
羽飞 已提交
139
{
140
  return data_;
羽飞's avatar
羽飞 已提交
141 142
}

143 144
std::string HistogramSnapShot::to_string()
{
羽飞's avatar
羽飞 已提交
145
  std::stringstream oss;
146 147
  oss << "mean:" << get_mean() << ",min:" << get_min() << ",max:" << get_max() << ",median:" << get_median()
      << ", 75th:" << get_75th() << ",90th:" << get_90th() << ",99th:" << get_99th() << ",999th:" << get_999th();
羽飞's avatar
羽飞 已提交
148 149 150 151

  return oss.str();
}

152
}  // namespace common