Skip to content

Commit

Permalink
pw_perf_test: Refactor event handler types
Browse files Browse the repository at this point in the history
This CL refactors the `EventHandler` to achieve two things:
 - It uses more pw_metric-friendly floats over int64_ts.
 - It separates logging the metrics to a `TestCaseMeasure` method to
   allow handling multiple measurements.

Change-Id: I83e29af07ed3049d8e5e614d2b806763aa1b626a
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/179611
Reviewed-by: Alexei Frolov <[email protected]>
Commit-Queue: Aaron Green <[email protected]>
  • Loading branch information
nopsledder authored and CQ Bot Account committed Dec 14, 2023
1 parent 9ea4f88 commit 2e5c85c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 62 deletions.
43 changes: 22 additions & 21 deletions pw_perf_test/logging_event_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,39 @@
namespace pw::perf_test {

void LoggingEventHandler::RunAllTestsStart(const TestRunInfo& summary) {
PW_LOG_INFO(PW_PERF_TEST_GOOGLESTYLE_RUN_ALL_TESTS_START);
PW_LOG_INFO(PW_PERF_TEST_GOOGLESTYLE_BEGINNING_SUMMARY,
PW_LOG_INFO(PW_PERF_TEST_GOOGLETEST_RUN_ALL_TESTS_START);
PW_LOG_INFO(PW_PERF_TEST_GOOGLETEST_BEGINNING_SUMMARY,
summary.total_tests,
summary.default_iterations);
}

void LoggingEventHandler::RunAllTestsEnd() {
PW_LOG_INFO(PW_PERF_TEST_GOOGLETEST_RUN_ALL_TESTS_END);
}

void LoggingEventHandler::TestCaseStart(const TestCase& info) {
PW_LOG_INFO(PW_PERF_TEST_GOOGLESTYLE_CASE_START, info.name);
PW_LOG_INFO(PW_PERF_TEST_GOOGLETEST_CASE_START, info.name);
}

void LoggingEventHandler::TestCaseEnd(const TestCase& info,
const Results& end_result) {
PW_LOG_INFO(PW_PERF_TEST_GOOGLESTYLE_CASE_RESULT,
static_cast<long>(end_result.mean),
internal::GetDurationUnitStr(),
static_cast<long>(end_result.min),
internal::GetDurationUnitStr(),
static_cast<long>(end_result.max),
internal::GetDurationUnitStr(),
end_result.iterations);
PW_LOG_INFO(PW_PERF_TEST_GOOGLESTYLE_CASE_END, info.name);
void LoggingEventHandler::TestCaseIteration(const TestIteration& iteration) {
PW_LOG_DEBUG(PW_PERF_TEST_GOOGLETEST_CASE_ITERATION,
static_cast<unsigned>(iteration.number),
static_cast<unsigned long>(iteration.result),
internal::GetDurationUnitStr());
}

void LoggingEventHandler::RunAllTestsEnd() {
PW_LOG_INFO(PW_PERF_TEST_GOOGLESTYLE_RUN_ALL_TESTS_END);
void LoggingEventHandler::TestCaseMeasure(const TestMeasurement& measurement) {
PW_LOG_INFO(PW_PERF_TEST_GOOGLETEST_CASE_MEASUREMENT,
static_cast<unsigned long>(measurement.mean),
internal::GetDurationUnitStr(),
static_cast<unsigned long>(measurement.min),
internal::GetDurationUnitStr(),
static_cast<unsigned long>(measurement.max),
internal::GetDurationUnitStr());
}

void LoggingEventHandler::TestCaseIteration(const IterationResult& result) {
PW_LOG_DEBUG(PW_PERF_TEST_GOOGLESTYLE_ITERATION_REPORT,
static_cast<long>(result.number),
static_cast<long>(result.result),
internal::GetDurationUnitStr());
void LoggingEventHandler::TestCaseEnd(const TestCase& info) {
PW_LOG_INFO(PW_PERF_TEST_GOOGLETEST_CASE_END, info.name);
}

} // namespace pw::perf_test
48 changes: 26 additions & 22 deletions pw_perf_test/public/pw_perf_test/event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,29 @@
namespace pw::perf_test {

/// Data reported on completion of an iteration.
struct IterationResult {
int64_t number;
int64_t result;
struct TestIteration {
uint32_t number = 0;
float result = 0;
};

/// Data reported upon the completion of an iteration.
struct Results {
int64_t mean;
int64_t max;
int64_t min;
int iterations;
/// Data reported for each `Measurement` upon completion of a performance test.
struct TestMeasurement {
float mean = 0;
float max = 0;
float min = 0;
};

/// Stores information on the upcoming collection of tests.
///
/// In order to match gtest, these integer types are not sized
struct TestRunInfo {
int total_tests;
int default_iterations;
int total_tests = 0;
int default_iterations = 0;
};

/// Describes a performance test case.
/// Describes the performance test being run.
struct TestCase {
const char* name;
const char* name = nullptr;
};

/// Collects and reports test results.
Expand All @@ -50,20 +51,23 @@ class EventHandler {
public:
virtual ~EventHandler() = default;

/// Called before all tests are run.
virtual void RunAllTestsStart(const TestRunInfo& summary) = 0;
/// A performance test is starting.
virtual void RunAllTestsStart(const TestRunInfo& test_run_info) = 0;

/// Called after all tests are run.
/// A performance test has ended.
virtual void RunAllTestsEnd() = 0;

/// Called when a new performance test is started.
virtual void TestCaseStart(const TestCase& info) = 0;
/// A performance test case is starting.
virtual void TestCaseStart(const TestCase& test_case) = 0;

/// A performance test case has completed an iteration.
virtual void TestCaseIteration(const TestIteration& test_iteration) = 0;

/// Called to output the results of an iteration.
virtual void TestCaseIteration(const IterationResult& result) = 0;
/// A performance test case has produced a `Measurement`.
virtual void TestCaseMeasure(const TestMeasurement& test_measurement) = 0;

/// Called after a performance test ends.
virtual void TestCaseEnd(const TestCase& info, const Results& end_result) = 0;
/// A performance test case has ended.
virtual void TestCaseEnd(const TestCase& test_case) = 0;
};

} // namespace pw::perf_test
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
// the License.
#pragma once

#define PW_PERF_TEST_GOOGLESTYLE_RUN_ALL_TESTS_START \
#define PW_PERF_TEST_GOOGLETEST_RUN_ALL_TESTS_START \
"[==========] Running all tests."
#define PW_PERF_TEST_GOOGLESTYLE_BEGINNING_SUMMARY \
"[ PLANNING ] %d test(s) with %d run(s) each."
#define PW_PERF_TEST_GOOGLESTYLE_RUN_ALL_TESTS_END \
#define PW_PERF_TEST_GOOGLETEST_BEGINNING_SUMMARY \
"[ PLANNING ] %u test(s) with %u run(s) each."
#define PW_PERF_TEST_GOOGLETEST_RUN_ALL_TESTS_END \
"[==========] Done running all tests."

#define PW_PERF_TEST_GOOGLESTYLE_CASE_START "[ RUN ] %s"
#define PW_PERF_TEST_GOOGLESTYLE_CASE_RESULT \
"[ RESULT ] MEAN: %ld %s, MIN: %ld %s, MAX: %ld %s, ITERATIONS: %d"
#define PW_PERF_TEST_GOOGLESTYLE_CASE_END "[ DONE ] %s"
#define PW_PERF_TEST_GOOGLESTYLE_ITERATION_REPORT "[ Iteration ] #%ld: %ld %s"
#define PW_PERF_TEST_GOOGLETEST_CASE_START "[ RUN ] %s"
#define PW_PERF_TEST_GOOGLETEST_CASE_ITERATION "[ Iteration ] #%u: %lu %s"
#define PW_PERF_TEST_GOOGLETEST_CASE_MEASUREMENT \
"[ RESULT ] MEAN: %lu %s, MIN: %lu %s, MAX: %lu %s"
#define PW_PERF_TEST_GOOGLETEST_CASE_END "[ DONE ] %s"
5 changes: 3 additions & 2 deletions pw_perf_test/public/pw_perf_test/logging_event_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ class LoggingEventHandler : public EventHandler {
void RunAllTestsStart(const TestRunInfo& summary) override;
void RunAllTestsEnd() override;
void TestCaseStart(const TestCase& info) override;
void TestCaseEnd(const TestCase& info, const Results& end_result) override;
void TestCaseIteration(const IterationResult& result) override;
void TestCaseIteration(const TestIteration& iteration) override;
void TestCaseMeasure(const TestMeasurement& measurement) override;
void TestCaseEnd(const TestCase& info) override;
};

} // namespace pw::perf_test
16 changes: 11 additions & 5 deletions pw_perf_test/state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ State CreateState(int durations,

bool State::KeepRunning() {
internal::Timestamp iteration_end = internal::GetCurrentTimestamp();
if (current_iteration_ == -1) {
++current_iteration_;
if (current_iteration_ < 0) {
current_iteration_ = 0;
event_handler_->TestCaseStart(test_info);
iteration_start_ = internal::GetCurrentTimestamp();
return true;
Expand All @@ -46,7 +46,8 @@ bool State::KeepRunning() {
PW_LOG_DEBUG("Iteration number: %d - Duration: %ld",
current_iteration_,
static_cast<long>(duration));
event_handler_->TestCaseIteration({current_iteration_, duration});
event_handler_->TestCaseIteration({static_cast<uint32_t>(current_iteration_),
static_cast<float>(duration)});
if (current_iteration_ == test_iterations_) {
PW_LOG_DEBUG("Total Duration: %ld Total Iterations: %d",
static_cast<long>(total_duration_),
Expand All @@ -55,8 +56,13 @@ bool State::KeepRunning() {
PW_LOG_DEBUG("Mean: %ld: ", static_cast<long>(mean_));
PW_LOG_DEBUG("Minimum: %ld", static_cast<long>(min_));
PW_LOG_DEBUG("Maxmimum: %ld", static_cast<long>(max_));
event_handler_->TestCaseEnd(test_info,
Results{mean_, max_, min_, test_iterations_});
TestMeasurement test_measurement = {
.mean = static_cast<float>(mean_),
.max = static_cast<float>(max_),
.min = static_cast<float>(min_),
};
event_handler_->TestCaseMeasure(test_measurement);
event_handler_->TestCaseEnd(test_info);
return false;
}
iteration_start_ = internal::GetCurrentTimestamp();
Expand Down
7 changes: 4 additions & 3 deletions pw_perf_test/state_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ namespace {
class EmptyEventHandler : public EventHandler {
public:
void RunAllTestsStart(const TestRunInfo&) override {}
void TestCaseStart(const TestCase&) override {}
void TestCaseEnd(const TestCase&, const Results&) override {}
void TestCaseIteration(const IterationResult&) override {}
void RunAllTestsEnd() override {}
void TestCaseStart(const TestCase&) override {}
void TestCaseIteration(const TestIteration&) override {}
void TestCaseMeasure(const TestMeasurement&) override {}
void TestCaseEnd(const TestCase&) override {}
};

EmptyEventHandler handler;
Expand Down

0 comments on commit 2e5c85c

Please sign in to comment.