#include "latency.hpp"

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <vector>

int main() {
    const std::array<Sample, 5> sample{{
        {14, 1, 0}, {85, 0, 0}, {50, 1, 0}, {72, 1, 0}, {UINT16_MAX, 1, 0}
    }};
    const Summary expected{136, 3, 1, 72};
    for (unsigned workers : {0u, 1u, 2u, 3u, 8u, 64u})
        assert(summarize_latency(sample, 50, workers) == expected);
    assert(summarize_latency({}, 50, 8) == Summary{});

    std::vector<Sample> batch;
    Summary reference{};
    for (unsigned i = 0; i < 10007; ++i) {
        const auto ms = static_cast<std::uint16_t>(i % 91 == 0 ? UINT16_MAX : i % 251);
        const auto flags = static_cast<std::uint8_t>(i % 7 == 0 ? 0 : 1);
        batch.push_back({ms, flags, 0});
        if (!flags || ms == UINT16_MAX) continue;
        ++reference.accepted;
        reference.total_ms += ms;
        reference.slow += ms > 50;
        reference.peak_ms = std::max(reference.peak_ms, ms);
    }
    for (unsigned workers = 1; workers <= 8; ++workers)
        assert(summarize_latency(batch, 50, workers) == reference);
    std::cout << "accepted=" << expected.accepted << "  slow=" << expected.slow
              << "  total_ms=" << expected.total_ms << "  peak_ms=" << expected.peak_ms
              << "\n10007 samples / 1..8 workers: OK\n";
}
