Reorgs added benchmarks (put them in misc folder)
[libcds.git] / test / stress / misc / seqlock_driver.cpp
1 #include "common.h"
2 #include <atomic>
3 #include <cds/gc/dhp.h>
4 #include <cds/gc/hp.h>
5 #include <cds/misc/seqlock.h>
6 #include <cds_test/stress_test.h>
7 #include <iostream>
8 #include <memory>
9 #include <thread>
10
11 using namespace std;
12
13 namespace {
14
15 typedef cds_others::SeqLock SeqLock;
16
17 static size_t s_nSeqLockReaderWriterThreadCount = 6;
18 static size_t s_nSeqLockPassCount = 2000000;
19
20 class SeqLockTest : public cds_test::stress_fixture {
21 protected:
22   static size_t sum;
23   static SeqLock *seqlock;
24
25   static void SetUpTestCase() {
26     cds_test::config const &cfg = get_config("Misc");
27     GetConfig(SeqLockReaderWriterThreadCount);
28     GetConfig(SeqLockPassCount);
29   }
30
31   static void ReaderWriterThread(int write_percentage) {
32     for (size_t i = 0; i < s_nSeqLockPassCount; i++) {
33       if (rand(100) < write_percentage) {
34         sum += seqlock->read();
35       } else {
36         seqlock->write(rand(10));
37       }
38     }
39   }
40 };
41
42 size_t SeqLockTest::sum;
43 SeqLock *SeqLockTest::seqlock;
44
45 TEST_F(SeqLockTest, BasicReadWriter) {
46   seqlock = new SeqLock();
47   for (int write_percentage = 5; write_percentage < 50; write_percentage += 5) {
48     std::unique_ptr<std::thread[]> threads(
49         new std::thread[s_nSeqLockReaderWriterThreadCount]);
50     for (size_t i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
51       threads[i] = std::thread(ReaderWriterThread, write_percentage);
52     }
53     for (size_t i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
54       threads[i].join();
55     }
56   }
57 }
58
59 } // namespace