文件IO操作开发笔记(二):使用Cpp的ofstream对磁盘文件存储进行性能测试以及测试工具

前言

  在做到个别项目对日志要求较高,要求并行写入的数据较多,尽管写入数据的线程放在子线程,仍然会造成界面程序的假死(实际上Qt还是在跑,只是磁盘消耗超过瓶颈,造成假死(注意:控制台还能看到打印输出,linux则能看到打印输出)。
  本篇升级了测试工具,并且测试了ofstream在USB3.0和M.2SSD上的写入性能。

 

版本v1.1.0

  更新版本版本,新增了c++的ofstream写入方式。
  在这里插入图片描述

 

测试工具v1.1.0下载地址

  请自行溯源搜索,发不出

 

使用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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章