histogram_snapshot.cpp 3.1 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/* 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()
{
}

HistogramSnapShot::HistogramSnapShot(const std::vector<double>& collection)
{
  set_collection(collection);
}

HistogramSnapShot::~HistogramSnapShot()
{
}

void HistogramSnapShot::set_collection(const std::vector<double>& collection)
{
    if (collection.empty())
    {
        return;
    }
    
    data_ = collection;
    std::sort(data_.begin(), data_.end());
}

size_t HistogramSnapShot::size() const
{
    return data_.size();
}

double HistogramSnapShot::get_value(double quantile)
{
    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);
}

double HistogramSnapShot::get_median()
{
    return get_value(0.5f);
}

double HistogramSnapShot::get_75th()
{
    return get_value(0.75f);
}

double HistogramSnapShot::get_90th()
{
    return get_value(0.90f);
}

double HistogramSnapShot::get_95th()
{
    return get_value(0.95f);
}
double HistogramSnapShot::get_99th()
{
    return get_value(0.99f);
    
}
double HistogramSnapShot::get_999th()
{
    return get_value(0.999f);
}

double HistogramSnapShot::get_max()
{
    if (data_.empty())
    {
        return 0.0f;
    }
    
    return static_cast<double>(*data_.rbegin());
}

double HistogramSnapShot::get_min()
{
    if (data_.empty())
    {
        return 0.0f;
    }
    
    return static_cast<double>(*data_.begin());
}

double HistogramSnapShot::get_mean()
{
    if (data_.empty())
    {
        return 0.0f;
    }
    
    return std::accumulate(data_.begin(), data_.end(), (double)0) * 1.0f / data_.size();
}


const std::vector<double> & HistogramSnapShot::get_values()
{
    return data_;
}

std::string HistogramSnapShot::to_string() {
  std::stringstream oss;
  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();

  return oss.str();
}


} // namespace common