Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions library/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ filegroup(
exclude = [
"context_equivalence.cpp",
"calc_par_test.cpp", # Uses GoogleTest, compiled separately
"test_timer_test.cpp", # Uses GoogleTest, compiled separately
"args_test.cpp", # Uses GoogleTest, compiled separately
],
),
)
Expand Down Expand Up @@ -56,4 +58,37 @@ cc_test(
],
)

cc_test(
name = "test_timer_test",
srcs = [
"test_timer_test.cpp",
"TestTimer.cpp",
"TestTimer.hpp",
],
size = "small",
copts = DDS_CPPOPTS,
linkopts = DDS_LINKOPTS,
local_defines = DDS_LOCAL_DEFINES,
deps = [
"@googletest//:gtest_main",
],
)

cc_test(
name = "args_test",
srcs = [
"args_test.cpp",
"args.cpp",
"args.hpp",
"cst.hpp",
],
size = "small",
copts = DDS_CPPOPTS,
linkopts = DDS_LINKOPTS,
local_defines = DDS_LOCAL_DEFINES,
deps = [
"@googletest//:gtest_main",
],
)


140 changes: 123 additions & 17 deletions library/tests/TestTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/


#include <algorithm>
#include <ctime>
#include <iostream>
#include <iomanip>

Expand All @@ -22,6 +24,7 @@ using std::setprecision;
using std::right;
using std::fixed;
using std::left;
using std::ostream;


TestTimer::TestTimer()
Expand All @@ -42,6 +45,12 @@ void TestTimer::reset()
user_cum_ = 0;
user_cum_old_ = 0;
sys_cum_ = 0;
user_min_ = 0;
user_max_ = 0;
sys_min_ = 0;
sys_max_ = 0;
batch_count_ = 0;
pending_hands_ = 0;
}


Expand All @@ -53,7 +62,7 @@ void TestTimer::set_name(const string& s)

void TestTimer::start(const int number)
{
count_ += number;
pending_hands_ = number;
user0_ = Clock::now();
sys0_ = clock();
}
Expand All @@ -65,11 +74,69 @@ void TestTimer::end()
clock_t sys1 = clock();

duration<double, std::milli> d = user1 - user0_;
int tuser = static_cast<int>(d.count());

user_cum_ += tuser;
sys_cum_ += static_cast<int>((1000 * (sys1 - sys0_)) /
const long tuser = static_cast<long>(d.count());
const long tsys = static_cast<long>((1000 * (sys1 - sys0_)) /
static_cast<double>(CLOCKS_PER_SEC));

TestTimer::record(pending_hands_, tuser, tsys);
pending_hands_ = 0;
}


void TestTimer::record(const int hands, const long user_ms, const long sys_ms)
{
if (hands <= 0)
return;

count_ += hands;
user_cum_ += user_ms;
sys_cum_ += sys_ms;

const double user_per_hand = user_ms / static_cast<double>(hands);
const double sys_per_hand = sys_ms / static_cast<double>(hands);
if (batch_count_ == 0)
{
user_min_ = user_max_ = user_per_hand;
sys_min_ = sys_max_ = sys_per_hand;
}
else
{
user_min_ = std::min(user_min_, user_per_hand);
user_max_ = std::max(user_max_, user_per_hand);
sys_min_ = std::min(sys_min_, sys_per_hand);
sys_max_ = std::max(sys_max_, sys_per_hand);
}
batch_count_++;
}


bool TestTimer::has_batch_times() const
{
return batch_count_ > 0;
}


double TestTimer::user_min_ms() const
{
return user_min_;
}


double TestTimer::user_max_ms() const
{
return user_max_;
}


double TestTimer::sys_min_ms() const
{
return sys_min_;
}


double TestTimer::sys_max_ms() const
{
return sys_max_;
}


Expand Down Expand Up @@ -128,47 +195,86 @@ void TestTimer::print_basic() const
}


void TestTimer::print_hands() const
void TestTimer::print_hands(
ostream& out,
const bool show_min,
const bool show_max) const
{
struct StreamFormatGuard
{
explicit StreamFormatGuard(ostream& os)
: os_(os),
flags_(os.flags()),
precision_(os.precision()),
fill_(os.fill())
{
}

~StreamFormatGuard()
{
os_.flags(flags_);
os_.precision(precision_);
os_.fill(fill_);
}

ostream& os_;
const std::ios_base::fmtflags flags_;
const std::streamsize precision_;
const char fill_;
};

const StreamFormatGuard format_guard(out);

if (name_ != "")
cout << setw(21) << left << "Timer name" <<
out << setw(21) << left << "Timer name" <<
setw(12) << right << name_ << "\n";

cout << setw(21) << left << "Number of hands" <<
out << setw(21) << left << "Number of hands" <<
setw(12) << right << count_ << "\n";

if (count_ == 0)
return;

if (user_cum_ == 0)
cout << setw(21) << left << "User time (ms)" <<
out << setw(21) << left << "User time (ms)" <<
setw(12) << right << "zero" << "\n";
else
{
cout << setw(21) << left << "User time (ms)" <<
out << setw(21) << left << "User time (ms)" <<
setw(12) << right << fixed <<
setprecision(0) << user_cum_ << "\n";
cout << setw(21) << left << "Avg user time (ms)" <<
out << setw(21) << left << "Avg user time (ms)" <<
setw(12) << right << fixed << setprecision(2) << user_cum_ /
static_cast<float>(count_) << "\n";
if (show_min && has_batch_times())
out << setw(21) << left << "Min user time (ms)" <<
setw(12) << right << fixed << setprecision(2) << user_min_ << "\n";
if (show_max && has_batch_times())
out << setw(21) << left << "Max user time (ms)" <<
setw(12) << right << fixed << setprecision(2) << user_max_ << "\n";
}

if (sys_cum_ == 0)
cout << setw(21) << left << "Sys time (ms)" <<
out << setw(21) << left << "Sys time (ms)" <<
setw(12) << right << "zero" << "\n";
else
{
cout << setw(21) << left << "Sys time (ms)" <<
out << setw(21) << left << "Sys time (ms)" <<
setw(12) << right << fixed << setprecision(0) << sys_cum_ << "\n";
cout << setw(21) << left << "Avg sys time (ms)" <<
out << setw(21) << left << "Avg sys time (ms)" <<
setw(12) << right << fixed << setprecision(2) << sys_cum_ /
static_cast<float>(count_) << "\n";
if (show_min && has_batch_times())
out << setw(21) << left << "Min sys time (ms)" <<
setw(12) << right << fixed << setprecision(2) << sys_min_ << "\n";
if (show_max && has_batch_times())
out << setw(21) << left << "Max sys time (ms)" <<
setw(12) << right << fixed << setprecision(2) << sys_max_ << "\n";
if (user_cum_ > 0) {
cout << setw(21) << left << "Ratio" <<
out << setw(21) << left << "Ratio" <<
setw(12) << right << fixed << setprecision(2) <<
sys_cum_ / static_cast<float>(user_cum_);
}
}
cout << endl;
out << endl;
}

41 changes: 40 additions & 1 deletion library/tests/TestTimer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@

#pragma once

#include <ostream>
#include <string>
#include <chrono>
#include <ctime>
#include <iostream>

using Clock = std::chrono::steady_clock;
using std::chrono::time_point;
Expand All @@ -32,6 +35,12 @@ class TestTimer
long user_cum_; ///< Cumulative user time (milliseconds)
long user_cum_old_; ///< Previous cumulative user time (milliseconds)
long sys_cum_; ///< Cumulative system time (milliseconds)
double user_min_; ///< Min per-hand user time across batches (ms)
double user_max_; ///< Max per-hand user time across batches (ms)
double sys_min_; ///< Min per-hand system time across batches (ms)
double sys_max_; ///< Max per-hand system time across batches (ms)
long batch_count_; ///< Number of completed batches
int pending_hands_; ///< Hands counted into the open start()/end() batch

time_point<Clock> user0_; ///< Wall-clock start time
clock_t sys0_; ///< CPU start time
Expand All @@ -55,6 +64,30 @@ class TestTimer
/// Stop timing and accumulate results.
void end();

/// Record one completed batch without wall-clock measurement.
/// Updates cumulative totals and per-hand min/max extremes.
/// Used by end() and by unit tests for deterministic extremes.
/// Non-positive hands is ignored (no cumulative or extreme updates).
/// @param hands Number of hands in the batch
/// @param user_ms Batch user (wall) time in milliseconds
/// @param sys_ms Batch system (CPU) time in milliseconds
void record(const int hands, const long user_ms, const long sys_ms);

/// Whether at least one batch has been recorded.
bool has_batch_times() const;

/// Minimum per-hand user time across batches (milliseconds).
double user_min_ms() const;

/// Maximum per-hand user time across batches (milliseconds).
double user_max_ms() const;

/// Minimum per-hand system time across batches (milliseconds).
double sys_min_ms() const;

/// Maximum per-hand system time across batches (milliseconds).
double sys_max_ms() const;

/// Print timer status while running.
/// @param reached Number of iterations completed so far
/// @param number Total number of iterations
Expand All @@ -66,5 +99,11 @@ class TestTimer
void print_basic() const;

/// Print detailed per-hand timer results.
void print_hands() const;
/// @param out Output stream
/// @param show_min Include min per-hand times across batches
/// @param show_max Include max per-hand times across batches
void print_hands(
std::ostream& out = std::cout,
bool show_min = false,
bool show_max = false) const;
};
Loading
Loading