前言
在做到个别项目对日志要求较高,要求并行写入的数据较多,尽管写入数据的线程放在子线程,仍然会造成界面程序的假死(实际上Qt还是在跑,只是磁盘消耗超过瓶颈,造成假死(注意:控制台还能看到打印输出,linux则能看到打印输出)。
本篇升级了测试工具,并且测试了ofstream在USB3.0和M.2SSD上的写入性能。
更新版本版本,新增了c++的ofstream写入方式。
![](https://img.haomeiwen.com/i22295287/1c63888d1754d841.png)
请自行溯源搜索,发不出
![](https://img.haomeiwen.com/i22295287/cc426b847d806082.png)
![](https://img.haomeiwen.com/i22295287/2ffee84ff15a1f20.png)
所以,线程越开越多,在某一个阈值线程数(实际打开操作的文件数)会导致性能大幅下降,而且会持续有多个阈值类似的。
![](https://img.haomeiwen.com/i22295287/4ec08733333c8572.png)
![](https://img.haomeiwen.com/i22295287/ef03aa712212f6d2.png)
![](https://img.haomeiwen.com/i22295287/e570f4a3f143f1e5.png)
![](https://img.haomeiwen.com/i22295287/a0bbece1212f1cac.png)
![](https://img.haomeiwen.com/i22295287/670da9ac1e7201a7.png)
![](https://img.haomeiwen.com/i22295287/c703619c60f3c68c.png)
结论:这个明显受到硬盘数据传输的影响。
voidFileIoTestManager::slot_optFileUseCppOfstream(intloopTime,intloopWrite,intdataSize,boolflush){QDir dir;QString dirPath=QString("%1/%2").arg(QApplication::applicationDirPath()).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh_mm_ss_zzz"));if(dir.mkpath(dirPath)){message(QString("创建文件夹成功: %1").arg(dirPath));}else{message(QString("创建文件夹失败: %1").arg(dirPath));}// 生成数据message(QString("生成测试数据,数据长度: %1").arg(dataSize));QByteArray byteArray;byteArray.append(dataSize,0xFF);message(QString("==========================测试开始=============================="));doubletotalTime=0;// 总计时间doublefileTotalTime=0;// 操作单个文件总时间doublewriteFileTime=0;// 单个文件单词写入时间totalTime=QDateTime::currentDateTime().toMSecsSinceEpoch()*1.0f;for(intloopIndex=0;loopIndex<loopTime;loopIndex++){QString filePath=QString("%1/%2_%3").arg(dirPath).arg(QDateTime::currentDateTime().toString("hh_mm_ss_zzz")).arg(loopIndex,6,10,QChar('0'));std::ofstream outFile;outFile.open(filePath.toUtf8().constData());writeFileTime=QDateTime::currentDateTime().toMSecsSinceEpoch();for(intwriteIndex=0;writeIndex<loopWrite;writeIndex++){// message(QString(" 第%1次写入文件,写入长度%2字节").arg(writeIndex + 1).arg(dataSize));outFile<<byteArray.constData();if(flush){outFile.flush();}if(_stop){outFile.close();message(QString("==========================测试手动停止==========================="));_stop=false;emitsignal_finished();return;}}writeFileTime=QDateTime::currentDateTime().toMSecsSinceEpoch()-writeFileTime;writeFileTime=writeFileTime/loopWrite;message(QString("每次写入数据平均耗时(不包含打开关闭文件): %1ms").arg(writeFileTime));// message(QString(" 第%1次关闭文件").arg(loopIndex + 1));outFile.close();}message(QString("==========================测试结果=============================="));totalTime=QDateTime::currentDateTime().toMSecsSinceEpoch()-totalTime;fileTotalTime=totalTime*1.0f/loopTime;message(QString("操作创建文件次数: %1, 单个文件循环写入次数: %2, 每次写入固定数据长度: %3, %4").arg(loopTime).arg(loopWrite).arg(dataSize).arg(flush?"每次使用flush":"不使用flush"));message(QString("总耗时: %1ms").arg(totalTime));message(QString("单个文件循环写入平均总耗时(包括打开关闭文件): %1ms").arg(fileTotalTime));message(QString("每次写入数据平均耗时(包括打开关闭文件: %1ms").arg(fileTotalTime*1.0f/loopWrite));message(QString("==========================测试结束=============================="));emitsignal_finished();return;}
![](https://img.haomeiwen.com/i22295287/380d5a7f51ea0284.png)
会持续补充测试其他方式,ofstream本次测试比QFile的性能还差一些。
网友评论