Refactors misc test cases
[libcds.git] / test / stress / misc / seqlock_driver.cpp
index e230de5..abf2583 100644 (file)
@@ -1,3 +1,4 @@
+#include "common.h"
 #include <atomic>
 #include <cds/gc/dhp.h>
 #include <cds/gc/hp.h>
@@ -12,30 +13,33 @@ namespace {
 
 typedef cds_others::SeqLock SeqLock;
 
+static size_t s_nSeqLockReaderWriterThreadCount = 6;
+static size_t s_nSeqLockPassCount = 2000000;
+
 class SeqLockTest : public cds_test::stress_fixture {
 protected:
   static int sum;
   static SeqLock *seqlock;
   static const int kReaderThreads = 0;
   static const int kWriterThreads = 0;
-  static const int kReaderWriterThreads = 6;
   static const int kWriterPercentage = 15;
-  static const int kPassCount = 15000;
 
-  static void SetUpTestCase() {}
+  static void SetUpTestCase() {
+    cds_test::config const &cfg = get_config("Misc");
+    GetConfig(SeqLockReaderWriterThreadCount);
+    GetConfig(SeqLockPassCount);
+  }
 
   static void ReaderThread() {}
 
   static void WriterThread() {}
 
-  static void ReaderWriterThread() {
-    for (int i = 0; i < kPassCount; i++) {
-      for (int j = 0; j < kPassCount; j++) {
-        if (rand(100) < kWriterPercentage) {
-          sum += seqlock->read();
-        } else {
-          seqlock->write(rand(10));
-        }
+  static void ReaderWriterThread(int write_percentage) {
+    for (int i = 0; i < s_nSeqLockPassCount; i++) {
+      if (rand(100) < write_percentage) {
+        sum += seqlock->read();
+      } else {
+        seqlock->write(rand(10));
       }
     }
   }
@@ -43,26 +47,18 @@ protected:
 
 int SeqLockTest::sum;
 SeqLock *SeqLockTest::seqlock;
-const int SeqLockTest::kReaderThreads;
-const int SeqLockTest::kWriterThreads;
-const int SeqLockTest::kReaderWriterThreads;
 
 TEST_F(SeqLockTest, BasicReadWriter) {
   seqlock = new SeqLock();
-  int num_threads = kReaderThreads + kWriterThreads + kReaderWriterThreads;
-  std::thread *threads = new std::thread[num_threads];
-  for (int i = 0; i < kReaderThreads; i++) {
-    threads[i] = std::thread(ReaderThread);
-  }
-  for (int i = kReaderThreads; i < (kReaderThreads + kWriterThreads); i++) {
-    threads[i] = std::thread(WriterThread);
-  }
-  for (int i = (kReaderThreads + kWriterThreads); i < num_threads; i++) {
-    threads[i] = std::thread(ReaderWriterThread);
-  }
-
-  for (int i = 0; i < num_threads; i++) {
-    threads[i].join();
+  for (int write_percentage = 5; write_percentage < 50; write_percentage += 5) {
+    std::thread *threads = new std::thread[s_nSeqLockReaderWriterThreadCount];
+    for (size_t i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
+      threads[i] = std::thread(ReaderWriterThread, write_percentage);
+    }
+    for (int i = 0; i < s_nSeqLockReaderWriterThreadCount; i++) {
+      threads[i].join();
+    }
+    delete[] threads;
   }
 }