Speed up sorting of dir entries when sorted by date
[profile/ivi/qtbase.git] / tests / benchmarks / corelib / io / qdir / 10000 / bench_qdir_10000.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtTest/QtTest>
43
44 #ifdef Q_OS_WIN
45 #   include <windows.h>
46 #else
47 #   include <sys/stat.h>
48 #   include <sys/types.h>
49 #   include <dirent.h>
50 #   include <unistd.h>
51 #endif
52
53 class bench_QDir_10000 : public QObject{
54   Q_OBJECT
55 public slots:
56     void initTestCase() {
57         QDir testdir = QDir::tempPath();
58
59         const QString subfolder_name = QLatin1String("test_speed");
60         QVERIFY(testdir.mkdir(subfolder_name));
61         QVERIFY(testdir.cd(subfolder_name));
62
63         for (uint i=0; i<10000; ++i) {
64             QFile file(testdir.absolutePath() + "/testfile_" + QString::number(i));
65             file.open(QIODevice::WriteOnly);
66         }
67     }
68     void cleanupTestCase() {
69         {
70             QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
71             testdir.setSorting(QDir::Unsorted);
72             testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
73             foreach (const QString &filename, testdir.entryList()) {
74                 testdir.remove(filename);
75             }
76         }
77         const QDir temp = QDir(QDir::tempPath());
78         temp.rmdir(QLatin1String("test_speed"));
79     }
80 private slots:
81     void baseline() {}
82
83     void sizeSpeed() {
84         QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
85         QBENCHMARK {
86             QFileInfoList fileInfoList = testdir.entryInfoList(QDir::Files, QDir::Unsorted);
87             foreach (const QFileInfo &fileInfo, fileInfoList) {
88                 fileInfo.isDir();
89                 fileInfo.size();
90             }
91         }
92     }
93     void sizeSpeedIterator() {
94         QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
95         QBENCHMARK {
96             QDirIterator dit(testdir.path(), QDir::Files);
97             while (dit.hasNext()) {
98                 dit.next();
99                 dit.fileInfo().isDir();
100                 dit.fileInfo().size();
101             }
102         }
103     }
104
105     void sizeSpeedWithoutFilter() {
106         QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
107         QBENCHMARK {
108             QFileInfoList fileInfoList = testdir.entryInfoList(QDir::NoFilter, QDir::Unsorted);
109             foreach (const QFileInfo &fileInfo, fileInfoList) {
110                 fileInfo.size();
111             }
112         }
113     }
114     void sizeSpeedWithoutFilterIterator() {
115         QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
116         QBENCHMARK {
117             QDirIterator dit(testdir.path());
118             while (dit.hasNext()) {
119                 dit.next();
120                 dit.fileInfo().isDir();
121                 dit.fileInfo().size();
122             }
123         }
124     }
125
126     void sizeSpeedWithoutFileInfoList() {
127         QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
128         testdir.setSorting(QDir::Unsorted);
129         QBENCHMARK {
130             QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Unsorted);
131             foreach (const QString &filename, fileList) {
132                 QFileInfo fileInfo(filename);
133                 fileInfo.size();
134             }
135         }
136     }
137
138     void iDontWantAnyStat() {
139         QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
140         testdir.setSorting(QDir::Unsorted);
141         testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
142         QBENCHMARK {
143             QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Unsorted);
144             foreach (const QString &filename, fileList) {
145
146             }
147         }
148     }
149     void iDontWantAnyStatIterator() {
150         QBENCHMARK {
151             QDirIterator dit(QDir::tempPath() + QLatin1String("/test_speed"));
152             while (dit.hasNext()) {
153                 dit.next();
154             }
155         }
156     }
157
158     void sorted_byTime() {
159         QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
160         testdir.setSorting(QDir::Time);
161         testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden);
162         QBENCHMARK {
163             QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Time);
164             foreach (const QString &filename, fileList) {
165
166             }
167         }
168     }
169
170     void sizeSpeedWithoutFilterLowLevel() {
171         QDir testdir(QDir::tempPath() + QLatin1String("/test_speed"));
172 #ifdef Q_OS_WIN
173         const wchar_t *dirpath = (wchar_t*)testdir.absolutePath().utf16();
174         wchar_t appendedPath[MAX_PATH];
175         wcscpy(appendedPath, dirpath);
176         wcscat(appendedPath, L"\\*");
177
178         WIN32_FIND_DATA fd;
179         HANDLE hSearch = FindFirstFileW(appendedPath, &fd);
180         QVERIFY(hSearch != INVALID_HANDLE_VALUE);
181
182         QBENCHMARK {
183             do {
184
185             } while (FindNextFile(hSearch, &fd));
186         }
187         FindClose(hSearch);
188 #else
189         DIR *dir = opendir(qPrintable(testdir.absolutePath()));
190         QVERIFY(dir);
191
192         QVERIFY(!chdir(qPrintable(testdir.absolutePath())));
193         QBENCHMARK {
194             struct dirent *item = readdir(dir);
195             while (item) {
196                 char *fileName = item->d_name;
197
198                 struct stat fileStat;
199                 QVERIFY(!stat(fileName, &fileStat));
200
201                 item = readdir(dir);
202             }
203         }
204         closedir(dir);
205 #endif
206     }
207 };
208
209 QTEST_MAIN(bench_QDir_10000)
210 #include "bench_qdir_10000.moc"