~ubuntu-branches/ubuntu/wily/qtbase-opensource-src/wily

« back to all changes in this revision

Viewing changes to tests/benchmarks/network/access/qnetworkdiskcache/tst_qnetworkdiskcache.cpp

  • Committer: Package Import Robot
  • Author(s): Timo Jyrinki
  • Date: 2013-02-05 12:46:17 UTC
  • Revision ID: package-import@ubuntu.com-20130205124617-c8jouts182j002fx
Tags: upstream-5.0.1+dfsg
ImportĀ upstreamĀ versionĀ 5.0.1+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
 
4
** Contact: http://www.qt-project.org/legal
 
5
**
 
6
** This file is part of the test suite of the Qt Toolkit.
 
7
**
 
8
** $QT_BEGIN_LICENSE:LGPL$
 
9
** Commercial License Usage
 
10
** Licensees holding valid commercial Qt licenses may use this file in
 
11
** accordance with the commercial license agreement provided with the
 
12
** Software or, alternatively, in accordance with the terms contained in
 
13
** a written agreement between you and Digia.  For licensing terms and
 
14
** conditions see http://qt.digia.com/licensing.  For further information
 
15
** use the contact form at http://qt.digia.com/contact-us.
 
16
**
 
17
** GNU Lesser General Public License Usage
 
18
** Alternatively, this file may be used under the terms of the GNU Lesser
 
19
** General Public License version 2.1 as published by the Free Software
 
20
** Foundation and appearing in the file LICENSE.LGPL included in the
 
21
** packaging of this file.  Please review the following information to
 
22
** ensure the GNU Lesser General Public License version 2.1 requirements
 
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 
24
**
 
25
** In addition, as a special exception, Digia gives you certain additional
 
26
** rights.  These rights are described in the Digia Qt LGPL Exception
 
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 
28
**
 
29
** GNU General Public License Usage
 
30
** Alternatively, this file may be used under the terms of the GNU
 
31
** General Public License version 3.0 as published by the Free Software
 
32
** Foundation and appearing in the file LICENSE.GPL included in the
 
33
** packaging of this file.  Please review the following information to
 
34
** ensure the GNU General Public License version 3.0 requirements will be
 
35
** met: http://www.gnu.org/copyleft/gpl.html.
 
36
**
 
37
**
 
38
** $QT_END_LICENSE$
 
39
**
 
40
****************************************************************************/
 
41
 
 
42
#include <QNetworkDiskCache>
 
43
#include <QNetworkCacheMetaData>
 
44
#include <QDir>
 
45
#include <QBuffer>
 
46
#include <QTextStream>
 
47
#include <QDebug>
 
48
#include <QtTest/QtTest>
 
49
#include <QIODevice>
 
50
#include <QStandardPaths>
 
51
 
 
52
 
 
53
 
 
54
enum Numbers { NumFakeCacheObjects   = 200,    //entries in pre-populated cache
 
55
               NumInsertions  = 100,           //insertions to be timed
 
56
               NumRemovals    = 100,           //removals to be timed
 
57
               NumReadContent = 100,           //meta requests to be timed
 
58
               HugeCacheLimit = 50*1024*1024,  // max size for a big cache
 
59
               TinyCacheLimit = 1*512*1024}; //  max size for a tiny cache
 
60
 
 
61
const QString fakeURLbase = "http://127.0.0.1/fake/";
 
62
//fake HTTP body aka payload
 
63
const QByteArray payload("Qt rocks!");
 
64
 
 
65
class tst_qnetworkdiskcache : public QObject
 
66
{
 
67
    Q_OBJECT
 
68
private:
 
69
    void injectFakeData();
 
70
    void insertOneItem();
 
71
    bool isUrlCached(quint32 id);
 
72
    void cleanRecursive(QString &path);
 
73
    void cleanupCacheObject();
 
74
    void initCacheObject();
 
75
    QString cacheDir;
 
76
    QNetworkDiskCache *cache;
 
77
 
 
78
public slots:
 
79
    void initTestCase();
 
80
    void cleanupTestCase();
 
81
 
 
82
private slots:
 
83
 
 
84
    void timeInsertion_data();
 
85
    void timeInsertion();
 
86
    void timeRead_data();
 
87
    void timeRead();
 
88
    void timeRemoval_data();
 
89
    void timeRemoval();
 
90
 
 
91
    void timeExpiration_data();
 
92
    void timeExpiration();
 
93
};
 
94
 
 
95
 
 
96
void tst_qnetworkdiskcache::initTestCase()
 
97
{
 
98
    cache = 0;
 
99
}
 
100
 
 
101
 
 
102
void tst_qnetworkdiskcache::cleanupTestCase()
 
103
{
 
104
    cleanupCacheObject();
 
105
    cleanRecursive(cacheDir);
 
106
}
 
107
 
 
108
void tst_qnetworkdiskcache::timeInsertion_data()
 
109
{
 
110
    QTest::addColumn<QString>("cacheRootDirectory");
 
111
 
 
112
    QString cacheLoc = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
 
113
    QTest::newRow("QStandardPaths Cache Location") << cacheLoc;
 
114
}
 
115
 
 
116
//This functions times an insert() operation.
 
117
//You can run it after populating the cache with
 
118
//fake data so that more realistic performance
 
119
//estimates are obtained.
 
120
void tst_qnetworkdiskcache::timeInsertion()
 
121
{
 
122
 
 
123
    QFETCH(QString, cacheRootDirectory);
 
124
 
 
125
    cacheDir = QString( cacheRootDirectory + QDir::separator() + "man_qndc");
 
126
    QDir d;
 
127
    qDebug() << "Setting cache directory to = " << d.absoluteFilePath(cacheDir);
 
128
 
 
129
    //Housekeeping
 
130
    cleanRecursive(cacheDir); // slow op.
 
131
    initCacheObject();
 
132
 
 
133
    cache->setCacheDirectory(cacheDir);
 
134
    cache->setMaximumCacheSize(qint64(HugeCacheLimit));
 
135
    cache->clear();
 
136
 
 
137
    //populate some fake data to simulate partially full cache
 
138
    injectFakeData(); // SLOW
 
139
 
 
140
    //Sanity-check that the first URL that we insert below isn't already in there.
 
141
    QVERIFY(isUrlCached(NumFakeCacheObjects) == false);
 
142
 
 
143
    // IMPORTANT: max cache size should be HugeCacheLimit, to avoid evictions below
 
144
    //time insertion of previously-uncached URLs.
 
145
    QBENCHMARK_ONCE {
 
146
        for (quint32 i = NumFakeCacheObjects; i < (NumFakeCacheObjects + NumInsertions); i++) {
 
147
            //prepare metata for url
 
148
            QNetworkCacheMetaData meta;
 
149
            QString fakeURL;
 
150
            QTextStream stream(&fakeURL);
 
151
            stream << fakeURLbase << i;
 
152
            QUrl url(fakeURL);
 
153
            meta.setUrl(url);
 
154
            meta.setSaveToDisk(true);
 
155
 
 
156
            //commit payload and metadata to disk
 
157
            QIODevice *device = cache->prepare(meta);
 
158
            device->write(payload);
 
159
            cache->insert(device);
 
160
        }
 
161
    }
 
162
 
 
163
    //SLOW cleanup
 
164
    cleanupCacheObject();
 
165
    cleanRecursive(cacheDir);
 
166
 
 
167
}
 
168
 
 
169
void tst_qnetworkdiskcache::timeRead_data()
 
170
{
 
171
    QTest::addColumn<QString>("cacheRootDirectory");
 
172
 
 
173
    QString cacheLoc = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
 
174
    QTest::newRow("QStandardPaths Cache Location") << cacheLoc;
 
175
}
 
176
 
 
177
//Times metadata as well payload lookup
 
178
// i.e metaData(), rawHeaders() and data()
 
179
void tst_qnetworkdiskcache::timeRead()
 
180
{
 
181
 
 
182
    QFETCH(QString, cacheRootDirectory);
 
183
 
 
184
    cacheDir = QString( cacheRootDirectory + QDir::separator() + "man_qndc");
 
185
    QDir d;
 
186
    qDebug() << "Setting cache directory to = " << d.absoluteFilePath(cacheDir);
 
187
 
 
188
    //Housekeeping
 
189
    cleanRecursive(cacheDir); // slow op.
 
190
    initCacheObject();
 
191
    cache->setCacheDirectory(cacheDir);
 
192
    cache->setMaximumCacheSize(qint64(HugeCacheLimit));
 
193
    cache->clear();
 
194
 
 
195
    //populate some fake data to simulate partially full cache
 
196
    injectFakeData();
 
197
 
 
198
    //Entries in the cache should be > what we try to remove
 
199
    QVERIFY(NumFakeCacheObjects > NumReadContent);
 
200
 
 
201
    //time metadata lookup of previously inserted URL.
 
202
    QBENCHMARK_ONCE {
 
203
        for (quint32 i = 0; i < NumReadContent; i++) {
 
204
            QString fakeURL;
 
205
            QTextStream stream(&fakeURL);
 
206
            stream << fakeURLbase << i;
 
207
            QUrl url(fakeURL);
 
208
 
 
209
            QNetworkCacheMetaData qndc = cache->metaData(url);
 
210
            QVERIFY(qndc.isValid()); // we must have read the metadata
 
211
 
 
212
            QNetworkCacheMetaData::RawHeaderList raw(qndc.rawHeaders());
 
213
            QVERIFY(raw.size()); // we must have parsed the headers from the meta
 
214
 
 
215
            QIODevice *iodevice(cache->data(url));
 
216
            QVERIFY(iodevice);    //must not be NULL
 
217
            iodevice->close();
 
218
            delete iodevice;
 
219
        }
 
220
    }
 
221
 
 
222
    //Cleanup (slow)
 
223
    cleanupCacheObject();
 
224
    cleanRecursive(cacheDir);
 
225
 
 
226
}
 
227
 
 
228
void tst_qnetworkdiskcache::timeRemoval_data()
 
229
{
 
230
    QTest::addColumn<QString>("cacheRootDirectory");
 
231
 
 
232
    QString cacheLoc = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
 
233
    QTest::newRow("QStandardPaths Cache Location") << cacheLoc;
 
234
}
 
235
 
 
236
void tst_qnetworkdiskcache::timeRemoval()
 
237
{
 
238
 
 
239
    QFETCH(QString, cacheRootDirectory);
 
240
 
 
241
    cacheDir = QString( cacheRootDirectory + QDir::separator() + "man_qndc");
 
242
    QDir d;
 
243
    qDebug() << "Setting cache directory to = " << d.absoluteFilePath(cacheDir);
 
244
 
 
245
    //Housekeeping
 
246
    initCacheObject();
 
247
    cleanRecursive(cacheDir); // slow op.
 
248
    cache->setCacheDirectory(cacheDir);
 
249
    // Make max cache size HUGE, so that evictions don't happen below
 
250
    cache->setMaximumCacheSize(qint64(HugeCacheLimit));
 
251
    cache->clear();
 
252
 
 
253
    //populate some fake data to simulate partially full cache
 
254
    injectFakeData();
 
255
 
 
256
    //Sanity-check that the URL is already in there somewhere
 
257
    QVERIFY(isUrlCached(NumRemovals-1) == true);
 
258
    //Entries in the cache should be > what we try to remove
 
259
    QVERIFY(NumFakeCacheObjects > NumRemovals);
 
260
 
 
261
    //time removal of previously-inserted URL.
 
262
    QBENCHMARK_ONCE {
 
263
        for (quint32 i = 0; i < NumRemovals; i++) {
 
264
            QString fakeURL;
 
265
            QTextStream stream(&fakeURL);
 
266
            stream << fakeURLbase << i;
 
267
            QUrl url(fakeURL);
 
268
            cache->remove(url);
 
269
        }
 
270
    }
 
271
 
 
272
    //Cleanup (slow)
 
273
    cleanupCacheObject();
 
274
    cleanRecursive(cacheDir);
 
275
 
 
276
}
 
277
 
 
278
void tst_qnetworkdiskcache::timeExpiration_data()
 
279
{
 
280
    QTest::addColumn<QString>("cacheRootDirectory");
 
281
 
 
282
    QString cacheLoc = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
 
283
    QTest::newRow("QStandardPaths Cache Location") << cacheLoc;
 
284
}
 
285
 
 
286
void tst_qnetworkdiskcache::timeExpiration()
 
287
{
 
288
 
 
289
    QFETCH(QString, cacheRootDirectory);
 
290
 
 
291
    cacheDir = QString( cacheRootDirectory + QDir::separator() + "man_qndc");
 
292
    QDir d;
 
293
    qDebug() << "Setting cache directory to = " << d.absoluteFilePath(cacheDir);
 
294
 
 
295
    //Housekeeping
 
296
    initCacheObject();
 
297
    cleanRecursive(cacheDir); // slow op.
 
298
    cache->setCacheDirectory(cacheDir);
 
299
    // Make max cache size HUGE, so that evictions don't happen below
 
300
    cache->setMaximumCacheSize(qint64(HugeCacheLimit));
 
301
    cache->clear();
 
302
 
 
303
    //populate some fake data to simulate partially full cache
 
304
    injectFakeData();
 
305
 
 
306
    //Sanity-check that the URL is already in there somewhere
 
307
    QVERIFY(isUrlCached(NumRemovals-1) == true);
 
308
    //Entries in the cache should be > what we try to remove
 
309
    QVERIFY(NumFakeCacheObjects > NumRemovals);
 
310
 
 
311
 
 
312
    //Set cache limit lower, so this force 1 round of eviction
 
313
    cache->setMaximumCacheSize(qint64(TinyCacheLimit));
 
314
 
 
315
    //time insertions of additional content, which is likely to internally cause evictions
 
316
    QBENCHMARK_ONCE {
 
317
        for (quint32 i = NumFakeCacheObjects; i < (NumFakeCacheObjects + NumInsertions); i++) {
 
318
            //prepare metata for url
 
319
            QNetworkCacheMetaData meta;
 
320
            QString fakeURL;
 
321
            QTextStream stream(&fakeURL);
 
322
            stream << fakeURLbase << i;//codescanner::leave
 
323
            QUrl url(fakeURL);
 
324
            meta.setUrl(url);
 
325
            meta.setSaveToDisk(true);
 
326
 
 
327
            //commit payload and metadata to disk
 
328
            QIODevice *device = cache->prepare(meta);
 
329
            device->write(payload);
 
330
            cache->insert(device); // this should trigger evictions, if TinyCacheLimit is small enough
 
331
        }
 
332
    }
 
333
 
 
334
    //Cleanup (slow)
 
335
    cleanupCacheObject();
 
336
    cleanRecursive(cacheDir);
 
337
 
 
338
}
 
339
// This function simulates a partially or fully occupied disk cache
 
340
// like a normal user of a cache might encounter is real-life browsing.
 
341
// The point of this is to trigger degradation in file-system and media performance
 
342
// that occur due to the quantity and layout of data.
 
343
void tst_qnetworkdiskcache::injectFakeData()
 
344
{
 
345
 
 
346
    QNetworkCacheMetaData::RawHeaderList headers;
 
347
    headers.append(qMakePair(QByteArray("X-TestHeader"),QByteArray("HeaderValue")));
 
348
 
 
349
 
 
350
    //Prep cache dir with fake data using QNetworkDiskCache APIs
 
351
    for (quint32 i = 0; i < NumFakeCacheObjects; i++) {
 
352
 
 
353
        //prepare metata for url
 
354
        QNetworkCacheMetaData meta;
 
355
        QString fakeURL;
 
356
        QTextStream stream(&fakeURL);
 
357
        stream << fakeURLbase << i;
 
358
        QUrl url(fakeURL);
 
359
        meta.setUrl(url);
 
360
        meta.setRawHeaders(headers);
 
361
        meta.setSaveToDisk(true);
 
362
 
 
363
        //commit payload and metadata to disk
 
364
        QIODevice *device = cache->prepare(meta);
 
365
        device->write(payload);
 
366
        cache->insert(device);
 
367
    }
 
368
 
 
369
}
 
370
 
 
371
 
 
372
// Checks if the fake URL #id is already cached or not.
 
373
bool tst_qnetworkdiskcache::isUrlCached(quint32 id)
 
374
{
 
375
    QString str;
 
376
    QTextStream stream(&str);
 
377
    stream << fakeURLbase << id;
 
378
    QUrl url(str);
 
379
    QIODevice *iod = cache->data(url);
 
380
    return ((iod == 0) ? false : true) ;
 
381
 
 
382
}
 
383
 
 
384
 
 
385
// Utility function for recursive directory cleanup.
 
386
void tst_qnetworkdiskcache::cleanRecursive(QString &path)
 
387
{
 
388
    QDirIterator it(path, QDir::Files | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
 
389
    while (it.hasNext()) {
 
390
        QFile f(it.next());
 
391
        bool err = f.remove();
 
392
        Q_UNUSED(err);
 
393
    }
 
394
 
 
395
    QDirIterator it2(path, QDir::AllDirs | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
 
396
    while (it2.hasNext()) {
 
397
        QString s(it2.next());
 
398
        QDir dir(s);
 
399
        dir.rmdir(s);
 
400
    }
 
401
}
 
402
 
 
403
void tst_qnetworkdiskcache::cleanupCacheObject()
 
404
{
 
405
    delete cache;
 
406
    cache = 0;
 
407
}
 
408
 
 
409
void tst_qnetworkdiskcache::initCacheObject()
 
410
{
 
411
 
 
412
   cache = new QNetworkDiskCache();
 
413
 
 
414
}
 
415
QTEST_MAIN(tst_qnetworkdiskcache)
 
416
#include "tst_qnetworkdiskcache.moc"