haproxy/include/proto
Willy Tarreau 2438f2b984 MINOR: freq_ctr: introduce a new averaging method
While the current functions report average event counts per period, we are
also interested in average values per event. For this we use a different
method. The principle is to rely on a long tail which sums the new value
with a fraction of the previous value, resulting in a sliding window of
infinite length depending on the precision we're interested in.

The idea is that we always keep (N-1)/N of the sum and add the new sampled
value. The sum over N values can be computed with a simple program for a
constant value 1 at each iteration :

    N
  ,---
   \       N - 1              e - 1
    >  ( --------- )^x ~= N * -----
   /         N                  e
  '---
  x = 1

Note: I'm not sure how to demonstrate this but at least this is easily
verified with a simple program, the sum equals N * 0.632120 for any N
moderately large (tens to hundreds).

Inserting a constant sample value V here simply results in :

   sum = V * N * (e - 1) / e

But we don't want to integrate over a small period, but infinitely. Let's
cut the infinity in P periods of N values. Each period M is exactly the same
as period M-1 with a factor of ((N-1)/N)^N applied. A test shows that given a
large N :

     N - 1           1
  ( ------- )^N ~=  ---
       N             e

Our sum is now a sum of each factor times  :

   N*P                                     P
  ,---                                   ,---
   \         N - 1               e - 1    \     1
    >  v ( --------- )^x ~= VN * -----  *  >   ---
   /           N                   e      /    e^x
  '---                                   '---
  x = 1                                  x = 0

For P "large enough", in tests we get this :

    P
  ,---
   \     1        e
    >   --- ~=  -----
   /    e^x     e - 1
  '---
  x = 0

This simplifies the sum above :

   N*P
  ,---
   \         N - 1
    >  v ( --------- )^x = VN
   /           N
  '---
  x = 1

So basically by summing values and applying the last result an (N-1)/N factor
we just get N times the values over the long term, so we can recover the
constant value V by dividing by N.

A value added at the entry of the sliding window of N values will thus be
reduced to 1/e or 36.7% after N terms have been added. After a second batch,
it will only be 1/e^2, or 13.5%, and so on. So practically speaking, each
old period of N values represents only a quickly fading ratio of the global
sum :

  period    ratio
    1       36.7%
    2       13.5%
    3       4.98%
    4       1.83%
    5       0.67%
    6       0.25%
    7       0.09%
    8       0.033%
    9       0.012%
   10       0.0045%

So after 10N samples, the initial value has already faded out by a factor of
22026, which is quite fast. If the sliding window is 1024 samples wide, it
means that a sample will only count for 1/22k of its initial value after 10k
samples went after it, which results in half of the value it would represent
using an arithmetic mean. The benefit of this method is that it's very cheap
in terms of computations when N is a power of two. This is very well suited
to record response times as large values will fade out faster than with an
arithmetic mean and will depend on sample count and not time.

Demonstrating all the above assumptions with maths instead of a program is
left as an exercise for the reader.
2014-06-17 17:15:51 +02:00
..
acl.h MINOR: pattern: store configuration reference for each acl or map pattern. 2014-03-17 18:06:07 +01:00
arg.h
auth.h MEDIUM: pattern: The match function browse itself the list or the tree. 2014-03-17 18:06:07 +01:00
backend.h MAJOR: checks: add support for a new "drain" administrative mode 2014-05-23 14:29:11 +02:00
channel.h CLEANUP: buffers: remove unused function buffer_contig_space_with_res() 2014-04-24 17:19:22 +02:00
checks.h MEDIUM: checks: simplify server up/down/nolb transitions 2014-05-23 14:29:11 +02:00
compression.h
connection.h MEDIUM: connection: Implement and extented PROXY Protocol V2 2014-05-09 08:25:38 +02:00
cttproxy.h
dumpstats.h MEDIUM: stats: reimplement HTTP keep-alive on the stats page 2014-04-24 17:24:56 +02:00
fd.h BUG/MEDIUM: polling: fix possible CPU hogging of worker processes after receiving SIGUSR1. 2014-05-20 14:57:36 +02:00
freq_ctr.h MINOR: freq_ctr: introduce a new averaging method 2014-06-17 17:15:51 +02:00
frontend.h
hdr_idx.h
lb_chash.h
lb_fas.h
lb_fwlc.h
lb_fwrr.h
lb_map.h
listener.h
log.h MINOR: configuration: File and line propagation 2014-03-17 18:06:08 +01:00
map.h
obj_type.h
pattern.h MEDIUM: pattern: use ebtree's longest match to index/lookup string beginning 2014-05-10 08:53:48 +02:00
payload.h
peers.h
pipe.h
port_range.h
proto_http.h MINOR: http: export the smp_fetch_cookie function 2014-05-02 18:05:15 +02:00
proto_tcp.h BUG/MEDIUM: stick-table: fix IPv4-to-IPv6 conversion in src_* fetches 2014-04-14 18:22:57 +02:00
proto_uxst.h
protocol.h
proxy.h MEDIUM: proxy: create a tree to store proxies by name 2014-03-15 07:48:35 +01:00
queue.h REORG: checks: put the functions in the appropriate files ! 2014-05-22 11:27:00 +02:00
raw_sock.h
sample.h MINOR: configuration: File and line propagation 2014-03-17 18:06:08 +01:00
server.h MAJOR: checks: add support for a new "drain" administrative mode 2014-05-23 14:29:11 +02:00
session.h MEDIUM: counters: stop relying on session flags at all 2014-01-28 23:34:45 +01:00
shctx.h BUG/MAJOR: ssl: Fallback to private session cache if current lock mode is not supported. 2014-05-08 22:46:32 +02:00
signal.h
ssl_sock.h MEDIUM: connection: Implement and extented PROXY Protocol V2 2014-05-09 08:25:38 +02:00
stick_table.h
stream_interface.h MEDIUM: stream-int: make si_connect() return an established state when possible 2013-12-31 23:32:12 +01:00
task.h
template.h