From 9eadcfcdb4d766a4a10b3828d4a574c71a4ce70f Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Tue, 6 Feb 2018 17:00:30 -0800 Subject: [PATCH] Adds unbounded queue test case --- .../stress-sequential-folly-queue.cpp | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 folly/stress-test/stress-sequential-folly-queue.cpp diff --git a/folly/stress-test/stress-sequential-folly-queue.cpp b/folly/stress-test/stress-sequential-folly-queue.cpp new file mode 100644 index 00000000..eade97fa --- /dev/null +++ b/folly/stress-test/stress-sequential-folly-queue.cpp @@ -0,0 +1,78 @@ +#include + +#include +#include +#include +#include + +namespace { + +const char* kTestName = "EnqueueDequeue"; +size_t kEnqueueStride = 10000; + +size_t kUSPSCQueueEnqueueCount = 1200000000; +const char* kUSPSCQueueBenchmarkName = "FollyUSPSCQueue"; + +size_t kUMPSCQueueEnqueueCount = 320000000; +const char* kUMPSCQueueBenchmarkName = "FollyUMPSCQueue"; + +size_t kUSPMCQueueEnqueueCount = 320000000; +const char* kUSPMCQueueBenchmarkName = "FollyUSPMCQueue"; + +size_t kUMPMCQueueEnqueueCount = 320000000; +const char* kUMPMCQueueBenchmarkName = "FollyMPMCQueue"; + +typedef folly::USPSCQueue USPSCQueue; +typedef folly::UMPSCQueue UMPSCQueue; +typedef folly::USPMCQueue USPMCQueue; +typedef folly::UMPMCQueue UMPMCQueue; + +} + +template +void run_queue(size_t enqueue_count, const char* bench_name) { + std::cout << "[ RUN ] " << kTestName << "." << bench_name << std::endl; + auto start_time = std::chrono::system_clock::now(); + + size_t nNo = 0; + size_t push_failure = 0; + size_t pop_sum = 0; + std::unique_ptr q(new Queue()); + while (nNo < enqueue_count) { + size_t curr_push_count = + std::min(enqueue_count - nNo, kEnqueueStride); + for (size_t i = 0; i < curr_push_count; i++) { + q->enqueue(nNo++); + } + size_t res; + for (size_t i = 0; i < curr_push_count; i++) { + q->dequeue(res); + pop_sum += res; + } + } + + auto finish_time = std::chrono::system_clock::now(); + auto dur = finish_time - start_time; + auto milisecs = std::chrono::duration_cast(dur); + + size_t supposed_sum = enqueue_count * (enqueue_count - 1) / 2; + if (pop_sum != supposed_sum) { + std::cout << "Sequential queue pop sum: " << pop_sum + << " != " << supposed_sum << "\n"; + std::cout << "[ FAILED ] " << kTestName << "." << bench_name + << " (" << milisecs.count() << " ms)" << std::endl; + assert(false && "Folly unbounded queue ERROR"); + } else { + std::cout << "[ OK ] " << kTestName << "." << bench_name + << " (" << milisecs.count() << " ms)" << std::endl; + } +} + +int main() { + run_queue(kUSPSCQueueEnqueueCount, kUSPSCQueueBenchmarkName); + run_queue(kUMPSCQueueEnqueueCount, kUMPSCQueueBenchmarkName); + run_queue(kUSPMCQueueEnqueueCount, kUSPMCQueueBenchmarkName); + run_queue(kUMPMCQueueEnqueueCount, kUMPMCQueueBenchmarkName); + + return 0; +} -- 2.34.1