qt6windows7/tests/benchmarks/sql/kernel/qsqlquery/main.cpp

119 lines
2.8 KiB
C++
Raw Normal View History

2023-10-30 06:33:08 +08:00
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include <QTest>
#include <QtSql/QtSql>
#include "../../../../auto/sql/kernel/qsqldatabase/tst_databases.h"
class tst_QSqlQuery : public QObject
{
Q_OBJECT
public:
2023-11-02 05:23:55 +08:00
using QObject::QObject;
2023-10-30 06:33:08 +08:00
public slots:
void initTestCase();
void cleanupTestCase();
void init();
void cleanup();
private slots:
void benchmark_data() { generic_data(); }
void benchmark();
void benchmarkSelectPrepared_data() { generic_data(); }
void benchmarkSelectPrepared();
private:
// returns all database connections
2023-11-02 05:23:55 +08:00
void generic_data(const QString &engine = QString());
2023-10-30 06:33:08 +08: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-11-02 05:23:55 +08:00
void tst_QSqlQuery::generic_data(const QString &engine)
2023-10-30 06:33:08 +08:00
{
2023-11-02 05:23:55 +08:00
if (dbs.fillTestTable(engine) == 0) {
2023-10-30 06:33:08 +08: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-11-02 05:23:55 +08:00
QFETCH(QString, dbName);
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
2023-10-30 06:33:08 +08:00
QSqlQuery q(db);
2023-11-02 05:23:55 +08:00
TableScope ts(db, "benchmark", __FILE__);
2023-10-30 06:33:08 +08:00
2023-11-02 05:23:55 +08:00
QVERIFY_SQL(q, exec("CREATE TABLE " + ts.tableName() + "(\n"
2023-10-30 06:33:08 +08:00
"MainKey INT NOT NULL,\n"
"OtherTextCol VARCHAR(45) NOT NULL,\n"
2023-11-02 05:23:55 +08:00
"PRIMARY KEY(MainKey))"));
2023-10-30 06:33:08 +08:00
int i=1;
QBENCHMARK {
2023-11-02 05:23:55 +08:00
const QString num = QString::number(i);
QVERIFY_SQL(q, exec("INSERT INTO " + ts.tableName() + " VALUES(" + num + ", 'Value" + num + "')"));
2023-10-30 06:33:08 +08:00
i++;
}
}
void tst_QSqlQuery::benchmarkSelectPrepared()
{
2023-11-02 05:23:55 +08:00
QFETCH(QString, dbName);
2023-10-30 06:33:08 +08:00
QSqlDatabase db = QSqlDatabase::database(dbName);
CHECK_DATABASE(db);
QSqlQuery q(db);
2023-11-02 05:23:55 +08:00
TableScope ts(db, "benchmark", __FILE__);
2023-10-30 06:33:08 +08:00
2023-11-02 05:23:55 +08:00
QVERIFY_SQL(q, exec("CREATE TABLE " + ts.tableName() + "(id INT NOT NULL)"));
2023-10-30 06:33:08 +08:00
const int NUM_ROWS = 1000;
int expectedSum = 0;
2023-11-02 05:23:55 +08:00
QString fillQuery = "INSERT INTO " + ts.tableName() + " VALUES (0)";
2023-10-30 06:33:08 +08:00
for (int i = 1; i < NUM_ROWS; ++i) {
fillQuery += ", (" + QString::number(i) + QLatin1Char(')');
expectedSum += i;
}
QVERIFY_SQL(q, exec(fillQuery));
2023-11-02 05:23:55 +08:00
QVERIFY_SQL(q, prepare("SELECT id FROM " + ts.tableName()));
2023-10-30 06:33:08 +08:00
QBENCHMARK {
QVERIFY_SQL(q, exec());
int sum = 0;
while (q.next())
sum += q.value(0).toInt();
QCOMPARE(sum, expectedSum);
}
}
#include "main.moc"