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