1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qtestlib.qdoc -->
<title>Qt 4.8: Chapter 5: Writing a Benchmark</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Chapter 5: Writing a Benchmark</li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="qtestlib-tutorial4.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qtestlib-tutorial4.html">Chapter 4</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#writing-a-benchmark">Writing a Benchmark</a></li>
<li class="level1"><a href="#data-functions">Data Functions</a></li>
<li class="level1"><a href="#external-tools">External Tools</a></li>
</ul>
</div>
<h1 class="title">Chapter 5: Writing a Benchmark</h1>
<span class="subtitle"></span>
<!-- $$$qtestlib/tutorial5-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="qtestlib-tutorial5-benchmarking-cpp.html">qtestlib/tutorial5/benchmarking.cpp</a></li>
<li><a href="qtestlib-tutorial5-containers-cpp.html">qtestlib/tutorial5/containers.cpp</a></li>
<li><a href="qtestlib-tutorial5-tutorial5-pro.html">qtestlib/tutorial5/tutorial5.pro</a></li>
</ul>
<p>In this final chapter we will demonstrate how to write benchmarks using <a href="qtestlib-manual.html#qtestlib">QTestLib</a>.</p>
<a name="writing-a-benchmark"></a>
<h2>Writing a Benchmark</h2>
<p>To create a benchmark we extend a test function with a QBENCHMARK macro. A benchmark test function will then typically consist of setup code and a QBENCHMARK macro that contains the code to be measured. This test function benchmarks <a href="qstring.html#localeAwareCompare">QString::localeAwareCompare</a>().</p>
<pre class="cpp"> <span class="type">void</span> TestBenchmark<span class="operator">::</span>simple()
{
<span class="type"><a href="qstring.html">QString</a></span> str1 <span class="operator">=</span> QLatin1String(<span class="string">"This is a test string"</span>);
<span class="type"><a href="qstring.html">QString</a></span> str2 <span class="operator">=</span> QLatin1String(<span class="string">"This is a test string"</span>);
QCOMPARE(str1<span class="operator">.</span>localeAwareCompare(str2)<span class="operator">,</span> <span class="number">0</span>);
QBENCHMARK {
str1<span class="operator">.</span>localeAwareCompare(str2);
}
}</pre>
<p>Setup can be done at the beginning of the function, the clock is not running at this point. The code inside the QBENCHMARK macro will be measured, and possibly repeated several times in order to get an accurate measurement.</p>
<p>Several <a href="qtestlib-manual.html#testlib-benchmarking-measurement">back-ends</a> are available and can be selected on the command line.</p>
<a name="data-functions"></a>
<h2>Data Functions</h2>
<p>Data functions are useful for creating benchmarks that compare multiple data inputs, for example locale aware compare against standard compare.</p>
<pre class="cpp"> <span class="type">void</span> TestBenchmark<span class="operator">::</span>multiple_data()
{
<span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>addColumn<span class="operator"><</span><span class="type">bool</span><span class="operator">></span>(<span class="string">"useLocaleCompare"</span>);
<span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>newRow(<span class="string">"locale aware compare"</span>) <span class="operator"><</span><span class="operator"><</span> <span class="keyword">true</span>;
<span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>newRow(<span class="string">"standard compare"</span>) <span class="operator"><</span><span class="operator"><</span> <span class="keyword">false</span>;
}</pre>
<p>The test function then uses the data to determine what to benchmark.</p>
<pre class="cpp"> <span class="type">void</span> TestBenchmark<span class="operator">::</span>multiple()
{
QFETCH(<span class="type">bool</span><span class="operator">,</span> useLocaleCompare);
<span class="type"><a href="qstring.html">QString</a></span> str1 <span class="operator">=</span> QLatin1String(<span class="string">"This is a test string"</span>);
<span class="type"><a href="qstring.html">QString</a></span> str2 <span class="operator">=</span> QLatin1String(<span class="string">"This is a test string"</span>);
<span class="type">int</span> result;
<span class="keyword">if</span> (useLocaleCompare) {
QBENCHMARK {
result <span class="operator">=</span> str1<span class="operator">.</span>localeAwareCompare(str2);
}
} <span class="keyword">else</span> {
QBENCHMARK {
result <span class="operator">=</span> (str1 <span class="operator">=</span><span class="operator">=</span> str2);
}
}
}</pre>
<p>The "if(useLocaleCompare)" switch is placed outside the QBENCHMARK macro to avoid measuring its overhead. Each benchmark test function can have one active QBENCHMARK macro.</p>
<a name="external-tools"></a>
<h2>External Tools</h2>
<p>Tools for handling and visualizing test data are available as part of the <a href="http://qt.gitorious.org/qt-labs/qtestlib-tools">qtestlib-tools</a> project in the <a href="http://labs.qt.nokia.com">Qt Labs</a> web site. These include a tool for comparing performance data obtained from test runs and a utility to generate Web-based graphs of performance data.</p>
<p>See the <a href="http://labs.qt.nokia.com/blogs/2008/12/05/qtestlib-now-with-nice-graphs-pointing-upwards/">qtestlib-tools announcement</a> for more information on these tools and a simple graphing example.</p>
</div>
<!-- @@@qtestlib/tutorial5 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qtestlib-tutorial4.html">Chapter 4</a>
</p>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|