2011-10-23 12:41:31 +00:00
/****************************************************************************
* *
2015-01-28 08:44:43 +00:00
* * Copyright ( C ) 2015 The Qt Company Ltd .
* * Contact : http : //www.qt.io/licensing/
2011-10-23 12:41:31 +00:00
* *
* * This file is part of the test suite of the Qt Toolkit .
* *
2014-08-21 13:51:22 +00:00
* * $ QT_BEGIN_LICENSE : LGPL21 $
2012-09-19 12:28:29 +00:00
* * Commercial License Usage
* * Licensees holding valid commercial Qt licenses may use this file in
* * accordance with the commercial license agreement provided with the
* * Software or , alternatively , in accordance with the terms contained in
2015-01-28 08:44:43 +00:00
* * a written agreement between you and The Qt Company . For licensing terms
* * and conditions see http : //www.qt.io/terms-conditions. For further
* * information use the contact form at http : //www.qt.io/contact-us.
2012-09-19 12:28:29 +00:00
* *
2011-10-23 12:41:31 +00:00
* * GNU Lesser General Public License Usage
2012-09-19 12:28:29 +00:00
* * Alternatively , this file may be used under the terms of the GNU Lesser
2014-08-21 13:51:22 +00:00
* * General Public License version 2.1 or version 3 as published by the Free
* * Software Foundation and appearing in the file LICENSE . LGPLv21 and
* * LICENSE . LGPLv3 included in the packaging of this file . Please review the
* * following information to ensure the GNU Lesser General Public License
* * requirements will be met : https : //www.gnu.org/licenses/lgpl.html and
* * http : //www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
2012-09-19 12:28:29 +00:00
* *
2015-01-28 08:44:43 +00:00
* * As a special exception , The Qt Company gives you certain additional
* * rights . These rights are described in The Qt Company LGPL Exception
2011-10-23 12:41:31 +00:00
* * version 1.1 , included in the file LGPL_EXCEPTION . txt in this package .
* *
* * $ QT_END_LICENSE $
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include <QtTest/QtTest>
# include <QtSql/QtSql>
# include "../../../../auto/sql/kernel/qsqldatabase/tst_databases.h"
2013-03-21 10:23:31 +00:00
const QString qtest ( qTableName ( " qtest " , __FILE__ , QSqlDatabase ( ) ) ) ;
2011-10-23 12:41:31 +00:00
class tst_QSqlQuery : public QObject
{
Q_OBJECT
public :
tst_QSqlQuery ( ) ;
virtual ~ tst_QSqlQuery ( ) ;
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
void generic_data ( const QString & engine = QString ( ) ) ;
void dropTestTables ( QSqlDatabase db ) ;
void createTestTables ( QSqlDatabase db ) ;
void populateTestTables ( QSqlDatabase db ) ;
tst_Databases dbs ;
} ;
QTEST_MAIN ( tst_QSqlQuery )
tst_QSqlQuery : : tst_QSqlQuery ( )
{
}
tst_QSqlQuery : : ~ tst_QSqlQuery ( )
{
}
void tst_QSqlQuery : : initTestCase ( )
{
dbs . open ( ) ;
for ( QStringList : : ConstIterator it = dbs . dbNames . begin ( ) ; it ! = dbs . dbNames . end ( ) ; + + it ) {
QSqlDatabase db = QSqlDatabase : : database ( ( * it ) ) ;
CHECK_DATABASE ( db ) ;
dropTestTables ( db ) ; //in case of leftovers
createTestTables ( db ) ;
populateTestTables ( db ) ;
}
}
void tst_QSqlQuery : : cleanupTestCase ( )
{
for ( QStringList : : ConstIterator it = dbs . dbNames . begin ( ) ; it ! = dbs . dbNames . end ( ) ; + + it ) {
QSqlDatabase db = QSqlDatabase : : database ( ( * it ) ) ;
CHECK_DATABASE ( db ) ;
dropTestTables ( db ) ;
}
dbs . close ( ) ;
}
void tst_QSqlQuery : : init ( )
{
}
void tst_QSqlQuery : : cleanup ( )
{
QFETCH ( QString , dbName ) ;
QSqlDatabase db = QSqlDatabase : : database ( dbName ) ;
CHECK_DATABASE ( db ) ;
2014-11-20 11:55:56 +00:00
const QSqlDriver : : DbmsType dbType = tst_Databases : : getDatabaseType ( db ) ;
2011-10-23 12:41:31 +00:00
if ( QTest : : currentTestFunction ( ) = = QLatin1String ( " numRowsAffected " )
| | QTest : : currentTestFunction ( ) = = QLatin1String ( " transactions " )
| | QTest : : currentTestFunction ( ) = = QLatin1String ( " size " )
| | QTest : : currentTestFunction ( ) = = QLatin1String ( " isActive " )
| | QTest : : currentTestFunction ( ) = = QLatin1String ( " lastInsertId " ) ) {
populateTestTables ( db ) ;
}
2014-09-16 14:03:14 +00:00
if ( QTest : : currentTestFailed ( ) & & ( dbType = = QSqlDriver : : Oracle | | db . driverName ( ) . startsWith ( " QODBC " ) ) ) {
2011-10-23 12:41:31 +00:00
//since Oracle ODBC totally craps out on error, we init again
db . close ( ) ;
db . open ( ) ;
}
}
void tst_QSqlQuery : : generic_data ( const QString & engine )
{
if ( dbs . fillTestTable ( engine ) = = 0 ) {
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 : : dropTestTables ( QSqlDatabase db )
{
2014-11-20 11:55:56 +00:00
QSqlDriver : : DbmsType dbType = tst_Databases : : getDatabaseType ( db ) ;
2011-10-23 12:41:31 +00:00
QStringList tablenames ;
// drop all the table in case a testcase failed
tablenames < < qtest
2013-03-21 10:23:31 +00:00
< < qTableName ( " qtest_null " , __FILE__ , db )
< < qTableName ( " qtest_blob " , __FILE__ , db )
< < qTableName ( " qtest_bittest " , __FILE__ , db )
< < qTableName ( " qtest_nullblob " , __FILE__ , db )
< < qTableName ( " qtest_rawtest " , __FILE__ , db )
< < qTableName ( " qtest_precision " , __FILE__ , db )
< < qTableName ( " qtest_prepare " , __FILE__ , db )
< < qTableName ( " qtestj1 " , __FILE__ , db )
< < qTableName ( " qtestj2 " , __FILE__ , db )
< < qTableName ( " char1Select " , __FILE__ , db )
< < qTableName ( " char1SU " , __FILE__ , db )
< < qTableName ( " qxmltest " , __FILE__ , db )
< < qTableName ( " qtest_exerr " , __FILE__ , db )
< < qTableName ( " qtest_empty " , __FILE__ , db )
< < qTableName ( " clobby " , __FILE__ , db )
< < qTableName ( " bindtest " , __FILE__ , db )
< < qTableName ( " more_results " , __FILE__ , db )
< < qTableName ( " blobstest " , __FILE__ , db )
< < qTableName ( " oraRowId " , __FILE__ , db )
< < qTableName ( " qtest_batch " , __FILE__ , db )
< < qTableName ( " bug6421 " , __FILE__ , db ) . toUpper ( )
< < qTableName ( " bug5765 " , __FILE__ , db )
< < qTableName ( " bug6852 " , __FILE__ , db )
< < qTableName ( " qtest_lockedtable " , __FILE__ , db )
< < qTableName ( " Planet " , __FILE__ , db )
< < qTableName ( " task_250026 " , __FILE__ , db )
< < qTableName ( " task_234422 " , __FILE__ , db )
< < qTableName ( " test141895 " , __FILE__ , db )
< < qTableName ( " qtest_oraOCINumber " , __FILE__ , db ) ;
2011-10-23 12:41:31 +00:00
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : PostgreSQL )
2013-03-21 10:23:31 +00:00
tablenames < < qTableName ( " task_233829 " , __FILE__ , db ) ;
2011-10-23 12:41:31 +00:00
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : SQLite )
2013-03-21 10:23:31 +00:00
tablenames < < qTableName ( " record_sqlite " , __FILE__ , db ) ;
2011-10-23 12:41:31 +00:00
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : MSSqlServer | | dbType = = QSqlDriver : : Oracle )
2013-03-21 10:23:31 +00:00
tablenames < < qTableName ( " qtest_longstr " , __FILE__ , db ) ;
2011-10-23 12:41:31 +00:00
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : MSSqlServer )
2013-03-21 10:23:31 +00:00
db . exec ( " DROP PROCEDURE " + qTableName ( " test141895_proc " , __FILE__ , db ) ) ;
2011-10-23 12:41:31 +00:00
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : MySqlServer )
2013-03-21 10:23:31 +00:00
db . exec ( " DROP PROCEDURE IF EXISTS " + qTableName ( " bug6852_proc " , __FILE__ , db ) ) ;
2011-10-23 12:41:31 +00:00
tst_Databases : : safeDropTables ( db , tablenames ) ;
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : Oracle ) {
2011-10-23 12:41:31 +00:00
QSqlQuery q ( db ) ;
2013-03-21 10:23:31 +00:00
q . exec ( " DROP PACKAGE " + qTableName ( " pkg " , __FILE__ , db ) ) ;
2011-10-23 12:41:31 +00:00
}
}
void tst_QSqlQuery : : createTestTables ( QSqlDatabase db )
{
2013-04-04 06:45:50 +00:00
const QString qtestNull = qTableName ( " qtest_null " , __FILE__ , db ) ;
2011-10-23 12:41:31 +00:00
QSqlQuery q ( db ) ;
2014-11-20 11:55:56 +00:00
QSqlDriver : : DbmsType dbType = tst_Databases : : getDatabaseType ( db ) ;
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : MySqlServer )
2011-10-23 12:41:31 +00:00
// ### stupid workaround until we find a way to hardcode this
// in the MySQL server startup script
q . exec ( " set table_type=innodb " ) ;
2014-09-16 14:03:14 +00:00
else if ( dbType = = QSqlDriver : : PostgreSQL )
2011-10-23 12:41:31 +00:00
QVERIFY_SQL ( q , exec ( " set client_min_messages='warning' " ) ) ;
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : PostgreSQL )
2011-10-23 12:41:31 +00:00
QVERIFY_SQL ( q , exec ( " create table " + qtest + " (id serial NOT NULL, t_varchar varchar(20), t_char char(20), primary key(id)) WITH OIDS " ) ) ;
else
QVERIFY_SQL ( q , exec ( " create table " + qtest + " (id int " + tst_Databases : : autoFieldName ( db ) + " NOT NULL, t_varchar varchar(20), t_char char(20), primary key(id)) " ) ) ;
2014-09-16 14:03:14 +00:00
if ( dbType = = QSqlDriver : : MSSqlServer | | dbType = = QSqlDriver : : Sybase )
2013-04-04 06:45:50 +00:00
QVERIFY_SQL ( q , exec ( " create table " + qtestNull + " (id int null, t_varchar varchar(20) null) " ) ) ;
2011-10-23 12:41:31 +00:00
else
2013-04-04 06:45:50 +00:00
QVERIFY_SQL ( q , exec ( " create table " + qtestNull + " (id int, t_varchar varchar(20)) " ) ) ;
2011-10-23 12:41:31 +00:00
}
void tst_QSqlQuery : : populateTestTables ( QSqlDatabase db )
{
QSqlQuery q ( db ) ;
2013-03-21 10:23:31 +00:00
const QString qtest_null ( qTableName ( " qtest_null " , __FILE__ , db ) ) ;
2011-10-23 12:41:31 +00:00
q . exec ( " delete from " + qtest ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest + " values (1, 'VarChar1', 'Char1') " ) ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest + " values (2, 'VarChar2', 'Char2') " ) ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest + " values (3, 'VarChar3', 'Char3') " ) ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest + " values (4, 'VarChar4', 'Char4') " ) ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest + " values (5, 'VarChar5', 'Char5') " ) ) ;
q . exec ( " delete from " + qtest_null ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest_null + " values (0, NULL) " ) ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest_null + " values (1, 'n') " ) ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest_null + " values (2, 'i') " ) ) ;
QVERIFY_SQL ( q , exec ( " insert into " + qtest_null + " values (3, NULL) " ) ) ;
}
void tst_QSqlQuery : : benchmark ( )
{
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 ) ;
2013-03-21 10:23:31 +00:00
const QString tableName ( qTableName ( " benchmark " , __FILE__ , db ) ) ;
2011-10-23 12:41:31 +00:00
tst_Databases : : safeDropTable ( db , tableName ) ;
QVERIFY_SQL ( q , exec ( " CREATE TABLE " + tableName + " ( \n "
" MainKey INT NOT NULL, \n "
" OtherTextCol VARCHAR(45) NOT NULL, \n "
" PRIMARY KEY(`MainKey`)) " ) ) ;
int i = 1 ;
QBENCHMARK {
QVERIFY_SQL ( q , exec ( " INSERT INTO " + tableName + " VALUES( " + QString : : number ( i ) + " , \" Value " + QString : : number ( i ) + " \" ) " ) ) ;
i + + ;
}
tst_Databases : : safeDropTable ( db , 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
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 ) ;
}
2011-10-23 12:41:31 +00:00
# include "main.moc"