前言
在做到個別項目對日誌要求較高,要求並行寫入的數據較多,儘管寫入數據的線程放在子線程,仍然會造成界面程序的假死(實際上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