// This function simulates a partially or fully occupied disk cache
// like a normal user of a cache might encounter is real-life browsing.
// The point of this is to trigger degradation in file-system and media performance
// that occur due to the quantity and layout of data.
void tst_qnetworkdiskcache::injectFakeData()
{

    QNetworkCacheMetaData::RawHeaderList headers;
    headers.append(qMakePair(QByteArray("X-TestHeader"),QByteArray("HeaderValue")));


    //Prep cache dir with fake data using QNetworkDiskCache APIs
    for (quint32 i = 0; i < NumFakeCacheObjects; i++) {

        //prepare metata for url
        QNetworkCacheMetaData meta;
        QString fakeURL;
        QTextStream stream(&fakeURL);
        stream << fakeURLbase << i;
        QUrl url(fakeURL);
        meta.setUrl(url);
        meta.setRawHeaders(headers);
        meta.setSaveToDisk(true);

        //commit payload and metadata to disk
        QIODevice *device = cache->prepare(meta);
        device->write(payload);
        cache->insert(device);
    }

}
void tst_qnetworkdiskcache::timeExpiration()
{

    QFETCH(QString, cacheRootDirectory);

    cacheDir = QString( cacheRootDirectory + QDir::separator() + "man_qndc");
    QDir d;
    qDebug() << "Setting cache directory to = " << d.absoluteFilePath(cacheDir);

    //Housekeeping
    initCacheObject();
    cleanRecursive(cacheDir); // slow op.
    cache->setCacheDirectory(cacheDir);
    // Make max cache size HUGE, so that evictions don't happen below
    cache->setMaximumCacheSize(qint64(HugeCacheLimit));
    cache->clear();

    //populate some fake data to simulate partially full cache
    injectFakeData();

    //Sanity-check that the URL is already in there somewhere
    QVERIFY(isUrlCached(NumRemovals-1) == true);
    //Entries in the cache should be > what we try to remove
    QVERIFY(NumFakeCacheObjects > NumRemovals);


    //Set cache limit lower, so this force 1 round of eviction
    cache->setMaximumCacheSize(qint64(TinyCacheLimit));

    //time insertions of additional content, which is likely to internally cause evictions
    QBENCHMARK_ONCE {
        for (quint32 i = NumFakeCacheObjects; i < (NumFakeCacheObjects + NumInsertions); i++) {
            //prepare metata for url
            QNetworkCacheMetaData meta;
            QString fakeURL;
            QTextStream stream(&fakeURL);
            stream << fakeURLbase << i;//codescanner::leave
            QUrl url(fakeURL);
            meta.setUrl(url);
            meta.setSaveToDisk(true);

            //commit payload and metadata to disk
            QIODevice *device = cache->prepare(meta);
            device->write(payload);
            cache->insert(device); // this should trigger evictions, if TinyCacheLimit is small enough
        }
    }

    //Cleanup (slow)
    cleanupCacheObject();
    cleanRecursive(cacheDir);

}
 virtual QNetworkCacheMetaData metaData(const QUrl &url)
 {
     QNetworkCacheMetaData metaData;
     if (!cachedData.isEmpty()) {
         metaData.setUrl(url);
         QDateTime now = QDateTime::currentDateTime();
         metaData.setLastModified(now.addDays(-1));
         metaData.setExpirationDate(now.addDays(1));
         metaData.setSaveToDisk(true);
     }
     return metaData;
 }
Esempio n. 4
0
void tst_QNetworkCacheMetaData::operatorEqual_data()
{
    QTest::addColumn<QNetworkCacheMetaData>("other");
    QTest::newRow("null") << QNetworkCacheMetaData();

    QNetworkCacheMetaData data;
    data.setUrl(QUrl(EXAMPLE_URL));
    QNetworkCacheMetaData::RawHeaderList headers;
    headers.append(QNetworkCacheMetaData::RawHeader("foo", "Bar"));
    data.setRawHeaders(headers);
    data.setLastModified(QDateTime::currentDateTime());
    data.setExpirationDate(QDateTime::currentDateTime());
    data.setSaveToDisk(false);
    QTest::newRow("valid") << data;
}
//This functions times an insert() operation.
//You can run it after populating the cache with
//fake data so that more realistic performance
//estimates are obtained.
void tst_qnetworkdiskcache::timeInsertion()
{

    QFETCH(QString, cacheRootDirectory);

    cacheDir = QString( cacheRootDirectory + QDir::separator() + "man_qndc");
    QDir d;
    qDebug() << "Setting cache directory to = " << d.absoluteFilePath(cacheDir);

    //Housekeeping
    cleanRecursive(cacheDir); // slow op.
    initCacheObject();

    cache->setCacheDirectory(cacheDir);
    cache->setMaximumCacheSize(qint64(HugeCacheLimit));
    cache->clear();

    //populate some fake data to simulate partially full cache
    injectFakeData(); // SLOW

    //Sanity-check that the first URL that we insert below isn't already in there.
    QVERIFY(isUrlCached(NumFakeCacheObjects) == false);

    // IMPORTANT: max cache size should be HugeCacheLimit, to avoid evictions below
    //time insertion of previously-uncached URLs.
    QBENCHMARK_ONCE {
        for (quint32 i = NumFakeCacheObjects; i < (NumFakeCacheObjects + NumInsertions); i++) {
            //prepare metata for url
            QNetworkCacheMetaData meta;
            QString fakeURL;
            QTextStream stream(&fakeURL);
            stream << fakeURLbase << i;
            QUrl url(fakeURL);
            meta.setUrl(url);
            meta.setSaveToDisk(true);

            //commit payload and metadata to disk
            QIODevice *device = cache->prepare(meta);
            device->write(payload);
            cache->insert(device);
        }
    }

    //SLOW cleanup
    cleanupCacheObject();
    cleanRecursive(cacheDir);

}
Esempio n. 6
0
void tst_QNetworkCacheMetaData::qnetworkcachemetadata()
{
    QNetworkCacheMetaData data;
    QCOMPARE(data.expirationDate(), QDateTime());
    QCOMPARE(data.isValid(), false);
    QCOMPARE(data.lastModified(), QDateTime());
    QCOMPARE(data.operator!=(QNetworkCacheMetaData()), false);
    QNetworkCacheMetaData metaData;
    QCOMPARE(data.operator=(metaData), QNetworkCacheMetaData());
    QCOMPARE(data.operator==(QNetworkCacheMetaData()), true);
    QCOMPARE(data.rawHeaders(), QNetworkCacheMetaData::RawHeaderList());
    QCOMPARE(data.saveToDisk(), true);
    QCOMPARE(data.url(), QUrl());
    data.setExpirationDate(QDateTime());
    data.setLastModified(QDateTime());
    data.setRawHeaders(QNetworkCacheMetaData::RawHeaderList());
    data.setSaveToDisk(false);
    data.setUrl(QUrl());
}
Esempio n. 7
0
void tst_QNetworkCacheMetaData::stream()
{
    QNetworkCacheMetaData data;
    data.setUrl(QUrl(EXAMPLE_URL));
    QNetworkCacheMetaData::RawHeaderList headers;
    headers.append(QNetworkCacheMetaData::RawHeader("foo", "Bar"));
    data.setRawHeaders(headers);
    data.setLastModified(QDateTime::currentDateTime());
    data.setExpirationDate(QDateTime::currentDateTime());
    data.setSaveToDisk(false);

    QBuffer buffer;
    buffer.open(QIODevice::ReadWrite);
    QDataStream stream(&buffer);
    stream << data;

    buffer.seek(0);
    QNetworkCacheMetaData data2;
    stream >> data2;
    QCOMPARE(data2, data);
}
Esempio n. 8
0
bool saveToCache(const QUrl& url, const QByteArray& file) {
    if (auto cache = NetworkAccessManager::getInstance().cache()) {
        if (!cache->metaData(url).isValid()) {
            QNetworkCacheMetaData metaData;
            metaData.setUrl(url);
            metaData.setSaveToDisk(true);
            metaData.setLastModified(QDateTime::currentDateTime());
            metaData.setExpirationDate(QDateTime()); // Never expires

            // ioDevice is managed by the cache and should either be passed back to insert or remove!
            if (auto ioDevice = cache->prepare(metaData)) {
                ioDevice->write(file);
                cache->insert(ioDevice);
                qCDebug(asset_client) << url.toDisplayString() << "saved to disk cache";
                return true;
            }
            qCWarning(asset_client) << "Could not save" << url.toDisplayString() << "to disk cache.";
        }
    } else {
        qCWarning(asset_client) << "No disk cache to save assets to.";
    }
    return false;
}