| 32 | |
| 33 | template <class TReaderWriterQueue> |
| 34 | class ReaderWriterQueueBenchmark : public CppBenchmark::BenchmarkPC |
| 35 | { |
| 36 | public: |
| 37 | using BenchmarkPC::BenchmarkPC; |
| 38 | |
| 39 | protected: |
| 40 | void Initialize(CppBenchmark::ContextPC& context) override |
| 41 | { |
| 42 | _queue = std::make_shared<TReaderWriterQueue>(); |
| 43 | _count = 1; |
| 44 | } |
| 45 | |
| 46 | void Cleanup(CppBenchmark::ContextPC& context) override |
| 47 | { |
| 48 | _queue.reset(); |
| 49 | } |
| 50 | |
| 51 | void RunProducer(CppBenchmark::ContextPC& context) override |
| 52 | { |
| 53 | if (_count >= items_to_produce) |
| 54 | { |
| 55 | if (_queue->try_enqueue(0)) |
| 56 | context.StopProduce(); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if (_queue->try_enqueue(_count)) |
| 61 | ++_count; |
| 62 | } |
| 63 | |
| 64 | void RunConsumer(CppBenchmark::ContextPC& context) override |
| 65 | { |
| 66 | int value = -1; |
| 67 | if (_queue->try_dequeue(value) && (value == 0)) |
| 68 | context.StopConsume(); |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | std::shared_ptr<TReaderWriterQueue> _queue; |
| 73 | std::atomic<int> _count; |
| 74 | }; |
| 75 | |
| 76 | class ProducerConsumerQueueBenchmark : public CppBenchmark::BenchmarkPC |
| 77 | { |
nothing calls this directly
no outgoing calls
no test coverage detected