Qt 5.9.1 参考手册 QtTest 第5章 写一个基准线

Qt 5.9.1 Reference Documentation

Chapter 5: Writing a Benchmark 

In this final chapter we will demonstrate how to write benchmarks using Qt Test. 

在这个最后的章节,我们将展示怎样用Qt测试框架写一个基准线。

Writing a Benchmark
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 QString::localeAwareCompare().
要创建一个基准线,我们需要用QBENCHMARK宏来扩展我们的的测试函数。一个基准线测试函数将会典型地由建立代码和一个包含被测试代码的宏组成。此处,我们的基准线测试函数用来测试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);
      }
  }


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.

在函数的开始部分就完成了初始建立的工作,此时时钟并未开始运行。在QBENCHMARK宏中的代码将会被测试,而且基本上会被执行多次来确保测量的准确性。

Several back-ends are available and can be selected on the command line. 

多个后端可以通过命令行来进行选择。

Data Functions
Data functions are useful for creating benchmarks that compare multiple data inputs, for example locale aware compare against standard compare.
数据函数对创建比较多种数据输入的基准线测试很有用处,例如本地比较和标准比较。

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


The test function then uses the data to determine what to 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);
  }


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. 

代码中的if开关被放置在QBENCHMARK宏之外来避免测量开销。每个基准测试函数可以有一个激活的QBENCHMARK宏。

External Tools

Tools for handling and visualizing test data are available as part of the qtestlib-tools project. These include a tool for comparing performance data obtained from test runs and a utility to generate Web-based graphs of performance data.

处理和可视化测试数据的工具可以在qtestlib-tools工程中找到。这些工具包含比较测试运行后性能数据的比较功能和基于web的图表展示功能。

See the qtestlib-tools announcement for more information on these tools and a simple graphing example.

参考qtestlib-tools的发布声明来获取更多的信息和相关的简单图表示例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值