Refactors folly test cases to use gtest
[folly.git] / folly / stress-test / stress-sequential-folly-map.cpp
index c9acaee9ef1266cb3d8281593b4142412431d506..53b95290ac6eee844b0d7c68e2eddcbba1909549 100644 (file)
@@ -2,29 +2,22 @@
 #include <folly/AtomicHashMap.h>
 #include <folly/AtomicUnorderedMap.h>
 
+#include <gtest/gtest.h>
 
-#include <chrono>
-#include <cassert>
-#include <iostream>
 #include <memory>
 
 namespace {
 
 const unsigned s_nInsertPercentage = 10;
-const char* kTestName = "InsDelFind";
 
 const size_t kConcurrentHashMapSize = 10000;
 const size_t kConcurrentHashMapPassCount = 36000;
-const char* kConcurrentHashMapBenchmarkName = "FollyConcurrentHashMap";
 
 const size_t kAtomicHashMapSize = 10000;
 const size_t kAtomicHashMapPassCount = 250000;
-const char* kAtomicHashMapBenchmarkName = "FollyAtomicHashMap";
 
 const size_t kAtomicUnorderedInsertMapSize = 10000;
 const size_t kAtomicUnorderedInsertMapPassCount = 500000;
-const char* kAtomicUnorderedInsertMapBenchmarkName =
-    "FollyAtomicUnorderedInsertMap";
 
 typedef folly::ConcurrentHashMap<size_t, size_t> ConcurrentHashMap;
 typedef folly::AtomicHashMap<size_t, size_t> AtomicHashMap;
@@ -48,11 +41,7 @@ bool map_contains(const Map* map, Key key) {
 }
 
 template <typename Map>
-void run_atomic_unordered_insert_map(size_t map_size, size_t pass_count,
-                                     const char* bench_name) {
-    std::cout << "[ RUN      ] " << kTestName << "." << bench_name << std::endl;
-    auto start_time = std::chrono::system_clock::now();
-
+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));
@@ -78,32 +67,15 @@ void run_atomic_unordered_insert_map(size_t map_size, size_t pass_count,
             {
                 if (map_contains(map.get(), i)) {
                     ++nFindSuccess;
-//                    std::cout << "Found" << i << "\n";
                 }
             }
         }
     }
-    auto finish_time = std::chrono::system_clock::now();
-    auto dur = finish_time - start_time;
-    auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
-
-    if (nFindSuccess != nInsertedNum) {
-        std::cout << "nFindSuccess=" << nFindSuccess << ", nInsertedNum="
-                  << nInsertedNum << std::endl;
-        std::cout << "[       FAILED ] " << kTestName << "." << bench_name
-                  << " (" << milisecs.count() << " ms)" << std::endl;
-        assert(false && "ConcurrentMap ERROR");
-    } else {
-        std::cout << "[       OK ] " << kTestName << "." << bench_name
-                  << " (" << milisecs.count() << " ms)" << std::endl;
-    }
+    EXPECT_EQ(nFindSuccess, nInsertedNum);
 }
 
 template <typename Map>
-void run_atomic_hashmap(size_t map_size, size_t pass_count, const char* bench_name) {
-    std::cout << "[ RUN      ] " << kTestName << "." << bench_name << std::endl;
-    auto start_time = std::chrono::system_clock::now();
-
+void run_atomic_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));
@@ -129,33 +101,15 @@ void run_atomic_hashmap(size_t map_size, size_t pass_count, const char* bench_na
             {
                 if (map_contains(map.get(), i)) {
                     ++nFindSuccess;
-//                    std::cout << "Found" << i << "\n";
                 }
             }
         }
     }
-    auto finish_time = std::chrono::system_clock::now();
-    auto dur = finish_time - start_time;
-    auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
-
-    if (nFindSuccess != nInsertedNum) {
-        std::cout << "nFindSuccess=" << nFindSuccess << ", nInsertedNum="
-                  << nInsertedNum << std::endl;
-        std::cout << "[       FAILED ] " << kTestName << "." << bench_name
-                  << " (" << milisecs.count() << " ms)" << std::endl;
-        assert(false && "ConcurrentMap ERROR");
-    } else {
-        std::cout << "[       OK ] " << kTestName << "." << bench_name
-                  << " (" << milisecs.count() << " ms)" << std::endl;
-    }
+    EXPECT_EQ(nFindSuccess, nInsertedNum);
 }
 
 template <typename Map>
-void run_concurrent_hashmap(size_t map_size, size_t pass_count,
-                            const char* bench_name) {
-    std::cout << "[ RUN      ] " << kTestName << "." << bench_name << std::endl;
-    auto start_time = std::chrono::system_clock::now();
-
+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));
@@ -167,53 +121,48 @@ void run_concurrent_hashmap(size_t map_size, size_t pass_count,
             if (i % s_nInsertPercentage == 1) {
                   auto iter = map->insert({i, n});
                   nInsertedNum++;
-//              std::cout << "Inserted" << i << "\n";
             }
             // Find
             {
                 if (map_contains(map.get(), i)) {
                     ++nFindSuccess;
-//                    std::cout << "Found" << i << "\n";
                 }
             }
             // Delete
             if (i % s_nInsertPercentage == 1) {
                 if (map_contains(map.get(), i)) {
                     map->erase(i);
-//                    std::cout << "Erased" << i << "\n";
                 }
             }
         }
     }
-    auto finish_time = std::chrono::system_clock::now();
-    auto dur = finish_time - start_time;
-    auto milisecs = std::chrono::duration_cast<std::chrono::milliseconds>(dur);
-
-    if (nFindSuccess != nInsertedNum) {
-        std::cout << "nFindSuccess=" << nFindSuccess << ", nInsertedNum="
-                  << nInsertedNum << std::endl;
-        std::cout << "[       FAILED ] " << kTestName << "." << bench_name
-                  << " (" << milisecs.count() << " ms)" << std::endl;
-        assert(false && "ConcurrentMap ERROR");
-    } else {
-        std::cout << "[       OK ] " << kTestName << "." << bench_name
-                  << " (" << milisecs.count() << " ms)" << std::endl;
-    }
+    EXPECT_EQ(nFindSuccess, nInsertedNum);
 }
 
-int main() {
+class FollyMapInsDelFindTest: public ::testing::Test {
+};
+
+TEST_F(FollyMapInsDelFindTest, FollyConcurrentHashMap) {
   run_concurrent_hashmap<ConcurrentHashMap>(
           kConcurrentHashMapSize,
-          kConcurrentHashMapPassCount,
-          kConcurrentHashMapBenchmarkName);
+          kConcurrentHashMapPassCount);
+}
+
+TEST_F(FollyMapInsDelFindTest, FollyAtomicHashMap) {
   run_atomic_hashmap<AtomicHashMap>(
           kAtomicHashMapSize,
-          kAtomicHashMapPassCount,
-          kAtomicHashMapBenchmarkName);
+          kAtomicHashMapPassCount);
+}
+
+TEST_F(FollyMapInsDelFindTest, FollyAtomicUnorderedInsertMap) {
   run_atomic_unordered_insert_map<AtomicUnorderedInsertMap>(
           kAtomicUnorderedInsertMapSize,
-          kAtomicUnorderedInsertMapPassCount,
-          kAtomicUnorderedInsertMapBenchmarkName);
+          kAtomicUnorderedInsertMapPassCount);
+}
 
-  return 0;
+int main(int argc, char** argv) {
+  // Init Google test
+  ::testing::InitGoogleTest(&argc, argv);
+  int result = RUN_ALL_TESTS();
+  return result;
 }