From 8c990508c67d74abf0ca096acb7a903a03ceee6d Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Mon, 12 Feb 2018 12:54:24 -0800 Subject: [PATCH] Uses cdsstree framework in folly sequential queue test cases --- .../stress-sequential-folly-queue.cpp | 309 ++++++++++-------- 1 file changed, 168 insertions(+), 141 deletions(-) diff --git a/folly/stress-test/stress-sequential-folly-queue.cpp b/folly/stress-test/stress-sequential-folly-queue.cpp index 115c5231..926652d0 100644 --- a/folly/stress-test/stress-sequential-folly-queue.cpp +++ b/folly/stress-test/stress-sequential-folly-queue.cpp @@ -1,80 +1,86 @@ -#include -#include -#include -#include - -#include - -#include - -namespace { - -// Unbounded queue -size_t kUnboundedQueueEnqueueStride = 10000; -size_t kUSPSCQueueEnqueueCount = 1200000000; -size_t kUMPSCQueueEnqueueCount = 320000000; -size_t kUSPMCQueueEnqueueCount = 320000000; -size_t kUMPMCQueueEnqueueCount = 320000000; - -typedef folly::USPSCQueue USPSCQueue; -typedef folly::UMPSCQueue UMPSCQueue; -typedef folly::USPMCQueue USPMCQueue; -typedef folly::UMPMCQueue UMPMCQueue; - -// Dynamic bounded queue -size_t kDynamicBoundedQueueEnqueueStride = 50000; -size_t kDynamicBoundedQueueCapacity = 200000; -size_t kDSPSCQueueEnqueueCount = 1200000000; -size_t kDMPSCQueueEnqueueCount = 320000000; -size_t kDSPMCQueueEnqueueCount = 320000000; -size_t kDMPMCQueueEnqueueCount = 320000000; - -typedef folly::DSPSCQueue DSPSCQueue; -typedef folly::DMPSCQueue DMPSCQueue; -typedef folly::DSPMCQueue DSPMCQueue; -typedef folly::DMPMCQueue DMPMCQueue; - -// AtomicLinkedList -size_t kAtomicLinkedListSize = 50000; -size_t kAtomicLinkedListPassCount = 10000; -typedef folly::AtomicLinkedList AtomicLinkedList; - -// MPMC Queue (linearizable) -size_t kMPMCQueueEnqueueStride = 10000; -size_t kMPMCQueueCapacity = 50000; -size_t kMPMCQueueEnqueueCount = 500000000; -typedef folly::MPMCQueue MPMCQueue; +#include "queue_test.h" + +namespace folly_test { + +class FollyQueueEnqueueDequeueTest_Sequential + : public cds_test::stress_fixture { +protected: + // Unbounded queue + static size_t s_nUnboundedQueueEnqueueStride; + static size_t s_nUSPSCQueueEnqueueCount; + static size_t s_nUMPSCQueueEnqueueCount; + static size_t s_nUSPMCQueueEnqueueCount; + static size_t s_nUMPMCQueueEnqueueCount; + + // Dynamic bounded queue + static size_t s_nDynamicBoundedQueueEnqueueStride; + static size_t s_nDynamicBoundedQueueCapacity; + static size_t s_nDSPSCQueueEnqueueCount; + static size_t s_nDMPSCQueueEnqueueCount; + static size_t s_nDSPMCQueueEnqueueCount; + static size_t s_nDMPMCQueueEnqueueCount; + + // AtomicLinkedList + static size_t s_nAtomicLinkedListSize; + static size_t s_nAtomicLinkedListPassCount; + + // MPMC Queue (linearizable) + static size_t s_nMPMCQueueEnqueueStride; + static size_t s_nMPMCQueueCapacity; + static size_t s_nMPMCQueueEnqueueCount; + + static void SetUpTestCase() { + const cds_test::config &cfg = get_config("SequentialFollyQueue"); + // Unbounded queue + GetConfigNonZeroExpected(UnboundedQueueEnqueueStride, 10000); + GetConfigNonZeroExpected(USPSCQueueEnqueueCount, 1200000000); + GetConfigNonZeroExpected(UMPSCQueueEnqueueCount, 320000000); + GetConfigNonZeroExpected(USPMCQueueEnqueueCount, 320000000); + GetConfigNonZeroExpected(UMPMCQueueEnqueueCount, 320000000); + // Dynamic bounded queue + GetConfigNonZeroExpected(DynamicBoundedQueueEnqueueStride, 50000); + GetConfigNonZeroExpected(DynamicBoundedQueueCapacity, 200000); + GetConfigNonZeroExpected(DSPSCQueueEnqueueCount, 1200000000); + GetConfigNonZeroExpected(DMPSCQueueEnqueueCount, 320000000); + GetConfigNonZeroExpected(DSPMCQueueEnqueueCount, 320000000); + GetConfigNonZeroExpected(DMPMCQueueEnqueueCount, 320000000); + // AtomicLinkedList + GetConfigNonZeroExpected(AtomicLinkedListSize, 50000); + GetConfigNonZeroExpected(AtomicLinkedListPassCount, 10000); + // MPMC Queue (linearizable) + GetConfigNonZeroExpected(MPMCQueueEnqueueStride, 10000); + GetConfigNonZeroExpected(MPMCQueueCapacity, 50000); + GetConfigNonZeroExpected(MPMCQueueEnqueueCount, 500000000); + } -} + static void run_atomic_linkedlist() { + for (size_t pass = 0; pass < s_nAtomicLinkedListPassCount; pass++) { + std::unique_ptr list(new AtomicLinkedList()); + bool in_order = true; + for (size_t i = 0; i < s_nAtomicLinkedListSize; i++) { + list->insertHead(i); + } + size_t nSum = 0; + auto func = [&nSum](size_t elem) { nSum += elem; }; + if (in_order) { + list->sweep(func); + } else { + list->reverseSweep(func); + } + in_order = !in_order; -void run_atomic_linkedlist() { - for (size_t pass = 0; pass < kAtomicLinkedListPassCount; pass++) { - std::unique_ptr list(new AtomicLinkedList()); - bool in_order = true; - for (size_t i = 0; i < kAtomicLinkedListSize; i++) { - list->insertHead(i); + size_t supposed_sum = + s_nAtomicLinkedListSize * (s_nAtomicLinkedListSize - 1) / 2; + EXPECT_EQ(nSum, supposed_sum); } - size_t nSum = 0; - auto func = [&nSum] (size_t elem) { nSum += elem; }; - if (in_order) { - list->sweep(func); - } else { - list->reverseSweep(func); - } - in_order = !in_order; - - size_t supposed_sum = kAtomicLinkedListSize * (kAtomicLinkedListSize - 1) / 2; - EXPECT_EQ(nSum, supposed_sum); } -} -template -void run_queue(Queue* q, size_t enqueue_count, size_t enqueue_stride) { + template + static void run_queue(Queue *q, size_t enqueue_count, size_t enqueue_stride) { size_t nNo = 0; size_t pop_sum = 0; while (nNo < enqueue_count) { - size_t curr_push_count = - std::min(enqueue_count - nNo, enqueue_stride); + size_t curr_push_count = std::min(enqueue_count - nNo, enqueue_stride); for (size_t i = 0; i < curr_push_count; i++) { q->enqueue(nNo++); } @@ -85,105 +91,126 @@ void run_queue(Queue* q, size_t enqueue_count, size_t enqueue_stride) { } } size_t supposed_sum = enqueue_count * (enqueue_count - 1) / 2; - EXPECT_EQ (pop_sum, supposed_sum); -} + EXPECT_EQ(pop_sum, supposed_sum); + } + + template + static void run_without_initial_capacity(size_t enqueue_count, + size_t enqueue_stride) { + std::unique_ptr q(new Queue()); + run_queue(q.get(), enqueue_count, enqueue_stride); + } + + template + static void run_with_initial_capacity(size_t queue_capacity, + size_t enqueue_count, + size_t enqueue_stride) { + std::unique_ptr q(new Queue(queue_capacity)); + run_queue(q.get(), enqueue_count, enqueue_stride); + } +}; // MPMC Specialization. template <> -void run_queue(MPMCQueue* q, size_t enqueue_count, size_t enqueue_stride) { - size_t nNo = 0; - size_t push_sum = 0; - size_t pop_sum = 0; - while (nNo < enqueue_count) { - size_t curr_push_count = - std::min(enqueue_count - nNo, enqueue_stride); - for (size_t i = 0; i < curr_push_count; i++) { - if (q->write(nNo)) { - push_sum += nNo; - nNo++; - } - } - size_t res; - while (q->read(res)) { - pop_sum += res; +void FollyQueueEnqueueDequeueTest_Sequential::run_queue(MPMCQueue *q, + size_t enqueue_count, + size_t enqueue_stride) { + size_t nNo = 0; + size_t push_sum = 0; + size_t pop_sum = 0; + while (nNo < enqueue_count) { + size_t curr_push_count = std::min(enqueue_count - nNo, enqueue_stride); + for (size_t i = 0; i < curr_push_count; i++) { + if (q->write(nNo)) { + push_sum += nNo; + nNo++; } } + size_t res; + while (q->read(res)) { + pop_sum += res; + } + } - size_t supposed_sum = enqueue_count * (enqueue_count - 1) / 2; - EXPECT_EQ(pop_sum, supposed_sum); -} - -template -void run_without_initial_capacity(size_t enqueue_count, size_t enqueue_stride) { - std::unique_ptr q(new Queue()); - run_queue(q.get(), enqueue_count, enqueue_stride); -} - -template -void run_with_initial_capacity(size_t queue_capacity, size_t enqueue_count, - size_t enqueue_stride) { - std::unique_ptr q(new Queue(queue_capacity)); - run_queue(q.get(), enqueue_count, enqueue_stride); + size_t supposed_sum = enqueue_count * (enqueue_count - 1) / 2; + EXPECT_EQ(pop_sum, supposed_sum); } -class FollyQueueEnqueueDequeueTest : public ::testing::Test { - -}; +// Unbounded queue +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nUnboundedQueueEnqueueStride; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nUSPSCQueueEnqueueCount; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nUMPSCQueueEnqueueCount; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nUSPMCQueueEnqueueCount; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nUMPMCQueueEnqueueCount; +// Dynamic bounded queue +size_t FollyQueueEnqueueDequeueTest_Sequential:: + s_nDynamicBoundedQueueEnqueueStride; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nDynamicBoundedQueueCapacity; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nDSPSCQueueEnqueueCount; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nDMPSCQueueEnqueueCount; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nDSPMCQueueEnqueueCount; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nDMPMCQueueEnqueueCount; +// AtomicLinkedList +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nAtomicLinkedListSize; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nAtomicLinkedListPassCount; +// MPMC Queue (linearizable) +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nMPMCQueueEnqueueStride; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nMPMCQueueCapacity; +size_t FollyQueueEnqueueDequeueTest_Sequential::s_nMPMCQueueEnqueueCount; -TEST_F(FollyQueueEnqueueDequeueTest, FollyMPMCQueue) { - run_with_initial_capacity( - kMPMCQueueCapacity, kMPMCQueueEnqueueCount, kMPMCQueueEnqueueStride); +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyMPMCQueue) { + run_with_initial_capacity(s_nMPMCQueueCapacity, + s_nMPMCQueueEnqueueCount, + s_nMPMCQueueEnqueueStride); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyAtomicLinkedList) { +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyAtomicLinkedList) { run_atomic_linkedlist(); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyUnboundedQueue_SPSC) { - run_without_initial_capacity( - kUSPSCQueueEnqueueCount, kUnboundedQueueEnqueueStride); +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyUnboundedQueue_SPSC) { + run_without_initial_capacity(s_nUSPSCQueueEnqueueCount, + s_nUnboundedQueueEnqueueStride); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyUnboundedQueue_MPSC) { - run_without_initial_capacity( - kUMPSCQueueEnqueueCount, kUnboundedQueueEnqueueStride); +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyUnboundedQueue_MPSC) { + run_without_initial_capacity(s_nUMPSCQueueEnqueueCount, + s_nUnboundedQueueEnqueueStride); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyUnboundedQueue_SPMC) { - run_without_initial_capacity( - kUSPMCQueueEnqueueCount, kUnboundedQueueEnqueueStride); +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyUnboundedQueue_SPMC) { + run_without_initial_capacity(s_nUSPMCQueueEnqueueCount, + s_nUnboundedQueueEnqueueStride); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyUnboundedQueue_MPMC) { - run_without_initial_capacity( - kUMPMCQueueEnqueueCount, - kUnboundedQueueEnqueueStride); +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyUnboundedQueue_MPMC) { + run_without_initial_capacity(s_nUMPMCQueueEnqueueCount, + s_nUnboundedQueueEnqueueStride); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyDynamicBoundedQueue_SPSC) { +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyDynamicBoundedQueue_SPSC) { // DynamicBoundedQueue - run_with_initial_capacity( - kDynamicBoundedQueueCapacity, - kDSPSCQueueEnqueueCount, kDynamicBoundedQueueEnqueueStride); + run_with_initial_capacity(s_nDynamicBoundedQueueCapacity, + s_nDSPSCQueueEnqueueCount, + s_nDynamicBoundedQueueEnqueueStride); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyDynamicBoundedQueue_MPSC) { - run_with_initial_capacity( - kDynamicBoundedQueueCapacity, - kDMPSCQueueEnqueueCount, - kDynamicBoundedQueueEnqueueStride); +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyDynamicBoundedQueue_MPSC) { + run_with_initial_capacity(s_nDynamicBoundedQueueCapacity, + s_nDMPSCQueueEnqueueCount, + s_nDynamicBoundedQueueEnqueueStride); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyDynamicBoundedQueue_SPMC) { - run_with_initial_capacity( - kDynamicBoundedQueueCapacity, - kDSPMCQueueEnqueueCount, - kDynamicBoundedQueueEnqueueStride); +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyDynamicBoundedQueue_SPMC) { + run_with_initial_capacity(s_nDynamicBoundedQueueCapacity, + s_nDSPMCQueueEnqueueCount, + s_nDynamicBoundedQueueEnqueueStride); } -TEST_F(FollyQueueEnqueueDequeueTest, FollyDynamicBoundedQueue_MPMC) { - run_with_initial_capacity( - kDynamicBoundedQueueCapacity, - kDMPMCQueueEnqueueCount, - kDynamicBoundedQueueEnqueueStride); +TEST_F(FollyQueueEnqueueDequeueTest_Sequential, FollyDynamicBoundedQueue_MPMC) { + run_with_initial_capacity(s_nDynamicBoundedQueueCapacity, + s_nDMPMCQueueEnqueueCount, + s_nDynamicBoundedQueueEnqueueStride); } + +} // namespace folly_test -- 2.34.1