Browse Source

Cutelyst: More picojson usage and a timer instead of a route to update the date (#6729)

Daniel Nicoletti 4 years ago
parent
commit
fcc04d0aaa

+ 2 - 2
frameworks/C++/cutelyst/src/CMakeLists.txt

@@ -5,7 +5,7 @@ project(cutelyst_benchmarks LANGUAGES CXX)
 cmake_policy(SET CMP0069 NEW)
 
 find_package(Qt5 5.6.0 REQUIRED COMPONENTS Core Network Sql)
-find_package(ASqlQt5 0.15.0 REQUIRED)
+find_package(ASqlQt5 0.43.0 REQUIRED)
 find_package(Cutelyst2Qt5 2.12 REQUIRED)
 find_package(Cutelee5 REQUIRED)
 find_package(PostgreSQL REQUIRED)
@@ -18,7 +18,7 @@ set(CMAKE_AUTOMOC ON)
 # to always look for includes there:
 set(CMAKE_INCLUDE_CURRENT_DIR ON)
 
-set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 
 file(GLOB_RECURSE TEMPLATES_SRC root/*)

+ 8 - 3
frameworks/C++/cutelyst/src/cutelyst-benchmarks.cpp

@@ -6,6 +6,7 @@
 #include <QtSql/QSqlDatabase>
 #include <QtSql/QSqlError>
 #include <QCoreApplication>
+#include <QTimer>
 #include <QThread>
 #include <QDebug>
 #include <QMutexLocker>
@@ -13,7 +14,6 @@
 
 #include <apool.h>
 
-#include "root.h"
 #include "jsontest.h"
 #include "singledatabasequerytest.h"
 #include "multipledatabasequeriestest.h"
@@ -38,7 +38,12 @@ bool cutelyst_benchmarks::init()
 {
     if (config(QStringLiteral("SendDate")).value<bool>()) {
         qDebug() << "Manually send date";
-        new Root(this);
+        auto dateT = new QTimer(this);
+        dateT->setInterval(1000);
+        dateT->setSingleShot(false);
+        connect(dateT, &QTimer::timeout, this, [=] {
+            defaultHeaders().setDateWithDateTime(QDateTime::currentDateTimeUtc());
+        });
     }
 
     auto view = new CuteleeView(this);
@@ -92,7 +97,7 @@ bool cutelyst_benchmarks::postFork()
         qDebug() << "ASql URI:" << uri.toString();
 
         APool::addDatabase(uri.toString());
-        APool::setDatabaseMaxIdleConnections(15);
+        APool::setDatabaseMaxIdleConnections(128);
     }
 
     qDebug() << "Connections" << QCoreApplication::applicationPid() << QThread::currentThread() << QSqlDatabase::connectionNames();

+ 14 - 12
frameworks/C++/cutelyst/src/databaseupdatestest.cpp

@@ -14,6 +14,8 @@
 
 #include <QLoggingCategory>
 
+#include "picojson.h"
+
 DatabaseUpdatesTest::DatabaseUpdatesTest(QObject *parent) : Controller(parent)
 {
 
@@ -28,7 +30,7 @@ void DatabaseUpdatesTest::updatep(Context *c)
         queries = 500;
     }
 
-    QJsonArray array;
+    picojson::array array;
     ASync async(c);
     static thread_local auto db = APool::database();
     for (int i = 0; i < queries; ++i) {
@@ -36,10 +38,10 @@ void DatabaseUpdatesTest::updatep(Context *c)
 
         int randomNumber = (qrand() % 10000) + 1;
 
-        array.append(QJsonObject{
-                         {QStringLiteral("id"), id},
-                         {QStringLiteral("randomNumber"), randomNumber}
-                     });
+        array.emplace_back(picojson::object({
+                            {"id", picojson::value(double(id))},
+                            {"randomNumber", picojson::value(double(randomNumber))}
+                        }));
 
         db.exec(APreparedQueryLiteral(u"SELECT randomNumber, id FROM world WHERE id=$1"),
                                {id}, [c, async] (AResult &result) {
@@ -57,7 +59,7 @@ void DatabaseUpdatesTest::updatep(Context *c)
         }, c);
     }
 
-    c->response()->setJsonArrayBody(array);
+    c->response()->setJsonBody(QByteArray::fromStdString(picojson::value(array).serialize()));
 }
 
 void DatabaseUpdatesTest::updateb(Context *c)
@@ -72,7 +74,7 @@ void DatabaseUpdatesTest::updateb(Context *c)
     QVariantList args;
     QVariantList argsIds;
 
-    QJsonArray array;
+    picojson::array array;
     ASync async(c);
     static thread_local auto db = APool::database();
     for (int i = 0; i < queries; ++i) {
@@ -84,10 +86,10 @@ void DatabaseUpdatesTest::updateb(Context *c)
         args.append(id);
         args.append(randomNumber);
 
-        array.append(QJsonObject{
-                         {QStringLiteral("id"), id},
-                         {QStringLiteral("randomNumber"), randomNumber}
-                     });
+        array.emplace_back(picojson::object({
+                            {"id", picojson::value(double(id))},
+                            {"randomNumber", picojson::value(double(randomNumber))}
+                        }));
 
         db.exec(APreparedQueryLiteral(u"SELECT randomNumber, id FROM world WHERE id=$1"),
                                {id}, [c, async] (AResult &result) {
@@ -107,7 +109,7 @@ void DatabaseUpdatesTest::updateb(Context *c)
         }
     }, c);
 
-    c->response()->setJsonArrayBody(array);
+    c->response()->setJsonBody(QByteArray::fromStdString(picojson::value(array).serialize()));
 }
 
 void DatabaseUpdatesTest::updates_postgres(Context *c)

+ 7 - 4
frameworks/C++/cutelyst/src/singledatabasequerytest.cpp

@@ -11,6 +11,8 @@
 #include <QJsonDocument>
 #include <QJsonObject>
 
+#include "picojson.h"
+
 SingleDatabaseQueryTest::SingleDatabaseQueryTest(QObject *parent) : Controller(parent)
 {
 
@@ -26,10 +28,11 @@ void SingleDatabaseQueryTest::dbp(Context *c)
                            {id}, [c, async] (AResult &result) {
         if (Q_LIKELY(!result.error() && result.size())) {
             auto it = result.begin();
-            c->response()->setJsonObjectBody({
-                                                 {QStringLiteral("id"), it[0].toInt()},
-                                                 {QStringLiteral("randomNumber"), it[1].toInt()}
-                                             });
+            c->response()->setJsonBody(QByteArray::fromStdString(
+                            picojson::value(picojson::object({
+                                                {"id", picojson::value(double(it[0].toInt()))},
+                                                {"randomNumber", picojson::value(double(it[1].toInt()))}
+                                            })).serialize()));
             return;
         }