#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;
}
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));
{
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));
{
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));
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;
}