folly/Histogram.h

Classes


Histogram

Histogram.h defines a simple histogram class, templated on the type of data you want to store. This class is useful for tracking a large stream of data points, where you want to remember the overall distribution of the data, but do not need to remember each data point individually.

Each histogram bucket stores the number of data points that fell in the bucket, as well as the overall sum of the data points in the bucket. Note that no overflow checking is performed, so if you have a bucket with a large number of very large values, it may overflow and cause inaccurate data for this bucket. As such, the histogram class is not well suited to storing data points with very large values. However, it works very well for smaller data points such as request latencies, request or response sizes, etc.

In addition to providing access to the raw bucket data, the Histogram class also provides methods for estimating percentile values. This allows you to estimate the median value (the 50th percentile) and other values such as the 95th or 99th percentiles.

All of the buckets have the same width. The number of buckets and bucket width is fixed for the lifetime of the histogram. As such, you do need to know your expected data range ahead of time in order to have accurate statistics. The histogram does keep one bucket to store all data points that fall below the histogram minimum, and one bucket for the data points above the maximum. However, because these buckets don't have a good lower/upper bound, percentile estimates in these buckets may be inaccurate.

HistogramBuckets

The Histogram class is built on top of HistogramBucketsHistogramBuckets provides an API very similar to Histogram, but allows a user-defined bucket class. This allows users to implement more complex histogram types that store more than just the count and sum in each bucket.

When computing percentile estimates HistogramBuckets allows user-defined functions for computing the average value and data count in each bucket. This allows you to define more complex buckets which may have multiple different ways of computing the average value and the count.

For example, one use case could be tracking timeseries data in each bucket. Each set of timeseries data can have independent data in the bucket, which can show how the data distribution is changing over time.

Example Usage


Say we have code that sends many requests to remote services, and want to generate a histogram showing how long the requests take. The following code will initialize histogram with 50 buckets, tracking values between 0 and 5000. (There are 50 buckets since the bucket width is specified as 100. If the bucket width is not an even multiple of the histogram range, the last bucket will simply be shorter than the others.)

 folly::Histogram<int64_t> latencies(, , );

The addValue() method is used to add values to the histogram. Each time a request finishes we can add its latency to the histogram:

latencies.addValue(now - startTime);

You can access each of the histogram buckets to display the overall distribution. Note that bucket 0 tracks all data points that were below the specified histogram minimum, and the last bucket tracks the data points that were above the maximum.

    unsigned int numBuckets = latencies.getNumBuckets();
cout << "Below min: " << latencies.getBucketByIndex().count << "\n";
for (unsigned int n = ; n < numBuckets - ; ++n) {
cout << latencies.getBucketMin(n) << "-" << latencies.getBucketMax(n)
<< ": " << latencies.getBucketByIndex(n).count << "\n";
}
cout << "Above max: "
<< latencies.getBucketByIndex(numBuckets - ).count << "\n";

You can also use the getPercentileEstimate() method to estimate the value at the Nth percentile in the distribution. For example, to estimate the median, as well as the 95th and 99th percentile values:

    int64_t median = latencies.getPercentileEstimate(0.5);
int64_t p95 = latencies.getPercentileEstimate(0.95);
int64_t p99 = latencies.getPercentileEstimate(0.99);

Thread Safety


Note that Histogram and HistogramBuckets objects are not thread-safe. If you wish to access a single Histogram from multiple threads, you must perform your own locking to ensure that multiple threads do not access it at the same time.

Histogram的更多相关文章

  1. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  2. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  3. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  4. LeetCode: Largest Rectangle in Histogram(直方图最大面积)

    http://blog.csdn.net/abcbc/article/details/8943485 具体的题目描述为: Given n non-negative integers represent ...

  5. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  6. Largest Rectangle in Histogram

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. 数据结构与算法(1)支线任务3——Largest Rectangle in Histogram

    题目如下:(https://leetcode.com/problems/largest-rectangle-in-histogram/) Given n non-negative integers r ...

  8. LeetCode之Largest Rectangle in Histogram浅析

    首先上题目 Given n non-negative integers representing the histogram's bar height where the width of each ...

  9. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

  10. Elasticsearch聚合 之 Histogram 直方图聚合

    Elasticsearch支持最直方图聚合,它在数字字段自动创建桶,并会扫描全部文档,把文档放入相应的桶中.这个数字字段既可以是文档中的某个字段,也可以通过脚本创建得出的. 桶的筛选规则 举个例子,有 ...

随机推荐

  1. dyld: lazy symbol binding failed: Symbol not found: ___sincosf_stret

    This is the error I get: dyld: lazy symbol binding failed: Symbol not found: ___sincosf_stret Refere ...

  2. 自己手写一个SpringMVC框架

    前端框架很多,但没有一个框架称霸,后端框架现在Spring已经完成大一统.所以学习Spring是Java程序员的必修课. Spring框架对于Java后端程序员来说再熟悉不过了,以前只知道它用的反射实 ...

  3. Android学习问题记录之open failed EACCES (Permission denied)

    1.问题描述 Android调用相机拍照保存,然后读取保存好的照片,在读取照片时出现异常(该异常是因为没有SD卡的读取权限所致): 11-08 11:07:46.421 8539-8539/com.c ...

  4. c++ 载入内存中dll ,以及内存注入

    用c++ 许多代码都得自己写, 这里是我自己修改的一个内存载入的一个封装库 , c++ 的程序员可以直接拿来用 特点如下: 直接在内存中载入,无磁盘占用 支持加壳保护的dll , 平时用的最多的vmp ...

  5. WebStorm的下载与安装

    百度搜索: 链接:http://www.jetbrains.com/webstorm/ 链接:http://www.jetbrains.com/student/ 学生免费授权计划 请从正规来源下载软件 ...

  6. POJ3422 Kaka's Matrix Travels 【费用流】*

    POJ3422 Kaka's Matrix Travels Description On an N × N chessboard with a non-negative number in each ...

  7. PHP匹配Email、URL、IP

    /* * 正则表达式匹配 */ $email = '137813369@qq.com'; $regex = '/\w+([−+.]\w+)*@\w+([−.]\w+)*\.\w+([−.]\w+)*/ ...

  8. {matlab}{计时函数}cputime

    putime 显示Matlab启动后所占用的CPU时间: tic,toc 秒表计时,tic是开始,toc是结束: clock,etime 前者显示系统时间,后者计算两次调用clock之间的时间差. e ...

  9. Gridview中实现求和统计功能

    GridView加入自动求和求平均值小计 效果图: 解决方案: private double sum = 0; //取指定列的数据和,你要根据具体情况对待可能你要处理的是int protected v ...

  10. python之面向对象(继承)

    类的继承 python之面向对象(继承) 面向对象的编程带来的主要好处之一是代码的重用,实现这种重用的方法之一是通过继承机制.继承完全可以理解成类之间的类型和子类型关系. 需要注意的地方:继承语法 c ...