diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-06 12:05:57 +0100 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-02-06 21:40:39 +0100 |
commit | e66261158e090141d853632776f909d00a4743c9 (patch) | |
tree | 48dce7103b97470447f971e58b648acea4f00cf0 /test/app.cpp | |
parent | ea39f5aa7f125ac921b893d0c555bb7988b6f341 (diff) |
test: add timing information in verbose mode
Diffstat (limited to 'test/app.cpp')
-rw-r--r-- | test/app.cpp | 118 |
1 files changed, 92 insertions, 26 deletions
diff --git a/test/app.cpp b/test/app.cpp index 7eb2b6af..6aaebb80 100644 --- a/test/app.cpp +++ b/test/app.cpp @@ -1,11 +1,25 @@ #include "app.hpp" -#include "compat/assert.hpp" #include "loader/loader.hpp" -#include <stdlib.h> -#include <cstdlib> +#include <stdlib.h> // NOLINT(*-deprecated-headers) +#include <cstdio> +#include <Corrade/Containers/StringView.h> +#include <Magnum/Math/Functions.h> +#include <Magnum/Timeline.h> +#include <Magnum/GL/Context.h> namespace floormat { +namespace { + +bool is_log_quiet() // copy-pasted from src/chunk.cpp +{ + using GLCCF = GL::Implementation::ContextConfigurationFlag; + auto flags = GL::Context::current().configurationFlags(); + return !!(flags & GLCCF::QuietLog); +} + +} // namespace + test_app::test_app(const Arguments& arguments): Application { arguments, @@ -21,27 +35,79 @@ test_app::~test_app() int test_app::exec() { - test_coords(); - test_json(); - test_tile_iter(); - test_magnum_math(); - test_entity(); - test_math(); - test_hash(); - test_intrusive_ptr(); - test_loader(); - test_bitmask(); - test_wall_atlas(); - test_wall_atlas2(); - test_serializer_1(); - test_serializer_2(); - test_scenery(); - test_raycast(); - test_path_search_node_pool(); - test_path_search(); - test_dijkstra(); - - zzz_test_misc(); + constexpr auto SV_flags = StringViewFlag::Global|StringViewFlag::NullTerminated; + constexpr auto name_prefix = "test_"_s; + +#define FM_TEST(name) { ( StringView{#name, arraySize(#name)-1, SV_flags} ), ( &(name) ), } + + constexpr struct { + StringView name; + void(*function)(); + } list[] = { + FM_TEST(test_coords), + FM_TEST(test_tile_iter), + FM_TEST(test_magnum_math), + FM_TEST(test_math), + FM_TEST(test_entity), + FM_TEST(test_intrusive_ptr), + FM_TEST(test_hash), + FM_TEST(test_json), + FM_TEST(test_wall_atlas), + FM_TEST(test_wall_atlas2), + FM_TEST(test_bitmask), + FM_TEST(test_loader), + FM_TEST(test_serializer1), + FM_TEST(test_scenery), + FM_TEST(test_raycast), + FM_TEST(test_path_search_pool), + FM_TEST(test_path_search), + FM_TEST(test_dijkstra), // todo add dummy atlases to avoid expensive loading + FM_TEST(test_serializer2), + FM_TEST(test_zzz_misc), + }; + + if (is_log_quiet()) + for (const auto [_, fun] : list) + (*fun)(); + else + { + FILE* const s = stdout; + static constexpr auto sep = ""_s; + constexpr auto get_tabs = [](StringView name) constexpr { + return (name.size()+sep.size()) / 8; + }; + constexpr size_t tab_limit = 5; + constexpr auto get_time = [](auto&& fn) { + Timeline t; + t.start(); + (*fn)(); + return t.currentFrameTime() * 1e3f; + }; + + size_t max_tabs = 1; + for (const auto [name, _] : list) + max_tabs = Math::max(max_tabs, get_tabs(name)); + max_tabs++; + if (max_tabs > tab_limit) + max_tabs = 1; + + std::fflush(s); + + for (auto [name, fun] : list) + { + name = name.exceptPrefix(name_prefix); + std::fwrite(name.data(), name.size(), 1, s); + std::fflush(stdout); + auto ms = get_time(fun); + std::fwrite(sep.data(), sep.size(), 1, s); + auto num_tabs = max_tabs - get_tabs(name); + fm_assert(num_tabs <= tab_limit); + for (auto i = 0uz; i < num_tabs; i++) + std::fputc('\t', s); + std::fprintf(s, "% 9.3f ms\n", (double)ms); + std::fflush(s); + } + } return 0; } @@ -53,9 +119,9 @@ int main(int argc, char** argv) #ifdef _WIN32 // NOLINTNEXTLINE(concurrency-mt-unsafe) if (const auto* s = std::getenv("MAGNUM_LOG"); !s || !*s) - _putenv("MAGNUM_LOG=quiet"); + ::_putenv("MAGNUM_LOG=quiet"); #else - setenv("MAGNUM_LOG", "quiet", 0); + ::setenv("MAGNUM_LOG", "quiet", 0); #endif floormat::test_app application{{argc, argv}}; return application.exec(); |