JayBeams  0.1
Another project to have fun coding.
Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | List of all members
jb::testing::microbenchmark< Fixture > Class Template Reference

Run a micro-benchmark on a given class. More...

#include <microbenchmark.hpp>

Inheritance diagram for jb::testing::microbenchmark< Fixture >:
jb::testing::microbenchmark_base

Public Types

typedef jb::testing::microbenchmark_config config
 
- Public Types inherited from jb::testing::microbenchmark_base
typedef std::chrono::steady_clock clock
 
typedef clock::duration duration
 
typedef std::pair< int, durationresult
 
typedef std::vector< resultresults
 

Public Member Functions

 microbenchmark ()
 Default constructor, create a default configuration and initialize from it. More...
 
 microbenchmark (config const &cfg)
 Constructor from a configuration. More...
 
template<typename... Args>
results run (Args &&... args)
 Run the microbenchmaark. More...
 
- Public Member Functions inherited from jb::testing::microbenchmark_base
 microbenchmark_base (microbenchmark_config const &cfg)
 Constructor from a configuration. More...
 
void typical_output (results const &r) const
 Produce the results of the test in a format that works for most cases. More...
 
void write_results (std::ostream &os, results const &r) const
 Stream the detailed results. More...
 

Private Member Functions

template<typename... Args>
results run_unsized (Args &&... args)
 Run a test without specifying the size and some additional arguments for the Fixture constructor. More...
 
results run_unsized ()
 Run a test without specifying the size and no additional arguments for the Fixture constructor. More...
 
template<typename... Args>
results run_fixed_size (Args &&... args)
 Run the test when the size is specified in the microbenchmark configuration. More...
 
template<typename... Args>
void run_sized (int size, results &r, Args &&... args)
 Construct a fixture for the given size and run the microbenchmkark. More...
 
void run_base (Fixture &fixture, results &r)
 Run a microbenchmkark for a constructed fixture. More...
 

Static Private Member Functions

static void run_iteration (Fixture &fixture, results &r)
 Run a single iteration of the test and return the results. More...
 

Additional Inherited Members

- Protected Attributes inherited from jb::testing::microbenchmark_base
microbenchmark_config config_
 

Detailed Description

template<typename Fixture>
class jb::testing::microbenchmark< Fixture >

Run a micro-benchmark on a given class.

This class is used to run microbenchmarks. To use it, you wrap your function or class in a Fixture that constructs the unit under test and calls it. For example, consider a class like this:

class Foo {
public:
Foo(std::string const& need_one_string);
std::string bar(std::string const& need_another_string);
};

To test the bar() function you would create a Fixture class like this:

class FooBarFixture {
public:
FooBarFixture(std::string const& pass_on)
: foo_(pass_on), input_string_("some value") {}
void run() const {
foo_.bar(input_string_);
}
private:
Foo foo_;
std::string input_string_;
};

Then you can run the test using:

jb::testing::microbenchmark<FooBarFixture> mb(jb::microbenchmark_config());
auto results = mb.run(std::string("some other value");
jb::microbenchmark_base::summary summary(results);
std::cout << summary; // or something more clever...
// for a full dump: mb.write_results(std::cout, results);

The framework creates a single instance of the Fixture for the full test, it worries about running a warmup iteration, collecting the results with minimal overhead, and creating a sensible summary.

Template Parameters
FixtureA type meeting the following requirements:
class Fixture {
public:
Fixture(Args ...); // default constructor, used for the default sized test
Fixture(int s, Args ...); // constructor for a test of size 's'
int run(); // run one iteration of the test, return the size used
};

Definition at line 131 of file microbenchmark.hpp.

Member Typedef Documentation

◆ config

template<typename Fixture>
typedef jb::testing::microbenchmark_config jb::testing::microbenchmark< Fixture >::config

Definition at line 133 of file microbenchmark.hpp.

Constructor & Destructor Documentation

◆ microbenchmark() [1/2]

template<typename Fixture>
jb::testing::microbenchmark< Fixture >::microbenchmark ( )
inline

Default constructor, create a default configuration and initialize from it.

Definition at line 139 of file microbenchmark.hpp.

◆ microbenchmark() [2/2]

template<typename Fixture>
jb::testing::microbenchmark< Fixture >::microbenchmark ( config const &  cfg)
inlineexplicit

Constructor from a configuration.

Definition at line 146 of file microbenchmark.hpp.

Member Function Documentation

◆ run()

template<typename Fixture>
template<typename... Args>
results jb::testing::microbenchmark< Fixture >::run ( Args &&...  args)
inline

Run the microbenchmaark.

Parameters
argsthe additional arguments, if any, for the Fixture constructor.
Template Parameters
Argsthe types for the additional arguments.

Definition at line 159 of file microbenchmark.hpp.

References jb::detail::reconfigure_this_thread().

Referenced by main().

◆ run_base()

template<typename Fixture>
void jb::testing::microbenchmark< Fixture >::run_base ( Fixture &  fixture,
results r 
)
inlineprivate

Run a microbenchmkark for a constructed fixture.

Run the warmup iterations and then run the actual iterations for the test. The results of the test are ignored during the warmup phase.

Parameters
fixturethe object under test
rwhere to store the results of the test

Definition at line 234 of file microbenchmark.hpp.

References jb::testing::detail::call_iteration_setup(), and jb::testing::detail::call_iteration_teardown().

◆ run_fixed_size()

template<typename Fixture>
template<typename... Args>
results jb::testing::microbenchmark< Fixture >::run_fixed_size ( Args &&...  args)
inlineprivate

Run the test when the size is specified in the microbenchmark configuration.

Parameters
argsadditional arguments for the Fixture
Template Parameters
Argsthe types for the additional arguments

Definition at line 204 of file microbenchmark.hpp.

◆ run_iteration()

template<typename Fixture>
static void jb::testing::microbenchmark< Fixture >::run_iteration ( Fixture &  fixture,
results r 
)
inlinestaticprivate

Run a single iteration of the test and return the results.

Parameters
fixturethe object under test
rwhere to store the results of the test

Definition at line 254 of file microbenchmark.hpp.

References jb::testing::detail::call_iteration_setup(), jb::testing::detail::call_iteration_teardown(), and jb::testing::defaults::size.

◆ run_sized()

template<typename Fixture>
template<typename... Args>
void jb::testing::microbenchmark< Fixture >::run_sized ( int  size,
results r,
Args &&...  args 
)
inlineprivate

Construct a fixture for the given size and run the microbenchmkark.

Parameters
sizethe size of the test
rwhere to store the results of the test
argsadditional arguments for the constructor, if any.
Template Parameters
Argsthe types of the additional arguments, if any.

Definition at line 219 of file microbenchmark.hpp.

◆ run_unsized() [1/2]

template<typename Fixture>
template<typename... Args>
results jb::testing::microbenchmark< Fixture >::run_unsized ( Args &&...  args)
inlineprivate

Run a test without specifying the size and some additional arguments for the Fixture constructor.

Parameters
argsadditional arguments for the Fixture
Template Parameters
Argsthe types for the additional arguments

Definition at line 178 of file microbenchmark.hpp.

◆ run_unsized() [2/2]

template<typename Fixture>
results jb::testing::microbenchmark< Fixture >::run_unsized ( )
inlineprivate

Run a test without specifying the size and no additional arguments for the Fixture constructor.

Definition at line 189 of file microbenchmark.hpp.


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