tests/benchmarks/qfile/main.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QDebug>
       
    43 #include <QTemporaryFile>
       
    44 #include <QFSFileEngine>
       
    45 #include <QString>
       
    46 #include <QDirIterator>
       
    47 
       
    48 #include <qtest.h>
       
    49 
       
    50 #include <stdio.h>
       
    51 
       
    52 #ifdef Q_OS_WIN
       
    53 # include <windows.h>
       
    54 # include <atlbase.h>
       
    55 #endif
       
    56 
       
    57 #define BUFSIZE 1024*512
       
    58 #define FACTOR 1024*512
       
    59 #define TF_SIZE FACTOR*81
       
    60 
       
    61 // 10 predefined (but random() seek positions
       
    62 // hardcoded to be comparable over several runs
       
    63 const int seekpos[] = {TF_SIZE*0.52,
       
    64                        TF_SIZE*0.23,
       
    65                        TF_SIZE*0.73,
       
    66                        TF_SIZE*0.77,
       
    67                        TF_SIZE*0.80,
       
    68                        TF_SIZE*0.12,
       
    69                        TF_SIZE*0.53,
       
    70                        TF_SIZE*0.21,
       
    71                        TF_SIZE*0.27,
       
    72                        TF_SIZE*0.78};
       
    73 
       
    74 const int sp_size = sizeof(seekpos)/sizeof(int);
       
    75 
       
    76 class tst_qfile: public QObject
       
    77 {
       
    78 Q_ENUMS(BenchmarkType)
       
    79 Q_OBJECT
       
    80 public:
       
    81     enum BenchmarkType {
       
    82         QFileBenchmark = 1,
       
    83         QFSFileEngineBenchmark,
       
    84         Win32Benchmark,
       
    85         PosixBenchmark,
       
    86         QFileFromPosixBenchmark
       
    87     };
       
    88 private slots:
       
    89     void initTestCase();
       
    90     void cleanupTestCase();
       
    91 
       
    92     void open_data();
       
    93     void open();
       
    94     void seek_data();
       
    95     void seek();
       
    96 
       
    97     void readSmallFiles_QFile();
       
    98     void readSmallFiles_QFSFileEngine();
       
    99     void readSmallFiles_posix();
       
   100     void readSmallFiles_Win32();
       
   101 
       
   102     void readSmallFiles_QFile_data();
       
   103     void readSmallFiles_QFSFileEngine_data();
       
   104     void readSmallFiles_posix_data();
       
   105     void readSmallFiles_Win32_data();
       
   106 
       
   107     void readBigFile_QFile_data();
       
   108     void readBigFile_QFSFileEngine_data();
       
   109     void readBigFile_posix_data();
       
   110     void readBigFile_Win32_data();
       
   111 
       
   112     void readBigFile_QFile();
       
   113     void readBigFile_QFSFileEngine();
       
   114     void readBigFile_posix();
       
   115     void readBigFile_Win32();
       
   116 
       
   117 private:
       
   118     void readBigFile_data(BenchmarkType type, QIODevice::OpenModeFlag t, QIODevice::OpenModeFlag b);
       
   119     void readBigFile();
       
   120     void readSmallFiles_data(BenchmarkType type, QIODevice::OpenModeFlag t, QIODevice::OpenModeFlag b);
       
   121     void readSmallFiles();
       
   122     void createFile();
       
   123     void fillFile(int factor=FACTOR);
       
   124     void removeFile();
       
   125     void createSmallFiles();
       
   126     void removeSmallFiles();
       
   127     QString filename;
       
   128     QString tmpDirName;
       
   129 };
       
   130 
       
   131 void tst_qfile::createFile()
       
   132 {
       
   133     QTemporaryFile tmpFile;
       
   134     tmpFile.setAutoRemove(false);
       
   135     if (!tmpFile.open())
       
   136         ::_exit(1);
       
   137     filename = tmpFile.fileName();
       
   138     tmpFile.close();
       
   139 }
       
   140 
       
   141 void tst_qfile::removeFile()
       
   142 {
       
   143     if (!filename.isEmpty())
       
   144         QFile::remove(filename);
       
   145 }
       
   146 
       
   147 void tst_qfile::fillFile(int factor)
       
   148 {
       
   149     QFile tmpFile(filename);
       
   150     tmpFile.open(QIODevice::WriteOnly);
       
   151     //for (int row=0; row<factor; ++row) {
       
   152     //    tmpFile.write(QByteArray().fill('0'+row%('0'-'z'), 80));
       
   153     //    tmpFile.write("\n");
       
   154     //}
       
   155     tmpFile.seek(factor*80);
       
   156     tmpFile.putChar('\n');
       
   157     tmpFile.close();
       
   158     // let IO settle
       
   159     QTest::qSleep(2000);
       
   160 }
       
   161 
       
   162 void tst_qfile::initTestCase()
       
   163 {
       
   164 }
       
   165 
       
   166 void tst_qfile::cleanupTestCase()
       
   167 {
       
   168 }
       
   169 
       
   170 void tst_qfile::readBigFile_QFile() { readBigFile(); }
       
   171 void tst_qfile::readBigFile_QFSFileEngine() { readBigFile(); }
       
   172 void tst_qfile::readBigFile_posix() { readBigFile(); }
       
   173 void tst_qfile::readBigFile_Win32() { readBigFile(); }
       
   174 
       
   175 void tst_qfile::readBigFile_QFile_data()
       
   176 {
       
   177     readBigFile_data(QFileBenchmark, QIODevice::NotOpen, QIODevice::NotOpen);
       
   178     readBigFile_data(QFileBenchmark, QIODevice::NotOpen, QIODevice::Unbuffered);
       
   179     readBigFile_data(QFileBenchmark, QIODevice::Text, QIODevice::NotOpen);
       
   180     readBigFile_data(QFileBenchmark, QIODevice::Text, QIODevice::Unbuffered);
       
   181 
       
   182 }
       
   183 
       
   184 void tst_qfile::readBigFile_QFSFileEngine_data()
       
   185 {
       
   186     readBigFile_data(QFSFileEngineBenchmark, QIODevice::NotOpen, QIODevice::NotOpen);
       
   187     readBigFile_data(QFSFileEngineBenchmark, QIODevice::NotOpen, QIODevice::Unbuffered);
       
   188     readBigFile_data(QFSFileEngineBenchmark, QIODevice::Text, QIODevice::NotOpen);
       
   189     readBigFile_data(QFSFileEngineBenchmark, QIODevice::Text, QIODevice::Unbuffered);
       
   190 }
       
   191 
       
   192 void tst_qfile::readBigFile_posix_data()
       
   193 {
       
   194     readBigFile_data(PosixBenchmark, QIODevice::NotOpen, QIODevice::NotOpen);
       
   195 }
       
   196 
       
   197 void tst_qfile::readBigFile_Win32_data()
       
   198 {
       
   199     readBigFile_data(Win32Benchmark, QIODevice::NotOpen, QIODevice::NotOpen);
       
   200 }
       
   201 
       
   202 
       
   203 void tst_qfile::readBigFile_data(BenchmarkType type, QIODevice::OpenModeFlag t, QIODevice::OpenModeFlag b)
       
   204 {
       
   205     QTest::addColumn<tst_qfile::BenchmarkType>("testType");
       
   206     QTest::addColumn<int>("blockSize");
       
   207     QTest::addColumn<QFile::OpenModeFlag>("textMode");
       
   208     QTest::addColumn<QFile::OpenModeFlag>("bufferedMode");
       
   209 
       
   210     const int bs[] = {1024, 1024*2, 1024*8, 1024*16, 1024*32,1024*512};
       
   211     int bs_entries = sizeof(bs)/sizeof(const int);
       
   212 
       
   213     QString flagstring;
       
   214     if (t & QIODevice::Text)       flagstring += "textMode ";
       
   215     if (b & QIODevice::Unbuffered) flagstring += "unbuffered ";
       
   216     if (flagstring.isEmpty())      flagstring = "none";
       
   217 
       
   218     for (int i=0; i<bs_entries; ++i)
       
   219         QTest::newRow((QString("BS: %1, Flags: %2" )).arg(bs[i]).arg(flagstring).toLatin1().constData()) << type << bs[i] << t << b;
       
   220   
       
   221 }
       
   222 
       
   223 void tst_qfile::readBigFile()
       
   224 {
       
   225     QFETCH(tst_qfile::BenchmarkType, testType);
       
   226     QFETCH(int, blockSize);
       
   227     QFETCH(QFile::OpenModeFlag, textMode);
       
   228     QFETCH(QFile::OpenModeFlag, bufferedMode);
       
   229 
       
   230     char buffer[BUFSIZE]; // we can't allocate buffers nice and dynamically in c++
       
   231     removeFile();
       
   232     createFile();
       
   233     fillFile();
       
   234 
       
   235     switch (testType) {
       
   236         case(QFileBenchmark): {
       
   237             QFile file(filename);
       
   238             file.open(QIODevice::ReadOnly|textMode|bufferedMode);
       
   239             QBENCHMARK {
       
   240                 while(!file.atEnd())
       
   241                     file.read(blockSize);
       
   242                 file.reset();
       
   243             }
       
   244             file.close();
       
   245         }
       
   246         break;
       
   247         case(QFSFileEngineBenchmark): {
       
   248             QFSFileEngine fse(filename);
       
   249             fse.open(QIODevice::ReadOnly|textMode|bufferedMode);
       
   250             QBENCHMARK {
       
   251                //qWarning() << fse.supportsExtension(QAbstractFileEngine::AtEndExtension);
       
   252                while(fse.read(buffer, blockSize));
       
   253                fse.seek(0);
       
   254             }
       
   255             fse.close();
       
   256         }
       
   257         break;
       
   258         case(PosixBenchmark): {
       
   259             QByteArray data = filename.toLocal8Bit();
       
   260             const char* cfilename = data.constData();
       
   261             FILE* cfile = ::fopen(cfilename, "rb");
       
   262             QBENCHMARK {
       
   263                 while(!feof(cfile))
       
   264                     ::fread(buffer, blockSize, 1, cfile);
       
   265                 ::fseek(cfile, 0, SEEK_SET);
       
   266             }
       
   267             ::fclose(cfile);
       
   268         }
       
   269         break;
       
   270         case(QFileFromPosixBenchmark): {
       
   271             // No gain in benchmarking this case
       
   272         }
       
   273         break;
       
   274         case(Win32Benchmark): {
       
   275 #ifdef Q_OS_WIN
       
   276             HANDLE hndl;
       
   277 
       
   278             // ensure we don't account string conversion
       
   279             wchar_t* cfilename = (wchar_t*)filename.utf16();
       
   280 
       
   281             hndl = CreateFile(cfilename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
       
   282             Q_ASSERT(hndl);
       
   283             wchar_t* nativeBuffer = new wchar_t[BUFSIZE];
       
   284             DWORD numberOfBytesRead;
       
   285 
       
   286             QBENCHMARK {
       
   287                 do {
       
   288                    ReadFile(hndl, nativeBuffer, blockSize, &numberOfBytesRead, NULL);
       
   289                 } while(numberOfBytesRead != 0);
       
   290                 SetFilePointer(hndl, 0, NULL, FILE_BEGIN);
       
   291             }
       
   292             delete[] nativeBuffer;
       
   293             CloseHandle(hndl);
       
   294 #else
       
   295             QFAIL("Not running on a non-Windows platform!");
       
   296 #endif
       
   297         }
       
   298         break;
       
   299     }
       
   300 }
       
   301 
       
   302 void tst_qfile::seek_data()
       
   303 {
       
   304     QTest::addColumn<tst_qfile::BenchmarkType>("testType");
       
   305     QTest::newRow("QFile") << QFileBenchmark;
       
   306     QTest::newRow("QFSFileEngine") << QFSFileEngineBenchmark;
       
   307     QTest::newRow("Posix FILE*") << PosixBenchmark;
       
   308 #ifdef Q_OS_WIN
       
   309     QTest::newRow("Win32 API") << Win32Benchmark;
       
   310 #endif
       
   311 }
       
   312 
       
   313 void tst_qfile::seek()
       
   314 {
       
   315     QFETCH(tst_qfile::BenchmarkType, testType);
       
   316     int i = 0;
       
   317 
       
   318     createFile();
       
   319     fillFile();
       
   320 
       
   321     switch (testType) {
       
   322         case(QFileBenchmark): {
       
   323             QFile file(filename);
       
   324             file.open(QIODevice::ReadOnly);
       
   325             QBENCHMARK {
       
   326                 i=(i+1)%sp_size;
       
   327                 file.seek(seekpos[i]);
       
   328             }
       
   329             file.close();
       
   330         }
       
   331         break;
       
   332         case(QFSFileEngineBenchmark): {
       
   333             QFSFileEngine fse(filename);
       
   334             fse.open(QIODevice::ReadOnly);
       
   335             QBENCHMARK {
       
   336                 i=(i+1)%sp_size;
       
   337                 fse.seek(seekpos[i]);
       
   338             }
       
   339             fse.close();
       
   340         }
       
   341         break;
       
   342         case(PosixBenchmark): {
       
   343             QByteArray data = filename.toLocal8Bit();
       
   344             const char* cfilename = data.constData();
       
   345             FILE* cfile = ::fopen(cfilename, "rb");
       
   346             QBENCHMARK {
       
   347                 i=(i+1)%sp_size;
       
   348                 ::fseek(cfile, seekpos[i], SEEK_SET);
       
   349             }
       
   350             ::fclose(cfile);
       
   351         }
       
   352         break;
       
   353         case(QFileFromPosixBenchmark): {
       
   354             // No gain in benchmarking this case
       
   355         }
       
   356         break;
       
   357         case(Win32Benchmark): {
       
   358 #ifdef Q_OS_WIN
       
   359             HANDLE hndl;
       
   360 
       
   361             // ensure we don't account string conversion
       
   362             wchar_t* cfilename = (wchar_t*)filename.utf16();
       
   363 
       
   364             hndl = CreateFile(cfilename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
       
   365             Q_ASSERT(hndl);
       
   366             QBENCHMARK {
       
   367                 i=(i+1)%sp_size;
       
   368                 SetFilePointer(hndl, seekpos[i], NULL, 0);
       
   369             }
       
   370             CloseHandle(hndl);
       
   371 #else
       
   372             QFAIL("Not running on a Windows plattform!");
       
   373 #endif
       
   374         }
       
   375         break;
       
   376     }
       
   377 }
       
   378 
       
   379 void tst_qfile::open_data()
       
   380 {
       
   381     QTest::addColumn<tst_qfile::BenchmarkType>("testType");
       
   382     QTest::newRow("QFile") << QFileBenchmark;
       
   383     QTest::newRow("QFSFileEngine") << QFSFileEngineBenchmark;
       
   384     QTest::newRow("Posix FILE*") << PosixBenchmark;
       
   385     QTest::newRow("QFile from FILE*") << QFileFromPosixBenchmark;
       
   386 #ifdef Q_OS_WIN
       
   387     QTest::newRow("Win32 API") << Win32Benchmark;
       
   388 #endif
       
   389 }
       
   390 
       
   391 void tst_qfile::open()
       
   392 {
       
   393     QFETCH(tst_qfile::BenchmarkType, testType);
       
   394 
       
   395     removeFile();
       
   396     createFile();
       
   397 
       
   398     switch (testType) {
       
   399         case(QFileBenchmark): {
       
   400             QBENCHMARK {
       
   401                 QFile file( filename );
       
   402                 file.open( QIODevice::ReadOnly );
       
   403                 file.close();
       
   404             }
       
   405         }
       
   406         break;
       
   407         case(QFSFileEngineBenchmark): {
       
   408             QBENCHMARK {
       
   409                 QFSFileEngine fse(filename);
       
   410                 fse.open(QIODevice::ReadOnly);
       
   411                 fse.close();
       
   412             }
       
   413         }
       
   414         break;
       
   415 
       
   416         case(PosixBenchmark): {
       
   417             // ensure we don't account toLocal8Bit()
       
   418             QByteArray data = filename.toLocal8Bit();
       
   419             const char* cfilename = data.constData();
       
   420 
       
   421             QBENCHMARK {
       
   422                 FILE* cfile = ::fopen(cfilename, "rb");
       
   423                 ::fclose(cfile);
       
   424             }
       
   425         }
       
   426         break;
       
   427         case(QFileFromPosixBenchmark): {
       
   428             // ensure we don't account toLocal8Bit()
       
   429             QByteArray data = filename.toLocal8Bit();
       
   430             const char* cfilename = data.constData();
       
   431             FILE* cfile = ::fopen(cfilename, "rb");
       
   432 
       
   433             QBENCHMARK {
       
   434                 QFile file;
       
   435                 file.open(cfile, QIODevice::ReadOnly);
       
   436                 file.close();
       
   437             }
       
   438         }
       
   439         break;
       
   440         case(Win32Benchmark): {
       
   441 #ifdef Q_OS_WIN
       
   442             HANDLE hndl;
       
   443 
       
   444             // ensure we don't account string conversion
       
   445             wchar_t* cfilename = (wchar_t*)filename.utf16();
       
   446 
       
   447             QBENCHMARK {
       
   448                 hndl = CreateFile(cfilename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
       
   449                 Q_ASSERT(hndl);
       
   450                 CloseHandle(hndl);
       
   451             }
       
   452 #else
       
   453         QFAIL("Not running on a non-Windows platform!");
       
   454 #endif
       
   455         }
       
   456         break;
       
   457     }
       
   458 
       
   459 }
       
   460 
       
   461 
       
   462 void tst_qfile::readSmallFiles_QFile() { readSmallFiles(); }
       
   463 void tst_qfile::readSmallFiles_QFSFileEngine() { readSmallFiles(); }
       
   464 void tst_qfile::readSmallFiles_posix() { readSmallFiles(); }
       
   465 void tst_qfile::readSmallFiles_Win32() { readSmallFiles(); }
       
   466 
       
   467 void tst_qfile::readSmallFiles_QFile_data()
       
   468 {
       
   469     readSmallFiles_data(QFileBenchmark, QIODevice::NotOpen, QIODevice::NotOpen);
       
   470     readSmallFiles_data(QFileBenchmark, QIODevice::NotOpen, QIODevice::Unbuffered);
       
   471     readSmallFiles_data(QFileBenchmark, QIODevice::Text, QIODevice::NotOpen);
       
   472     readSmallFiles_data(QFileBenchmark, QIODevice::Text, QIODevice::Unbuffered);
       
   473 
       
   474 }
       
   475 
       
   476 void tst_qfile::readSmallFiles_QFSFileEngine_data()
       
   477 {
       
   478     readSmallFiles_data(QFSFileEngineBenchmark, QIODevice::NotOpen, QIODevice::NotOpen);
       
   479     readSmallFiles_data(QFSFileEngineBenchmark, QIODevice::NotOpen, QIODevice::Unbuffered);
       
   480     readSmallFiles_data(QFSFileEngineBenchmark, QIODevice::Text, QIODevice::NotOpen);
       
   481     readSmallFiles_data(QFSFileEngineBenchmark, QIODevice::Text, QIODevice::Unbuffered);
       
   482 }
       
   483 
       
   484 void tst_qfile::readSmallFiles_posix_data()
       
   485 {
       
   486     readSmallFiles_data(PosixBenchmark, QIODevice::NotOpen, QIODevice::NotOpen);
       
   487 }
       
   488 
       
   489 void tst_qfile::readSmallFiles_Win32_data()
       
   490 {
       
   491     readSmallFiles_data(Win32Benchmark, QIODevice::NotOpen, QIODevice::NotOpen);
       
   492 }
       
   493 
       
   494 
       
   495 void tst_qfile::readSmallFiles_data(BenchmarkType type, QIODevice::OpenModeFlag t, QIODevice::OpenModeFlag b)
       
   496 {
       
   497     QTest::addColumn<tst_qfile::BenchmarkType>("testType");
       
   498     QTest::addColumn<int>("blockSize");
       
   499     QTest::addColumn<QFile::OpenModeFlag>("textMode");
       
   500     QTest::addColumn<QFile::OpenModeFlag>("bufferedMode");
       
   501 
       
   502     const int bs[] = {1024, 1024*2, 1024*8, 1024*16, 1024*32,1024*512};
       
   503     int bs_entries = sizeof(bs)/sizeof(const int);
       
   504 
       
   505     QString flagstring;
       
   506     if (t & QIODevice::Text)       flagstring += "textMode ";
       
   507     if (b & QIODevice::Unbuffered) flagstring += "unbuffered ";
       
   508     if (flagstring.isEmpty())      flagstring = "none";
       
   509 
       
   510     for (int i=0; i<bs_entries; ++i)
       
   511         QTest::newRow((QString("BS: %1, Flags: %2" )).arg(bs[i]).arg(flagstring).toLatin1().constData()) << type << bs[i] << t << b;
       
   512 
       
   513 }
       
   514 
       
   515 void tst_qfile::createSmallFiles()
       
   516 {
       
   517     QDir dir = QDir::temp();
       
   518     Q_ASSERT(dir.mkdir("tst"));
       
   519     dir.cd("tst");
       
   520     tmpDirName = dir.absolutePath();
       
   521 
       
   522     for (int i = 0; i < 1000; ++i)
       
   523     {
       
   524         QFile f(tmpDirName+"/"+QString::number(i));
       
   525         f.open(QIODevice::WriteOnly);
       
   526         f.seek(512);
       
   527         f.close();
       
   528     }
       
   529 }
       
   530 
       
   531 void tst_qfile::removeSmallFiles()
       
   532 {
       
   533     QDirIterator it(tmpDirName, QDirIterator::FollowSymlinks);
       
   534     while (it.hasNext())
       
   535         QFile::remove(it.next());
       
   536     QDir::temp().rmdir("tst");
       
   537 }
       
   538 
       
   539 
       
   540 void tst_qfile::readSmallFiles()
       
   541 {
       
   542     QFETCH(tst_qfile::BenchmarkType, testType);
       
   543     QFETCH(int, blockSize);
       
   544     QFETCH(QFile::OpenModeFlag, textMode);
       
   545     QFETCH(QFile::OpenModeFlag, bufferedMode);
       
   546 
       
   547     removeSmallFiles();
       
   548     createSmallFiles();
       
   549     QDir dir(tmpDirName);
       
   550     const QStringList files = dir.entryList(QDir::NoDotAndDotDot|QDir::NoSymLinks);
       
   551     char buffer[BUFSIZE]; // we can't allocate buffers nice and dynamically in c++
       
   552 
       
   553     switch (testType) {
       
   554         case(QFileBenchmark): {
       
   555             QList<QFile*> fileList;
       
   556             Q_FOREACH(QString file, files) {
       
   557                 QFile *f = new QFile(file);
       
   558                 f->open(QIODevice::ReadOnly|textMode|bufferedMode);
       
   559                 fileList.append(f);
       
   560             }
       
   561 
       
   562             QBENCHMARK {
       
   563                 Q_FOREACH(QFile *file, fileList) {
       
   564                     while (!file->atEnd()) {
       
   565                        file->read(buffer, blockSize);
       
   566                     }
       
   567                 }
       
   568             }
       
   569 
       
   570             Q_FOREACH(QFile *file, fileList) {
       
   571                 file->close();
       
   572                 delete file;
       
   573             }
       
   574         }
       
   575         break;
       
   576         case(QFSFileEngineBenchmark): {
       
   577             QList<QFSFileEngine*> fileList;
       
   578             Q_FOREACH(QString file, files) {
       
   579                 QFSFileEngine *fse = new QFSFileEngine(file);
       
   580                 fse->open(QIODevice::ReadOnly|textMode|bufferedMode);
       
   581                 fileList.append(fse);
       
   582             }
       
   583 
       
   584             QBENCHMARK {
       
   585                 Q_FOREACH(QFSFileEngine *fse, fileList) {
       
   586                     while (fse->read(buffer, blockSize));
       
   587                 }
       
   588             }
       
   589 
       
   590             Q_FOREACH(QFSFileEngine *fse, fileList) {
       
   591                 fse->close();
       
   592                 delete fse;
       
   593             }
       
   594         }
       
   595         break;
       
   596         case(PosixBenchmark): {
       
   597             QList<FILE*> fileList;
       
   598             Q_FOREACH(QString file, files) {
       
   599                 fileList.append(::fopen(QFile::encodeName(file).constData(), "rb"));
       
   600             }
       
   601 
       
   602             QBENCHMARK {
       
   603                 Q_FOREACH(FILE* cfile, fileList) {
       
   604                     while(!feof(cfile))
       
   605                         ::fread(buffer, blockSize, 1, cfile);
       
   606                     ::fseek(cfile, 0, SEEK_SET);
       
   607                 }
       
   608             }
       
   609 
       
   610             Q_FOREACH(FILE* cfile, fileList) {
       
   611                 ::fclose(cfile);
       
   612             }
       
   613         }
       
   614         break;
       
   615         case(QFileFromPosixBenchmark): {
       
   616             // No gain in benchmarking this case
       
   617         }
       
   618         break;
       
   619         case(Win32Benchmark): {
       
   620 #ifdef Q_OS_WIN
       
   621             HANDLE hndl;
       
   622 
       
   623             // ensure we don't account string conversion
       
   624             wchar_t* cfilename = (wchar_t*)filename.utf16();
       
   625 
       
   626             hndl = CreateFile(cfilename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0);
       
   627             Q_ASSERT(hndl);
       
   628             wchar_t* nativeBuffer = new wchar_t[BUFSIZE];
       
   629             DWORD numberOfBytesRead;
       
   630             QBENCHMARK {
       
   631                 do {
       
   632                    ReadFile(hndl, nativeBuffer, blockSize, &numberOfBytesRead, NULL);
       
   633                 } while(numberOfBytesRead != 0);
       
   634             }
       
   635             delete nativeBuffer;
       
   636             CloseHandle(hndl);
       
   637 #else
       
   638             QFAIL("Not running on a non-Windows platform!");
       
   639 #endif
       
   640         }
       
   641         break;
       
   642     }
       
   643 }
       
   644 
       
   645 Q_DECLARE_METATYPE(tst_qfile::BenchmarkType)
       
   646 Q_DECLARE_METATYPE(QIODevice::OpenMode)
       
   647 Q_DECLARE_METATYPE(QIODevice::OpenModeFlag)
       
   648 
       
   649 QTEST_MAIN(tst_qfile)
       
   650 
       
   651 #include "main.moc"