latester: Add percentiles to statistics
authorMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 4 Feb 2014 17:32:56 +0000 (18:32 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 4 Feb 2014 17:32:56 +0000 (18:32 +0100)
latester/histogram.h
latester/latester.c

index 80d629e..b7c1cca 100644 (file)
@@ -52,5 +52,41 @@ void histogram_fprint(struct histogram *h, FILE *f)
 }
 
 
+struct histogram_stats {
+       unsigned count;
+       unsigned long long sum;
+       unsigned avg;
+       unsigned percentile[100];
+};
+
+void histogram_stats(struct histogram *h, struct histogram_stats *s)
+{
+       unsigned long long sum;
+       unsigned i, j;
+
+       if (!s)
+               return;
+
+       memset(s, 0, sizeof(*s));
+
+       for (i = 0; i < h->allocated; i++) {
+               s->count += h->data[i];
+               s->sum += h->data[i] * i * h->resolution;
+       }
+       if (s->count == 0)
+               return;
+
+       s->avg = s->sum / s->count;
+
+       for (i = 0, j = 0, sum = 0; i < 100; i++) {
+               while (sum <= i * s->count / 100)
+                       sum += h->data[j++];
+
+               s->percentile[i] = (j-1) * h->resolution;
+       }
+       while (sum < i * s->count / 100)
+               sum += h->data[j++];
+       s->percentile[100] = (j-1) * h->resolution;
+}
 
 #endif
index 8b1fb86..ff627dc 100644 (file)
@@ -865,6 +865,7 @@ int main(int argc, const char *argv[])
                fclose(opt.f_msgs);
 
        if (opt.f_stat) {
+               struct histogram_stats hs;
                fprintf(opt.f_stat, "cmdline='");
                for (i=0; i<argc; i++)
                        fprintf(opt.f_stat, "%s%s", argv[i], i < argc-1 ? " " : "");
@@ -872,7 +873,7 @@ int main(int argc, const char *argv[])
 
                timespec_subtract(&diff, &stats.tac, &stats.tic);
                fprintf(opt.f_stat, "duration=%s # seconds\n", tstamp_str(NULL, &diff));
-       
+
                fprintf(opt.f_stat, "sent=%d\n", count);
                fprintf(opt.f_stat, "overrun=%d\n", stats.overrun);
                if (stats.overrun && !opt.quiet)
@@ -890,6 +891,12 @@ int main(int argc, const char *argv[])
                if (stats.timeouts && !opt.quiet)
                        printf("invalid_frame=%d\n", stats.timeouts);
 
+               histogram_stats(&histogram_gw, &hs);
+               double avg = hs.count ? (double)hs.sum/hs.count : 0;
+               fprintf(opt.f_stat, "avg=%g\n", avg);
+               for (i = 0; i <= 20; i++)
+                       fprintf(opt.f_stat, "percentile%d=%d\n", i*5, hs.percentile[i*5]);
+
                fclose(opt.f_stat);
        }