From 8724cf43c27922c2a9eb10846bafbe2f3d8f7cef Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Sun, 29 Sep 2019 19:08:30 -0400 Subject: nserver: Add stats.h --- nserver/src/stats.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 nserver/src/stats.c (limited to 'nserver/src/stats.c') diff --git a/nserver/src/stats.c b/nserver/src/stats.c new file mode 100644 index 0000000..68b99cc --- /dev/null +++ b/nserver/src/stats.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +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)); +} -- cgit v1.2.3