文为原创文章,转载请注明原文出处
 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/128626548
红胖子(红模仿)的博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ffmpeg、OSG、单片机、软硬结合等等)持续更新中…(点击传送门)
其他(编程相关)
上一篇:《文件IO操作开发笔记(一):使用Qt的QFile对磁盘文件存储进行性能测试以及测试工具》
 下一篇:敬请期待…
前言
  在做到个别项目对日志要求较高,要求并行写入的数据较多,尽管写入数据的线程放在子线程,仍然会造成界面程序的假死(实际上Qt还是在跑,只是磁盘消耗超过瓶颈,造成假死(注意:控制台还能看到打印输出,linux则能看到打印输出)。
   本篇升级了测试工具,并且测试了ofstream在USB3.0和M.2SSD上的写入性能。
版本v1.1.0
  更新版本版本,新增了c++的ofstream写入方式。
   
测试工具v1.1.0下载地址
CSDN粉丝0积分下载地址:https://download.csdn.net/download/qq21497936/87376952
使用C++的ofstream测试结果
USB3.0移动硬盘测试结果
   
     
所以,线程越开越多,在某一个阈值线程数(实际打开操作的文件数)会导致性能大幅下降,而且会持续有多个阈值类似的。
M.2主板上SSD测试结果
    
    
使用C++的ofstream(用flush)测试结果
USB3.0移动硬盘测试结果
    
     
M.2主板上SSD测试结果
  
   
   结论:这个明显受到硬盘数据传输的影响。
关键代码
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;
}
 
工程模板v1.1.0
  
后续
会持续补充测试其他方式,ofstream本次测试比QFile的性能还差一些。
上一篇:《文件IO操作开发笔记(一):使用Qt的QFile对磁盘文件存储进行性能测试以及测试工具》
 下一篇:敬请期待…
文为原创文章,转载请注明原文出处
 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/128626548



















