16 Star 104 Fork 32

pcd / QPerf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
providetest.cpp 2.05 KB
一键复制 编辑 原始数据 按行查看 历史
pcd 提交于 2021-04-14 02:17 . :recycle:change c++11 feature.
#include "providetest.h"
#include <QTest>
#include <QThreadPool>
/**
* @brief The ReadRunner class
* 读取线程的对象。
*/
class ReadRunner: public QRunnable{
public:
ReadRunner(int total, ProvideTest *owner);
// QRunnable interface
public:
void run();
private:
ProvideTest *m_owner;
int m_total;
};
/**
* @brief The WriteRunner class
* 写入的线程对象。
*/
class WriteRunner: public QRunnable{
public:
WriteRunner(int total, ProvideTest *owner);
// QRunnable interface
public:
void run();
private:
ProvideTest *m_owner;
int m_total;
};
ProvideTest::ProvideTest()
{
}
void ProvideTest::run()
{
int total = 10000000; // 测试数量,越多越容易重现。
m_list.reserve(total);
m_list2.reserve(total);
ReadRunner *read = new ReadRunner(total, this);
QThreadPool pool;
pool.start(read);
WriteRunner *write = new WriteRunner(total, this);
pool.start(write);
}
void ProvideTest::runTest(QVector<int> &src)
{
int size = src.size();
if(size < 0)
{
QString msg = QString("QVector failed size:%1;cur size:%2").arg(size).arg(src.size());
QFAIL(msg.toUtf8().data());
}
}
void ProvideTest::runTest(QList<int> &src)
{
int size = src.size();
if(size < 0)
{
QString msg = QString("QList failed size:%1;cur size:%2").arg(size).arg(src.size());
QFAIL(msg.toUtf8().data());
}
}
void ReadRunner::run()
{
int count = m_total;
auto fun1 = [=](QVector<int> &item){this->m_owner->runTest(item);};
auto fun2 = [=](QList<int> &item){this->m_owner->runTest(item);};
for(int i = 0; i < count; i++){
fun1(m_owner->m_list);
fun2(m_owner->m_list2);
}
}
void WriteRunner::run()
{
int count = m_total;
for(int i = 0; i < count; i++){
m_owner->m_list.append(i);
m_owner->m_list2.append(i);
}
}
ReadRunner::ReadRunner(int total, ProvideTest *owner)
{
m_total = total;
m_owner = owner;
}
WriteRunner::WriteRunner(int total, ProvideTest *owner)
{
m_total = total;
m_owner = owner;
}
C++
1
https://gitee.com/andwp/qperf.git
git@gitee.com:andwp/qperf.git
andwp
qperf
QPerf
master

搜索帮助