16 int BenchmarkPC::CountLaunches()
const
21 void BenchmarkPC::Launch(
int& current,
int total, LauncherHandler& handler)
32 _settings._params.emplace_back(-1, -1, -1);
37 int producers = std::get<0>(pc);
38 int consumers = std::get<1>(pc);
44 ContextPC context(producers, consumers, std::get<0>(param), std::get<1>(param), std::get<2>(param));
54 handler.onLaunching(++current, total, *
this, context, attempt);
64 Barrier barrier(producers + consumers);
67 context._current->StartCollectingMetrics();
68 context._metrics->AddOperations(1);
71 for (
int i = 0; i < producers; ++i)
73 _threads.emplace_back([
this, &barrier, &context, latency_params, latency_auto, producers, infinite, operations, duration, i]()
76 ContextPC producer_context(context);
79 std::shared_ptr<Phase> producer_phase = context.StartPhaseThreadSafe(
"producer-" + std::to_string(i));
80 PhaseCore* producer_phase_core =
dynamic_cast<PhaseCore*
>(producer_phase.get());
83 producer_context._current = producer_phase_core;
84 producer_context._metrics = &producer_phase_core->current();
85 producer_context._metrics->AddOperations(-1);
86 producer_context._metrics->SetThreads(producers);
89 producer_context._current->InitLatencyHistogram(latency_params);
94 bool producer_infinite = infinite;
95 int64_t producer_duration = duration;
96 int64_t producer_operations = operations;
98 uint64_t timestamp = 0;
104 if (producer_duration > 0)
107 uint64_t timespan = 0;
110 for (; timespan < 1000000000; ++count)
121 producer_operations = (1000000000ull * count * producer_duration) / timespan;
124 producer_context._current->StartCollectingMetrics();
125 while (!producer_context.produce_stopped() && !producer_context.canceled() && (producer_infinite || (producer_operations > 0)))
128 producer_context._metrics->AddOperations(1);
142 producer_operations -= 1;
144 producer_context._current->StopCollectingMetrics();
155 for (
int i = 0; i < consumers; ++i)
157 _threads.emplace_back([
this, &barrier, &context, latency_params, latency_auto, consumers, i]()
160 ContextPC consumer_context(context);
163 std::shared_ptr<Phase> consumer_phase = context.StartPhaseThreadSafe(
"consumer-" + std::to_string(i));
164 PhaseCore* consumer_phase_core =
dynamic_cast<PhaseCore*
>(consumer_phase.get());
167 consumer_context._current = consumer_phase_core;
168 consumer_context._metrics = &consumer_phase_core->current();
169 consumer_context._metrics->AddOperations(-1);
170 consumer_context._metrics->SetThreads(consumers);
173 consumer_context._current->InitLatencyHistogram(latency_params);
181 consumer_context._current->StartCollectingMetrics();
182 while (!consumer_context.consume_stopped() && !consumer_context.canceled())
185 consumer_context._metrics->AddOperations(1);
188 uint64_t timestamp = 0;
199 consumer_context._current->StopCollectingMetrics();
210 for (
auto& thread : _threads)
217 context._current->StopCollectingMetrics();
223 handler.onLaunched(current, total, *
this, context, attempt);
226 context._current->MergeMetrics();
227 context._current->ResetMetrics();
Barrier synchronization primitive definition.
Producers/Consumers benchmark base definition.
bool _launched
Benchmark launched flag.
Settings _settings
Benchmark settings.
static void UpdateBenchmarkNames(std::vector< std::shared_ptr< PhaseCore >> &phases)
Update benchmark names for the given benchmark phases collection.
void InitBenchmarkContext(Context &context)
Initialize benchmark context.
static void UpdateBenchmarkThreads(std::vector< std::shared_ptr< PhaseCore >> &phases)
Update benchmark threads metrics for the given benchmark phases collection.
static void UpdateBenchmarkMetrics(std::vector< std::shared_ptr< PhaseCore >> &phases)
Update benchmark metrics for the given benchmark phases collection.
std::vector< std::shared_ptr< PhaseCore > > _phases
Benchmark phases.
virtual void RunConsumer(ContextPC &context)=0
Consumer run method.
virtual void RunProducer(ContextPC &context)=0
Producer run method.
virtual void Initialize(ContextPC &context)
Initialize benchmark.
virtual void Cleanup(ContextPC &context)
Cleanup benchmark.
virtual void CleanupProducer(ContextPC &context)
Cleanup producer.
virtual void InitializeProducer(ContextPC &context)
Initialize producer.
virtual void InitializeConsumer(ContextPC &context)
Initialize consumer.
virtual void CleanupConsumer(ContextPC &context)
Cleanup consumer.
const std::vector< std::tuple< int, int, int > > & params() const noexcept
Get collection of independent parameters in a benchmark plan.
int attempts() const noexcept
Get count of independent benchmark attempts.
int64_t duration() const noexcept
Get benchmark duration in milliseconds.
bool infinite() const noexcept
Is benchmark running with infinite count of operations (until cancel)?
const std::tuple< int64_t, int64_t, int > & latency() const noexcept
Get latency parameters.
int64_t operations() const noexcept
Get count of operations.
const std::vector< std::tuple< int, int > > & pc() const noexcept
Get collection of independent producers/consumers counts in a benchmark plan.
bool latency_auto() const noexcept
Get automatic latency update flag.
static uint64_t Timestamp()
Get the current timestamp in nanoseconds.
Launcher handler definition.
C++ Benchmark project definitions.