SlidingTimeWindowReservoir
</>复制代码
/**
* A {@link Reservoir} implementation backed by a sliding window that stores only the measurements made
* in the last {@code N} seconds (or other time unit).
*/
public class SlidingTimeWindowReservoir implements Reservoir {
// allow for this many duplicate ticks before overwriting measurements
private static final int COLLISION_BUFFER = 256;
// only trim on updating once every N
private static final int TRIM_THRESHOLD = 256;
private final Clock clock;
private final ConcurrentSkipListMap measurements;
private final long window;
private final AtomicLong lastTick;
private final AtomicLong count;
/**
* Creates a new {@link SlidingTimeWindowReservoir} with the given window of time.
*
* @param window the window of time
* @param windowUnit the unit of {@code window}
*/
public SlidingTimeWindowReservoir(long window, TimeUnit windowUnit) {
this(window, windowUnit, Clock.defaultClock());
}
/**
* Creates a new {@link SlidingTimeWindowReservoir} with the given clock and window of time.
*
* @param window the window of time
* @param windowUnit the unit of {@code window}
* @param clock the {@link Clock} to use
*/
public SlidingTimeWindowReservoir(long window, TimeUnit windowUnit, Clock clock) {
this.clock = clock;
this.measurements = new ConcurrentSkipListMap();
this.window = windowUnit.toNanos(window) * COLLISION_BUFFER;
this.lastTick = new AtomicLong(clock.getTick() * COLLISION_BUFFER);
this.count = new AtomicLong();
}
@Override
public int size() {
trim();
return measurements.size();
}
@Override
public void update(long value) {
if (count.incrementAndGet() % TRIM_THRESHOLD == 0) {
trim();
}
measurements.put(getTick(), value);
}
@Override
public Snapshot getSnapshot() {
trim();
return new UniformSnapshot(measurements.values());
}
private long getTick() {
for (; ; ) {
final long oldTick = lastTick.get();
final long tick = clock.getTick() * COLLISION_BUFFER;
// ensure the tick is strictly incrementing even if there are duplicate ticks
final long newTick = tick - oldTick > 0 ? tick : oldTick + 1;
if (lastTick.compareAndSet(oldTick, newTick)) {
return newTick;
}
}
}
private void trim() {
measurements.headMap(getTick() - window).clear();
}
}
ConcurrentSkipListMap.headMap
</>复制代码
public void update(long value) {
if (count.incrementAndGet() % TRIM_THRESHOLD == 0) {
trim();
}
measurements.put(getTick(), value);
}
private void trim() {
measurements.headMap(getTick() - window).clear();
}
返回key小于/等于指定值的部分,然后清除掉
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66892.html
摘要:百分位数第百分位数是这样一个值,它使得至少有的数据项小于或等于这个值,且至少有的数据项大于或等于这个值。即使极值变动大,相比其他几个,还是比较接近实际数据,曲线会有明显变动,不像其他的一段时间可能都是平滑的。 基本概念 mean(平均值) 均值是就全部数据计算的,它具有优良的数学性质,是实际中应用最广泛的集中趋势测度值.其主要缺点是易受数据极端值的影响,对于偏态分布的数据,均值的代表性...
摘要:单位介绍既然扯到了这个单位,那就有必要先解释下这个单位具体含义,上的解释注意图中的画线部分,这个单位需要高版本的浏览器支持,不过,一般用于移动端布局,所以,基本上无需考虑浏览器版本问题,放心用吧。相对于根元素的大小,自动计算出其具体值。 A. 先看一个函数: /* * 设置根元素字体大小 * @param Number minSW 最小缩放的设备屏幕宽度 * @param Nu...
摘要:需求控制编辑框上传的图片不超过。从文件中找到如下图代码位置,选取需要上传的图片,获取图片的大小,将其当做新增参数,给标签新增属性。我的解决方法就是将在中加载出来的模态框中,关闭按钮的属性替换成代码的事件,用来隐藏模态框,如下图所示。 这里先看一下summernote.js模样,如下图,可以添加图片、视频、链接等。showImg(https://segmentfault.com/img/...
摘要:首先屏幕是由一个一个显示单元组成的每一个显示单元都是物理世界真实存在的把一个显示单元的大小称为一个物理像素通常我们所说的分辨率就是指一块屏幕显示单元的个数比如叉表示这块屏幕由叉个显示单元组成其次通常情况下个显示单元对应计算机系统中的也就是说 首先 屏幕是由一个一个显示单元组成的.1 每一个显示单元都是物理世界真实存在的;2 把一个显示单元的大小称为一个物理像素;3 通常我们所说的 分辨...
阅读 1895·2019-08-30 15:55
阅读 1060·2019-08-30 15:44
阅读 960·2019-08-30 10:48
阅读 2142·2019-08-29 13:42
阅读 3282·2019-08-29 11:16
阅读 1491·2019-08-29 11:09
阅读 2134·2019-08-26 11:46
阅读 705·2019-08-26 11:44