BDE 4.14.0 Production release
Loading...
Searching...
No Matches
bslmt::ThroughputBenchmark Class Reference

#include <bslmt_throughputbenchmark.h>

Classes

struct  ThreadGroup
 Data used by a thread group. More...
 

Public Types

typedef bsl::function< void(int)> RunFunction
 
typedef bsl::function< void(bool)> InitializeSampleFunction
 
typedef bsl::function< void(bool)> ShutdownSampleFunction
 
typedef bsl::function< void(bool)> CleanupSampleFunction
 
typedef bsl::function< void()> InitializeThreadFunction
 
typedef bsl::function< void()> CleanupThreadFunction
 

Public Member Functions

 BSLMF_NESTED_TRAIT_DECLARATION (ThroughputBenchmark, bslma::UsesBslmaAllocator)
 
 ThroughputBenchmark (bslma::Allocator *basicAllocator=0)
 
int addThreadGroup (const RunFunction &runFunction, int numThreads, bsls::Types::Int64 busyWorkAmount)
 
int addThreadGroup (const RunFunction &runFunction, int numThreads, bsls::Types::Int64 busyWorkAmount, const InitializeThreadFunction &initializeFunctor, const CleanupThreadFunction &cleanupFunctor)
 
void execute (ThroughputBenchmarkResult *result, int millisecondsPerSample, int numSamples)
 
void execute (ThroughputBenchmarkResult *result, int millisecondsPerSample, int numSamples, const InitializeSampleFunction &initializeFunctor, const ShutdownSampleFunction &shutdownFunctor, const CleanupSampleFunction &cleanupFunctor)
 
int numThreads () const
 Return the total number of threads.
 
int numThreadGroups () const
 Return the number of thread groups.
 
int numThreadsInGroup (int threadGroupIndex) const
 
bslma::Allocatorallocator () const
 Return the allocator used by this object.
 

Static Public Member Functions

static unsigned int antiOptimization ()
 
static void busyWork (bsls::Types::Int64 busyWorkAmount)
 
static bsls::Types::Int64 estimateBusyWorkAmount (bsls::TimeInterval duration)
 

Friends

class ThroughputBenchmark_WorkFunction
 
class ThroughputBenchmark_TestUtil
 

Detailed Description

This class is a mechanism that provides performance testing for multi- threaded components. It allows running different thread functions at the same time, and simulates a work load between subsequent calls to the tested thread functions. The results are loaded into a bslmt::ThroughputBenchmarkResult object, which provides access to counts of the work done by each thread, thread group, and sample, divided by the number of actual seconds of execution.

See bslmt_throughputbenchmark

Member Typedef Documentation

◆ CleanupSampleFunction

An alias to a function meeting the following contract:

void cleanupSample(bool isLast);
// Clean up after the sample run. If the specified 'isLast' is
// 'true', this is the last sample run.

◆ CleanupThreadFunction

An alias to a function meeting the following contract:

void cleanupThread();
// Clean up after each thread in a sample run.

◆ InitializeSampleFunction

An alias to a function meeting the following contract:

void initializeSample(bool isFirst);
// Initialize the sample run. If the specified 'isFirst' is
// 'true', this is the first sample run.

◆ InitializeThreadFunction

An alias to a function meeting the following contract:

void initializeThread();
// Initialize each thread in a sample run.

◆ RunFunction

An alias to a function meeting the following contract:

void runTest(int threadIndex);
// Run the main part of the benchmark having the specified
// 'threadIndex'. The behavior is undefined unless
// 'threadIndex' is in the range '[0, numThreadsInGroup)',
// where 'numThreadsInGroup' is the number of threads in a
// thread group for the associated throughput benchmark.

◆ ShutdownSampleFunction

An alias to a function meeting the following contract:

void shutdownSample(bool isLast);
// Clean up at the end of the sample run, before threads have
// been joined. If the specified 'isLast' is 'true', this is
// the last sample run.

Constructor & Destructor Documentation

◆ ThroughputBenchmark()

bslmt::ThroughputBenchmark::ThroughputBenchmark ( bslma::Allocator basicAllocator = 0)
explicit

Create an empty ThroughputBenchmark object. Optionally specify a basicAllocator used to supply memory. If basicAllocator is 0, the currently installed default allocator is used.

Member Function Documentation

◆ addThreadGroup() [1/2]

int bslmt::ThroughputBenchmark::addThreadGroup ( const RunFunction runFunction,
int  numThreads,
bsls::Types::Int64  busyWorkAmount 
)

◆ addThreadGroup() [2/2]

int bslmt::ThroughputBenchmark::addThreadGroup ( const RunFunction runFunction,
int  numThreads,
bsls::Types::Int64  busyWorkAmount,
const InitializeThreadFunction initializeFunctor,
const CleanupThreadFunction cleanupFunctor 
)

Create a set of threads, with cardinality the specified numThreads, that will repeatedly execute the specified runFunction followed by the specified busyWork, with the specified busyWorkAmount as its argument. Return the index for the thread group. Optionally specify initializeFunctor, which is run at the beginning of each thread of the sample and accepts a boolean flag isFirst, that is set to true on the first sample, and false otherwise. Optionally specify cleanupFunctor, which is run at the end of each thread of the sample and accepts a boolean flag isLast, that is set to true on the last sample, and false otherwise. Return an id for the added thread group. The behavior is undefined unless 0 < numThreads and 0 <= busyWorkAmount.

◆ allocator()

bslma::Allocator * bslmt::ThroughputBenchmark::allocator ( ) const
inline

◆ antiOptimization()

static unsigned int bslmt::ThroughputBenchmark::antiOptimization ( )
static

Return the value calculated by busyWork. Note that this method is provided to prevent the compiler from optimizing the simulated workload away.

◆ BSLMF_NESTED_TRAIT_DECLARATION()

bslmt::ThroughputBenchmark::BSLMF_NESTED_TRAIT_DECLARATION ( ThroughputBenchmark  ,
bslma::UsesBslmaAllocator   
)

◆ busyWork()

static void bslmt::ThroughputBenchmark::busyWork ( bsls::Types::Int64  busyWorkAmount)
static

Perform arithmetic operations to consume an amount of time in linear relation to the specified busyWorkAmount. Note that the duration of busyWork invoked with a particular busyWorkAmount will vary with system load.

◆ estimateBusyWorkAmount()

static bsls::Types::Int64 bslmt::ThroughputBenchmark::estimateBusyWorkAmount ( bsls::TimeInterval  duration)
static

Return an estimate of the work amount so that busyWork invoked with the returned work amount executes, approximately, for the specified duration. Note that this estimate varies with system load.

◆ execute() [1/2]

void bslmt::ThroughputBenchmark::execute ( ThroughputBenchmarkResult result,
int  millisecondsPerSample,
int  numSamples 
)

◆ execute() [2/2]

void bslmt::ThroughputBenchmark::execute ( ThroughputBenchmarkResult result,
int  millisecondsPerSample,
int  numSamples,
const InitializeSampleFunction initializeFunctor,
const ShutdownSampleFunction shutdownFunctor,
const CleanupSampleFunction cleanupFunctor 
)

Run the tests previously added with calls to the addThreadGroup method. The tests are run for the specified numSamples times. Each sample is run for the specified millisecondsPerSample duration. The results are stored in the specified result object. Optionally specify initializeFunctor, which is run at the beginning of the sample and accepts a boolean flag isFirst, that is set to true on the first sample, and false otherwise. Optionally specify shutdownFunctor, which is run at the end of each sample before threads have been joined, and accepts a boolean flag isLast, that is set to true on the last sample, and false otherwise. Optionally specify cleanupFunctor, which is run at the end of each sample after threads have been joined, and accepts a boolean flag isLast, that is set to true on the last sample, and false otherwise. The behavior is undefined unless 0 < millisecondsPerSample, 0 < numSamples, and 0 < numThreadGroups(). Also see {Structure of a Test}.

◆ numThreadGroups()

int bslmt::ThroughputBenchmark::numThreadGroups ( ) const
inline

◆ numThreads()

int bslmt::ThroughputBenchmark::numThreads ( ) const
inline

◆ numThreadsInGroup()

int bslmt::ThroughputBenchmark::numThreadsInGroup ( int  threadGroupIndex) const
inline

Return the number of threads in the specified threadGroupIndex. The behavior is undefined unless 0 <= threadGroupIndex < numThreadGroups().

Friends And Related Symbol Documentation

◆ ThroughputBenchmark_TestUtil

friend class ThroughputBenchmark_TestUtil
friend

◆ ThroughputBenchmark_WorkFunction

friend class ThroughputBenchmark_WorkFunction
friend

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