采样最近X秒内收到的平均值 [英] Sample the average of values received in last X seconds

查看:75
本文介绍了采样最近X秒内收到的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个分派成功和失败事件的类,我需要维护该类在最近X秒钟内的平均失败次数/事件总数的统计信息。

I have a class that dispatches a Success and a Failure event and I need to maintain a statistic on the average number of failure/total number of events in the last X seconds from that class.

我在考虑使用循环链接列表并为每个事件附加成功或失败节点的思路。然后计算列表中的故障节点数与总节点数,但这有两个主要缺点:

I was thinking something along the lines of using a circular linked list and append a success or failure node for each event. Then count the numbers of failure nodes vs. total nodes in the list, but this has two major drawbacks:


  1. 我需要不断扩展列表大小的增加/减少,以适应最后X秒的要求(每秒的事件数可以更改)

  2. 我需要不断循环遍历列表并计算所有事件(可能很昂贵,因为我每秒可能会发生100个此类事件)

有人知道过另一种从列表中计算平均值的方法吗? X秒钟内收到的采样数是多少?

Does anyone know of another way to compute average values from a list of samples received in the last X seconds?

推荐答案

您应使用采样频率(a-la MRTG)。假设您只需要一秒钟的精度,并且要保持过去一分钟的平均值,您将有一个固定的表,其中包含过去60秒(包括现在)在内的60个条目。并保持当前的全局条目。

You should use a sampling frequency (a-la MRTG). Say you only need one second precision and to maintain the average for the past minute, you will have a fixed table of 60 entries referring to the past 60 seconds (including the present one). And also maintain the current global entry.

每个条目都包含一个平均值和多个事件。每个条目的两个值都从0开始。

Each entry consists of an average value and a number of events. Every entry starts at 0 for both value.

收到新事件后,您可以像这样更改当前条目和全局条目:

When you receive a new event, you change the current and the global entry like that:


average = ((number * average) + 1) / (number + 1)
number = number + 1

在每个采样间隔,您都使用最早的条目来更改全局条目:

At each sampling interval you change the global entry using the oldest entry:


global.average = ((global.number * global.average) - (oldest.number * oldest.average)) / (global.number - oldest.number)
global.number = global.number - oldest.number

然后将最旧的条目重置为0并开始将其用作当前条目。

And you reset the oldest entry to 0 and start using it as the current one.

这篇关于采样最近X秒内收到的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆