Qt文档阅读笔记-单元测试中构建标准检测程序(CPU、Linux性能、内存泄漏等)

这里需要介绍一个宏,主要就是靠使用这个宏完成功能:

QBENCHMARK{
	//TODO
}

在单元测试中,这个宏在那个函数中,那么这个函数就会进行多次测试,如果不需要多次调用可以用下面的这宏代替:

QBENCHMARK_ONECE{
	//TODO
}

通过多次测试,得到其性能,下面给出一张表,在单元测试的时候可以添加如下的参数:

Qt文档阅读笔记-单元测试中构建标准检测程序(CPU、Linux性能、内存泄漏等)_第1张图片

程序运行截图如下:

默认运行:

-tickcounter参数运行:

Qt文档阅读笔记-单元测试中构建标准检测程序(CPU、Linux性能、内存泄漏等)_第2张图片

源码如下:

test5.pro

QT += widgets testlib
SOURCES = benchmarking.cpp
INSTALLS += target

benchmark.cpp

#include 
#include 
#include 

class TestBenchmark : public QObject{

	Q_OBJECT

private slots:
	void simple();
	void multiple_data();
	void multiple();
	void series_data();
	void series();
};


void TestBenchmark::simple(){

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

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

	QBENCHMARK{
		
		str1.localeAwareCompare(str2);
	}
}

void TestBenchmark::multiple_data(){

	QTest::addColumn("useLocaleCompare");
	QTest::newRow("locale aware compare") << true;
	QTest::newRow("standard compare") << false;
}

void TestBenchmark::multiple(){

	QFETCH(bool, useLocaleCompare);
	QString str1 = QString("This is a test string");
	QString str2 = QString("This is a test string");

	int result;
	if(useLocaleCompare){

		QBENCHMARK{

			result += str1.localeAwareCompare(str2);
		}
	}
	else{
		
		QBENCHMARK{

			result = (str1 == str2);
		}
	}

	Q_UNUSED(result);
}

void TestBenchmark::series_data(){

	QTest::addColumn("useLocaleCompare");
	QTest::addColumn("stringSize");

	for(int i = 1; i < 10000; i += 2000){

		QByteArray size = QByteArray::number(i);
		QTest::newRow(("locale aware compare -- " + size).constData()) << true << i;
		QTest::newRow(("standard compare -- " + size).constData()) << false << i;
	}
}

void TestBenchmark::series(){

	QFETCH(bool, useLocaleCompare);
	QFETCH(int, stringSize);

	QString str1 = QString().fill('A', stringSize);
	QString str2 = QString().fill('A', stringSize);
	int result;

	if(useLocaleCompare){

		QBENCHMARK{
		
			result = str1.localeAwareCompare(str2);
		}
	}
	else{

		QBENCHMARK{

			result = (str1 == str2);
		}
	}
	Q_UNUSED(result);
}

QTEST_MAIN(TestBenchmark)
#include "benchmarking.moc"

 

 

你可能感兴趣的:(C/C++,Qt,文档阅读笔记,C,C++,Qt)