BDE 4.14.0 Production release
|
Provide a performance test harness for multi-threaded components.
This component defines a mechanism, bslmt::ThroughputBenchmark
, that provides performance testing for multi- threaded components. 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.
A test is composed from one or more thread groups, each running one or more threads. Each thread in a thread group executes a thread function, with a simulated work load executing between subsequent calls to the thread function. To provide reliability, the test is executed multiple times. A single execution of a test is referred to as a "sample execution" and its result referred to as a "sample". To support fine tuning of the test, it is possible to provide initialize and cleanup functions for a sample and / or a thread.
This section illustrates intended use of this component.
In the following example we test the throughput of a bsl::queue<int>
in a multi-threaded environment, where multiple "producer" threads are pushing elements, and multiple "consumer" threads are popping these elements.
First, we define a global queue, a mutex to protect this queue, and a semaphore for a "pop" operation to block on:
Next, we define a counter value we push in:
Then, we define simple push and pop functions that manipulate this queue:
Next, we define a thread "cleanup" function for the push thread group, which pushes a couple of extra elements to make sure that the pop thread group will not hang on an empty queue:
Then, we create a bslmt::ThroughputBenchmark
object and add push and pop thread groups, each with 2 threads and a work load (arithmetic operations to consume an amount of time) of 100:
Now, we create a bslmt::ThroughputBenchmarkResult
object to contain the result, and call execute
to run the benchmark for 500 millseconds 10 times:
Finally, we print the median of the throughput of the consumer thread group.