Davidcor89-lip6 / b4mesh

Développment du projet b4mesh sur le materiel de GreenCom.
2 stars 0 forks source link

Codebase cleanup #46

Open GuillaumeDuaForSquad opened 3 years ago

GuillaumeDuaForSquad commented 3 years ago

Lie a #40

Pour les probes de std::chrono, il faudra ajouter une option de compilation pour les activer/desactiver,
afin que cela ne pollue pas le binaire finale.
Sinon, utiliser une vrai solution de probes pour metrics, comme Prometheus

Sinon, encapsuler ca proprement voir exemple sur godbolt

#include <chrono>
#include <iostream>
#include <functional>

template <typename clock_t>
    requires (std::chrono::is_clock_v<clock_t>) // retirer le requierement si pas C++20, ou l'emuler avec un `std::enable_if_t`
struct chrono_probe {
    using clock_type = clock_t;
    using time_point_type = std::chrono::time_point<clock_type>;

    chrono_probe() = default;
    chrono_probe(chrono_probe&&) = default;
    chrono_probe& operator=(chrono_probe&&) = default;

    template <typename finaly_t>
    chrono_probe(finaly_t && finaly_v)
    : finaly{std::forward<decltype(finaly_v)>(finaly_v)}
    {}

    ~chrono_probe()
    {
        if (not finaly) return;
        finaly(clock_t::now() - start_time);
    }

    const time_point_type start_time = clock_t::now();
private:
    using diff_time_t = decltype(std::declval<time_point_type>() - std::declval<time_point_type>());
    using finaly_storage_t = std::function<void(diff_time_t&&)>;
    const finaly_storage_t finaly;
};

#include <thread>
void usage()
{
    using clock_type = std::chrono::steady_clock;
    using chrono_probe_type = chrono_probe<clock_type>;

    static_assert(std::is_move_constructible_v<chrono_probe_type>);

    using namespace std::chrono_literals;    

    const auto print_elapsed_time = chrono_probe_type{
        [](auto && elapsed_time) {
            std::cout << (elapsed_time / 1ms) << '\n';
        }
    };
    std::this_thread::sleep_for(1s);
}

auto main() -> int {
    usage();
}

Console output :

1000
GuillaumeDuaForSquad commented 3 years ago

@Davidcor89-lip6 Also, we should consider removing such probes for release build-type

As this project involves performances measurement, we'd better use MDD Metrics-Driven-Developement techniques using, as mentioned before, Prometheus or such technology.