HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_TestBench Class Reference

Wrapper around the nanobench object for unit performance tests. More...

#include <UT_TestManager.h>

Public Member Functions

 UT_TestBench (const UT_StringRef &title)
 
 ~UT_TestBench ()
 
bool run (const UT_StringRef &test_name, UT_Function< void(UT_TestUnit &)> fn)
 
bool saveAndCompareResults (const UT_StringRef &file_name_root) const
 

Detailed Description

Wrapper around the nanobench object for unit performance tests.

This class wraps the nanobench::Bench object for performance testing, and streamlines the workflows for deploying it in unit tests.

Example:

static void
performanceTest(UT_TestUnit &test_unit)
{
int result = expensiveComputation();
if (result != 42)
test_unit.fail("Wrong computation: got %d, expected 42", result);
}
static bool
{
UT_TestBench bench;
bool ok = true;
// Time the call to the function.
ok &= bench.run("Forty two", performanceTest));
// Time the call to the lambda.
ok &= bench.run("Loop", [](UT_testUnit &test_unit)
auto sum = 0;
for(auto i=0; i<1000; i++)
sum += i;
});
// Prints the nanobench timing report, saves it to file for the future,
// tries to read a corresponding report from the ./correct subdir,
// and returns true if the performance has not deteriorated
// (thus passing the performance measurement test).
ok &= bench.saveAndCompareResults("MyPerformanceTest");
return ok;
}

Definition at line 200 of file UT_TestManager.h.

Constructor & Destructor Documentation

UT_TestBench::UT_TestBench ( const UT_StringRef title)
UT_TestBench::~UT_TestBench ( )

Member Function Documentation

bool UT_TestBench::run ( const UT_StringRef test_name,
UT_Function< void(UT_TestUnit &)>  fn 
)

Runs and times the given function f. Prints the unit test wall clock time to the output as a summary.

Stores the nanobench timing in the internal structures for later comparisons and detection of the performance regressions.

It passes a UT_TestUnit to that function, for setting status. I.e., bad results may be fast, but they should not be considered for parformance timing, and hence status should be set to error. Returns true if that status is a success or false otherwise.

Parameters
test_nameThe name of the timing test to appear in the reports.
fnThe test function to time.
bool UT_TestBench::saveAndCompareResults ( const UT_StringRef file_name_root) const

Saves the nanobench timings from the previously performed runs to a file with the given name in the current working directory. Compares the timings with a similarly named file in a ./correct subdir, if it exists. If it does not exists, it creates one as a base line for the future runs. comparing to the baseline times, and false otherise. Prints the report of the current run times comparison to the times recorded as a baseline in the ./correct file. Returns true if the current timings are within an acceptable delta

Parameters
file_name_rootThe base name to use for constructing the timing result file name.
Returns
True if the current timings are within an acceptable delta; False otherwise.

The documentation for this class was generated from the following file: