TestSuite.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibTest/Macros.h> // intentionally first -- we redefine VERIFY and friends in here
  8. #include <AK/Function.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibTest/TestSuite.h>
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include <sys/time.h>
  14. namespace Test {
  15. TestSuite* TestSuite::s_global = nullptr;
  16. class TestElapsedTimer {
  17. public:
  18. TestElapsedTimer() { restart(); }
  19. void restart() { gettimeofday(&m_started, nullptr); }
  20. u64 elapsed_milliseconds()
  21. {
  22. struct timeval now = {};
  23. gettimeofday(&now, nullptr);
  24. struct timeval delta = {};
  25. timersub(&now, &m_started, &delta);
  26. return delta.tv_sec * 1000 + delta.tv_usec / 1000;
  27. }
  28. private:
  29. struct timeval m_started = {};
  30. };
  31. // Declared in Macros.h
  32. void current_test_case_did_fail()
  33. {
  34. TestSuite::the().current_test_case_did_fail();
  35. }
  36. // Declared in TestCase.h
  37. void add_test_case_to_suite(NonnullRefPtr<TestCase> const& test_case)
  38. {
  39. TestSuite::the().add_case(test_case);
  40. }
  41. // Declared in TestCase.h
  42. void set_suite_setup_function(Function<void()> setup)
  43. {
  44. TestSuite::the().set_suite_setup(move(setup));
  45. }
  46. int TestSuite::main(DeprecatedString const& suite_name, Span<StringView> arguments)
  47. {
  48. m_suite_name = suite_name;
  49. Core::ArgsParser args_parser;
  50. bool do_tests_only = getenv("TESTS_ONLY") != nullptr;
  51. bool do_benchmarks_only = false;
  52. bool do_list_cases = false;
  53. StringView search_string = "*"sv;
  54. args_parser.add_option(do_tests_only, "Only run tests.", "tests", 0);
  55. args_parser.add_option(do_benchmarks_only, "Only run benchmarks.", "bench", 0);
  56. args_parser.add_option(m_benchmark_repetitions, "Number of times to repeat each benchmark (default 1)", "benchmark_repetitions", 0, "N");
  57. args_parser.add_option(do_list_cases, "List available test cases.", "list", 0);
  58. args_parser.add_positional_argument(search_string, "Only run matching cases.", "pattern", Core::ArgsParser::Required::No);
  59. args_parser.parse(arguments);
  60. if (m_setup)
  61. m_setup();
  62. auto const& matching_tests = find_cases(search_string, !do_benchmarks_only, !do_tests_only);
  63. if (do_list_cases) {
  64. outln("Available cases for {}:", suite_name);
  65. for (auto const& test : matching_tests) {
  66. outln(" {}", test->name());
  67. }
  68. return 0;
  69. }
  70. outln("Running {} cases out of {}.", matching_tests.size(), m_cases.size());
  71. return run(matching_tests);
  72. }
  73. Vector<NonnullRefPtr<TestCase>> TestSuite::find_cases(DeprecatedString const& search, bool find_tests, bool find_benchmarks)
  74. {
  75. Vector<NonnullRefPtr<TestCase>> matches;
  76. for (auto& t : m_cases) {
  77. if (!search.is_empty() && !t->name().matches(search, CaseSensitivity::CaseInsensitive)) {
  78. continue;
  79. }
  80. if (!find_tests && !t->is_benchmark()) {
  81. continue;
  82. }
  83. if (!find_benchmarks && t->is_benchmark()) {
  84. continue;
  85. }
  86. matches.append(t);
  87. }
  88. return matches;
  89. }
  90. int TestSuite::run(Vector<NonnullRefPtr<TestCase>> const& tests)
  91. {
  92. size_t test_count = 0;
  93. size_t test_failed_count = 0;
  94. size_t benchmark_count = 0;
  95. TestElapsedTimer global_timer;
  96. for (auto const& t : tests) {
  97. auto const test_type = t->is_benchmark() ? "benchmark" : "test";
  98. auto const repetitions = t->is_benchmark() ? m_benchmark_repetitions : 1;
  99. warnln("Running {} '{}'.", test_type, t->name());
  100. m_current_test_case_passed = true;
  101. u64 total_time = 0;
  102. u64 sum_of_squared_times = 0;
  103. u64 min_time = NumericLimits<u64>::max();
  104. u64 max_time = 0;
  105. for (u64 i = 0; i < repetitions; ++i) {
  106. TestElapsedTimer timer;
  107. t->func()();
  108. auto const iteration_time = timer.elapsed_milliseconds();
  109. total_time += iteration_time;
  110. sum_of_squared_times += iteration_time * iteration_time;
  111. min_time = min(min_time, iteration_time);
  112. max_time = max(max_time, iteration_time);
  113. }
  114. if (repetitions != 1) {
  115. double average = total_time / double(repetitions);
  116. double average_squared = average * average;
  117. double standard_deviation = sqrt((sum_of_squared_times + repetitions * average_squared - 2 * total_time * average) / (repetitions - 1));
  118. dbgln("{} {} '{}' on average in {:.1f}±{:.1f}ms (min={}ms, max={}ms, total={}ms)",
  119. m_current_test_case_passed ? "Completed" : "Failed", test_type, t->name(),
  120. average, standard_deviation, min_time, max_time, total_time);
  121. } else {
  122. dbgln("{} {} '{}' in {}ms", m_current_test_case_passed ? "Completed" : "Failed", test_type, t->name(), total_time);
  123. }
  124. if (t->is_benchmark()) {
  125. m_benchtime += total_time;
  126. benchmark_count++;
  127. } else {
  128. m_testtime += total_time;
  129. test_count++;
  130. }
  131. if (!m_current_test_case_passed) {
  132. test_failed_count++;
  133. }
  134. }
  135. dbgln("Finished {} tests and {} benchmarks in {}ms ({}ms tests, {}ms benchmarks, {}ms other).",
  136. test_count,
  137. benchmark_count,
  138. global_timer.elapsed_milliseconds(),
  139. m_testtime,
  140. m_benchtime,
  141. global_timer.elapsed_milliseconds() - (m_testtime + m_benchtime));
  142. if (test_count != 0)
  143. dbgln("Out of {} tests, {} passed and {} failed.", test_count, test_count - test_failed_count, test_failed_count);
  144. return (int)test_failed_count;
  145. }
  146. }