Skip to content

Commit

Permalink
lib,stats,dict: Make stats_dist_get_avg() return double
Browse files Browse the repository at this point in the history
  • Loading branch information
mrannanj committed Aug 22, 2018
1 parent 09c856b commit e58933f
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/dict/main.c
Expand Up @@ -27,7 +27,7 @@ static bool proctitle_updated;
static void
add_stats_string(string_t *str, struct stats_dist *stats, const char *name)
{
str_printfa(str, ", %u %s:%"PRIu64"/%"PRIu64"/%"PRIu64"/%"PRIu64,
str_printfa(str, ", %u %s:%"PRIu64"/%.02f/%"PRIu64"/%"PRIu64,
stats_dist_get_count(stats), name,
stats_dist_get_min(stats)/1000, stats_dist_get_avg(stats)/1000,
stats_dist_get_95th(stats)/1000, stats_dist_get_max(stats)/1000);
Expand Down
6 changes: 3 additions & 3 deletions src/lib/stats-dist.c
Expand Up @@ -87,12 +87,12 @@ uint64_t stats_dist_get_max(const struct stats_dist *stats)
return stats->max;
}

uint64_t stats_dist_get_avg(const struct stats_dist *stats)
double stats_dist_get_avg(const struct stats_dist *stats)
{
if (stats->count == 0)
return 0;

return (stats->sum + stats->count/2) / stats->count;
return (double)stats->sum / stats->count;
}

static void stats_dist_ensure_sorted(struct stats_dist *stats)
Expand Down Expand Up @@ -127,7 +127,7 @@ double stats_dist_get_variance(const struct stats_dist *stats)
if (stats->count == 0)
return 0;

double avg = (double)(int64_t)stats_dist_get_avg(stats);
double avg = stats_dist_get_avg(stats);
double count = (stats->count < stats->sample_count)
? stats->count
: stats->sample_count;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/stats-dist.h
Expand Up @@ -21,7 +21,7 @@ uint64_t stats_dist_get_min(const struct stats_dist *stats);
/* Returns events' maximum. */
uint64_t stats_dist_get_max(const struct stats_dist *stats);
/* Returns events' average. */
uint64_t stats_dist_get_avg(const struct stats_dist *stats);
double stats_dist_get_avg(const struct stats_dist *stats);
/* Returns events' approximate (through random subsampling) median. */
uint64_t stats_dist_get_median(const struct stats_dist *stats);
/* Returns events' variance */
Expand Down
8 changes: 6 additions & 2 deletions src/lib/test-stats-dist.c
Expand Up @@ -3,6 +3,9 @@
#include "test-lib.h"
#include "stats-dist.h"
#include "sort.h"
#include "math.h"

#define DBL_EQ(a, b) (fabs((a)-(b)) < 0.001)

static void
test_stats_dist_verify(const struct stats_dist *t, const int64_t *input,
Expand Down Expand Up @@ -31,7 +34,8 @@ test_stats_dist_verify(const struct stats_dist *t, const int64_t *input,
test_assert_idx(stats_dist_get_sum(t) == sum, input_size);
test_assert_idx(stats_dist_get_min(t) == min, input_size);
test_assert_idx(stats_dist_get_max(t) == max, input_size);
test_assert_idx(stats_dist_get_avg(t) == (sum + input_size/2)/input_size, input_size);
test_assert_idx(DBL_EQ(stats_dist_get_avg(t), (double)sum/input_size),
input_size);

/* these aren't always fully accurate: */
test_assert_idx(stats_dist_get_median(t) >= copy[(input_size-1)/2] &&
Expand Down Expand Up @@ -86,7 +90,7 @@ void test_stats_dist(void)
test_assert(stats_dist_get_sum(t) == (i-1)*i/2);
test_assert(stats_dist_get_min(t) == 0);
test_assert(stats_dist_get_max(t) == i-1);
test_assert(stats_dist_get_avg(t) == i/2);
test_assert(DBL_EQ(stats_dist_get_avg(t), 4999.500000));
/* just test that these work: */
test_assert(stats_dist_get_median(t) > 0 && stats_dist_get_median(t) < i-1);
test_assert(stats_dist_get_95th(t) > 0 && stats_dist_get_95th(t) < i-1);
Expand Down
2 changes: 1 addition & 1 deletion src/stats/client-reader.c
Expand Up @@ -51,7 +51,7 @@ static void reader_client_dump_stats(string_t *str, struct stats_dist *stats,
else if (strcmp(field, "max") == 0)
str_printfa(str, "%"PRIu64, stats_dist_get_max(stats));
else if (strcmp(field, "avg") == 0)
str_printfa(str, "%"PRIu64, stats_dist_get_avg(stats));
str_printfa(str, "%.02f", stats_dist_get_avg(stats));
else if (strcmp(field, "median") == 0)
str_printfa(str, "%"PRIu64, stats_dist_get_median(stats));
else if (strcmp(field, "variance") == 0)
Expand Down

0 comments on commit e58933f

Please sign in to comment.