文件IO操作开发笔记(二):使用Cpp的ofstream对磁盘文件存储进行性能测试以及测试工具
前言
在做到个别项目对日志要求较高,要求并行写入的数据较多,尽管写入数据的线程放在子线程,仍然会造成界面程序的假死(实际上Qt还是在跑,只是磁盘消耗超过瓶颈,造成假死(注意:控制台还能看到打印输出,linux则能看到打印输出)。
本篇升级了测试工具,并且测试了ofstream在USB3.0和M.2SSD上的写入性能。
更新版本版本,新增了c++的ofstream写入方式。
请自行溯源搜索,发不出
所以,线程越开越多,在某一个阈值线程数(实际打开操作的文件数)会导致性能大幅下降,而且会持续有多个阈值类似的。
结论:这个明显受到硬盘数据传输的影响。
void FileIoTestManager::slot_optFileUseCppOfstream(int loopTime, int loopWrite, int dataSize, bool flush)
{
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("==========================测试开始=============================="));
double totalTime = 0; // 总计时间
double fileTotalTime = 0; // 操作单个文件总时间
double writeFileTime = 0; // 单个文件单词写入时间
totalTime = QDateTime::currentDateTime().toMSecsSinceEpoch() * 1.0f;
for(int loopIndex = 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(int writeIndex = 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;
emit signal_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("==========================测试结束=============================="));
emit signal_finished();
return;
}
会持续补充测试其他方式,ofstream本次测试比QFile的性能还差一些。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-01-10 地图开发笔记(一):百度地图介绍、使用和Qt内嵌地图Demo