tests/manual/qnetworkreply/main.cpp
branchRCL_3
changeset 4 3b1da2848fc7
equal deleted inserted replaced
3:41300fa6a67c 4:3b1da2848fc7
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 // This file contains benchmarks for QNetworkReply functions.
       
    42 
       
    43 #include <QDebug>
       
    44 #include <qtest.h>
       
    45 #include <QtTest/QtTest>
       
    46 #include <QtNetwork/qnetworkreply.h>
       
    47 #include <QtNetwork/qnetworkrequest.h>
       
    48 #include <QtNetwork/qnetworkaccessmanager.h>
       
    49 #include "../../auto/network-settings.h"
       
    50 
       
    51 #define BANDWIDTH_LIMIT_BYTES (1024*100)
       
    52 #define TIME_ESTIMATION_SECONDS (97)
       
    53 
       
    54 class tst_qnetworkreply : public QObject
       
    55 {
       
    56     Q_OBJECT
       
    57     private slots:
       
    58     void limiting_data();
       
    59     void limiting();
       
    60 
       
    61 };
       
    62 
       
    63 QNetworkReply *reply;
       
    64 
       
    65 class HttpReceiver : public QObject
       
    66 {
       
    67     Q_OBJECT
       
    68     public slots:
       
    69     void finishedSlot() {
       
    70         quint64 bytesPerSec = (reply->header(QNetworkRequest::ContentLengthHeader).toLongLong()) / (stopwatch.elapsed() / 1000.0);
       
    71         qDebug() << "Finished HTTP(S) request with" << bytesPerSec << "bytes/sec";
       
    72         QVERIFY(bytesPerSec < BANDWIDTH_LIMIT_BYTES*1.05);
       
    73         QVERIFY(bytesPerSec > BANDWIDTH_LIMIT_BYTES*0.95);
       
    74         timer->stop();
       
    75         QTestEventLoop::instance().exitLoop();
       
    76     }
       
    77     void readyReadSlot() {
       
    78     }
       
    79     void timeoutSlot() {
       
    80         reply->read(BANDWIDTH_LIMIT_BYTES).size();
       
    81     }
       
    82     void startTimer() {
       
    83         stopwatch.start();
       
    84         timer = new QTimer(this);
       
    85         QObject::connect(timer, SIGNAL(timeout()), this, SLOT(timeoutSlot()));
       
    86         timer->start(1000);
       
    87     }
       
    88 protected:
       
    89     QTimer *timer;
       
    90     QTime stopwatch;
       
    91 };
       
    92 
       
    93 void tst_qnetworkreply::limiting_data()
       
    94 {
       
    95     QTest::addColumn<QUrl>("url");
       
    96 
       
    97     QTest::newRow("HTTP") << QUrl("http://" + QtNetworkSettings::serverName() + "/mediumfile");
       
    98 #ifndef QT_NO_OPENSSL
       
    99     QTest::newRow("HTTP+SSL") << QUrl("https://" + QtNetworkSettings::serverName() + "/mediumfile");
       
   100 #endif
       
   101 
       
   102 }
       
   103 
       
   104 void tst_qnetworkreply::limiting()
       
   105 {
       
   106     HttpReceiver receiver;
       
   107     QNetworkAccessManager manager;
       
   108 
       
   109     QFETCH(QUrl, url);
       
   110     QNetworkRequest req (url);
       
   111 
       
   112     qDebug() << "Starting. This will take a while (around" << TIME_ESTIMATION_SECONDS << "sec).";
       
   113     qDebug() << "Please check the actual bandwidth usage with a network monitor, e.g. the KDE";
       
   114     qDebug() << "network plasma widget. It should be around" << BANDWIDTH_LIMIT_BYTES << "bytes/sec.";
       
   115     reply = manager.get(req);
       
   116     reply->ignoreSslErrors();
       
   117     reply->setReadBufferSize(BANDWIDTH_LIMIT_BYTES);
       
   118     QObject::connect(reply, SIGNAL(readyRead()), &receiver, SLOT(readyReadSlot()));
       
   119     QObject::connect(reply, SIGNAL(finished()), &receiver, SLOT(finishedSlot()));
       
   120     receiver.startTimer();
       
   121 
       
   122     // event loop
       
   123     QTestEventLoop::instance().enterLoop(TIME_ESTIMATION_SECONDS + 20);
       
   124     QVERIFY(!QTestEventLoop::instance().timeout());
       
   125 }
       
   126 
       
   127 
       
   128 QTEST_MAIN(tst_qnetworkreply)
       
   129 
       
   130 #include "main.moc"