From 4dff524e55a10822fd3889b484fb5be94eae7d09 Mon Sep 17 00:00:00 2001 From: Snaipe Date: Mon, 23 Mar 2015 18:37:51 +0100 Subject: [PATCH] Added stats on suites --- include/criterion/hooks.h | 2 ++ include/criterion/logging.h | 2 +- include/criterion/stats.h | 17 ++++++++++++- src/report.c | 4 ++-- src/runner.c | 28 +++++++++++++++------- src/stats.c | 48 ++++++++++++++++++++++++++++++++----- src/stats.h | 2 ++ 7 files changed, 85 insertions(+), 18 deletions(-) diff --git a/include/criterion/hooks.h b/include/criterion/hooks.h index b47b330..a8b7bfa 100644 --- a/include/criterion/hooks.h +++ b/include/criterion/hooks.h @@ -28,12 +28,14 @@ typedef enum { PRE_ALL, + PRE_SUITE, PRE_INIT, PRE_TEST, ASSERT, TEST_CRASH, POST_TEST, POST_FINI, + POST_SUITE, POST_ALL, } e_report_status; diff --git a/include/criterion/logging.h b/include/criterion/logging.h index 6d205be..776361a 100644 --- a/include/criterion/logging.h +++ b/include/criterion/logging.h @@ -49,7 +49,7 @@ struct criterion_output_provider { void (*log_test_crash)(struct criterion_test_stats *stats); void (*log_post_test )(struct criterion_test_stats *stats); void (*log_post_fini )(struct criterion_test_stats *stats); - void (*log_post_suite)(struct criterion_suite_set *set); + void (*log_post_suite)(struct criterion_suite_stats *stats); void (*log_post_all )(struct criterion_global_stats *stats); }; diff --git a/include/criterion/stats.h b/include/criterion/stats.h index cfd4ea2..6d84e8c 100644 --- a/include/criterion/stats.h +++ b/include/criterion/stats.h @@ -51,7 +51,8 @@ struct criterion_test_stats { struct criterion_test_stats *next; }; -struct criterion_global_stats { +struct criterion_suite_stats { + struct criterion_suite *suite; struct criterion_test_stats *tests; size_t nb_tests; size_t nb_asserts; @@ -60,6 +61,20 @@ struct criterion_global_stats { size_t tests_passed; size_t asserts_failed; size_t asserts_passed; + + struct criterion_suite_stats *next; +}; + +struct criterion_global_stats { + struct criterion_suite_stats *suites; + size_t nb_suites; + size_t nb_tests; + size_t nb_asserts; + size_t tests_failed; + size_t tests_crashed; + size_t tests_passed; + size_t asserts_failed; + size_t asserts_passed; }; #endif /* !CRITERION_STATS_H_ */ diff --git a/src/report.c b/src/report.c index a811e86..1b86678 100644 --- a/src/report.c +++ b/src/report.c @@ -99,8 +99,8 @@ IMPL_REPORT_HOOK(POST_FINI)(struct criterion_test_stats *stats) { log(post_fini, stats); } -IMPL_REPORT_HOOK(POST_SUITE)(struct criterion_suite_set *set) { - log(post_suite, set); +IMPL_REPORT_HOOK(POST_SUITE)(struct criterion_suite_stats *stats) { + log(post_suite, stats); } IMPL_REPORT_HOOK(POST_ALL)(struct criterion_global_stats *stats) { diff --git a/src/runner.c b/src/runner.c index 432c169..3491193 100644 --- a/src/runner.c +++ b/src/runner.c @@ -90,7 +90,10 @@ struct criterion_test_set *criterion_init(void) { }, dtor_test_set); } -typedef void (*f_test_run)(struct criterion_global_stats *, struct criterion_test *, struct criterion_suite *); +typedef void (*f_test_run)(struct criterion_global_stats *, + struct criterion_suite_stats *, + struct criterion_test *, + struct criterion_suite *); static void map_tests(struct criterion_test_set *set, struct criterion_global_stats *stats, f_test_run fun) { FOREACH_SET(struct criterion_suite_set *s, set->suites) { @@ -99,15 +102,20 @@ static void map_tests(struct criterion_test_set *set, struct criterion_global_st report(PRE_SUITE, s); + smart struct criterion_suite_stats *suite_stats = suite_stats_init(&s->suite); + + struct event ev = { .kind = PRE_SUITE }; + stat_push_event(stats, suite_stats, NULL, &ev); + FOREACH_SET(struct criterion_test *t, s->tests) { - fun(stats, t, &s->suite); + fun(stats, suite_stats, t, &s->suite); if (criterion_options.fail_fast && stats->tests_failed > 0) break; if (!is_runner()) return; } - report(POST_SUITE, s); + report(POST_SUITE, suite_stats); } } @@ -135,7 +143,11 @@ static void run_test_child(struct criterion_test *test, struct criterion_suite * send_event(POST_FINI, NULL, 0); } -static void run_test(struct criterion_global_stats *stats, struct criterion_test *test, struct criterion_suite *suite) { +static void run_test(struct criterion_global_stats *stats, + struct criterion_suite_stats *suite_stats, + struct criterion_test *test, + struct criterion_suite *suite) { + if (test->data->disabled) return; @@ -147,7 +159,7 @@ static void run_test(struct criterion_global_stats *stats, struct criterion_test struct event *ev; while ((ev = worker_read_event(proc)) != NULL) { - stat_push_event(stats, test_stats, ev); + stat_push_event(stats, suite_stats, test_stats, ev); switch (ev->kind) { case PRE_INIT: report(PRE_INIT, test); break; case PRE_TEST: report(PRE_TEST, test); break; @@ -163,16 +175,16 @@ static void run_test(struct criterion_global_stats *stats, struct criterion_test test_stats->signal = status.status; if (test->data->signal == 0) { struct event ev = { .kind = TEST_CRASH }; - stat_push_event(stats, test_stats, &ev); + stat_push_event(stats, suite_stats, test_stats, &ev); report(TEST_CRASH, test_stats); } else { double elapsed_time = 0; struct event ev = { .kind = POST_TEST, .data = &elapsed_time }; - stat_push_event(stats, test_stats, &ev); + stat_push_event(stats, suite_stats, test_stats, &ev); report(POST_TEST, test_stats); ev = (struct event) { .kind = POST_FINI, .data = NULL }; - stat_push_event(stats, test_stats, &ev); + stat_push_event(stats, suite_stats, test_stats, &ev); report(POST_FINI, test_stats); } } diff --git a/src/stats.c b/src/stats.c index 9fc063e..7066960 100644 --- a/src/stats.c +++ b/src/stats.c @@ -28,22 +28,35 @@ #include static void nothing() {}; +static void push_pre_suite(); static void push_pre_test(); static void push_assert(); static void push_post_test(); static void push_test_crash(); typedef struct criterion_global_stats s_glob_stats; +typedef struct criterion_suite_stats s_suite_stats; typedef struct criterion_test_stats s_test_stats; typedef struct criterion_assert_stats s_assert_stats; static void destroy_stats(void *ptr, UNUSED void *meta) { s_glob_stats *stats = ptr; - sfree(stats->tests); + sfree(stats->suites); } s_glob_stats *stats_init(void) { - return unique_ptr(s_glob_stats, {0}, destroy_stats); + return unique_ptr(s_glob_stats, .dtor = destroy_stats); +} + +static void destroy_suite_stats(void *ptr, UNUSED void *meta) { + s_suite_stats *stats = ptr; + sfree(stats->tests); +} + +s_suite_stats *suite_stats_init(struct criterion_suite *s) { + return shared_ptr(s_suite_stats, { + .suite = s, + }, destroy_suite_stats); } static void destroy_test_stats(void *ptr, UNUSED void *meta) { @@ -61,31 +74,45 @@ s_test_stats *test_stats_init(struct criterion_test *t) { } void stat_push_event(s_glob_stats *stats, + s_suite_stats *suite, s_test_stats *test, struct event *data) { - static void (*const handles[])(s_glob_stats *, s_test_stats *, void *) = { + static void (*const handles[])(s_glob_stats *, s_suite_stats *, s_test_stats *, void *) = { nothing, // PRE_ALL + push_pre_suite, // PRE_SUITE nothing, // PRE_INIT push_pre_test, // PRE_TEST push_assert, // ASSERT push_test_crash, // TEST_CRASH push_post_test, // POST_TEST nothing, // POST_FINI + nothing, // PRE_SUITE nothing, // POST_ALL }; assert(data->kind > 0); assert(data->kind <= (signed long long) (sizeof (handles) / sizeof (void (*)(void)))); - handles[data->kind](stats, test, data->data); + handles[data->kind](stats, suite, test, data->data); +} + +static void push_pre_suite(s_glob_stats *stats, + s_suite_stats *suite, + UNUSED s_test_stats *test, + UNUSED void *ptr) { + suite->next = stats->suites; + stats->suites = sref(suite); + ++stats->nb_suites; } static void push_pre_test(s_glob_stats *stats, + s_suite_stats *suite, s_test_stats *test, UNUSED void *ptr) { - test->next = stats->tests; - stats->tests = sref(test); + test->next = suite->tests; + suite->tests = sref(test); ++stats->nb_tests; + ++suite->nb_tests; } static void destroy_assert(void *ptr, UNUSED void *meta) { @@ -94,6 +121,7 @@ static void destroy_assert(void *ptr, UNUSED void *meta) { } static void push_assert(s_glob_stats *stats, + s_suite_stats *suite, s_test_stats *test, s_assert_stats *data) { s_assert_stats *dup = unique_ptr(s_assert_stats, (*data), destroy_assert); @@ -102,9 +130,11 @@ static void push_assert(s_glob_stats *stats, if (data->passed) { ++stats->asserts_passed; + ++suite->asserts_passed; ++test->passed_asserts; } else { ++stats->asserts_failed; + ++suite->asserts_failed; ++test->failed_asserts; } @@ -113,21 +143,27 @@ static void push_assert(s_glob_stats *stats, } static void push_post_test(s_glob_stats *stats, + s_suite_stats *suite, s_test_stats *test, double *ptr) { test->elapsed_time = *ptr; if (test->failed_asserts > 0 || test->signal != test->test->data->signal) { test->failed = 1; ++stats->tests_failed; + ++suite->tests_failed; } else { ++stats->tests_passed; + ++suite->tests_passed; } } static void push_test_crash(s_glob_stats *stats, + s_suite_stats *suite, s_test_stats *test, UNUSED void *ptr) { test->failed = 1; + ++suite->tests_failed; + ++suite->tests_crashed; ++stats->tests_failed; ++stats->tests_crashed; } diff --git a/src/stats.h b/src/stats.h index 54a3118..e024282 100644 --- a/src/stats.h +++ b/src/stats.h @@ -29,7 +29,9 @@ struct criterion_global_stats *stats_init(void); struct criterion_test_stats *test_stats_init(struct criterion_test *t); +struct criterion_suite_stats *suite_stats_init(struct criterion_suite *s); void stat_push_event(struct criterion_global_stats *stats, + struct criterion_suite_stats *suite, struct criterion_test_stats *test, struct event *data);