qttestlib-tutorial5-example.html 7.99 KB
Newer Older
xuebingbing's avatar
xuebingbing committed
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qttestlib-manual.qdoc -->
  <title>Chapter 5: Writing a Benchmark | Qt Test 5.11</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.11</a></td><td ><a href="qttest-index.html">Qt Test</a></td><td >Chapter 5: Writing a Benchmark</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.11.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qttestlib-tutorial4-example.html" />
  <link rel="next" href="qttestlib-tutorial6.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qttestlib-tutorial4-example.html">Chapter 4</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="qttestlib-tutorial6.html">Chapter 6</a>
</p><p/>
<div class="sidebar">
<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>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Chapter 5: Writing a Benchmark</h1>
<span class="subtitle"></span>
<!-- $$$tutorial5-brief -->
<p>How to write a benchmark.</p>
<!-- @@@tutorial5 -->
<!-- $$$tutorial5-description -->
<div class="descr"> <a name="details"></a>
<p>In this final chapter we will demonstrate how to write benchmarks using Qt Test.</p>
<a name="writing-a-benchmark"></a>
<h2 id="writing-a-benchmark">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="../qtcore/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="../qtcore/qstring.html">QString</a></span> str1 <span class="operator">=</span> QLatin1String(<span class="string">&quot;This is a test string&quot;</span>);
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> str2 <span class="operator">=</span> QLatin1String(<span class="string">&quot;This is a test string&quot;</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="qtest-overview.html#testlib-benchmarking-measurement">back-ends</a> are available and can be selected on the command line.</p>
<a name="data-functions"></a>
<h2 id="data-functions">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">&lt;</span>bool<span class="operator">&gt;</span>(<span class="string">&quot;useLocaleCompare&quot;</span>);
      <span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>newRow(<span class="string">&quot;locale aware compare&quot;</span>) <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="keyword">true</span>;
      <span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>newRow(<span class="string">&quot;standard compare&quot;</span>) <span class="operator">&lt;</span><span class="operator">&lt;</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(bool<span class="operator">,</span> useLocaleCompare);
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> str1 <span class="operator">=</span> QLatin1String(<span class="string">&quot;This is a test string&quot;</span>);
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> str2 <span class="operator">=</span> QLatin1String(<span class="string">&quot;This is a test string&quot;</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);
          }
      }
      Q_UNUSED(result);
  }

</pre>
<p>The &quot;if (useLocaleCompare)&quot; 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 id="external-tools">External Tools</h2>
<p>Tools for handling and visualizing test data are available as part of the <a href="https://code.qt.io/cgit/%7bnon-gerrit%7d/qt-labs/qtestlib-tools.git/">qtestlib-tools</a> project. 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://blog.qt.io/blog/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>
<p>Files:</p>
<ul>
<li><a href="qttestlib-tutorial5-benchmarking-cpp.html">tutorial5/benchmarking.cpp</a></li>
<li><a href="qttestlib-tutorial5-tutorial5-pro.html">tutorial5/tutorial5.pro</a></li>
</ul>
</div>
<!-- @@@tutorial5 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qttestlib-tutorial4-example.html">Chapter 4</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="qttestlib-tutorial6.html">Chapter 6</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2018 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    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.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>