2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
2024-02-02 13:36:10 +00:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
2011-10-23 12:41:31 +00:00
|
|
|
|
2020-11-26 16:31:50 +00:00
|
|
|
#include <QTest>
|
2011-10-23 12:41:31 +00:00
|
|
|
#include <QtSql/QtSql>
|
|
|
|
|
|
|
|
#include "../../../../auto/sql/kernel/qsqldatabase/tst_databases.h"
|
|
|
|
|
|
|
|
class tst_QSqlQuery : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2023-02-28 20:33:46 +00:00
|
|
|
using QObject::QObject;
|
2011-10-23 12:41:31 +00:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
void initTestCase();
|
|
|
|
void cleanupTestCase();
|
|
|
|
void init();
|
|
|
|
void cleanup();
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void benchmark_data() { generic_data(); }
|
|
|
|
void benchmark();
|
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly
into the QVariant of the desired type. This gets rid of the temporary
string allocations and greatly improves the performance of the added
benchmark. On my machine, the results are:
Before:
0.562 msecs per iteration (total: 563, iterations: 1000)
1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000)
After:
0.381 msecs per iteration (total: 381, iterations: 1000)
774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000)
Note that the same could be applied to floating point data types in
the future. Additionally, special support for MYSQL_TIME structure
coult be added to get rid of the string conversions there.
To ensure everything keeps working, a new auto test is added as well
that verifies the select statements and insertions of integral data
into a MySql table works as intended.
[ChangeLog][QtSql] Improve performance when reading integer values
from MySQL databases via prepared statements.
Change-Id: I21dd9277661971ded934546f09535014b63f8eb8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
2014-12-22 17:25:13 +00:00
|
|
|
void benchmarkSelectPrepared_data() { generic_data(); }
|
|
|
|
void benchmarkSelectPrepared();
|
2011-10-23 12:41:31 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// returns all database connections
|
2023-03-25 11:13:45 +00:00
|
|
|
void generic_data(const QString &engine = QString());
|
2011-10-23 12:41:31 +00:00
|
|
|
|
|
|
|
tst_Databases dbs;
|
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_MAIN(tst_QSqlQuery)
|
|
|
|
|
|
|
|
void tst_QSqlQuery::initTestCase()
|
|
|
|
{
|
|
|
|
dbs.open();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlQuery::cleanupTestCase()
|
|
|
|
{
|
|
|
|
dbs.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlQuery::init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlQuery::cleanup()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-03-25 11:13:45 +00:00
|
|
|
void tst_QSqlQuery::generic_data(const QString &engine)
|
2011-10-23 12:41:31 +00:00
|
|
|
{
|
2023-03-25 11:13:45 +00:00
|
|
|
if (dbs.fillTestTable(engine) == 0) {
|
2011-10-23 12:41:31 +00:00
|
|
|
if (engine.isEmpty())
|
|
|
|
QSKIP( "No database drivers are available in this Qt configuration");
|
|
|
|
else
|
|
|
|
QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QSqlQuery::benchmark()
|
|
|
|
{
|
2023-03-25 11:13:45 +00:00
|
|
|
QFETCH(QString, dbName);
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
2011-10-23 12:41:31 +00:00
|
|
|
QSqlQuery q(db);
|
2023-02-28 20:33:46 +00:00
|
|
|
TableScope ts(db, "benchmark", __FILE__);
|
2011-10-23 12:41:31 +00:00
|
|
|
|
2023-02-28 20:33:46 +00:00
|
|
|
QVERIFY_SQL(q, exec("CREATE TABLE " + ts.tableName() + "(\n"
|
2011-10-23 12:41:31 +00:00
|
|
|
"MainKey INT NOT NULL,\n"
|
|
|
|
"OtherTextCol VARCHAR(45) NOT NULL,\n"
|
2023-03-25 11:13:45 +00:00
|
|
|
"PRIMARY KEY(MainKey))"));
|
2011-10-23 12:41:31 +00:00
|
|
|
|
|
|
|
int i=1;
|
|
|
|
|
|
|
|
QBENCHMARK {
|
2023-02-28 20:33:46 +00:00
|
|
|
const QString num = QString::number(i);
|
2023-03-25 11:13:45 +00:00
|
|
|
QVERIFY_SQL(q, exec("INSERT INTO " + ts.tableName() + " VALUES(" + num + ", 'Value" + num + "')"));
|
2011-10-23 12:41:31 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly
into the QVariant of the desired type. This gets rid of the temporary
string allocations and greatly improves the performance of the added
benchmark. On my machine, the results are:
Before:
0.562 msecs per iteration (total: 563, iterations: 1000)
1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000)
After:
0.381 msecs per iteration (total: 381, iterations: 1000)
774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000)
Note that the same could be applied to floating point data types in
the future. Additionally, special support for MYSQL_TIME structure
coult be added to get rid of the string conversions there.
To ensure everything keeps working, a new auto test is added as well
that verifies the select statements and insertions of integral data
into a MySql table works as intended.
[ChangeLog][QtSql] Improve performance when reading integer values
from MySQL databases via prepared statements.
Change-Id: I21dd9277661971ded934546f09535014b63f8eb8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
2014-12-22 17:25:13 +00:00
|
|
|
void tst_QSqlQuery::benchmarkSelectPrepared()
|
|
|
|
{
|
2023-03-25 11:13:45 +00:00
|
|
|
QFETCH(QString, dbName);
|
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly
into the QVariant of the desired type. This gets rid of the temporary
string allocations and greatly improves the performance of the added
benchmark. On my machine, the results are:
Before:
0.562 msecs per iteration (total: 563, iterations: 1000)
1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000)
After:
0.381 msecs per iteration (total: 381, iterations: 1000)
774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000)
Note that the same could be applied to floating point data types in
the future. Additionally, special support for MYSQL_TIME structure
coult be added to get rid of the string conversions there.
To ensure everything keeps working, a new auto test is added as well
that verifies the select statements and insertions of integral data
into a MySql table works as intended.
[ChangeLog][QtSql] Improve performance when reading integer values
from MySQL databases via prepared statements.
Change-Id: I21dd9277661971ded934546f09535014b63f8eb8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
2014-12-22 17:25:13 +00:00
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
CHECK_DATABASE(db);
|
|
|
|
QSqlQuery q(db);
|
2023-02-28 20:33:46 +00:00
|
|
|
TableScope ts(db, "benchmark", __FILE__);
|
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly
into the QVariant of the desired type. This gets rid of the temporary
string allocations and greatly improves the performance of the added
benchmark. On my machine, the results are:
Before:
0.562 msecs per iteration (total: 563, iterations: 1000)
1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000)
After:
0.381 msecs per iteration (total: 381, iterations: 1000)
774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000)
Note that the same could be applied to floating point data types in
the future. Additionally, special support for MYSQL_TIME structure
coult be added to get rid of the string conversions there.
To ensure everything keeps working, a new auto test is added as well
that verifies the select statements and insertions of integral data
into a MySql table works as intended.
[ChangeLog][QtSql] Improve performance when reading integer values
from MySQL databases via prepared statements.
Change-Id: I21dd9277661971ded934546f09535014b63f8eb8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
2014-12-22 17:25:13 +00:00
|
|
|
|
2023-02-28 20:33:46 +00:00
|
|
|
QVERIFY_SQL(q, exec("CREATE TABLE " + ts.tableName() + "(id INT NOT NULL)"));
|
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly
into the QVariant of the desired type. This gets rid of the temporary
string allocations and greatly improves the performance of the added
benchmark. On my machine, the results are:
Before:
0.562 msecs per iteration (total: 563, iterations: 1000)
1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000)
After:
0.381 msecs per iteration (total: 381, iterations: 1000)
774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000)
Note that the same could be applied to floating point data types in
the future. Additionally, special support for MYSQL_TIME structure
coult be added to get rid of the string conversions there.
To ensure everything keeps working, a new auto test is added as well
that verifies the select statements and insertions of integral data
into a MySql table works as intended.
[ChangeLog][QtSql] Improve performance when reading integer values
from MySQL databases via prepared statements.
Change-Id: I21dd9277661971ded934546f09535014b63f8eb8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
2014-12-22 17:25:13 +00:00
|
|
|
|
|
|
|
const int NUM_ROWS = 1000;
|
|
|
|
int expectedSum = 0;
|
2023-02-28 20:33:46 +00:00
|
|
|
QString fillQuery = "INSERT INTO " + ts.tableName() + " VALUES (0)";
|
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly
into the QVariant of the desired type. This gets rid of the temporary
string allocations and greatly improves the performance of the added
benchmark. On my machine, the results are:
Before:
0.562 msecs per iteration (total: 563, iterations: 1000)
1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000)
After:
0.381 msecs per iteration (total: 381, iterations: 1000)
774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000)
Note that the same could be applied to floating point data types in
the future. Additionally, special support for MYSQL_TIME structure
coult be added to get rid of the string conversions there.
To ensure everything keeps working, a new auto test is added as well
that verifies the select statements and insertions of integral data
into a MySql table works as intended.
[ChangeLog][QtSql] Improve performance when reading integer values
from MySQL databases via prepared statements.
Change-Id: I21dd9277661971ded934546f09535014b63f8eb8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
2014-12-22 17:25:13 +00:00
|
|
|
for (int i = 1; i < NUM_ROWS; ++i) {
|
2015-10-13 07:46:56 +00:00
|
|
|
fillQuery += ", (" + QString::number(i) + QLatin1Char(')');
|
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly
into the QVariant of the desired type. This gets rid of the temporary
string allocations and greatly improves the performance of the added
benchmark. On my machine, the results are:
Before:
0.562 msecs per iteration (total: 563, iterations: 1000)
1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000)
After:
0.381 msecs per iteration (total: 381, iterations: 1000)
774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000)
Note that the same could be applied to floating point data types in
the future. Additionally, special support for MYSQL_TIME structure
coult be added to get rid of the string conversions there.
To ensure everything keeps working, a new auto test is added as well
that verifies the select statements and insertions of integral data
into a MySql table works as intended.
[ChangeLog][QtSql] Improve performance when reading integer values
from MySQL databases via prepared statements.
Change-Id: I21dd9277661971ded934546f09535014b63f8eb8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
2014-12-22 17:25:13 +00:00
|
|
|
expectedSum += i;
|
|
|
|
}
|
|
|
|
QVERIFY_SQL(q, exec(fillQuery));
|
|
|
|
|
2023-02-28 20:33:46 +00:00
|
|
|
QVERIFY_SQL(q, prepare("SELECT id FROM " + ts.tableName()));
|
Remove temporary string allocations when reading prepared statement.
Instead, we use the binary MySQL encoding and copy the data directly
into the QVariant of the desired type. This gets rid of the temporary
string allocations and greatly improves the performance of the added
benchmark. On my machine, the results are:
Before:
0.562 msecs per iteration (total: 563, iterations: 1000)
1,922,479.330 instructions per iteration (total: 1,922,479,330, iterations: 1000)
After:
0.381 msecs per iteration (total: 381, iterations: 1000)
774,132.957 instructions per iteration (total: 774,132,958, iterations: 1000)
Note that the same could be applied to floating point data types in
the future. Additionally, special support for MYSQL_TIME structure
coult be added to get rid of the string conversions there.
To ensure everything keeps working, a new auto test is added as well
that verifies the select statements and insertions of integral data
into a MySql table works as intended.
[ChangeLog][QtSql] Improve performance when reading integer values
from MySQL databases via prepared statements.
Change-Id: I21dd9277661971ded934546f09535014b63f8eb8
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
2014-12-22 17:25:13 +00:00
|
|
|
QBENCHMARK {
|
|
|
|
QVERIFY_SQL(q, exec());
|
|
|
|
int sum = 0;
|
|
|
|
|
|
|
|
while (q.next())
|
|
|
|
sum += q.value(0).toInt();
|
|
|
|
|
|
|
|
QCOMPARE(sum, expectedSum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-23 12:41:31 +00:00
|
|
|
#include "main.moc"
|