drh | 7720d64 | 2008-04-03 16:01:27 +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 | ** 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. |
| 9 | ** |
| 10 | ** To compile this program, first compile the SQLite library separately |
| 11 | ** will full optimizations. For example: |
| 12 | ** |
| 13 | ** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c |
| 14 | ** |
| 15 | ** Then link against this program. But to do optimize this program |
| 16 | ** because that defeats the hi-res timer. |
| 17 | ** |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 18 | ** gcc speedtest16.c sqlite3.o -ldl -I../src |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 19 | ** |
| 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: |
| 22 | ** |
drh | 17afdd2 | 2008-04-03 17:57:24 +0000 | [diff] [blame] | 23 | ** ./a.out database.db test.sql |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 24 | */ |
| 25 | #include <stdio.h> |
| 26 | #include <string.h> |
| 27 | #include <stdlib.h> |
drh | d543a42 | 2008-04-03 19:40:59 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
| 29 | #include <unistd.h> |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 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 | |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 34 | /* |
| 35 | ** hwtime.h contains inline assembler code for implementing |
| 36 | ** high-performance timing routines. |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 37 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 38 | #include "hwtime.h" |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 39 | |
| 40 | /* |
| 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. |
| 44 | */ |
| 45 | static void *asciiToUtf16le(const char *z){ |
| 46 | int n = strlen(z); |
| 47 | char *z16; |
| 48 | int i, j; |
| 49 | |
| 50 | z16 = malloc( n*2 + 2 ); |
| 51 | for(i=j=0; i<=n; i++){ |
| 52 | z16[j++] = z[i]; |
| 53 | z16[j++] = 0; |
| 54 | } |
| 55 | return (void*)z16; |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | ** Timers |
| 60 | */ |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 61 | static sqlite_uint64 prepTime = 0; |
| 62 | static sqlite_uint64 runTime = 0; |
| 63 | static sqlite_uint64 finalizeTime = 0; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | ** Prepare and run a single statement of SQL. |
| 67 | */ |
| 68 | static void prepareAndRun(sqlite3 *db, const char *zSql){ |
| 69 | void *utf16; |
| 70 | sqlite3_stmt *pStmt; |
| 71 | const void *stmtTail; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 72 | sqlite_uint64 iStart, iElapse; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 73 | int rc; |
| 74 | |
| 75 | printf("****************************************************************\n"); |
| 76 | printf("SQL statement: [%s]\n", zSql); |
| 77 | utf16 = asciiToUtf16le(zSql); |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 78 | iStart = sqlite3Hwtime(); |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 79 | rc = sqlite3_prepare16_v2(db, utf16, -1, &pStmt, &stmtTail); |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 80 | iElapse = sqlite3Hwtime() - iStart; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 81 | prepTime += iElapse; |
| 82 | printf("sqlite3_prepare16_v2() returns %d in %llu cycles\n", rc, iElapse); |
| 83 | if( rc==SQLITE_OK ){ |
| 84 | int nRow = 0; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 85 | iStart = sqlite3Hwtime(); |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 86 | while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; } |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 87 | iElapse = sqlite3Hwtime() - iStart; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 88 | runTime += iElapse; |
| 89 | printf("sqlite3_step() returns %d after %d rows in %llu cycles\n", |
| 90 | rc, nRow, iElapse); |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 91 | iStart = sqlite3Hwtime(); |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 92 | rc = sqlite3_finalize(pStmt); |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 93 | iElapse = sqlite3Hwtime() - iStart; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 94 | finalizeTime += iElapse; |
| 95 | printf("sqlite3_finalize() returns %d in %llu cycles\n", rc, iElapse); |
| 96 | } |
| 97 | free(utf16); |
| 98 | } |
| 99 | |
| 100 | int main(int argc, char **argv){ |
| 101 | void *utf16; |
| 102 | sqlite3 *db; |
| 103 | int rc; |
| 104 | int nSql; |
| 105 | char *zSql; |
| 106 | int i, j; |
| 107 | FILE *in; |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 108 | sqlite_uint64 iStart, iElapse; |
| 109 | sqlite_uint64 iSetup = 0; |
drh | 17afdd2 | 2008-04-03 17:57:24 +0000 | [diff] [blame] | 110 | int nStmt = 0; |
| 111 | int nByte = 0; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 112 | |
drh | 17afdd2 | 2008-04-03 17:57:24 +0000 | [diff] [blame] | 113 | if( argc!=3 ){ |
| 114 | fprintf(stderr, "Usage: %s FILENAME SQL-SCRIPT\n" |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 115 | "Runs SQL-SCRIPT as UTF16 against a UTF16 database\n", |
| 116 | argv[0]); |
| 117 | exit(1); |
| 118 | } |
drh | 17afdd2 | 2008-04-03 17:57:24 +0000 | [diff] [blame] | 119 | in = fopen(argv[2], "r"); |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 120 | fseek(in, 0L, SEEK_END); |
| 121 | nSql = ftell(in); |
| 122 | zSql = malloc( nSql+1 ); |
| 123 | fseek(in, 0L, SEEK_SET); |
| 124 | nSql = fread(zSql, 1, nSql, in); |
| 125 | zSql[nSql] = 0; |
| 126 | |
| 127 | printf("SQLite version: %d\n", sqlite3_libversion_number()); |
drh | 17afdd2 | 2008-04-03 17:57:24 +0000 | [diff] [blame] | 128 | unlink(argv[1]); |
| 129 | utf16 = asciiToUtf16le(argv[1]); |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 130 | iStart = sqlite3Hwtime(); |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 131 | rc = sqlite3_open16(utf16, &db); |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 132 | iElapse = sqlite3Hwtime() - iStart; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 133 | iSetup = iElapse; |
| 134 | printf("sqlite3_open16() returns %d in %llu cycles\n", rc, iElapse); |
| 135 | free(utf16); |
| 136 | for(i=j=0; j<nSql; j++){ |
| 137 | if( zSql[j]==';' ){ |
| 138 | int isComplete; |
| 139 | char c = zSql[j+1]; |
| 140 | zSql[j+1] = 0; |
| 141 | isComplete = sqlite3_complete(&zSql[i]); |
| 142 | zSql[j+1] = c; |
| 143 | if( isComplete ){ |
| 144 | zSql[j] = 0; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 145 | while( i<j && ISSPACE(zSql[i]) ){ i++; } |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 146 | if( i<j ){ |
drh | 17afdd2 | 2008-04-03 17:57:24 +0000 | [diff] [blame] | 147 | nStmt++; |
| 148 | nByte += j-i; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 149 | prepareAndRun(db, &zSql[i]); |
| 150 | } |
| 151 | zSql[j] = ';'; |
| 152 | i = j+1; |
| 153 | } |
| 154 | } |
| 155 | } |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 156 | iStart = sqlite3Hwtime(); |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 157 | sqlite3_close(db); |
shane | 9bcbdad | 2008-05-29 20:22:37 +0000 | [diff] [blame] | 158 | iElapse = sqlite3Hwtime() - iStart; |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 159 | iSetup += iElapse; |
| 160 | printf("sqlite3_close() returns in %llu cycles\n", iElapse); |
| 161 | printf("\n"); |
drh | 17afdd2 | 2008-04-03 17:57:24 +0000 | [diff] [blame] | 162 | printf("Statements run: %15d\n", nStmt); |
| 163 | printf("Bytes of SQL text: %15d\n", nByte); |
drh | 7720d64 | 2008-04-03 16:01:27 +0000 | [diff] [blame] | 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); |
| 170 | return 0; |
| 171 | } |