aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorrsiddharth <s@ricketyspace.net>2019-09-29 19:08:30 -0400
committerrsiddharth <s@ricketyspace.net>2020-04-17 20:56:34 -0400
commit8724cf43c27922c2a9eb10846bafbe2f3d8f7cef (patch)
treeb3293ae472ff993ed8c066733b83a359e1ad5fe7
parent8f6dbe6d60a5425d7d21361d37c47d168a43d50a (diff)
downloadnserver-8724cf43c27922c2a9eb10846bafbe2f3d8f7cef.tar.gz
nserver-8724cf43c27922c2a9eb10846bafbe2f3d8f7cef.tar.bz2
nserver-8724cf43c27922c2a9eb10846bafbe2f3d8f7cef.tar.xz
nserver-8724cf43c27922c2a9eb10846bafbe2f3d8f7cef.zip
nserver: Add stats.h
-rw-r--r--nserver/src/stats.c54
-rw-r--r--nserver/src/stats.h33
2 files changed, 87 insertions, 0 deletions
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 <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));
+}
diff --git a/nserver/src/stats.h b/nserver/src/stats.h
new file mode 100644
index 0000000..997a505
--- /dev/null
+++ b/nserver/src/stats.h
@@ -0,0 +1,33 @@
+#ifndef stats_h
+#define stats_h
+#include <math.h>
+
+typedef struct Stats {
+ double sum;
+ double sumsq;
+ unsigned long n;
+ double min;
+ double max;
+} Stats;
+
+Stats *Stats_recreate(double sum, double sumsq, unsigned long n,
+ double min, double max);
+
+Stats *Stats_create();
+
+void Stats_sample(Stats *st, double s);
+
+void Stats_dump(Stats *st);
+
+static inline double Stats_mean(Stats *st)
+{
+ return st->sum / st->n;
+}
+
+static inline double Stats_stddev(Stats *st)
+{
+ return sqrt((st->sumsq - (st->sum * st->sum / st->n)) /
+ (st->n - 1));
+}
+
+#endif