close

http://doc.qt.io/qt-5/qttestlib-tutorial5-example.html

如何寫benchmark

 

在最後一章中,我們將演示如何使用Qt Test編寫基準測試 (benchmark)

 

Writing a Benchmark

為了創建benchmark,我們使用QBENCHMARK macro擴展test function 然後,benchmark test function通常包含設置代碼(setup code)和包含要測量的codeQBENCHMARK macro test function設基準QString :: localeAwareCompare()。

 

void TestBenchmark::simple()

{

    QString str1 = QLatin1String("This is a test string");

    QString str2 = QLatin1String("This is a test string");

 

    QCOMPARE(str1.localeAwareCompare(str2), 0);

 

    QBENCHMARK {

        str1.localeAwareCompare(str2);

    }

}

 

可以在function開始時進行setup,此時時鐘不運行。 QBENCHMARK macro內的代碼將會被測量(measured),並可能重複幾次以獲得準確的測量結果。

 

有幾個後端(back-ends)可用,可以在command line中選擇。

 

 

Data Functions

Data function可用於創建比較多個數據輸入(data inputs)benchmarks,例如locale aware comparestandard compare

void TestBenchmark::multiple_data()
{
    QTest::addColumn<bool>("useLocaleCompare");
    QTest::newRow("locale aware compare") << true;
    QTest::newRow("standard compare") << false;
}

然後,test function使用data來決定要進行benchmark的內容。

void TestBenchmark::multiple()
{
    QFETCH(bool, useLocaleCompare);
    QString str1 = QLatin1String("This is a test string");
    QString str2 = QLatin1String("This is a test string");
 
    int result;
    if (useLocaleCompare) {
        QBENCHMARK {
            result = str1.localeAwareCompare(str2);
        }
    } else {
        QBENCHMARK {
            result = (str1 == str2);
        }
    }
    Q_UNUSED(result);
}

ifuseLocaleCompare)”開關位於QBENCHMARK macro之外,以避免測量其開銷(overhead) 每個benchmark test function都可以有一個activeQBENCHMARK macro

 

 

External Tools

qtestlib-tools project的一部分提供了用於處理和可視化測試數據(visualizing test data)的工具。 這些工具包括用於比較從測試運行(test runs) 中獲得的性能數據(performance data) 和用於生成性能數據的基於Web的圖表的實用程序的工具。

 

有關這些工具的更多信息和簡單的圖形示例,請參閱qtestlib-tools公告。

 

arrow
arrow
    文章標籤
    QT
    全站熱搜

    lynn770707 發表在 痞客邦 留言(0) 人氣()