首先,我將介紹一下我的結果,並且想知道如果您認爲我的解決方案有風險,不好和不合格。我們有一個正常的std地圖。我們希望爲多線程讀/寫操作安全。並測試其速度。隨着助推器,我們有2個選項使用 - mutable boost::mutex
或boost::shared_mutex
。所以這就是我想起...C++:如何爲模板類創建小型速度測試?
我創建了一個test interface data structure,map with scoped_locks,map with shared_locks。現在來了intresting部分 - 如何測試thaima與minimall代碼量?
我創建了一個測試案例:
template< typename t1, typename t2>
void test(t1 k1, t2 v1)
{
map_t_1 k = boost::lexical_cast<map_t_1>(k1);
map_t_2 v = boost::lexical_cast<map_t_2>(v1);
Ds.put(k, v);
if (Ds.containsKey(k))
{
Ds.get(k);
Ds.get(k);
Ds.get(k);
}
Ds.remove(k);
}
和測試:(這是最incompedent部分恕我直言)
void test_int(int i)
{
boost::shared_lock<boost::shared_mutex> lock_r(results);
boost::shared_lock<boost::shared_mutex> lock(tests);
test<int, int>(i, i);
}
void test_string(std::string s)
{
s += "abcdefghigklmnop";
std::string r = "abcdefghigklmnop" + s;
boost::shared_lock<boost::shared_mutex> lock_r(results);
boost::shared_lock<boost::shared_mutex> lock(tests);
test<std::string, std::string>(s, r);
}
,我讓程序員創建一個TET到chouse使用字符串或INT測試:
//code inside tester class
void submit_test(int test_number)
{
if (test_type == "int")
{
io_service.post(boost::bind(&test_map_wraper_pooled<map_wraper_t, map_t_1, map_t_2>::test_int, this, test_number));
}
else if (test_type == "string")
{
io_service.post(boost::bind(&test_map_wraper_pooled<map_wraper_t, map_t_1, map_t_2>::test_string, this, boost::lexical_cast<std::string>(test_number)));
}
}
// code programer can write to test my map on ints
test_map_wraper_pooled<general_map_data_structure<int, int>, int, int > GeneralMapTest(tasks_n);
GeneralMapTest.start_tests("int");
//or on strings
test_map_wraper_pooled<general_map_data_structure<std::string, std::string>, std::string, std::string > GeneralMapTest(tasks_n);
GeneralMapTest.start_tests("string");
所以這就是我爲了快速和骯髒的類型測試而出現的。你認爲什麼 - 是否有可能創建一個小型測試套件,並在測試代碼中添加外部類型,以便編譯它們?
正如你可以看到的例子[這裏](http://code.google.com/p/cloudobserver/source/browse/branches/v0.5/ConcurrentMaps/src/general_map_data_structure.h?r=1497)我傾向於測試不是一個真正的std地圖,而是一個帶鎖的提升。所以在我正在測試的類中處理鎖。那個類是我通過帶鎖的地圖界面。我將它與另一個與鎖類相提並論。 – Rella
@Kiss:一些評論:有趣的是,您選擇使用'mutable'關鍵字作爲互斥體,但沒有將任何方法標記爲'const'。我會比'map_t_1'使用更明智的名字。你的'get'方法正在修改map(添加之前不存在的鍵),在'put'方法中返回* key *沒有意義,這可能涉及內存分配(認爲是'std :: string') ,你的方法都沒有通過引用(也可能是不需要的副本)來引用'key' /'value','containsKey'不是*那*有用(不保證稍後的'get'將檢索任何東西)...... –
格雷特!)我會努力=) – Rella