资讯专栏INFORMATION COLUMN

SlidingTimeWindowReservoir的大小控制

txgcwm / 2635人阅读

SlidingTimeWindowReservoir

</>复制代码

  1. /**
  2. * A {@link Reservoir} implementation backed by a sliding window that stores only the measurements made
  3. * in the last {@code N} seconds (or other time unit).
  4. */
  5. public class SlidingTimeWindowReservoir implements Reservoir {
  6. // allow for this many duplicate ticks before overwriting measurements
  7. private static final int COLLISION_BUFFER = 256;
  8. // only trim on updating once every N
  9. private static final int TRIM_THRESHOLD = 256;
  10. private final Clock clock;
  11. private final ConcurrentSkipListMap measurements;
  12. private final long window;
  13. private final AtomicLong lastTick;
  14. private final AtomicLong count;
  15. /**
  16. * Creates a new {@link SlidingTimeWindowReservoir} with the given window of time.
  17. *
  18. * @param window the window of time
  19. * @param windowUnit the unit of {@code window}
  20. */
  21. public SlidingTimeWindowReservoir(long window, TimeUnit windowUnit) {
  22. this(window, windowUnit, Clock.defaultClock());
  23. }
  24. /**
  25. * Creates a new {@link SlidingTimeWindowReservoir} with the given clock and window of time.
  26. *
  27. * @param window the window of time
  28. * @param windowUnit the unit of {@code window}
  29. * @param clock the {@link Clock} to use
  30. */
  31. public SlidingTimeWindowReservoir(long window, TimeUnit windowUnit, Clock clock) {
  32. this.clock = clock;
  33. this.measurements = new ConcurrentSkipListMap();
  34. this.window = windowUnit.toNanos(window) * COLLISION_BUFFER;
  35. this.lastTick = new AtomicLong(clock.getTick() * COLLISION_BUFFER);
  36. this.count = new AtomicLong();
  37. }
  38. @Override
  39. public int size() {
  40. trim();
  41. return measurements.size();
  42. }
  43. @Override
  44. public void update(long value) {
  45. if (count.incrementAndGet() % TRIM_THRESHOLD == 0) {
  46. trim();
  47. }
  48. measurements.put(getTick(), value);
  49. }
  50. @Override
  51. public Snapshot getSnapshot() {
  52. trim();
  53. return new UniformSnapshot(measurements.values());
  54. }
  55. private long getTick() {
  56. for (; ; ) {
  57. final long oldTick = lastTick.get();
  58. final long tick = clock.getTick() * COLLISION_BUFFER;
  59. // ensure the tick is strictly incrementing even if there are duplicate ticks
  60. final long newTick = tick - oldTick > 0 ? tick : oldTick + 1;
  61. if (lastTick.compareAndSet(oldTick, newTick)) {
  62. return newTick;
  63. }
  64. }
  65. }
  66. private void trim() {
  67. measurements.headMap(getTick() - window).clear();
  68. }
  69. }
ConcurrentSkipListMap.headMap

</>复制代码

  1. public void update(long value) {
  2. if (count.incrementAndGet() % TRIM_THRESHOLD == 0) {
  3. trim();
  4. }
  5. measurements.put(getTick(), value);
  6. }
  7. private void trim() {
  8. measurements.headMap(getTick() - window).clear();
  9. }

返回key小于/等于指定值的部分,然后清除掉

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66892.html

相关文章

  • 关于codahaleHistogramMetric

    摘要:百分位数第百分位数是这样一个值,它使得至少有的数据项小于或等于这个值,且至少有的数据项大于或等于这个值。即使极值变动大,相比其他几个,还是比较接近实际数据,曲线会有明显变动,不像其他的一段时间可能都是平滑的。 基本概念 mean(平均值) 均值是就全部数据计算的,它具有优良的数学性质,是实际中应用最广泛的集中趋势测度值.其主要缺点是易受数据极端值的影响,对于偏态分布的数据,均值的代表性...

    JiaXinYi 评论0 收藏0
  • rem 单位实现页面自适应布局

    摘要:单位介绍既然扯到了这个单位,那就有必要先解释下这个单位具体含义,上的解释注意图中的画线部分,这个单位需要高版本的浏览器支持,不过,一般用于移动端布局,所以,基本上无需考虑浏览器版本问题,放心用吧。相对于根元素的大小,自动计算出其具体值。 A. 先看一个函数: /* * 设置根元素字体大小 * @param Number minSW 最小缩放的设备屏幕宽度 * @param Nu...

    xavier 评论0 收藏0
  • summernote.js实际运用中遇到上传图片大小控制及bootstrap模态框嵌套关闭这两个问

    摘要:需求控制编辑框上传的图片不超过。从文件中找到如下图代码位置,选取需要上传的图片,获取图片的大小,将其当做新增参数,给标签新增属性。我的解决方法就是将在中加载出来的模态框中,关闭按钮的属性替换成代码的事件,用来隐藏模态框,如下图所示。 这里先看一下summernote.js模样,如下图,可以添加图片、视频、链接等。showImg(https://segmentfault.com/img/...

    madthumb 评论0 收藏0
  • viewport 理解

    摘要:首先屏幕是由一个一个显示单元组成的每一个显示单元都是物理世界真实存在的把一个显示单元的大小称为一个物理像素通常我们所说的分辨率就是指一块屏幕显示单元的个数比如叉表示这块屏幕由叉个显示单元组成其次通常情况下个显示单元对应计算机系统中的也就是说 首先 屏幕是由一个一个显示单元组成的.1 每一个显示单元都是物理世界真实存在的;2 把一个显示单元的大小称为一个物理像素;3 通常我们所说的 分辨...

    makeFoxPlay 评论0 收藏0

发表评论

0条评论

txgcwm

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<