2011-09-01 69 views
1

首先,我將介紹一下我的結果,並且想知道如果您認爲我的解決方案有風險,不好和不合格。我們有一個正常的std地圖。我們希望爲多線程讀/寫操作安全。並測試其速度。隨着助推器,我們有2個選項使用 - mutable boost::mutexboost::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"); 

這裏是我tester類和main應用示例。

所以這就是我爲了快速和骯髒的類型測試而出現的。你認爲什麼 - 是否有可能創建一個小型測試套件,並在測試代碼中添加外部類型,以便編譯它們?

回答

2

如果您試圖測量兩種不同實現的性能,您應該使用模擬壓力下該類的預期行爲的測試。這可能涉及相當多的操作比put,contains,get; get; get;,remove,並且可能在循環中運行一段時間。此外,我不太明白爲什麼lexical_cast或測試執行鎖定的原因是地圖中未處理的鎖嗎?

請注意,向容器添加鎖定不會使容器線程安全,您需要調整接口,並且以異常安全的方式執行此操作並非微不足道。您不會顯示類的接口,並且用例也不會顯示它們(get返回的類型是什麼?),但您應該知道,容器中的任何操作都不應該在容器中產生引用或指針,因爲這會破壞線程安全。

+0

正如你可以看到的例子[這裏](http://code.google.com/p/cloudobserver/source/browse/branches/v0.5/ConcurrentMaps/src/general_map_data_structure.h?r=1497)我傾向於測試不是一個真正的std地圖,而是一個帶鎖的提升。所以在我正在測試的類中處理鎖。那個類是我通過帶鎖的地圖界面。我將它與另一個與鎖類相提並論。 – Rella

+0

@Kiss:一些評論:有趣的是,您選擇使用'mutable'關鍵字作爲互斥體,但沒有將任何方法標記爲'const'。我會比'map_t_1'使用更明智的名字。你的'get'方法正在修改map(添加之前不存在的鍵),在'put'方法中返回* key *沒有意義,這可能涉及內存分配(認爲是'std :: string') ,你的方法都沒有通過引用(也可能是不需要的副本)來引用'key' /'value','containsKey'不是*那*有用(不保證稍後的'get'將檢索任何東西)...... –

+0

格雷特!)我會努力=) – Rella