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. 【SQL查询】正则表达式匹配字符串

    1. 元字符说明 元字符 含义 ^ 匹配输入字符串的开始位置. $ 匹配输入字符串的结尾位置. * 匹配前面的字符零次或多次. + 匹配前面的字符一次或多次. ? 匹配前面的字符零次或一次. . 匹配 ...

  2. three.js入门系列之旋转的圆台、球体、正方体

    先来张图: 一.调整机位和辅助线 由上述代码可知,现在的机位是三维坐标轴上的点(2,2,2),方框的那一句很重要,有了这一句,你将获得上帝视角!!! 接下来添加辅助线(立体空间三轴): 这样就添加了一 ...

  3. Azure .Net应用架构原型

    本文介绍一种部署在azure 上.net 应用的一种常用架构对接者.这个角色可以是Api GateWay或代理或负载均衡器.目的有多方面,集中管理机器的注册和监控,安全,负载均衡,请求过滤,反向代理等 ...

  4. angular 中不要使用location.href

    location.href = '#/HKorderList?gid='+gid+'&gname='+encodeURIComponent(gname)+'&cPeriod='+$(' ...

  5. python基础第一章

    Python基础 第一个python程序 变量 程序交互 基本数据类型 格式化输出 基本运算符 流程控制if...else... 流程控制-循环 第一个python程序 文件执行 1.用notepad ...

  6. [javascript][转载]jQuery获取Select选择的Text和 Value

    原文地址: http://www.cnblogs.com/yaoshiyou/archive/2010/08/24/1806939.html http://www.cnblogs.com/SAL292 ...

  7. 【测试技术】websocket-client

    Websocket Client 继续上周的议题.因为我不会写go,不会写websocket客户端,导致整个测试过程我是个完美的酱油党.上周我终于把客户端服务写好了. 选择Websokcet框架 现在 ...

  8. [译]TLS中的RC4被攻破了,现在该怎么办?

    原文链接:https://community.qualys.com/blogs/securitylabs/2013/03/19/rc4-in-tls-is-broken-now-what 原文发表时间 ...

  9. Quartz 2D编程指南(7) - 阴影(Shadows)

    阴影是绘制在一个图形对象下的且有一定偏移的图片,它用于模拟光源照射到图形对象上所形成的阴影效果,如果7-1所示.文本也可以有阴影.阴影可以让一幅图像看上去是立体的或者是浮动的. 阴影有三个属性: 1. ...

  10. thinkphp5 下 的Nginx 伪静态

    server { listen 80; server_name all.bjed.com; root "F:\www\asdata"; location / { index ind ...