SlidingAverageTime

Sliding Average Time Class

SlidingAverageTime offers a few more functions. It is for the use case when you don't want to add one value at once, but instead add on top of the last value until push() is called, which should be done periodically. The class is aware of that period and adjusts the added values accordingly. You tell it how much time a single completed value corresponds to and what time output resultion you desire.

Usage Example

1 
2 import ocean.math.SlidingAverage;
3 import ocean.io.select.EpollSelectDispatcher;
4 import ocean.io.select.client.TimerEvent;
5 
6 import ocean.io.Stdout;
7 
8 void main ()
9 {
10     // One stat output for the amount of records
11     auto avg_stats = new SlidingAverageTime!(size_t)(100, 50, 1000);
12     // one stat output for the amount of bytes
13     auto avg_byte_stats = new SlidingAverageTime!(size_t)(100, 50, 1000);
14 
15     // Called by the udpate_timer
16     bool update ( )
17     {
18         // Push accumulated data to the list of values used for caluclation
19         // of average
20         avg_stats.push();
21         return true;
22     }
23 
24     // called by the display timer
25     bool display_stats ( )
26     {
27         Stdout.formatln("Processed {} (avg {}) records,\n"
28                         "          {} bytes, (avg {} bytes)",
29                         avg_stats.last(), avg_stats.average(),
30                         avg_byte_stats.last, avg_byte_stats.average());
31     }
32 
33     auto epoll = new EpollSelectDispatcher;
34     auto update_timer = new TimerEvent(&update);
35     auto display_timer = new TimerEvent(&display_stats);
36 
37     // Fire timer every 50ms
38     update_timer.set(0, 0, 0, 50);
39 
40     // Fire timer every 1000ms
41     display_timer.set(0, 0, 1, 0);
42 
43     epoll.register(update_timer);
44     epoll.register(display_stats);
45 
46     // Assume that some epoll triggered handler calls this method every time
47     // the program has to process incoming data
48     void process_record ( ubyte[] data )
49     {
50         do_important_stuff();
51         avg_stats++;  // one new record
52         avg_byte_stats += data.length; // that much new data was handled
53     }
54 
55     epoll.eventLoop();
56 }

Constructors

this
this(size_t window_size, size_t resolution, size_t output_resolution)

Constructor

Members

Functions

last
T last()

Returns the last finished value

opAssign
T opAssign(T val)

Sets the current value to val

opOpAssign
T opOpAssign(T val)

Adds the given value to the current value

opUnary
T opUnary()

Increments the current value by one

push
real push()

Adds current value to the time window history. Calculates the new average and returns it

Variables

current
T current;

Contains the latest value to which new values are currently being added

resolution
real resolution;

Resolution that the output needs to be multiplied with

Meta