author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 3 | 41300fa6a67c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
Q_DECLARE_METATYPE(tst_qfile::BenchmarkType) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
Q_DECLARE_METATYPE(QIODevice::OpenMode) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
133 |
Q_DECLARE_METATYPE(QIODevice::OpenModeFlag) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
134 |
|
0 | 135 |
void tst_qfile::createFile() |
136 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
removeFile(); // Cleanup in case previous test case aborted before cleaning up |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
|
0 | 139 |
QTemporaryFile tmpFile; |
140 |
tmpFile.setAutoRemove(false); |
|
141 |
if (!tmpFile.open()) |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
142 |
::exit(1); |
0 | 143 |
filename = tmpFile.fileName(); |
144 |
tmpFile.close(); |
|
145 |
} |
|
146 |
||
147 |
void tst_qfile::removeFile() |
|
148 |
{ |
|
149 |
if (!filename.isEmpty()) |
|
150 |
QFile::remove(filename); |
|
151 |
} |
|
152 |
||
153 |
void tst_qfile::fillFile(int factor) |
|
154 |
{ |
|
155 |
QFile tmpFile(filename); |
|
156 |
tmpFile.open(QIODevice::WriteOnly); |
|
157 |
//for (int row=0; row<factor; ++row) { |
|
158 |
// tmpFile.write(QByteArray().fill('0'+row%('0'-'z'), 80)); |
|
159 |
// tmpFile.write("\n"); |
|
160 |
//} |
|
161 |
tmpFile.seek(factor*80); |
|
162 |
tmpFile.putChar('\n'); |
|
163 |
tmpFile.close(); |
|
164 |
// let IO settle |
|
165 |
QTest::qSleep(2000); |
|
166 |
} |
|
167 |
||
168 |
void tst_qfile::initTestCase() |
|
169 |
{ |
|
170 |
} |
|
171 |
||
172 |
void tst_qfile::cleanupTestCase() |
|
173 |
{ |
|
174 |
} |
|
175 |
||
176 |
void tst_qfile::readBigFile_QFile() { readBigFile(); } |
|
177 |
void tst_qfile::readBigFile_QFSFileEngine() { readBigFile(); } |
|
178 |
void tst_qfile::readBigFile_posix() { readBigFile(); } |
|
179 |
void tst_qfile::readBigFile_Win32() { readBigFile(); } |
|
180 |
||
181 |
void tst_qfile::readBigFile_QFile_data() |
|
182 |
{ |
|
183 |
readBigFile_data(QFileBenchmark, QIODevice::NotOpen, QIODevice::NotOpen); |
|
184 |
readBigFile_data(QFileBenchmark, QIODevice::NotOpen, QIODevice::Unbuffered); |
|
185 |
readBigFile_data(QFileBenchmark, QIODevice::Text, QIODevice::NotOpen); |
|
186 |
readBigFile_data(QFileBenchmark, QIODevice::Text, QIODevice::Unbuffered); |
|
187 |
||
188 |
} |
|
189 |
||
190 |
void tst_qfile::readBigFile_QFSFileEngine_data() |
|
191 |
{ |
|
192 |
readBigFile_data(QFSFileEngineBenchmark, QIODevice::NotOpen, QIODevice::NotOpen); |
|
193 |
readBigFile_data(QFSFileEngineBenchmark, QIODevice::NotOpen, QIODevice::Unbuffered); |
|
194 |
readBigFile_data(QFSFileEngineBenchmark, QIODevice::Text, QIODevice::NotOpen); |
|
195 |
readBigFile_data(QFSFileEngineBenchmark, QIODevice::Text, QIODevice::Unbuffered); |
|
196 |
} |
|
197 |
||
198 |
void tst_qfile::readBigFile_posix_data() |
|
199 |
{ |
|
200 |
readBigFile_data(PosixBenchmark, QIODevice::NotOpen, QIODevice::NotOpen); |
|
201 |
} |
|
202 |
||
203 |
void tst_qfile::readBigFile_Win32_data() |
|
204 |
{ |
|
205 |
readBigFile_data(Win32Benchmark, QIODevice::NotOpen, QIODevice::NotOpen); |
|
206 |
} |
|
207 |
||
208 |
||
209 |
void tst_qfile::readBigFile_data(BenchmarkType type, QIODevice::OpenModeFlag t, QIODevice::OpenModeFlag b) |
|
210 |
{ |
|
211 |
QTest::addColumn<tst_qfile::BenchmarkType>("testType"); |
|
212 |
QTest::addColumn<int>("blockSize"); |
|
213 |
QTest::addColumn<QFile::OpenModeFlag>("textMode"); |
|
214 |
QTest::addColumn<QFile::OpenModeFlag>("bufferedMode"); |
|
215 |
||
216 |
const int bs[] = {1024, 1024*2, 1024*8, 1024*16, 1024*32,1024*512}; |
|
217 |
int bs_entries = sizeof(bs)/sizeof(const int); |
|
218 |
||
219 |
QString flagstring; |
|
220 |
if (t & QIODevice::Text) flagstring += "textMode "; |
|
221 |
if (b & QIODevice::Unbuffered) flagstring += "unbuffered "; |
|
222 |
if (flagstring.isEmpty()) flagstring = "none"; |
|
223 |
||
224 |
for (int i=0; i<bs_entries; ++i) |
|
225 |
QTest::newRow((QString("BS: %1, Flags: %2" )).arg(bs[i]).arg(flagstring).toLatin1().constData()) << type << bs[i] << t << b; |
|
226 |
} |
|
227 |
||
228 |
void tst_qfile::readBigFile() |
|
229 |
{ |
|
230 |
QFETCH(tst_qfile::BenchmarkType, testType); |
|
231 |
QFETCH(int, blockSize); |
|
232 |
QFETCH(QFile::OpenModeFlag, textMode); |
|
233 |
QFETCH(QFile::OpenModeFlag, bufferedMode); |
|
234 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
#ifndef Q_OS_WIN |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
236 |
if (testType == Win32Benchmark) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
237 |
QSKIP("This is Windows only benchmark.", SkipSingle); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
238 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
239 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
240 |
char *buffer = new char[BUFSIZE]; |
0 | 241 |
createFile(); |
242 |
fillFile(); |
|
243 |
||
244 |
switch (testType) { |
|
245 |
case(QFileBenchmark): { |
|
246 |
QFile file(filename); |
|
247 |
file.open(QIODevice::ReadOnly|textMode|bufferedMode); |
|
248 |
QBENCHMARK { |
|
249 |
while(!file.atEnd()) |
|
250 |
file.read(blockSize); |
|
251 |
file.reset(); |
|
252 |
} |
|
253 |
file.close(); |
|
254 |
} |
|
255 |
break; |
|
256 |
case(QFSFileEngineBenchmark): { |
|
257 |
QFSFileEngine fse(filename); |
|
258 |
fse.open(QIODevice::ReadOnly|textMode|bufferedMode); |
|
259 |
QBENCHMARK { |
|
260 |
//qWarning() << fse.supportsExtension(QAbstractFileEngine::AtEndExtension); |
|
261 |
while(fse.read(buffer, blockSize)); |
|
262 |
fse.seek(0); |
|
263 |
} |
|
264 |
fse.close(); |
|
265 |
} |
|
266 |
break; |
|
267 |
case(PosixBenchmark): { |
|
268 |
QByteArray data = filename.toLocal8Bit(); |
|
269 |
const char* cfilename = data.constData(); |
|
270 |
FILE* cfile = ::fopen(cfilename, "rb"); |
|
271 |
QBENCHMARK { |
|
272 |
while(!feof(cfile)) |
|
273 |
::fread(buffer, blockSize, 1, cfile); |
|
274 |
::fseek(cfile, 0, SEEK_SET); |
|
275 |
} |
|
276 |
::fclose(cfile); |
|
277 |
} |
|
278 |
break; |
|
279 |
case(QFileFromPosixBenchmark): { |
|
280 |
// No gain in benchmarking this case |
|
281 |
} |
|
282 |
break; |
|
283 |
case(Win32Benchmark): { |
|
284 |
#ifdef Q_OS_WIN |
|
285 |
HANDLE hndl; |
|
286 |
||
287 |
// ensure we don't account string conversion |
|
288 |
wchar_t* cfilename = (wchar_t*)filename.utf16(); |
|
289 |
||
290 |
hndl = CreateFile(cfilename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); |
|
291 |
Q_ASSERT(hndl); |
|
292 |
wchar_t* nativeBuffer = new wchar_t[BUFSIZE]; |
|
293 |
DWORD numberOfBytesRead; |
|
294 |
||
295 |
QBENCHMARK { |
|
296 |
do { |
|
297 |
ReadFile(hndl, nativeBuffer, blockSize, &numberOfBytesRead, NULL); |
|
298 |
} while(numberOfBytesRead != 0); |
|
299 |
SetFilePointer(hndl, 0, NULL, FILE_BEGIN); |
|
300 |
} |
|
301 |
delete[] nativeBuffer; |
|
302 |
CloseHandle(hndl); |
|
303 |
#else |
|
304 |
QFAIL("Not running on a non-Windows platform!"); |
|
305 |
#endif |
|
306 |
} |
|
307 |
break; |
|
308 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
309 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
310 |
removeFile(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
311 |
delete[] buffer; |
0 | 312 |
} |
313 |
||
314 |
void tst_qfile::seek_data() |
|
315 |
{ |
|
316 |
QTest::addColumn<tst_qfile::BenchmarkType>("testType"); |
|
317 |
QTest::newRow("QFile") << QFileBenchmark; |
|
318 |
QTest::newRow("QFSFileEngine") << QFSFileEngineBenchmark; |
|
319 |
QTest::newRow("Posix FILE*") << PosixBenchmark; |
|
320 |
#ifdef Q_OS_WIN |
|
321 |
QTest::newRow("Win32 API") << Win32Benchmark; |
|
322 |
#endif |
|
323 |
} |
|
324 |
||
325 |
void tst_qfile::seek() |
|
326 |
{ |
|
327 |
QFETCH(tst_qfile::BenchmarkType, testType); |
|
328 |
int i = 0; |
|
329 |
||
330 |
createFile(); |
|
331 |
fillFile(); |
|
332 |
||
333 |
switch (testType) { |
|
334 |
case(QFileBenchmark): { |
|
335 |
QFile file(filename); |
|
336 |
file.open(QIODevice::ReadOnly); |
|
337 |
QBENCHMARK { |
|
338 |
i=(i+1)%sp_size; |
|
339 |
file.seek(seekpos[i]); |
|
340 |
} |
|
341 |
file.close(); |
|
342 |
} |
|
343 |
break; |
|
344 |
case(QFSFileEngineBenchmark): { |
|
345 |
QFSFileEngine fse(filename); |
|
346 |
fse.open(QIODevice::ReadOnly); |
|
347 |
QBENCHMARK { |
|
348 |
i=(i+1)%sp_size; |
|
349 |
fse.seek(seekpos[i]); |
|
350 |
} |
|
351 |
fse.close(); |
|
352 |
} |
|
353 |
break; |
|
354 |
case(PosixBenchmark): { |
|
355 |
QByteArray data = filename.toLocal8Bit(); |
|
356 |
const char* cfilename = data.constData(); |
|
357 |
FILE* cfile = ::fopen(cfilename, "rb"); |
|
358 |
QBENCHMARK { |
|
359 |
i=(i+1)%sp_size; |
|
360 |
::fseek(cfile, seekpos[i], SEEK_SET); |
|
361 |
} |
|
362 |
::fclose(cfile); |
|
363 |
} |
|
364 |
break; |
|
365 |
case(QFileFromPosixBenchmark): { |
|
366 |
// No gain in benchmarking this case |
|
367 |
} |
|
368 |
break; |
|
369 |
case(Win32Benchmark): { |
|
370 |
#ifdef Q_OS_WIN |
|
371 |
HANDLE hndl; |
|
372 |
||
373 |
// ensure we don't account string conversion |
|
374 |
wchar_t* cfilename = (wchar_t*)filename.utf16(); |
|
375 |
||
376 |
hndl = CreateFile(cfilename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); |
|
377 |
Q_ASSERT(hndl); |
|
378 |
QBENCHMARK { |
|
379 |
i=(i+1)%sp_size; |
|
380 |
SetFilePointer(hndl, seekpos[i], NULL, 0); |
|
381 |
} |
|
382 |
CloseHandle(hndl); |
|
383 |
#else |
|
384 |
QFAIL("Not running on a Windows plattform!"); |
|
385 |
#endif |
|
386 |
} |
|
387 |
break; |
|
388 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
389 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
390 |
removeFile(); |
0 | 391 |
} |
392 |
||
393 |
void tst_qfile::open_data() |
|
394 |
{ |
|
395 |
QTest::addColumn<tst_qfile::BenchmarkType>("testType"); |
|
396 |
QTest::newRow("QFile") << QFileBenchmark; |
|
397 |
QTest::newRow("QFSFileEngine") << QFSFileEngineBenchmark; |
|
398 |
QTest::newRow("Posix FILE*") << PosixBenchmark; |
|
399 |
QTest::newRow("QFile from FILE*") << QFileFromPosixBenchmark; |
|
400 |
#ifdef Q_OS_WIN |
|
401 |
QTest::newRow("Win32 API") << Win32Benchmark; |
|
402 |
#endif |
|
403 |
} |
|
404 |
||
405 |
void tst_qfile::open() |
|
406 |
{ |
|
407 |
QFETCH(tst_qfile::BenchmarkType, testType); |
|
408 |
||
409 |
createFile(); |
|
410 |
||
411 |
switch (testType) { |
|
412 |
case(QFileBenchmark): { |
|
413 |
QBENCHMARK { |
|
414 |
QFile file( filename ); |
|
415 |
file.open( QIODevice::ReadOnly ); |
|
416 |
file.close(); |
|
417 |
} |
|
418 |
} |
|
419 |
break; |
|
420 |
case(QFSFileEngineBenchmark): { |
|
421 |
QBENCHMARK { |
|
422 |
QFSFileEngine fse(filename); |
|
423 |
fse.open(QIODevice::ReadOnly); |
|
424 |
fse.close(); |
|
425 |
} |
|
426 |
} |
|
427 |
break; |
|
428 |
||
429 |
case(PosixBenchmark): { |
|
430 |
// ensure we don't account toLocal8Bit() |
|
431 |
QByteArray data = filename.toLocal8Bit(); |
|
432 |
const char* cfilename = data.constData(); |
|
433 |
||
434 |
QBENCHMARK { |
|
435 |
FILE* cfile = ::fopen(cfilename, "rb"); |
|
436 |
::fclose(cfile); |
|
437 |
} |
|
438 |
} |
|
439 |
break; |
|
440 |
case(QFileFromPosixBenchmark): { |
|
441 |
// ensure we don't account toLocal8Bit() |
|
442 |
QByteArray data = filename.toLocal8Bit(); |
|
443 |
const char* cfilename = data.constData(); |
|
444 |
FILE* cfile = ::fopen(cfilename, "rb"); |
|
445 |
||
446 |
QBENCHMARK { |
|
447 |
QFile file; |
|
448 |
file.open(cfile, QIODevice::ReadOnly); |
|
449 |
file.close(); |
|
450 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
451 |
::fclose(cfile); |
0 | 452 |
} |
453 |
break; |
|
454 |
case(Win32Benchmark): { |
|
455 |
#ifdef Q_OS_WIN |
|
456 |
HANDLE hndl; |
|
457 |
||
458 |
// ensure we don't account string conversion |
|
459 |
wchar_t* cfilename = (wchar_t*)filename.utf16(); |
|
460 |
||
461 |
QBENCHMARK { |
|
462 |
hndl = CreateFile(cfilename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); |
|
463 |
Q_ASSERT(hndl); |
|
464 |
CloseHandle(hndl); |
|
465 |
} |
|
466 |
#else |
|
467 |
QFAIL("Not running on a non-Windows platform!"); |
|
468 |
#endif |
|
469 |
} |
|
470 |
break; |
|
471 |
} |
|
472 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
473 |
removeFile(); |
0 | 474 |
} |
475 |
||
476 |
||
477 |
void tst_qfile::readSmallFiles_QFile() { readSmallFiles(); } |
|
478 |
void tst_qfile::readSmallFiles_QFSFileEngine() { readSmallFiles(); } |
|
479 |
void tst_qfile::readSmallFiles_posix() { readSmallFiles(); } |
|
480 |
void tst_qfile::readSmallFiles_Win32() { readSmallFiles(); } |
|
481 |
||
482 |
void tst_qfile::readSmallFiles_QFile_data() |
|
483 |
{ |
|
484 |
readSmallFiles_data(QFileBenchmark, QIODevice::NotOpen, QIODevice::NotOpen); |
|
485 |
readSmallFiles_data(QFileBenchmark, QIODevice::NotOpen, QIODevice::Unbuffered); |
|
486 |
readSmallFiles_data(QFileBenchmark, QIODevice::Text, QIODevice::NotOpen); |
|
487 |
readSmallFiles_data(QFileBenchmark, QIODevice::Text, QIODevice::Unbuffered); |
|
488 |
||
489 |
} |
|
490 |
||
491 |
void tst_qfile::readSmallFiles_QFSFileEngine_data() |
|
492 |
{ |
|
493 |
readSmallFiles_data(QFSFileEngineBenchmark, QIODevice::NotOpen, QIODevice::NotOpen); |
|
494 |
readSmallFiles_data(QFSFileEngineBenchmark, QIODevice::NotOpen, QIODevice::Unbuffered); |
|
495 |
readSmallFiles_data(QFSFileEngineBenchmark, QIODevice::Text, QIODevice::NotOpen); |
|
496 |
readSmallFiles_data(QFSFileEngineBenchmark, QIODevice::Text, QIODevice::Unbuffered); |
|
497 |
} |
|
498 |
||
499 |
void tst_qfile::readSmallFiles_posix_data() |
|
500 |
{ |
|
501 |
readSmallFiles_data(PosixBenchmark, QIODevice::NotOpen, QIODevice::NotOpen); |
|
502 |
} |
|
503 |
||
504 |
void tst_qfile::readSmallFiles_Win32_data() |
|
505 |
{ |
|
506 |
readSmallFiles_data(Win32Benchmark, QIODevice::NotOpen, QIODevice::NotOpen); |
|
507 |
} |
|
508 |
||
509 |
||
510 |
void tst_qfile::readSmallFiles_data(BenchmarkType type, QIODevice::OpenModeFlag t, QIODevice::OpenModeFlag b) |
|
511 |
{ |
|
512 |
QTest::addColumn<tst_qfile::BenchmarkType>("testType"); |
|
513 |
QTest::addColumn<int>("blockSize"); |
|
514 |
QTest::addColumn<QFile::OpenModeFlag>("textMode"); |
|
515 |
QTest::addColumn<QFile::OpenModeFlag>("bufferedMode"); |
|
516 |
||
517 |
const int bs[] = {1024, 1024*2, 1024*8, 1024*16, 1024*32,1024*512}; |
|
518 |
int bs_entries = sizeof(bs)/sizeof(const int); |
|
519 |
||
520 |
QString flagstring; |
|
521 |
if (t & QIODevice::Text) flagstring += "textMode "; |
|
522 |
if (b & QIODevice::Unbuffered) flagstring += "unbuffered "; |
|
523 |
if (flagstring.isEmpty()) flagstring = "none"; |
|
524 |
||
525 |
for (int i=0; i<bs_entries; ++i) |
|
526 |
QTest::newRow((QString("BS: %1, Flags: %2" )).arg(bs[i]).arg(flagstring).toLatin1().constData()) << type << bs[i] << t << b; |
|
527 |
||
528 |
} |
|
529 |
||
530 |
void tst_qfile::createSmallFiles() |
|
531 |
{ |
|
532 |
QDir dir = QDir::temp(); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
533 |
dir.mkdir("tst"); |
0 | 534 |
dir.cd("tst"); |
535 |
tmpDirName = dir.absolutePath(); |
|
536 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
537 |
#ifdef Q_OS_SYMBIAN |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
538 |
for (int i = 0; i < 100; ++i) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
539 |
#else |
0 | 540 |
for (int i = 0; i < 1000; ++i) |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
541 |
#endif |
0 | 542 |
{ |
543 |
QFile f(tmpDirName+"/"+QString::number(i)); |
|
544 |
f.open(QIODevice::WriteOnly); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
545 |
f.seek(511); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
546 |
f.putChar('\n'); |
0 | 547 |
f.close(); |
548 |
} |
|
549 |
} |
|
550 |
||
551 |
void tst_qfile::removeSmallFiles() |
|
552 |
{ |
|
553 |
QDirIterator it(tmpDirName, QDirIterator::FollowSymlinks); |
|
554 |
while (it.hasNext()) |
|
555 |
QFile::remove(it.next()); |
|
556 |
QDir::temp().rmdir("tst"); |
|
557 |
} |
|
558 |
||
559 |
||
560 |
void tst_qfile::readSmallFiles() |
|
561 |
{ |
|
562 |
QFETCH(tst_qfile::BenchmarkType, testType); |
|
563 |
QFETCH(int, blockSize); |
|
564 |
QFETCH(QFile::OpenModeFlag, textMode); |
|
565 |
QFETCH(QFile::OpenModeFlag, bufferedMode); |
|
566 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
567 |
#ifndef Q_OS_WIN |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
568 |
if (testType == Win32Benchmark) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
569 |
QSKIP("This is Windows only benchmark.", SkipSingle); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
570 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
571 |
|
0 | 572 |
createSmallFiles(); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
573 |
|
0 | 574 |
QDir dir(tmpDirName); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
575 |
const QStringList files = dir.entryList(QDir::NoDotAndDotDot|QDir::NoSymLinks|QDir::Files); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
576 |
char *buffer = new char[BUFSIZE]; |
0 | 577 |
|
578 |
switch (testType) { |
|
579 |
case(QFileBenchmark): { |
|
580 |
QList<QFile*> fileList; |
|
581 |
Q_FOREACH(QString file, files) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
582 |
QFile *f = new QFile(tmpDirName+ "/" + file); |
0 | 583 |
f->open(QIODevice::ReadOnly|textMode|bufferedMode); |
584 |
fileList.append(f); |
|
585 |
} |
|
586 |
||
587 |
QBENCHMARK { |
|
588 |
Q_FOREACH(QFile *file, fileList) { |
|
589 |
while (!file->atEnd()) { |
|
590 |
file->read(buffer, blockSize); |
|
591 |
} |
|
592 |
} |
|
593 |
} |
|
594 |
||
595 |
Q_FOREACH(QFile *file, fileList) { |
|
596 |
file->close(); |
|
597 |
delete file; |
|
598 |
} |
|
599 |
} |
|
600 |
break; |
|
601 |
case(QFSFileEngineBenchmark): { |
|
602 |
QList<QFSFileEngine*> fileList; |
|
603 |
Q_FOREACH(QString file, files) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
604 |
QFSFileEngine *fse = new QFSFileEngine(tmpDirName+ "/" + file); |
0 | 605 |
fse->open(QIODevice::ReadOnly|textMode|bufferedMode); |
606 |
fileList.append(fse); |
|
607 |
} |
|
608 |
||
609 |
QBENCHMARK { |
|
610 |
Q_FOREACH(QFSFileEngine *fse, fileList) { |
|
611 |
while (fse->read(buffer, blockSize)); |
|
612 |
} |
|
613 |
} |
|
614 |
||
615 |
Q_FOREACH(QFSFileEngine *fse, fileList) { |
|
616 |
fse->close(); |
|
617 |
delete fse; |
|
618 |
} |
|
619 |
} |
|
620 |
break; |
|
621 |
case(PosixBenchmark): { |
|
622 |
QList<FILE*> fileList; |
|
623 |
Q_FOREACH(QString file, files) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
624 |
fileList.append(::fopen(QFile::encodeName(tmpDirName+ "/" + file).constData(), "rb")); |
0 | 625 |
} |
626 |
||
627 |
QBENCHMARK { |
|
628 |
Q_FOREACH(FILE* cfile, fileList) { |
|
629 |
while(!feof(cfile)) |
|
630 |
::fread(buffer, blockSize, 1, cfile); |
|
631 |
::fseek(cfile, 0, SEEK_SET); |
|
632 |
} |
|
633 |
} |
|
634 |
||
635 |
Q_FOREACH(FILE* cfile, fileList) { |
|
636 |
::fclose(cfile); |
|
637 |
} |
|
638 |
} |
|
639 |
break; |
|
640 |
case(QFileFromPosixBenchmark): { |
|
641 |
// No gain in benchmarking this case |
|
642 |
} |
|
643 |
break; |
|
644 |
case(Win32Benchmark): { |
|
645 |
#ifdef Q_OS_WIN |
|
646 |
HANDLE hndl; |
|
647 |
||
648 |
// ensure we don't account string conversion |
|
649 |
wchar_t* cfilename = (wchar_t*)filename.utf16(); |
|
650 |
||
651 |
hndl = CreateFile(cfilename, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); |
|
652 |
Q_ASSERT(hndl); |
|
653 |
wchar_t* nativeBuffer = new wchar_t[BUFSIZE]; |
|
654 |
DWORD numberOfBytesRead; |
|
655 |
QBENCHMARK { |
|
656 |
do { |
|
657 |
ReadFile(hndl, nativeBuffer, blockSize, &numberOfBytesRead, NULL); |
|
658 |
} while(numberOfBytesRead != 0); |
|
659 |
} |
|
660 |
delete nativeBuffer; |
|
661 |
CloseHandle(hndl); |
|
662 |
#else |
|
663 |
QFAIL("Not running on a non-Windows platform!"); |
|
664 |
#endif |
|
665 |
} |
|
666 |
break; |
|
667 |
} |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
668 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
669 |
removeSmallFiles(); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
670 |
delete[] buffer; |
0 | 671 |
} |
672 |
||
673 |
QTEST_MAIN(tst_qfile) |
|
674 |
||
675 |
#include "main.moc" |