summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMilian Wolff <milian.wolff@kdab.com>2014-12-22 18:25:13 +0100
committerSérgio Martins <sergio.martins@kdab.com>2015-08-13 16:53:45 +0000
commit194403a3483b7317cc9511bc8b2ab307775643c5 (patch)
treecc223d55f395e24d8f74c9cf371ac02fe0b48256 /tests
parent807ec8ea48281d5dbe365895fd9679da5b6753a5 (diff)
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>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp64
-rw-r--r--tests/benchmarks/sql/kernel/qsqlquery/main.cpp40
2 files changed, 104 insertions, 0 deletions
diff --git a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp
index 61586eb841..b98ab68ae9 100644
--- a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp
+++ b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp
@@ -241,6 +241,10 @@ private slots:
void aggregateFunctionTypes_data() { generic_data(); }
void aggregateFunctionTypes();
+
+ void integralTypesMysql_data() { generic_data("QMYSQL"); }
+ void integralTypesMysql();
+
private:
// returns all database connections
void generic_data(const QString &engine=QString());
@@ -3974,5 +3978,65 @@ void tst_QSqlQuery::aggregateFunctionTypes()
}
}
+template<typename T>
+void runIntegralTypesMysqlTest(QSqlDatabase &db, const QString &tableName, const QString &type, const bool withPreparedStatement,
+ const T min = std::numeric_limits<T>::min(), const T max = std::numeric_limits<T>::max())
+{
+ QSqlQuery q(db);
+ QVERIFY_SQL(q, exec("DROP TABLE IF EXISTS " + tableName));
+ QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + " (id " + type + ")"));
+
+ const int steps = 20;
+ const T increment = max / steps - min / steps;
+
+ // insert some values
+ QVector<T> values;
+ values.resize(steps);
+ T v = min;
+ if (withPreparedStatement) {
+ QVERIFY_SQL(q, prepare("INSERT INTO " + tableName + " (id) VALUES (?)"));
+ }
+ for (int i = 0; i < values.size(); ++i) {
+ if (withPreparedStatement) {
+ q.bindValue(0, v);
+ QVERIFY_SQL(q, exec());
+ } else {
+ QVERIFY_SQL(q, exec("INSERT INTO " + tableName + " (id) VALUES (" + QString::number(v) + ")"));
+ }
+ values[i] = v;
+ v += increment;
+ }
+
+ // ensure we can read them back properly
+ QVERIFY_SQL(q, exec("SELECT id FROM " + tableName));
+ QVector<T> actualValues;
+ actualValues.reserve(values.size());
+ while (q.next()) {
+ actualValues << q.value(0).value<T>();
+ }
+ QCOMPARE(actualValues, values);
+}
+
+void tst_QSqlQuery::integralTypesMysql()
+{
+ QFETCH(QString, dbName);
+ QSqlDatabase db = QSqlDatabase::database(dbName);
+ CHECK_DATABASE(db);
+
+ for (int i = 0; i < 2; ++i) {
+ const bool withPreparedStatement = (i == 1);
+ runIntegralTypesMysqlTest<char>(db, "tinyIntTest", "TINYINT", withPreparedStatement);
+ runIntegralTypesMysqlTest<unsigned char>(db, "unsignedTinyIntTest", "TINYINT UNSIGNED", withPreparedStatement);
+ runIntegralTypesMysqlTest<char>(db, "smallIntTest", "SMALLINT", withPreparedStatement);
+ runIntegralTypesMysqlTest<unsigned char>(db, "unsignedSmallIntTest", "SMALLINT UNSIGNED", withPreparedStatement);
+ runIntegralTypesMysqlTest<int>(db, "mediumIntTest", "MEDIUMINT", withPreparedStatement, -(1 << 23), (1 << 23) - 1);
+ runIntegralTypesMysqlTest<unsigned int>(db, "unsignedMediumIntTest", "MEDIUMINT UNSIGNED", withPreparedStatement, 0, (1 << 24) - 1);
+ runIntegralTypesMysqlTest<int>(db, "intTest", "INT", withPreparedStatement);
+ runIntegralTypesMysqlTest<unsigned int>(db, "unsignedIntTest", "INT UNSIGNED", withPreparedStatement);
+ runIntegralTypesMysqlTest<long long>(db, "bigIntTest", "BIGINT", withPreparedStatement);
+ runIntegralTypesMysqlTest<unsigned long long>(db, "unsignedBigIntTest", "BIGINT UNSIGNED", withPreparedStatement);
+ }
+}
+
QTEST_MAIN( tst_QSqlQuery )
#include "tst_qsqlquery.moc"
diff --git a/tests/benchmarks/sql/kernel/qsqlquery/main.cpp b/tests/benchmarks/sql/kernel/qsqlquery/main.cpp
index 63aa6f7250..b5937b76b0 100644
--- a/tests/benchmarks/sql/kernel/qsqlquery/main.cpp
+++ b/tests/benchmarks/sql/kernel/qsqlquery/main.cpp
@@ -55,6 +55,8 @@ public slots:
private slots:
void benchmark_data() { generic_data(); }
void benchmark();
+ void benchmarkSelectPrepared_data() { generic_data(); }
+ void benchmarkSelectPrepared();
private:
// returns all database connections
@@ -264,4 +266,42 @@ void tst_QSqlQuery::benchmark()
tst_Databases::safeDropTable( db, tableName );
}
+void tst_QSqlQuery::benchmarkSelectPrepared()
+{
+ QFETCH( QString, dbName );
+ QSqlDatabase db = QSqlDatabase::database(dbName);
+ CHECK_DATABASE(db);
+ if (tst_Databases::getMySqlVersion(db).section(QChar('.'), 0, 0).toInt() < 5)
+ QSKIP("Test requires MySQL >= 5.0");
+
+ QSqlQuery q(db);
+ const QString tableName(qTableName("benchmark", __FILE__, db));
+
+ tst_Databases::safeDropTable(db, tableName);
+
+ QVERIFY_SQL(q, exec("CREATE TABLE " + tableName + "(id INT NOT NULL)"));
+
+ const int NUM_ROWS = 1000;
+ int expectedSum = 0;
+ QString fillQuery = "INSERT INTO " + tableName + " VALUES (0)";
+ for (int i = 1; i < NUM_ROWS; ++i) {
+ fillQuery += ", (" + QString::number(i) + ")";
+ expectedSum += i;
+ }
+ QVERIFY_SQL(q, exec(fillQuery));
+
+ QVERIFY_SQL(q, prepare("SELECT id FROM "+tableName));
+ QBENCHMARK {
+ QVERIFY_SQL(q, exec());
+ int sum = 0;
+
+ while (q.next())
+ sum += q.value(0).toInt();
+
+ QCOMPARE(sum, expectedSum);
+ }
+
+ tst_Databases::safeDropTable(db, tableName);
+}
+
#include "main.moc"