test: add speedometer test

This commit is contained in:
Marcus Holland-Moritz 2024-01-04 22:05:58 +01:00
parent 4868612f4d
commit c7c467230c
3 changed files with 106 additions and 9 deletions

View File

@ -608,6 +608,7 @@ if(WITH_TESTS)
metadata_requirements_test
pcm_sample_transformer_test
pcmaudio_categorizer_test
speedometer_test
terminal_test
tool_main_test
tools_test

View File

@ -26,14 +26,17 @@
namespace dwarfs {
template <typename T>
class speedometer {
template <typename ClockT, typename ValueT>
class basic_speedometer {
public:
speedometer(std::chrono::milliseconds window_length)
using clock_type = ClockT;
using value_type = ValueT;
basic_speedometer(std::chrono::milliseconds window_length)
: window_length_{window_length} {}
void put(T s) {
auto now = std::chrono::steady_clock::now();
void put(value_type s) {
auto now = clock_type::now();
auto old = now - window_length_;
while (!samples_.empty() && samples_.front().first < old) {
@ -43,9 +46,9 @@ class speedometer {
samples_.emplace_back(now, s);
}
T num_per_second() const {
value_type num_per_second() const {
if (samples_.size() < 2) {
return T();
return value_type();
}
auto const& first = samples_.front();
auto const& last = samples_.back();
@ -53,14 +56,17 @@ class speedometer {
auto const dv = last.second - first.second;
auto const elapsed_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(dt).count();
return elapsed_ms > 0 ? (1000 * dv) / elapsed_ms : T();
return elapsed_ms > 0 ? (1000 * dv) / elapsed_ms : value_type();
}
void clear() { samples_.clear(); }
private:
std::deque<std::pair<std::chrono::steady_clock::time_point, T>> samples_;
std::deque<std::pair<typename clock_type::time_point, value_type>> samples_;
std::chrono::milliseconds window_length_;
};
template <typename T>
using speedometer = basic_speedometer<std::chrono::steady_clock, T>;
} // namespace dwarfs

90
test/speedometer_test.cpp Normal file
View File

@ -0,0 +1,90 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "dwarfs/speedometer.h"
using namespace dwarfs;
namespace {
class mock_clock {
public:
using duration = std::chrono::microseconds;
using time_point = std::chrono::time_point<mock_clock>;
static time_point now() { return now_; }
static void advance(duration d) { now_ += d; }
private:
static thread_local inline time_point now_;
};
template <typename T>
using test_speedometer = basic_speedometer<mock_clock, T>;
} // namespace
TEST(speedometer_test, basic) {
using namespace std::chrono_literals;
test_speedometer<int> s{2s};
EXPECT_EQ(0, s.num_per_second());
mock_clock::advance(250ms);
s.put(10000);
EXPECT_EQ(0, s.num_per_second());
mock_clock::advance(250ms); // [250ms, 500ms]
s.put(20000); // [10000 -> 20000]
EXPECT_EQ(40000, s.num_per_second());
mock_clock::advance(750ms); // [250ms, 1250ms]
s.put(90000); // [10000 -> 90000]
EXPECT_EQ(80000, s.num_per_second());
mock_clock::advance(500ms); // [250ms, 1750ms]
s.put(115000); // [10000 -> 115000]
EXPECT_EQ(70000, s.num_per_second());
mock_clock::advance(500ms); // [250ms, 2250ms]
s.put(130000); // [10000 -> 130000]
EXPECT_EQ(60000, s.num_per_second());
mock_clock::advance(500ms); // [1250ms, 2750ms]
s.put(150000); // [90000 -> 150000]
EXPECT_EQ(40000, s.num_per_second());
mock_clock::advance(750ms); // [1750ms, 3500ms]
s.put(176250); // [115000 -> 176250]
EXPECT_EQ(35000, s.num_per_second());
}