summaryrefslogtreecommitdiffstats
path: root/src/stats/test-stats-common.c
blob: 3e307a9e586e3280d91729f44ecb98a873d2245b (plain)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* Copyright (c) 2019 Dovecot authors, see the included COPYING file */

#include "test-stats-common.h"
#include <time.h>
#include <unistd.h>

struct event_category test_category = {
	.name = "test",
};

struct event_category child_test_category = {
	.name = "child",
	.parent = &test_category,
};

pool_t test_pool;
struct stats_metrics *stats_metrics = NULL;
time_t stats_startup_time;

static bool callback_added = FALSE;

static struct stats_settings *read_settings(const char *settings)
{
	struct istream *is = test_istream_create(settings);
	const char *error;
	struct setting_parser_context *ctx =
		settings_parser_init(test_pool, &stats_setting_parser_info, 0);
	if (settings_parse_stream_read(ctx, is) < 0)
		i_fatal("Failed to parse settings: %s",
			settings_parser_get_error(ctx));
	if (!settings_parser_check(ctx, test_pool, &error))
		i_fatal("Failed to parse settings: %s",
			error);
	struct stats_settings *set = settings_parser_get(ctx);
	settings_parser_deinit(&ctx);
	i_stream_unref(&is);
	return set;
}

void test_init(const char *settings_blob)
{
	if (!callback_added) {
		event_register_callback(test_stats_callback);
		callback_added = TRUE;
	}

	stats_event_categories_init();
	test_pool = pool_alloconly_create(MEMPOOL_GROWING"test pool", 2048);
	stats_startup_time = time(NULL);

	/* register test categories */
	stats_event_category_register(test_category.name, NULL);
	stats_event_category_register(child_test_category.name,
				      &test_category);
	struct stats_settings *set = read_settings(settings_blob);
	stats_metrics = stats_metrics_init(set);
}

void test_deinit(void)
{
	stats_metrics_deinit(&stats_metrics);
	stats_event_categories_deinit();
	pool_unref(&test_pool);
}

void test_event_send(struct event *event)
{
        struct failure_context ctx = {
                .type = LOG_TYPE_DEBUG,
        };

	usleep(1); /* make sure duration>0 always */
        event_send(event, &ctx, "hello");
}

uint64_t get_stats_dist_field(const char *metric_name, enum stats_dist_field field)
{
        struct stats_metrics_iter *iter =
		stats_metrics_iterate_init(stats_metrics);
        const struct metric *metric;
        while((metric = stats_metrics_iterate(iter)) != NULL)
                if (strcmp(metric->name, metric_name) == 0)
                        break;

        /* bug in test if not found */
        i_assert(metric != NULL);

        stats_metrics_iterate_deinit(&iter);

        switch(field) {
        case STATS_DIST_COUNT:
                return stats_dist_get_count(metric->duration_stats);
        case STATS_DIST_SUM:
                return stats_dist_get_sum(metric->duration_stats);
        default:
                i_unreached();
        }
}