0

I'm writing small programm like ab to test my small rest web service. My app can produce about 200-300 request per second, but ab could about 8k-9k (on the same machine). After small investigation I found the issue - if I use empty QByteArray it works fast. Here is code:

// simplesender.h
#ifndef SIMPLESENDER_H
#define SIMPLESENDER_H
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QQueue>

#include <QObject>

class SimpleSender : public QObject
{
    Q_OBJECT
public:
    explicit SimpleSender(QObject *parent = 0)
    {
        connect(&qnam, &QNetworkAccessManager::finished, this, &SimpleSender::slFinished);
        url = QUrl("http://localhost:8080");
        m_in_progress = 0;
        sendHttpRequest();
    }

private slots:
    void slFinished(QNetworkReply *reply)
    {
        QByteArray b = reply->readAll();
        Q_UNUSED(b);
        m_in_progress--;
        sendHttpRequest();
        reply->deleteLater();
    }

private:
    int m_in_progress;
    QUrl url;

    QNetworkAccessManager qnam;
    void sendHttpRequest()
    {
        while (m_in_progress < 10) {
            QNetworkRequest r(url);
            QByteArray bb = QByteArray("t"); // with this line 200 r/s
            //QByteArray bb; // with this line 8k r/s
            qnam.put(r, bb);

            m_in_progress++;
        }
    }

};

#endif // SIMPLESENDER_H

in main.cpp

#include <simplesender.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    SimpleSender hs(&a);
    return a.exec();
}

My question is how to improve performance.

KoVadim
  • 707
  • 1
  • 7
  • 22
  • I do not ask for code review. It is small example that show the issue. – KoVadim Aug 26 '16 at 15:10
  • your title says that `QNetworkAccessManager::put` is too slow. Are other request types (`QNetworkAccessManager::get`, `QNetworkAccessManager::post`, ..) faster for you? – Mike Aug 26 '16 at 16:12
  • get is fast enough. put with empty QByteArray also fast. – KoVadim Aug 26 '16 at 16:13
  • Make a debug build of Qt, run it under a profiler, and see why it happens. To improve performance, you likely have to fix the Qt bug that causes this problem. What exact Qt version, compiler version and platform this is? – Kuba hasn't forgotten Monica Aug 26 '16 at 16:19
  • I test with Qt 5.5.1 and 5.2.1, x64, Gentoo and Ubuntu. I profiled it and it looks like my code spend about 5-6%. I will try with debug Qt library. – KoVadim Aug 26 '16 at 16:27
  • See where it's blocking for I/O. Also see if this has anything to do with Qt. You'd expect same behavior when you use e.g. [curl](http://stackoverflow.com/q/13782198/1329652) from the command line and time the same number of executions. – Kuba hasn't forgotten Monica Aug 28 '16 at 00:06

0 Answers0