2 ** Performance test for SQLite.
4 ** This program reads ASCII text from a file named on the command-line.
5 ** It converts each SQL statement into UTF16 and submits it to SQLite
6 ** for evaluation. A new UTF16 database is created at the beginning of
7 ** the program. All statements are timed using the high-resolution timer
8 ** built into Intel-class processors.
10 ** To compile this program, first compile the SQLite library separately
11 ** will full optimizations. For example:
13 ** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c
15 ** Then link against this program. But to do optimize this program
16 ** because that defeats the hi-res timer.
18 ** gcc speedtest16.c sqlite3.o -ldl -I../src
20 ** Then run this program with a single argument which is the name of
21 ** a file containing SQL script that you want to test:
23 ** ./a.out database.db test.sql
32 #define ISSPACE(X) isspace((unsigned char)(X))
35 ** hwtime.h contains inline assembler code for implementing
36 ** high-performance timing routines.
41 ** Convert a zero-terminated ASCII string into a zero-terminated
42 ** UTF-16le string. Memory to hold the returned string comes
43 ** from malloc() and should be freed by the caller.
45 static void *asciiToUtf16le(const char *z
){
50 z16
= malloc( n
*2 + 2 );
51 for(i
=j
=0; i
<=n
; i
++){
61 static sqlite_uint64 prepTime
= 0;
62 static sqlite_uint64 runTime
= 0;
63 static sqlite_uint64 finalizeTime
= 0;
66 ** Prepare and run a single statement of SQL.
68 static void prepareAndRun(sqlite3
*db
, const char *zSql
){
72 sqlite_uint64 iStart
, iElapse
;
75 printf("****************************************************************\n");
76 printf("SQL statement: [%s]\n", zSql
);
77 utf16
= asciiToUtf16le(zSql
);
78 iStart
= sqlite3Hwtime();
79 rc
= sqlite3_prepare16_v2(db
, utf16
, -1, &pStmt
, &stmtTail
);
80 iElapse
= sqlite3Hwtime() - iStart
;
82 printf("sqlite3_prepare16_v2() returns %d in %llu cycles\n", rc
, iElapse
);
85 iStart
= sqlite3Hwtime();
86 while( (rc
=sqlite3_step(pStmt
))==SQLITE_ROW
){ nRow
++; }
87 iElapse
= sqlite3Hwtime() - iStart
;
89 printf("sqlite3_step() returns %d after %d rows in %llu cycles\n",
91 iStart
= sqlite3Hwtime();
92 rc
= sqlite3_finalize(pStmt
);
93 iElapse
= sqlite3Hwtime() - iStart
;
94 finalizeTime
+= iElapse
;
95 printf("sqlite3_finalize() returns %d in %llu cycles\n", rc
, iElapse
);
100 int main(int argc
, char **argv
){
108 sqlite_uint64 iStart
, iElapse
;
109 sqlite_uint64 iSetup
= 0;
114 fprintf(stderr
, "Usage: %s FILENAME SQL-SCRIPT\n"
115 "Runs SQL-SCRIPT as UTF16 against a UTF16 database\n",
119 in
= fopen(argv
[2], "r");
120 fseek(in
, 0L, SEEK_END
);
122 zSql
= malloc( nSql
+1 );
123 fseek(in
, 0L, SEEK_SET
);
124 nSql
= fread(zSql
, 1, nSql
, in
);
127 printf("SQLite version: %d\n", sqlite3_libversion_number());
129 utf16
= asciiToUtf16le(argv
[1]);
130 iStart
= sqlite3Hwtime();
131 rc
= sqlite3_open16(utf16
, &db
);
132 iElapse
= sqlite3Hwtime() - iStart
;
134 printf("sqlite3_open16() returns %d in %llu cycles\n", rc
, iElapse
);
136 for(i
=j
=0; j
<nSql
; j
++){
141 isComplete
= sqlite3_complete(&zSql
[i
]);
145 while( i
<j
&& ISSPACE(zSql
[i
]) ){ i
++; }
149 prepareAndRun(db
, &zSql
[i
]);
156 iStart
= sqlite3Hwtime();
158 iElapse
= sqlite3Hwtime() - iStart
;
160 printf("sqlite3_close() returns in %llu cycles\n", iElapse
);
162 printf("Statements run: %15d\n", nStmt
);
163 printf("Bytes of SQL text: %15d\n", nByte
);
164 printf("Total prepare time: %15llu cycles\n", prepTime
);
165 printf("Total run time: %15llu cycles\n", runTime
);
166 printf("Total finalize time: %15llu cycles\n", finalizeTime
);
167 printf("Open/Close time: %15llu cycles\n", iSetup
);
168 printf("Total Time: %15llu cycles\n",
169 prepTime
+ runTime
+ finalizeTime
+ iSetup
);