2008-04-03 16:01:27 +00:00
/*
* * Performance test for SQLite .
* *
* * This program reads ASCII text from a file named on the command - line .
* * It converts each SQL statement into UTF16 and submits it to SQLite
* * for evaluation . A new UTF16 database is created at the beginning of
* * the program . All statements are timed using the high - resolution timer
* * built into Intel - class processors .
* *
* * To compile this program , first compile the SQLite library separately
* * will full optimizations . For example :
* *
* * gcc - c - O6 - DSQLITE_THREADSAFE = 0 sqlite3 . c
* *
* * Then link against this program . But to do optimize this program
* * because that defeats the hi - res timer .
* *
2008-05-29 20:22:37 +00:00
* * gcc speedtest16 . c sqlite3 . o - ldl - I . . / src
2008-04-03 16:01:27 +00:00
* *
* * Then run this program with a single argument which is the name of
* * a file containing SQL script that you want to test :
* *
2008-04-03 17:57:24 +00:00
* * . / a . out database . db test . sql
2008-04-03 16:01:27 +00:00
*/
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
2008-04-03 19:40:59 +00:00
# include <ctype.h>
# include <unistd.h>
2008-04-03 16:01:27 +00:00
# include "sqlite3.h"
2015-10-29 13:48:15 +00:00
# define ISSPACE(X) isspace((unsigned char)(X))
2008-05-29 20:22:37 +00:00
/*
* * hwtime . h contains inline assembler code for implementing
* * high - performance timing routines .
2008-04-03 16:01:27 +00:00
*/
2008-05-29 20:22:37 +00:00
# include "hwtime.h"
2008-04-03 16:01:27 +00:00
/*
* * Convert a zero - terminated ASCII string into a zero - terminated
* * UTF - 16l e string . Memory to hold the returned string comes
* * from malloc ( ) and should be freed by the caller .
*/
static void * asciiToUtf16le ( const char * z ) {
int n = strlen ( z ) ;
char * z16 ;
int i , j ;
z16 = malloc ( n * 2 + 2 ) ;
for ( i = j = 0 ; i < = n ; i + + ) {
z16 [ j + + ] = z [ i ] ;
z16 [ j + + ] = 0 ;
}
return ( void * ) z16 ;
}
/*
* * Timers
*/
2008-05-29 20:22:37 +00:00
static sqlite_uint64 prepTime = 0 ;
static sqlite_uint64 runTime = 0 ;
static sqlite_uint64 finalizeTime = 0 ;
2008-04-03 16:01:27 +00:00
/*
* * Prepare and run a single statement of SQL .
*/
static void prepareAndRun ( sqlite3 * db , const char * zSql ) {
void * utf16 ;
sqlite3_stmt * pStmt ;
const void * stmtTail ;
2008-05-29 20:22:37 +00:00
sqlite_uint64 iStart , iElapse ;
2008-04-03 16:01:27 +00:00
int rc ;
printf ( " **************************************************************** \n " ) ;
printf ( " SQL statement: [%s] \n " , zSql ) ;
utf16 = asciiToUtf16le ( zSql ) ;
2008-05-29 20:22:37 +00:00
iStart = sqlite3Hwtime ( ) ;
2008-04-03 16:01:27 +00:00
rc = sqlite3_prepare16_v2 ( db , utf16 , - 1 , & pStmt , & stmtTail ) ;
2008-05-29 20:22:37 +00:00
iElapse = sqlite3Hwtime ( ) - iStart ;
2008-04-03 16:01:27 +00:00
prepTime + = iElapse ;
printf ( " sqlite3_prepare16_v2() returns %d in %llu cycles \n " , rc , iElapse ) ;
if ( rc = = SQLITE_OK ) {
int nRow = 0 ;
2008-05-29 20:22:37 +00:00
iStart = sqlite3Hwtime ( ) ;
2008-04-03 16:01:27 +00:00
while ( ( rc = sqlite3_step ( pStmt ) ) = = SQLITE_ROW ) { nRow + + ; }
2008-05-29 20:22:37 +00:00
iElapse = sqlite3Hwtime ( ) - iStart ;
2008-04-03 16:01:27 +00:00
runTime + = iElapse ;
printf ( " sqlite3_step() returns %d after %d rows in %llu cycles \n " ,
rc , nRow , iElapse ) ;
2008-05-29 20:22:37 +00:00
iStart = sqlite3Hwtime ( ) ;
2008-04-03 16:01:27 +00:00
rc = sqlite3_finalize ( pStmt ) ;
2008-05-29 20:22:37 +00:00
iElapse = sqlite3Hwtime ( ) - iStart ;
2008-04-03 16:01:27 +00:00
finalizeTime + = iElapse ;
printf ( " sqlite3_finalize() returns %d in %llu cycles \n " , rc , iElapse ) ;
}
free ( utf16 ) ;
}
int main ( int argc , char * * argv ) {
void * utf16 ;
sqlite3 * db ;
int rc ;
int nSql ;
char * zSql ;
int i , j ;
FILE * in ;
2008-05-29 20:22:37 +00:00
sqlite_uint64 iStart , iElapse ;
sqlite_uint64 iSetup = 0 ;
2008-04-03 17:57:24 +00:00
int nStmt = 0 ;
int nByte = 0 ;
2008-04-03 16:01:27 +00:00
2008-04-03 17:57:24 +00:00
if ( argc ! = 3 ) {
fprintf ( stderr , " Usage: %s FILENAME SQL-SCRIPT \n "
2008-04-03 16:01:27 +00:00
" Runs SQL-SCRIPT as UTF16 against a UTF16 database \n " ,
argv [ 0 ] ) ;
exit ( 1 ) ;
}
2008-04-03 17:57:24 +00:00
in = fopen ( argv [ 2 ] , " r " ) ;
2008-04-03 16:01:27 +00:00
fseek ( in , 0L , SEEK_END ) ;
nSql = ftell ( in ) ;
zSql = malloc ( nSql + 1 ) ;
fseek ( in , 0L , SEEK_SET ) ;
nSql = fread ( zSql , 1 , nSql , in ) ;
zSql [ nSql ] = 0 ;
printf ( " SQLite version: %d \n " , sqlite3_libversion_number ( ) ) ;
2008-04-03 17:57:24 +00:00
unlink ( argv [ 1 ] ) ;
utf16 = asciiToUtf16le ( argv [ 1 ] ) ;
2008-05-29 20:22:37 +00:00
iStart = sqlite3Hwtime ( ) ;
2008-04-03 16:01:27 +00:00
rc = sqlite3_open16 ( utf16 , & db ) ;
2008-05-29 20:22:37 +00:00
iElapse = sqlite3Hwtime ( ) - iStart ;
2008-04-03 16:01:27 +00:00
iSetup = iElapse ;
printf ( " sqlite3_open16() returns %d in %llu cycles \n " , rc , iElapse ) ;
free ( utf16 ) ;
for ( i = j = 0 ; j < nSql ; j + + ) {
if ( zSql [ j ] = = ' ; ' ) {
int isComplete ;
char c = zSql [ j + 1 ] ;
zSql [ j + 1 ] = 0 ;
isComplete = sqlite3_complete ( & zSql [ i ] ) ;
zSql [ j + 1 ] = c ;
if ( isComplete ) {
zSql [ j ] = 0 ;
2015-10-29 13:48:15 +00:00
while ( i < j & & ISSPACE ( zSql [ i ] ) ) { i + + ; }
2008-04-03 16:01:27 +00:00
if ( i < j ) {
2008-04-03 17:57:24 +00:00
nStmt + + ;
nByte + = j - i ;
2008-04-03 16:01:27 +00:00
prepareAndRun ( db , & zSql [ i ] ) ;
}
zSql [ j ] = ' ; ' ;
i = j + 1 ;
}
}
}
2008-05-29 20:22:37 +00:00
iStart = sqlite3Hwtime ( ) ;
2008-04-03 16:01:27 +00:00
sqlite3_close ( db ) ;
2008-05-29 20:22:37 +00:00
iElapse = sqlite3Hwtime ( ) - iStart ;
2008-04-03 16:01:27 +00:00
iSetup + = iElapse ;
printf ( " sqlite3_close() returns in %llu cycles \n " , iElapse ) ;
printf ( " \n " ) ;
2008-04-03 17:57:24 +00:00
printf ( " Statements run: %15d \n " , nStmt ) ;
printf ( " Bytes of SQL text: %15d \n " , nByte ) ;
2008-04-03 16:01:27 +00:00
printf ( " Total prepare time: %15llu cycles \n " , prepTime ) ;
printf ( " Total run time: %15llu cycles \n " , runTime ) ;
printf ( " Total finalize time: %15llu cycles \n " , finalizeTime ) ;
printf ( " Open/Close time: %15llu cycles \n " , iSetup ) ;
printf ( " Total Time: %15llu cycles \n " ,
prepTime + runTime + finalizeTime + iSetup ) ;
return 0 ;
}