2 ** Performance test for SQLite.
4 ** This program reads ASCII text from a file named on the command-line
5 ** and submits that text to SQLite for evaluation. A new database
6 ** is created at the beginning of the program. All statements are
7 ** timed using the high-resolution timer built into Intel-class processors.
9 ** To compile this program, first compile the SQLite library separately
10 ** will full optimizations. For example:
12 ** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c
14 ** Then link against this program. But to do optimize this program
15 ** because that defeats the hi-res timer.
17 ** gcc speedtest8.c sqlite3.o -ldl -I../src
19 ** Then run this program with a single argument which is the name of
20 ** a file containing SQL script that you want to test:
22 ** ./a.out test.db test.sql
34 #include <sys/times.h>
41 ** hwtime.h contains inline assembler code for implementing
42 ** high-performance timing routines.
49 static sqlite_uint64 prepTime = 0;
50 static sqlite_uint64 runTime = 0;
51 static sqlite_uint64 finalizeTime = 0;
54 ** Prepare and run a single statement of SQL.
56 static void prepareAndRun(sqlite3 *db, const char *zSql, int bQuiet){
59 sqlite_uint64 iStart, iElapse;
63 printf("***************************************************************\n");
65 if (!bQuiet) printf("SQL statement: [%s]\n", zSql);
66 iStart = sqlite3Hwtime();
67 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &stmtTail);
68 iElapse = sqlite3Hwtime() - iStart;
71 printf("sqlite3_prepare_v2() returns %d in %llu cycles\n", rc, iElapse);
75 iStart = sqlite3Hwtime();
76 while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; }
77 iElapse = sqlite3Hwtime() - iStart;
80 printf("sqlite3_step() returns %d after %d rows in %llu cycles\n",
83 iStart = sqlite3Hwtime();
84 rc = sqlite3_finalize(pStmt);
85 iElapse = sqlite3Hwtime() - iStart;
86 finalizeTime += iElapse;
88 printf("sqlite3_finalize() returns %d in %llu cycles\n", rc, iElapse);
93 int main(int argc, char **argv){
100 sqlite_uint64 iStart, iElapse;
101 sqlite_uint64 iSetup = 0;
104 const char *zArgv0 = argv[0];
106 #if !defined(_MSC_VER)
107 struct tms tmsStart, tmsEnd;
108 clock_t clkStart, clkEnd;
112 extern sqlite3_vfs *sqlite3_instvfs_binarylog(char *, char *, char *);
113 extern void sqlite3_instvfs_destroy(sqlite3_vfs *);
114 sqlite3_vfs *pVfs = 0;
120 if( argc>4 && (strcmp(argv[1], "-log")==0) ){
121 pVfs = sqlite3_instvfs_binarylog("oslog", 0, argv[2]);
122 sqlite3_vfs_register(pVfs, 1);
130 ** Increasing the priority slightly above normal can help with
131 ** repeatability of testing. Note that with Cygwin, -5 equates
132 ** to "High", +5 equates to "Low", and anything in between
133 ** equates to "Normal".
135 if( argc>4 && (strcmp(argv[1], "-priority")==0) ){
136 #if defined(_MSC_VER)
137 int new_priority = atoi(argv[2]);
138 if(!SetPriorityClass(GetCurrentProcess(),
139 (new_priority<=-5) ? HIGH_PRIORITY_CLASS :
140 (new_priority<=0) ? ABOVE_NORMAL_PRIORITY_CLASS :
141 (new_priority==0) ? NORMAL_PRIORITY_CLASS :
142 (new_priority<5) ? BELOW_NORMAL_PRIORITY_CLASS :
143 IDLE_PRIORITY_CLASS)){
144 printf ("error setting priority\n");
148 struct sched_param myParam;
149 sched_getparam(0, &myParam);
150 printf ("Current process priority is %d.\n", (int)myParam.sched_priority);
151 myParam.sched_priority = atoi(argv[2]);
152 printf ("Setting process priority to %d.\n", (int)myParam.sched_priority);
153 if (sched_setparam (0, &myParam) != 0){
154 printf ("error setting priority\n");
163 if( argc>3 && strcmp(argv[1], "-quiet")==0 ){
174 fprintf(stderr, "Usage: %s [options] FILENAME SQL-SCRIPT\n"
175 "Runs SQL-SCRIPT against a UTF8 database\n"
180 "\t-priority <value> : set priority of task\n"
181 "\t-quiet : only display summary results\n",
186 in = fopen(argv[2], "r");
187 fseek(in, 0L, SEEK_END);
189 zSql = malloc( nSql+1 );
190 fseek(in, 0L, SEEK_SET);
191 nSql = fread(zSql, 1, nSql, in);
194 printf("SQLite version: %d\n", sqlite3_libversion_number());
196 #if !defined(_MSC_VER)
197 clkStart = times(&tmsStart);
199 iStart = sqlite3Hwtime();
200 rc = sqlite3_open(argv[1], &db);
201 iElapse = sqlite3Hwtime() - iStart;
203 if (!bQuiet) printf("sqlite3_open() returns %d in %llu cycles\n", rc, iElapse);
204 for(i=j=0; j<nSql; j++){
209 isComplete = sqlite3_complete(&zSql[i]);
213 while( i<j && isspace(zSql[i]) ){ i++; }
216 if( n>=6 && memcmp(&zSql[i], ".crash",6)==0 ) exit(1);
219 prepareAndRun(db, &zSql[i], bQuiet);
226 iStart = sqlite3Hwtime();
228 iElapse = sqlite3Hwtime() - iStart;
229 #if !defined(_MSC_VER)
230 clkEnd = times(&tmsEnd);
233 if (!bQuiet) printf("sqlite3_close() returns in %llu cycles\n", iElapse);
236 printf("Statements run: %15d stmts\n", nStmt);
237 printf("Bytes of SQL text: %15d bytes\n", nByte);
238 printf("Total prepare time: %15llu cycles\n", prepTime);
239 printf("Total run time: %15llu cycles\n", runTime);
240 printf("Total finalize time: %15llu cycles\n", finalizeTime);
241 printf("Open/Close time: %15llu cycles\n", iSetup);
242 printf("Total time: %15llu cycles\n",
243 prepTime + runTime + finalizeTime + iSetup);
245 #if !defined(_MSC_VER)
247 printf("Total user CPU time: %15.3g secs\n", (tmsEnd.tms_utime - tmsStart.tms_utime)/(double)CLOCKS_PER_SEC );
248 printf("Total system CPU time: %15.3g secs\n", (tmsEnd.tms_stime - tmsStart.tms_stime)/(double)CLOCKS_PER_SEC );
249 printf("Total real time: %15.3g secs\n", (clkEnd -clkStart)/(double)CLOCKS_PER_SEC );
254 sqlite3_instvfs_destroy(pVfs);
255 printf("vfs log written to %s\n", argv[0]);