summaryrefslogtreecommitdiffstats
path: root/nserver/src/stats.c
blob: 68b99cc77aeab57da373d6ef4ecc55afbbaa478e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <math.h>
#include <stats.h>
#include <stdlib.h>
#include <dbg.h>

Stats *Stats_recreate(double sum, double sumsq, unsigned long n,
                      double min, double max)
{
    Stats *st  = malloc(sizeof(Stats));
    check_mem(st);

    st->sum =  sum;
    st->sumsq = sumsq;
    st->n  = n;
    st->min = min;
    st->max = max;

    return st;

 error:
    return NULL;
}

Stats *Stats_create()
{
    return Stats_recreate(0.0, 0.0, 0L, 0.0, 0.0);
}

void Stats_sample(Stats *st, double s)
{
    st->sum  += s;
    st->sumsq += s * s;

    if (st->n == 0) {
        st->min = s;
        st->max = s;
    } else {
        if (st->min > s)
            st->min = s;
        if (st->max < s)
            st->max = s;
    }

    st->n += 1;
}

void Stats_dump(Stats *st)
{
    fprintf(stderr,
            "sum: %f, sumsq: %f, n: %ld, "
            "min: %f, max: %f, mean: %f, stddev: %f",
            st->sum, st->sumsq, st->n, st->min, st->max, Stats_mean(st),
            Stats_stddev(st));
}