drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Performance test for SQLite. |
| 3 | ** |
| 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. |
| 8 | ** |
| 9 | ** To compile this program, first compile the SQLite library separately |
| 10 | ** will full optimizations. For example: |
| 11 | ** |
| 12 | ** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c |
| 13 | ** |
| 14 | ** Then link against this program. But to do optimize this program |
| 15 | ** because that defeats the hi-res timer. |
| 16 | ** |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 17 | ** gcc speedtest8.c sqlite3.o -ldl -I../src |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 18 | ** |
| 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: |
| 21 | ** |
| 22 | ** ./a.out test.db test.sql |
| 23 | */ |
| 24 | #include <stdio.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <ctype.h> |
| 28 | #include <unistd.h> |
| 29 | #include <stdarg.h> |
| 30 | #include "sqlite3.h" |
| 31 | |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 32 | #define ISSPACE(X) isspace((unsigned char)(X)) |
| 33 | |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 34 | #include "test_osinst.c" |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | ** Prepare and run a single statement of SQL. |
| 38 | */ |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 39 | static void prepareAndRun(sqlite3_vfs *pInstVfs, sqlite3 *db, const char *zSql){ |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 40 | sqlite3_stmt *pStmt; |
| 41 | const char *stmtTail; |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 42 | int rc; |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 43 | char zMessage[1024]; |
| 44 | zMessage[1023] = '\0'; |
| 45 | |
| 46 | sqlite3_uint64 iTime; |
| 47 | |
| 48 | sqlite3_snprintf(1023, zMessage, "sqlite3_prepare_v2: %s", zSql); |
| 49 | sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage); |
| 50 | |
| 51 | iTime = sqlite3Hwtime(); |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 52 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &stmtTail); |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 53 | iTime = sqlite3Hwtime() - iTime; |
| 54 | sqlite3_instvfs_binarylog_call(pInstVfs,BINARYLOG_PREPARE_V2,iTime,rc,zSql); |
| 55 | |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 56 | if( rc==SQLITE_OK ){ |
| 57 | int nRow = 0; |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 58 | |
| 59 | sqlite3_snprintf(1023, zMessage, "sqlite3_step loop: %s", zSql); |
| 60 | sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage); |
| 61 | iTime = sqlite3Hwtime(); |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 62 | while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; } |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 63 | iTime = sqlite3Hwtime() - iTime; |
| 64 | sqlite3_instvfs_binarylog_call(pInstVfs, BINARYLOG_STEP, iTime, rc, zSql); |
| 65 | |
| 66 | sqlite3_snprintf(1023, zMessage, "sqlite3_finalize: %s", zSql); |
| 67 | sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage); |
| 68 | iTime = sqlite3Hwtime(); |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 69 | rc = sqlite3_finalize(pStmt); |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 70 | iTime = sqlite3Hwtime() - iTime; |
| 71 | sqlite3_instvfs_binarylog_call(pInstVfs, BINARYLOG_FINALIZE, iTime, rc, zSql); |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 75 | static int stringcompare(const char *zLeft, const char *zRight){ |
| 76 | int ii; |
| 77 | for(ii=0; zLeft[ii] && zRight[ii]; ii++){ |
| 78 | if( zLeft[ii]!=zRight[ii] ) return 0; |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 79 | } |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 80 | return( zLeft[ii]==zRight[ii] ); |
| 81 | } |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 82 | |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 83 | static char *readScriptFile(const char *zFile, int *pnScript){ |
| 84 | sqlite3_vfs *pVfs = sqlite3_vfs_find(0); |
| 85 | sqlite3_file *p; |
| 86 | int rc; |
| 87 | sqlite3_int64 nByte; |
| 88 | char *zData = 0; |
| 89 | int flags = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_DB; |
| 90 | |
| 91 | p = (sqlite3_file *)malloc(pVfs->szOsFile); |
| 92 | rc = pVfs->xOpen(pVfs, zFile, p, flags, &flags); |
| 93 | if( rc!=SQLITE_OK ){ |
| 94 | goto error_out; |
| 95 | } |
| 96 | |
| 97 | rc = p->pMethods->xFileSize(p, &nByte); |
| 98 | if( rc!=SQLITE_OK ){ |
| 99 | goto close_out; |
| 100 | } |
| 101 | |
| 102 | zData = (char *)malloc(nByte+1); |
| 103 | rc = p->pMethods->xRead(p, zData, nByte, 0); |
| 104 | if( rc!=SQLITE_OK ){ |
| 105 | goto close_out; |
| 106 | } |
| 107 | zData[nByte] = '\0'; |
| 108 | |
| 109 | p->pMethods->xClose(p); |
| 110 | free(p); |
| 111 | *pnScript = nByte; |
| 112 | return zData; |
| 113 | |
| 114 | close_out: |
| 115 | p->pMethods->xClose(p); |
| 116 | |
| 117 | error_out: |
| 118 | free(p); |
| 119 | free(zData); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int main(int argc, char **argv){ |
| 124 | |
| 125 | const char zUsageMsg[] = |
| 126 | "Usage: %s options...\n" |
| 127 | " where available options are:\n" |
| 128 | "\n" |
| 129 | " -db DATABASE-FILE (database file to operate on)\n" |
| 130 | " -script SCRIPT-FILE (script file to read sql from)\n" |
| 131 | " -log LOG-FILE (log file to create)\n" |
| 132 | " -logdata (log all data to log file)\n" |
| 133 | "\n" |
| 134 | " Options -db, -script and -log are compulsory\n" |
| 135 | "\n" |
| 136 | ; |
| 137 | |
| 138 | const char *zDb = 0; |
| 139 | const char *zScript = 0; |
| 140 | const char *zLog = 0; |
| 141 | int logdata = 0; |
| 142 | |
| 143 | int ii; |
| 144 | int i, j; |
| 145 | int rc; |
| 146 | |
| 147 | sqlite3_vfs *pInstVfs; /* Instrumentation VFS */ |
| 148 | |
| 149 | char *zSql = 0; |
| 150 | int nSql; |
| 151 | |
| 152 | sqlite3 *db; |
| 153 | |
| 154 | for(ii=1; ii<argc; ii++){ |
| 155 | if( stringcompare("-db", argv[ii]) && (ii+1)<argc ){ |
| 156 | zDb = argv[++ii]; |
| 157 | } |
| 158 | |
| 159 | else if( stringcompare("-script", argv[ii]) && (ii+1)<argc ){ |
| 160 | zScript = argv[++ii]; |
| 161 | } |
| 162 | |
| 163 | else if( stringcompare("-log", argv[ii]) && (ii+1)<argc ){ |
| 164 | zLog = argv[++ii]; |
| 165 | } |
| 166 | |
| 167 | else if( stringcompare("-logdata", argv[ii]) ){ |
| 168 | logdata = 1; |
| 169 | } |
| 170 | |
| 171 | else { |
| 172 | goto usage; |
| 173 | } |
| 174 | } |
| 175 | if( !zDb || !zScript || !zLog ) goto usage; |
| 176 | |
| 177 | zSql = readScriptFile(zScript, &nSql); |
| 178 | if( !zSql ){ |
| 179 | fprintf(stderr, "Failed to read script file\n"); |
| 180 | return -1; |
| 181 | } |
| 182 | |
| 183 | pInstVfs = sqlite3_instvfs_binarylog("logging", 0, zLog, logdata); |
| 184 | |
| 185 | rc = sqlite3_open_v2( |
| 186 | zDb, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, "logging" |
| 187 | ); |
| 188 | if( rc!=SQLITE_OK ){ |
| 189 | fprintf(stderr, "Failed to open db: %s\n", sqlite3_errmsg(db)); |
| 190 | return -2; |
| 191 | } |
| 192 | |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 193 | for(i=j=0; j<nSql; j++){ |
| 194 | if( zSql[j]==';' ){ |
| 195 | int isComplete; |
| 196 | char c = zSql[j+1]; |
| 197 | zSql[j+1] = 0; |
| 198 | isComplete = sqlite3_complete(&zSql[i]); |
| 199 | zSql[j+1] = c; |
| 200 | if( isComplete ){ |
| 201 | zSql[j] = 0; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 202 | while( i<j && ISSPACE(zSql[i]) ){ i++; } |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 203 | if( i<j ){ |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 204 | prepareAndRun(pInstVfs, db, &zSql[i]); |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 205 | } |
| 206 | zSql[j] = ';'; |
| 207 | i = j+1; |
| 208 | } |
| 209 | } |
| 210 | } |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 211 | |
| 212 | sqlite3_instvfs_destroy(pInstVfs); |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 213 | return 0; |
danielk1977 | d186854 | 2008-06-11 11:00:31 +0000 | [diff] [blame] | 214 | |
| 215 | usage: |
| 216 | fprintf(stderr, zUsageMsg, argv[0]); |
| 217 | return -3; |
drh | c2a721f | 2008-04-11 21:20:48 +0000 | [diff] [blame] | 218 | } |