Refactors Folly map test cases to use cdsstress library
[folly.git] / folly / stress-test / stress-sequential-folly-map.cpp
index 53b95290ac6eee844b0d7c68e2eddcbba1909549..61ce5f9ea5288b18fe09043feeda57592558bff8 100644 (file)
-#include <folly/concurrency/ConcurrentHashMap.h>
-#include <folly/AtomicHashMap.h>
-#include <folly/AtomicUnorderedMap.h>
+#include "map_test.h"
 
-#include <gtest/gtest.h>
+namespace folly_test {
 
-#include <memory>
+class FollyMapInsDelFindTest_Sequential : public cds_test::stress_fixture {
+protected:
+  static unsigned s_nInsertDeletePercentage;
+  static size_t s_nMapSize;
 
-namespace {
+  enum actions { do_find, do_insert, do_delete };
+  static const unsigned int kShuffleSize = 100;
+  static actions s_arrShuffle[kShuffleSize];
 
-const unsigned s_nInsertPercentage = 10;
+  static size_t s_nConcurrentHashMapPassCount;
+  static size_t s_nAtomicHashMapPassCount;
+  static size_t s_nAtomicUnorderedInsertMapPassCount;
 
-const size_t kConcurrentHashMapSize = 10000;
-const size_t kConcurrentHashMapPassCount = 36000;
+  static void SetUpTestCase() {
+    const cds_test::config& cfg = get_config("SequentialFollyMap");
+    GetConfigNonZeroExpected(InsertDeletePercentage, 5);
+    GetConfigNonZeroExpected(MapSize, 10000);
+    GetConfigNonZeroExpected(ConcurrentHashMapPassCount, 1000);
+    GetConfigNonZeroExpected(AtomicHashMapPassCount, 1000);
+    GetConfigNonZeroExpected(AtomicUnorderedInsertMapPassCount, 1000);
+  }
 
-const size_t kAtomicHashMapSize = 10000;
-const size_t kAtomicHashMapPassCount = 250000;
-
-const size_t kAtomicUnorderedInsertMapSize = 10000;
-const size_t kAtomicUnorderedInsertMapPassCount = 500000;
-
-typedef folly::ConcurrentHashMap<size_t, size_t> ConcurrentHashMap;
-typedef folly::AtomicHashMap<size_t, size_t> AtomicHashMap;
-typedef folly::AtomicUnorderedInsertMap64<size_t, size_t>
-    AtomicUnorderedInsertMap;
-}
-
-template <typename Key>
-bool map_contains(const AtomicUnorderedInsertMap* map, Key key) {
-  return map->find(key) != map->cend();
-}
-
-template <typename Key>
-bool map_contains(const ConcurrentHashMap* map, Key key) {
-  return map->find(key) != map->cend();
-}
-
-template <typename Map, typename Key>
-bool map_contains(const Map* map, Key key) {
-  return map->find(key) != map->end();
-}
-
-template <typename Map>
-void run_atomic_unordered_insert_map(size_t map_size, size_t pass_count) {
-    size_t nInsertedNum = 0;
-    size_t nFindSuccess = 0;
-    std::unique_ptr<Map> map(new Map(map_size));
-    for (size_t count = 0; count < pass_count; count++) {
-        for (size_t i = 0; i < map_size; ++i) {
-            // The number to operate on the map.
-            size_t n = map_size + i;
-            // Insert
-            if (i % s_nInsertPercentage == 1) {
-                auto iter = map->find(i);
-                if (iter != map->cend()) {
-                  if (iter->second == n) {
-                    map->emplace(i, n / 2);
-                  } else {
-                    map->emplace(i, n);
-                  }
-                } else {
-                  map->emplace(i, n);
-                }
-                nInsertedNum++;
-            }
-            // Find
-            {
-                if (map_contains(map.get(), i)) {
-                    ++nFindSuccess;
-                }
-            }
-        }
-    }
-    EXPECT_EQ(nFindSuccess, nInsertedNum);
-}
-
-template <typename Map>
-void run_atomic_hashmap(size_t map_size, size_t pass_count) {
+  template <typename Map> static void run_test(Map* map, size_t pass_count) {
     size_t nInsertedNum = 0;
+    size_t nNotInsertedNum = 0;
     size_t nFindSuccess = 0;
-    std::unique_ptr<Map> map(new Map(map_size));
-    for (size_t count = 0; count < pass_count; count++) {
-        for (size_t i = 0; i < map_size; ++i) {
-            // The number to operate on the map.
-            size_t n = map_size + i;
-            // Insert
-            if (i % s_nInsertPercentage == 1) {
-                auto iter = map->find(i);
-                if (iter != map->end()) {
-                  if (iter->second == n) {
-                    iter->second = n / 2;
-                  } else {
-                    iter->second = n;
-                  }
-                } else {
-                  map->insert({i, n});
-                }
-                nInsertedNum++;
-            }
-            // Find
-            {
-                if (map_contains(map.get(), i)) {
-                    ++nFindSuccess;
-                }
-            }
-        }
-    }
-    EXPECT_EQ(nFindSuccess, nInsertedNum);
-}
+    size_t nFindFailed = 0;
+    size_t nDeletedNum = 0;
+    size_t nOperations = 0;
 
-template <typename Map>
-void run_concurrent_hashmap(size_t map_size, size_t pass_count) {
-    size_t nInsertedNum = 0;
-    size_t nFindSuccess = 0;
-    std::unique_ptr<Map> map(new Map(map_size));
+    // The number to operate on the map.
+    size_t n = s_nMapSize;
     for (size_t count = 0; count < pass_count; count++) {
-        for (size_t i = 0; i < map_size; ++i) {
-            // The number to operate on the map.
-            size_t n = map_size + i;
-            // Insert
-            if (i % s_nInsertPercentage == 1) {
-                  auto iter = map->insert({i, n});
-                  nInsertedNum++;
-            }
-            // Find
-            {
-                if (map_contains(map.get(), i)) {
-                    ++nFindSuccess;
-                }
-            }
-            // Delete
-            if (i % s_nInsertPercentage == 1) {
-                if (map_contains(map.get(), i)) {
-                    map->erase(i);
-                }
-            }
+      // Insert
+      unsigned mod = count % kShuffleSize;
+      if (mod < s_nInsertDeletePercentage) {
+        if (map_insert(map, n, n)) {
+          nInsertedNum++;
+        } else if (map_insert(map, n, n + 1)) {
+          nInsertedNum++;
+        } else {
+          nNotInsertedNum++;
         }
+      } else {
+        nNotInsertedNum++;
+      }
+      // Find
+      if (map_find(map, n)) {
+        ++nFindSuccess;
+      } else {
+        ++nFindFailed;
+      }
+      // Delete
+      if (mod < s_nInsertDeletePercentage && map_delete(map, n)) {
+        nDeletedNum++;
+      }
+      if (++n == 2 * s_nMapSize) {
+        n = s_nMapSize;
+      }
     }
-    EXPECT_EQ(nFindSuccess, nInsertedNum);
-}
-
-class FollyMapInsDelFindTest: public ::testing::Test {
+    EXPECT_EQ(nInsertedNum, nDeletedNum);
+    EXPECT_EQ(nInsertedNum, nFindSuccess);
+    EXPECT_EQ(nFindFailed, nNotInsertedNum);
+  }
 };
 
-TEST_F(FollyMapInsDelFindTest, FollyConcurrentHashMap) {
-  run_concurrent_hashmap<ConcurrentHashMap>(
-          kConcurrentHashMapSize,
-          kConcurrentHashMapPassCount);
+size_t FollyMapInsDelFindTest_Sequential::s_nMapSize;
+unsigned FollyMapInsDelFindTest_Sequential::s_nInsertDeletePercentage;
+const unsigned int FollyMapInsDelFindTest_Sequential::kShuffleSize;
+FollyMapInsDelFindTest_Sequential::actions
+    FollyMapInsDelFindTest_Sequential::s_arrShuffle
+        [FollyMapInsDelFindTest_Sequential::kShuffleSize];
+
+size_t FollyMapInsDelFindTest_Sequential::s_nConcurrentHashMapPassCount;
+size_t FollyMapInsDelFindTest_Sequential::s_nAtomicHashMapPassCount;
+size_t FollyMapInsDelFindTest_Sequential::s_nAtomicUnorderedInsertMapPassCount;
+
+TEST_F(FollyMapInsDelFindTest_Sequential, FollyConcurrentHashMap) {
+  std::unique_ptr<ConcurrentHashMap> map(
+      new ConcurrentHashMap(s_nMapSize));
+  run_test(map.get(), s_nConcurrentHashMapPassCount);
 }
 
-TEST_F(FollyMapInsDelFindTest, FollyAtomicHashMap) {
-  run_atomic_hashmap<AtomicHashMap>(
-          kAtomicHashMapSize,
-          kAtomicHashMapPassCount);
+TEST_F(FollyMapInsDelFindTest_Sequential, FollyAtomicHashMap) {
+  std::unique_ptr<AtomicHashMap> map(
+      new AtomicHashMap(s_nMapSize));
+  run_test(map.get(), s_nAtomicHashMapPassCount);
 }
 
-TEST_F(FollyMapInsDelFindTest, FollyAtomicUnorderedInsertMap) {
-  run_atomic_unordered_insert_map<AtomicUnorderedInsertMap>(
-          kAtomicUnorderedInsertMapSize,
-          kAtomicUnorderedInsertMapPassCount);
+TEST_F(FollyMapInsDelFindTest_Sequential, FollyAtomicUnorderedInsertMap) {
+  std::unique_ptr<AtomicUnorderedInsertMap> map(
+      new AtomicUnorderedInsertMap(s_nMapSize));
+  run_test(map.get(), s_nAtomicUnorderedInsertMapPassCount);
 }
 
-int main(int argc, char** argv) {
-  // Init Google test
-  ::testing::InitGoogleTest(&argc, argv);
-  int result = RUN_ALL_TESTS();
-  return result;
-}
+} // namespace folly_test