From b2483eaa1c48b4bb4ecae3539f57adad89535d03 Mon Sep 17 00:00:00 2001 From: Peizhao Ou Date: Fri, 9 Feb 2018 14:29:28 -0800 Subject: [PATCH] Refactors sequential¶llel test cases --- test/CMakeLists.txt | 4 +- test/junction_parallel_driver.cpp | 87 +---------- test/junction_sequential_driver.cpp | 215 +++++++++++----------------- test/main.cpp | 12 ++ test/test.h | 84 +++++++++++ 5 files changed, 184 insertions(+), 218 deletions(-) create mode 100644 test/main.cpp create mode 100644 test/test.h diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index adb76de..604bd03 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,8 +25,8 @@ set(JUNCTION_LIB gtest ) -add_executable(${SEQUENTIAL_DRIVER} junction_sequential_driver.cpp) +add_executable(${SEQUENTIAL_DRIVER} junction_sequential_driver.cpp main.cpp) target_link_libraries(${SEQUENTIAL_DRIVER} ${JUNCTION_LIB}) -add_executable(${PARALLEL_DRIVER} junction_parallel_driver.cpp) +add_executable(${PARALLEL_DRIVER} junction_parallel_driver.cpp main.cpp) target_link_libraries(${PARALLEL_DRIVER} ${JUNCTION_LIB}) diff --git a/test/junction_parallel_driver.cpp b/test/junction_parallel_driver.cpp index b6f74f6..dd1bda2 100644 --- a/test/junction_parallel_driver.cpp +++ b/test/junction_parallel_driver.cpp @@ -1,26 +1,9 @@ -#include -#include -#include -#include - -#include -#include - -#include -#include -#include -#include -#include +#include "test.h" namespace junction_test { class JunctionMapInsDelFindTest_Parallel : public cds_test::stress_fixture { protected: - typedef junction::ConcurrentMap_Grampa GrampaMap; - typedef junction::ConcurrentMap_Linear LinearMap; - typedef junction::ConcurrentMap_Leapfrog LeapfrogMap; - typedef junction::ConcurrentMap_Crude CrudeMap; - static unsigned s_nInsertPercentage; static unsigned s_nDeletePercentage; static size_t s_nGCFrequency; // Run GC after "s_nGCFrequency" operations. @@ -71,64 +54,6 @@ protected: GetConfigNonZeroExpected(LeapfrogPassCount, 850000000); } - template - static bool map_insert(Map* map, Key key, Value value) { - auto iter = map->insertOrFind(key); - if (!iter.getValue() || iter.getValue() != value) { - // Insert/update the pair - iter.assignValue(value); - return true; - } else { - return false; - } - } - - template - static bool map_delete(Map* map, Key key) { - auto iter = map->find(key); - if (iter.getValue()) { - iter.eraseValue(); - return true; - } else { - return false; - } - } - - template - static bool map_find(Map* map, Key key) { - auto iter = map->find(key); - if (iter.getValue()) { - return true; - } else { - return false; - } - } - - // Specialization for CrudeMap - template - static bool map_insert(CrudeMap* map, Key key, Value value) { - auto old_val = map->get(key); - if (!old_val || old_val != value) { - map->assign(key, value); - return true; - } else { - return false; - } - } - - template static bool map_delete(CrudeMap* map, Key key) { - if (!map->get(key)) { - map->assign(key, ((Key)0)); - return true; - } else { - return false; - } - } - - template static bool map_find(CrudeMap* map, Key key) { - return map->get(key) != ((Key)0); - } - template static void run_test(Map* map, size_t pass_count) { auto qsbrContext = junction::DefaultQSBR.createContext(); @@ -221,13 +146,3 @@ TEST_F(JunctionMapInsDelFindTest_Parallel, JunctionMapGrampa) { } } // namespace junction_test - -int main(int argc, char** argv) { - // Read test config file - cds_test::init_config(argc, argv); - - // Init Google test - ::testing::InitGoogleTest(&argc, argv); - int result = RUN_ALL_TESTS(); - return result; -} diff --git a/test/junction_sequential_driver.cpp b/test/junction_sequential_driver.cpp index f7779e4..4b081c0 100644 --- a/test/junction_sequential_driver.cpp +++ b/test/junction_sequential_driver.cpp @@ -1,156 +1,111 @@ -#include -#include -#include -#include - -#include - -#include +#include "test.h" namespace junction_test { -class JunctionMapInsDelFindTest : public ::testing::Test { +class JunctionMapInsDelFindTest_Sequential : public cds_test::stress_fixture { protected: - typedef junction::ConcurrentMap_Grampa GrampaMap; - typedef junction::ConcurrentMap_Linear LinearMap; - typedef junction::ConcurrentMap_Leapfrog LeapfrogMap; - typedef junction::ConcurrentMap_Crude CrudeMap; - - static const unsigned s_nInsertPercentage = 10; - // Run GC after "kGCFrequency" operations. - static const size_t kGCFrequency = 3000; - static const size_t kLeapfrogGCFrequency = 1500; - - static const size_t kCrudeMapSize = 10000; - static const size_t kCrudePassCount = 400000; - - static const size_t kGrampaMapSize = 20000; - static const size_t kGrampaPassCount = 60000; - - static const size_t kLinearMapSize = 20000; - static const size_t kLinearPassCount = 70000; - - static const size_t kLeapfrogMapSize = 20000; - static const size_t kLeapfrogPassCount = 75000; - - static void SetUpTestCase() {} - - template - static void run_test(size_t map_size, size_t pass_count, - size_t gc_frequency) { - size_t nInsertedNum = 0; - size_t nFindSuccess = 0; - size_t nOperations = 0; - std::unique_ptr map(new Map()); - auto qsbrContext = junction::DefaultQSBR.createContext(); - 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->insertOrFind(i); - if (!iter.getValue()) { - iter.assignValue(n); - nInsertedNum++; - // std::cout << "Inserted" << i << "\n"; - } - } - // Find - { - auto iter = map->find(i); - if (iter.getValue()) { - ++nFindSuccess; - // std::cout << "Found" << i << "\n"; - } - } - // Delete - if (i % s_nInsertPercentage == 1) { - auto iter = map->find(i); - if (iter.getValue()) { - iter.eraseValue(); - // std::cout << "Erased" << i << "\n"; - } - } - if (++nOperations > gc_frequency) { - junction::DefaultQSBR.update(qsbrContext); - nOperations = 0; - } - } - } - junction::DefaultQSBR.update(qsbrContext); - junction::DefaultQSBR.destroyContext(qsbrContext); - EXPECT_EQ(nFindSuccess, nInsertedNum); + static unsigned s_nInsertDeletePercentage; + static size_t s_nGCFrequency; // Run GC after "s_nGCFrequency" operations. + static size_t s_nMapKeyRange; + static size_t s_nCrudeMapCapacity; + + enum actions { do_find, do_insert, do_delete }; + static const unsigned int kShuffleSize = 100; + static actions s_arrShuffle[kShuffleSize]; + + static size_t s_nCrudePassCount; + static size_t s_nGrampaPassCount; + static size_t s_nLinearPassCount; + static size_t s_nLeapfrogPassCount; + + static void SetUpTestCase() { + const cds_test::config& cfg = get_config("SequentialJunction"); + GetConfigNonZeroExpected(InsertDeletePercentage, 5); + GetConfigNonZeroExpected(MapKeyRange, 20000); + GetConfigNonZeroExpected(CrudeMapCapacity, s_nMapKeyRange * 64); + GetConfigNonZeroExpected(GCFrequency, 1500); + GetConfigNonZeroExpected(CrudePassCount, 1500000000); + GetConfigNonZeroExpected(GrampaPassCount, 650000000); + GetConfigNonZeroExpected(LinearPassCount, 900000000); + GetConfigNonZeroExpected(LeapfrogPassCount, 850000000); } - template - void run_crude_map(size_t map_size, size_t pass_count, size_t gc_frequency) { + template static void run_test(Map* map, size_t pass_count) { + auto qsbrContext = junction::DefaultQSBR.createContext(); size_t nInsertedNum = 0; + size_t nNotInsertedNum = 0; size_t nFindSuccess = 0; + size_t nFindFailed = 0; + size_t nDeletedNum = 0; size_t nOperations = 0; - // Seems like the crude map won't resize, so better have a large enough - // capacity. - std::unique_ptr map(new Map(map_size * 32)); - auto qsbrContext = junction::DefaultQSBR.createContext(); + + // The number to operate on the map. + size_t n = s_nMapKeyRange; 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) { - map->assign(i, n); - nInsertedNum++; - // std::cout << "Inserted" << i << "\n"; - } - // Find - { - if (map->get(i)) { - ++nFindSuccess; - // std::cout << "Found" << i << "\n"; - } - } - // Delete - if (i % s_nInsertPercentage == 1) { - if (map->get(i)) { - map->assign(n, 0); - // std::cout << "Erased" << i << "\n"; - } - } - if (++nOperations > gc_frequency) { - junction::DefaultQSBR.update(qsbrContext); - nOperations = 0; - } + // Insert + unsigned mod = count % kShuffleSize; + if (mod < s_nInsertDeletePercentage && map_insert(map, n, n)) { + nInsertedNum++; + } else { + nNotInsertedNum++; + } + // Find + if (map_find(map, n)) { + ++nFindSuccess; + } else { + ++nFindFailed; + } + // Delete + if (mod < s_nInsertDeletePercentage && map_delete(map, n)) { + nDeletedNum++; + } + if (++nOperations > s_nGCFrequency) { + junction::DefaultQSBR.update(qsbrContext); + nOperations = 0; + } + if (++n == 2 * s_nMapKeyRange) { + n = s_nMapKeyRange; } } junction::DefaultQSBR.update(qsbrContext); junction::DefaultQSBR.destroyContext(qsbrContext); - - EXPECT_EQ(nFindSuccess, nInsertedNum); + EXPECT_EQ(nInsertedNum, nDeletedNum); + EXPECT_EQ(nInsertedNum, nFindSuccess); + EXPECT_EQ(nFindFailed, nNotInsertedNum); } }; -TEST_F(JunctionMapInsDelFindTest, JunctionMapCrude) { - run_crude_map(kCrudeMapSize, kCrudePassCount, kGCFrequency); +size_t JunctionMapInsDelFindTest_Sequential::s_nMapKeyRange; +size_t JunctionMapInsDelFindTest_Sequential::s_nCrudeMapCapacity; +size_t JunctionMapInsDelFindTest_Sequential::s_nGCFrequency; +unsigned JunctionMapInsDelFindTest_Sequential::s_nInsertDeletePercentage; +const unsigned int JunctionMapInsDelFindTest_Sequential::kShuffleSize; +JunctionMapInsDelFindTest_Sequential::actions + JunctionMapInsDelFindTest_Sequential::s_arrShuffle + [JunctionMapInsDelFindTest_Sequential::kShuffleSize]; +size_t JunctionMapInsDelFindTest_Sequential::s_nCrudePassCount; +size_t JunctionMapInsDelFindTest_Sequential::s_nGrampaPassCount; +size_t JunctionMapInsDelFindTest_Sequential::s_nLinearPassCount; +size_t JunctionMapInsDelFindTest_Sequential::s_nLeapfrogPassCount; + +TEST_F(JunctionMapInsDelFindTest_Sequential, JunctionMapCrude) { + std::unique_ptr map(new CrudeMap(s_nCrudeMapCapacity)); + run_test(map.get(), s_nCrudePassCount); } -TEST_F(JunctionMapInsDelFindTest, JunctionMapLeapfrog) { - run_test(kLeapfrogMapSize, kLeapfrogPassCount, - kLeapfrogGCFrequency); +TEST_F(JunctionMapInsDelFindTest_Sequential, JunctionMapLeapfrog) { + std::unique_ptr map(new LeapfrogMap()); + run_test(map.get(), s_nLeapfrogPassCount); } -TEST_F(JunctionMapInsDelFindTest, JunctionMapLinear) { - run_test(kLinearMapSize, kLinearPassCount, kGCFrequency); +TEST_F(JunctionMapInsDelFindTest_Sequential, JunctionMapLinear) { + std::unique_ptr map(new LinearMap()); + run_test(map.get(), s_nLinearPassCount); } -TEST_F(JunctionMapInsDelFindTest, JunctionMapGrampa) { - run_test(kGrampaMapSize, kGrampaPassCount, kGCFrequency); +TEST_F(JunctionMapInsDelFindTest_Sequential, JunctionMapGrampa) { + std::unique_ptr map(new GrampaMap()); + run_test(map.get(), s_nGrampaPassCount); } } // namespace junction_test - -int main(int argc, char** argv) { - // Init Google test - ::testing::InitGoogleTest(&argc, argv); - int result = RUN_ALL_TESTS(); - return 0; -} diff --git a/test/main.cpp b/test/main.cpp new file mode 100644 index 0000000..9469c7b --- /dev/null +++ b/test/main.cpp @@ -0,0 +1,12 @@ +#include "test.h" +#include + +int main(int argc, char** argv) { + // Read test config file + cds_test::init_config(argc, argv); + + // Init Google test + ::testing::InitGoogleTest(&argc, argv); + int result = RUN_ALL_TESTS(); + return result; +} diff --git a/test/test.h b/test/test.h new file mode 100644 index 0000000..1b54d13 --- /dev/null +++ b/test/test.h @@ -0,0 +1,84 @@ +#ifndef _JUNCTION_TEST_H +#define _JUNCTION_TEST_H + +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include + +namespace junction_test { + +typedef junction::ConcurrentMap_Grampa GrampaMap; +typedef junction::ConcurrentMap_Linear LinearMap; +typedef junction::ConcurrentMap_Leapfrog LeapfrogMap; +typedef junction::ConcurrentMap_Crude CrudeMap; + +template +static bool map_insert(Map* map, Key key, Value value) { + auto iter = map->insertOrFind(key); + if (!iter.getValue() || iter.getValue() != value) { + // Insert/update the pair + iter.assignValue(value); + return true; + } else { + return false; + } +} + +template +static bool map_delete(Map* map, Key key) { + auto iter = map->find(key); + if (iter.getValue()) { + iter.eraseValue(); + return true; + } else { + return false; + } +} + +template static bool map_find(Map* map, Key key) { + auto iter = map->find(key); + if (iter.getValue()) { + return true; + } else { + return false; + } +} + +// Specialization for CrudeMap +template +static bool map_insert(CrudeMap* map, Key key, Value value) { + auto old_val = map->get(key); + if (!old_val || old_val != value) { + map->assign(key, value); + return true; + } else { + return false; + } +} + +template static bool map_delete(CrudeMap* map, Key key) { + if (map->get(key)) { + map->assign(key, ((Key)0)); + return true; + } else { + return false; + } +} + +template static bool map_find(CrudeMap* map, Key key) { + return map->get(key) != ((Key)0); +} + +} // namespace junction_test + +#endif -- 2.34.1