drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** A program for performance testing. |
| 3 | ** |
| 4 | ** The available command-line options are described below: |
| 5 | */ |
| 6 | static const char zHelp[] = |
| 7 | "Usage: %s [--options] DATABASE\n" |
| 8 | "Options:\n" |
| 9 | " --autovacuum Enable AUTOVACUUM mode\n" |
| 10 | " --cachesize N Set the cache size to N\n" |
| 11 | " --exclusive Enable locking_mode=EXCLUSIVE\n" |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 12 | " --explain Like --sqlonly but with added EXPLAIN keywords\n" |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 13 | " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 14 | " --incrvacuum Enable incremenatal vacuum mode\n" |
drh | 1dae26b | 2015-02-03 19:20:03 +0000 | [diff] [blame] | 15 | " --journal M Set the journal_mode to M\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 16 | " --key KEY Set the encryption key to KEY\n" |
| 17 | " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n" |
drh | 26f4198 | 2016-12-12 23:24:08 +0000 | [diff] [blame] | 18 | " --mmap SZ MMAP the first SZ bytes of the database file\n" |
drh | 2e43e96 | 2015-07-03 14:34:25 +0000 | [diff] [blame] | 19 | " --multithread Set multithreaded mode\n" |
| 20 | " --nomemstat Disable memory statistics\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 21 | " --nosync Set PRAGMA synchronous=OFF\n" |
| 22 | " --notnull Add NOT NULL constraints to table columns\n" |
| 23 | " --pagesize N Set the page size to N\n" |
| 24 | " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n" |
| 25 | " --primarykey Use PRIMARY KEY instead of UNIQUE where appropriate\n" |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 26 | " --repeat N Repeat each SELECT N times (default: 1)\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 27 | " --reprepare Reprepare each statement upon every invocation\n" |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 28 | " --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n" |
drh | 2e43e96 | 2015-07-03 14:34:25 +0000 | [diff] [blame] | 29 | " --serialized Set serialized threading mode\n" |
| 30 | " --singlethread Set single-threaded mode - disables all mutexing\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 31 | " --sqlonly No-op. Only show the SQL that would have been run.\n" |
drh | 0d84718 | 2015-07-02 01:38:39 +0000 | [diff] [blame] | 32 | " --shrink-memory Invoke sqlite3_db_release_memory() frequently.\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 33 | " --size N Relative test size. Default=100\n" |
| 34 | " --stats Show statistics at the end\n" |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 35 | " --temp N N from 0 to 9. 0: no temp table. 9: all temp tables\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 36 | " --testset T Run test-set T\n" |
| 37 | " --trace Turn on SQL tracing\n" |
drh | 46a06bb | 2014-04-18 13:57:39 +0000 | [diff] [blame] | 38 | " --threads N Use up to N threads for sorting\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 39 | " --utf16be Set text encoding to UTF-16BE\n" |
| 40 | " --utf16le Set text encoding to UTF-16LE\n" |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 41 | " --verify Run additional verification steps.\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 42 | " --without-rowid Use WITHOUT ROWID where appropriate\n" |
| 43 | ; |
| 44 | |
| 45 | |
| 46 | #include "sqlite3.h" |
| 47 | #include <assert.h> |
| 48 | #include <stdio.h> |
| 49 | #include <stdlib.h> |
| 50 | #include <stdarg.h> |
| 51 | #include <string.h> |
| 52 | #include <ctype.h> |
drh | eb25713 | 2016-12-31 14:33:05 +0000 | [diff] [blame] | 53 | #ifndef _WIN32 |
| 54 | # include <unistd.h> |
| 55 | #else |
| 56 | # include <io.h> |
| 57 | #endif |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 58 | #define ISSPACE(X) isspace((unsigned char)(X)) |
| 59 | #define ISDIGIT(X) isdigit((unsigned char)(X)) |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 60 | |
drh | 4307690 | 2015-06-18 15:26:09 +0000 | [diff] [blame] | 61 | #if SQLITE_VERSION_NUMBER<3005000 |
| 62 | # define sqlite3_int64 sqlite_int64 |
| 63 | #endif |
drh | cfb8f8d | 2015-07-23 20:44:49 +0000 | [diff] [blame] | 64 | #ifdef SQLITE_ENABLE_RBU |
| 65 | # include "sqlite3rbu.h" |
dan | 54fc214 | 2015-03-05 16:21:20 +0000 | [diff] [blame] | 66 | #endif |
| 67 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 68 | /* All global state is held in this structure */ |
| 69 | static struct Global { |
| 70 | sqlite3 *db; /* The open database connection */ |
| 71 | sqlite3_stmt *pStmt; /* Current SQL statement */ |
| 72 | sqlite3_int64 iStart; /* Start-time for the current test */ |
| 73 | sqlite3_int64 iTotal; /* Total time */ |
| 74 | int bWithoutRowid; /* True for --without-rowid */ |
| 75 | int bReprepare; /* True to reprepare the SQL on each rerun */ |
| 76 | int bSqlOnly; /* True to print the SQL once only */ |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 77 | int bExplain; /* Print SQL with EXPLAIN prefix */ |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 78 | int bVerify; /* Try to verify that results are correct */ |
drh | 0d84718 | 2015-07-02 01:38:39 +0000 | [diff] [blame] | 79 | int bMemShrink; /* Call sqlite3_db_release_memory() often */ |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 80 | int eTemp; /* 0: no TEMP. 9: always TEMP. */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 81 | int szTest; /* Scale factor for test iterations */ |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 82 | int nRepeat; /* Repeat selects this many times */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 83 | const char *zWR; /* Might be WITHOUT ROWID */ |
| 84 | const char *zNN; /* Might be NOT NULL */ |
| 85 | const char *zPK; /* Might be UNIQUE or PRIMARY KEY */ |
| 86 | unsigned int x, y; /* Pseudo-random number generator state */ |
| 87 | int nResult; /* Size of the current result */ |
| 88 | char zResult[3000]; /* Text of the current result */ |
| 89 | } g; |
| 90 | |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 91 | /* Return " TEMP" or "", as appropriate for creating a table. |
| 92 | */ |
| 93 | static const char *isTemp(int N){ |
| 94 | return g.eTemp>=N ? " TEMP" : ""; |
| 95 | } |
| 96 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 97 | |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 98 | /* Print an error message and exit */ |
| 99 | static void fatal_error(const char *zMsg, ...){ |
| 100 | va_list ap; |
| 101 | va_start(ap, zMsg); |
| 102 | vfprintf(stderr, zMsg, ap); |
| 103 | va_end(ap); |
| 104 | exit(1); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* |
| 108 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 109 | ** is not a hex digit. |
| 110 | */ |
| 111 | static int hexDigitValue(char c){ |
| 112 | if( c>='0' && c<='9' ) return c - '0'; |
| 113 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 114 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 115 | return -1; |
| 116 | } |
| 117 | |
drh | 290ea40 | 2013-12-01 18:10:01 +0000 | [diff] [blame] | 118 | /* Provide an alternative to sqlite3_stricmp() in older versions of |
| 119 | ** SQLite */ |
| 120 | #if SQLITE_VERSION_NUMBER<3007011 |
| 121 | # define sqlite3_stricmp strcmp |
| 122 | #endif |
| 123 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 124 | /* |
| 125 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 126 | */ |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 127 | static int integerValue(const char *zArg){ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 128 | sqlite3_int64 v = 0; |
| 129 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 130 | { "KiB", 1024 }, |
| 131 | { "MiB", 1024*1024 }, |
| 132 | { "GiB", 1024*1024*1024 }, |
| 133 | { "KB", 1000 }, |
| 134 | { "MB", 1000000 }, |
| 135 | { "GB", 1000000000 }, |
| 136 | { "K", 1000 }, |
| 137 | { "M", 1000000 }, |
| 138 | { "G", 1000000000 }, |
| 139 | }; |
| 140 | int i; |
| 141 | int isNeg = 0; |
| 142 | if( zArg[0]=='-' ){ |
| 143 | isNeg = 1; |
| 144 | zArg++; |
| 145 | }else if( zArg[0]=='+' ){ |
| 146 | zArg++; |
| 147 | } |
| 148 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 149 | int x; |
| 150 | zArg += 2; |
| 151 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 152 | v = (v<<4) + x; |
| 153 | zArg++; |
| 154 | } |
| 155 | }else{ |
| 156 | while( isdigit(zArg[0]) ){ |
| 157 | v = v*10 + zArg[0] - '0'; |
| 158 | zArg++; |
| 159 | } |
| 160 | } |
| 161 | for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ |
| 162 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 163 | v *= aMult[i].iMult; |
| 164 | break; |
| 165 | } |
| 166 | } |
mistachkin | b87875a | 2013-11-27 18:00:20 +0000 | [diff] [blame] | 167 | if( v>0x7fffffff ) fatal_error("parameter too large - max 2147483648"); |
drh | dcb5fa0 | 2013-11-27 14:50:51 +0000 | [diff] [blame] | 168 | return (int)(isNeg? -v : v); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* Return the current wall-clock time, in milliseconds */ |
| 172 | sqlite3_int64 speedtest1_timestamp(void){ |
drh | 4307690 | 2015-06-18 15:26:09 +0000 | [diff] [blame] | 173 | #if SQLITE_VERSION_NUMBER<3005000 |
| 174 | return 0; |
| 175 | #else |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 176 | static sqlite3_vfs *clockVfs = 0; |
| 177 | sqlite3_int64 t; |
| 178 | if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); |
drh | d79e9c5 | 2013-12-02 01:24:05 +0000 | [diff] [blame] | 179 | #if SQLITE_VERSION_NUMBER>=3007000 |
drh | 290ea40 | 2013-12-01 18:10:01 +0000 | [diff] [blame] | 180 | if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 181 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
drh | d79e9c5 | 2013-12-02 01:24:05 +0000 | [diff] [blame] | 182 | }else |
| 183 | #endif |
| 184 | { |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 185 | double r; |
| 186 | clockVfs->xCurrentTime(clockVfs, &r); |
| 187 | t = (sqlite3_int64)(r*86400000.0); |
| 188 | } |
| 189 | return t; |
drh | 4307690 | 2015-06-18 15:26:09 +0000 | [diff] [blame] | 190 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* Return a pseudo-random unsigned integer */ |
| 194 | unsigned int speedtest1_random(void){ |
| 195 | g.x = (g.x>>1) ^ ((1+~(g.x&1)) & 0xd0000001); |
| 196 | g.y = g.y*1103515245 + 12345; |
| 197 | return g.x ^ g.y; |
| 198 | } |
| 199 | |
| 200 | /* Map the value in within the range of 1...limit into another |
| 201 | ** number in a way that is chatic and invertable. |
| 202 | */ |
| 203 | unsigned swizzle(unsigned in, unsigned limit){ |
| 204 | unsigned out = 0; |
| 205 | while( limit ){ |
| 206 | out = (out<<1) | (in&1); |
| 207 | in >>= 1; |
| 208 | limit >>= 1; |
| 209 | } |
| 210 | return out; |
| 211 | } |
| 212 | |
| 213 | /* Round up a number so that it is a power of two minus one |
| 214 | */ |
| 215 | unsigned roundup_allones(unsigned limit){ |
| 216 | unsigned m = 1; |
| 217 | while( m<limit ) m = (m<<1)+1; |
| 218 | return m; |
| 219 | } |
| 220 | |
| 221 | /* The speedtest1_numbername procedure below converts its argment (an integer) |
| 222 | ** into a string which is the English-language name for that number. |
| 223 | ** The returned string should be freed with sqlite3_free(). |
| 224 | ** |
| 225 | ** Example: |
| 226 | ** |
| 227 | ** speedtest1_numbername(123) -> "one hundred twenty three" |
| 228 | */ |
| 229 | int speedtest1_numbername(unsigned int n, char *zOut, int nOut){ |
| 230 | static const char *ones[] = { "zero", "one", "two", "three", "four", "five", |
| 231 | "six", "seven", "eight", "nine", "ten", "eleven", "twelve", |
| 232 | "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", |
| 233 | "eighteen", "nineteen" }; |
| 234 | static const char *tens[] = { "", "ten", "twenty", "thirty", "forty", |
| 235 | "fifty", "sixty", "seventy", "eighty", "ninety" }; |
| 236 | int i = 0; |
| 237 | |
| 238 | if( n>=1000000000 ){ |
| 239 | i += speedtest1_numbername(n/1000000000, zOut+i, nOut-i); |
| 240 | sqlite3_snprintf(nOut-i, zOut+i, " billion"); |
| 241 | i += (int)strlen(zOut+i); |
| 242 | n = n % 1000000000; |
| 243 | } |
| 244 | if( n>=1000000 ){ |
| 245 | if( i && i<nOut-1 ) zOut[i++] = ' '; |
| 246 | i += speedtest1_numbername(n/1000000, zOut+i, nOut-i); |
| 247 | sqlite3_snprintf(nOut-i, zOut+i, " million"); |
| 248 | i += (int)strlen(zOut+i); |
| 249 | n = n % 1000000; |
| 250 | } |
| 251 | if( n>=1000 ){ |
| 252 | if( i && i<nOut-1 ) zOut[i++] = ' '; |
| 253 | i += speedtest1_numbername(n/1000, zOut+i, nOut-i); |
| 254 | sqlite3_snprintf(nOut-i, zOut+i, " thousand"); |
| 255 | i += (int)strlen(zOut+i); |
| 256 | n = n % 1000; |
| 257 | } |
| 258 | if( n>=100 ){ |
| 259 | if( i && i<nOut-1 ) zOut[i++] = ' '; |
| 260 | sqlite3_snprintf(nOut-i, zOut+i, "%s hundred", ones[n/100]); |
| 261 | i += (int)strlen(zOut+i); |
| 262 | n = n % 100; |
| 263 | } |
| 264 | if( n>=20 ){ |
| 265 | if( i && i<nOut-1 ) zOut[i++] = ' '; |
| 266 | sqlite3_snprintf(nOut-i, zOut+i, "%s", tens[n/10]); |
| 267 | i += (int)strlen(zOut+i); |
| 268 | n = n % 10; |
| 269 | } |
| 270 | if( n>0 ){ |
| 271 | if( i && i<nOut-1 ) zOut[i++] = ' '; |
| 272 | sqlite3_snprintf(nOut-i, zOut+i, "%s", ones[n]); |
| 273 | i += (int)strlen(zOut+i); |
| 274 | } |
| 275 | if( i==0 ){ |
| 276 | sqlite3_snprintf(nOut-i, zOut+i, "zero"); |
| 277 | i += (int)strlen(zOut+i); |
| 278 | } |
| 279 | return i; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /* Start a new test case */ |
| 284 | #define NAMEWIDTH 60 |
| 285 | static const char zDots[] = |
| 286 | "......................................................................."; |
| 287 | void speedtest1_begin_test(int iTestNum, const char *zTestName, ...){ |
| 288 | int n = (int)strlen(zTestName); |
| 289 | char *zName; |
| 290 | va_list ap; |
| 291 | va_start(ap, zTestName); |
| 292 | zName = sqlite3_vmprintf(zTestName, ap); |
| 293 | va_end(ap); |
| 294 | n = (int)strlen(zName); |
| 295 | if( n>NAMEWIDTH ){ |
| 296 | zName[NAMEWIDTH] = 0; |
| 297 | n = NAMEWIDTH; |
| 298 | } |
| 299 | if( g.bSqlOnly ){ |
| 300 | printf("/* %4d - %s%.*s */\n", iTestNum, zName, NAMEWIDTH-n, zDots); |
| 301 | }else{ |
| 302 | printf("%4d - %s%.*s ", iTestNum, zName, NAMEWIDTH-n, zDots); |
| 303 | fflush(stdout); |
| 304 | } |
| 305 | sqlite3_free(zName); |
| 306 | g.nResult = 0; |
| 307 | g.iStart = speedtest1_timestamp(); |
drh | dcb5fa0 | 2013-11-27 14:50:51 +0000 | [diff] [blame] | 308 | g.x = 0xad131d0b; |
| 309 | g.y = 0x44f9eac8; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* Complete a test case */ |
| 313 | void speedtest1_end_test(void){ |
| 314 | sqlite3_int64 iElapseTime = speedtest1_timestamp() - g.iStart; |
| 315 | if( !g.bSqlOnly ){ |
| 316 | g.iTotal += iElapseTime; |
| 317 | printf("%4d.%03ds\n", (int)(iElapseTime/1000), (int)(iElapseTime%1000)); |
| 318 | } |
| 319 | if( g.pStmt ){ |
| 320 | sqlite3_finalize(g.pStmt); |
| 321 | g.pStmt = 0; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* Report end of testing */ |
| 326 | void speedtest1_final(void){ |
| 327 | if( !g.bSqlOnly ){ |
| 328 | printf(" TOTAL%.*s %4d.%03ds\n", NAMEWIDTH-5, zDots, |
| 329 | (int)(g.iTotal/1000), (int)(g.iTotal%1000)); |
| 330 | } |
| 331 | } |
| 332 | |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 333 | /* Print an SQL statement to standard output */ |
| 334 | static void printSql(const char *zSql){ |
| 335 | int n = (int)strlen(zSql); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 336 | while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ){ n--; } |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 337 | if( g.bExplain ) printf("EXPLAIN "); |
| 338 | printf("%.*s;\n", n, zSql); |
| 339 | if( g.bExplain |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 340 | #if SQLITE_VERSION_NUMBER>=3007017 |
drh | 2555550 | 2013-12-21 17:14:58 +0000 | [diff] [blame] | 341 | && ( sqlite3_strglob("CREATE *", zSql)==0 |
| 342 | || sqlite3_strglob("DROP *", zSql)==0 |
| 343 | || sqlite3_strglob("ALTER *", zSql)==0 |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 344 | ) |
drh | 2555550 | 2013-12-21 17:14:58 +0000 | [diff] [blame] | 345 | #endif |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 346 | ){ |
| 347 | printf("%.*s;\n", n, zSql); |
| 348 | } |
| 349 | } |
| 350 | |
drh | 0d84718 | 2015-07-02 01:38:39 +0000 | [diff] [blame] | 351 | /* Shrink memory used, if appropriate and if the SQLite version is capable |
| 352 | ** of doing so. |
| 353 | */ |
| 354 | void speedtest1_shrink_memory(void){ |
| 355 | #if SQLITE_VERSION_NUMBER>=3007010 |
| 356 | if( g.bMemShrink ) sqlite3_db_release_memory(g.db); |
| 357 | #endif |
| 358 | } |
| 359 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 360 | /* Run SQL */ |
| 361 | void speedtest1_exec(const char *zFormat, ...){ |
| 362 | va_list ap; |
| 363 | char *zSql; |
| 364 | va_start(ap, zFormat); |
| 365 | zSql = sqlite3_vmprintf(zFormat, ap); |
| 366 | va_end(ap); |
| 367 | if( g.bSqlOnly ){ |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 368 | printSql(zSql); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 369 | }else{ |
drh | e19f832 | 2013-11-23 11:45:58 +0000 | [diff] [blame] | 370 | char *zErrMsg = 0; |
| 371 | int rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg); |
| 372 | if( zErrMsg ) fatal_error("SQL error: %s\n%s\n", zErrMsg, zSql); |
| 373 | if( rc!=SQLITE_OK ) fatal_error("exec error: %s\n", sqlite3_errmsg(g.db)); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 374 | } |
| 375 | sqlite3_free(zSql); |
drh | 0d84718 | 2015-07-02 01:38:39 +0000 | [diff] [blame] | 376 | speedtest1_shrink_memory(); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* Prepare an SQL statement */ |
| 380 | void speedtest1_prepare(const char *zFormat, ...){ |
| 381 | va_list ap; |
| 382 | char *zSql; |
| 383 | va_start(ap, zFormat); |
| 384 | zSql = sqlite3_vmprintf(zFormat, ap); |
| 385 | va_end(ap); |
| 386 | if( g.bSqlOnly ){ |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 387 | printSql(zSql); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 388 | }else{ |
| 389 | int rc; |
| 390 | if( g.pStmt ) sqlite3_finalize(g.pStmt); |
| 391 | rc = sqlite3_prepare_v2(g.db, zSql, -1, &g.pStmt, 0); |
| 392 | if( rc ){ |
| 393 | fatal_error("SQL error: %s\n", sqlite3_errmsg(g.db)); |
| 394 | } |
| 395 | } |
| 396 | sqlite3_free(zSql); |
| 397 | } |
| 398 | |
| 399 | /* Run an SQL statement previously prepared */ |
| 400 | void speedtest1_run(void){ |
| 401 | int i, n, len; |
| 402 | if( g.bSqlOnly ) return; |
| 403 | assert( g.pStmt ); |
| 404 | g.nResult = 0; |
| 405 | while( sqlite3_step(g.pStmt)==SQLITE_ROW ){ |
| 406 | n = sqlite3_column_count(g.pStmt); |
| 407 | for(i=0; i<n; i++){ |
| 408 | const char *z = (const char*)sqlite3_column_text(g.pStmt, i); |
| 409 | if( z==0 ) z = "nil"; |
| 410 | len = (int)strlen(z); |
| 411 | if( g.nResult+len<sizeof(g.zResult)-2 ){ |
| 412 | if( g.nResult>0 ) g.zResult[g.nResult++] = ' '; |
| 413 | memcpy(g.zResult + g.nResult, z, len+1); |
| 414 | g.nResult += len; |
| 415 | } |
| 416 | } |
| 417 | } |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 418 | #if SQLITE_VERSION_NUMBER>=3006001 |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 419 | if( g.bReprepare ){ |
| 420 | sqlite3_stmt *pNew; |
| 421 | sqlite3_prepare_v2(g.db, sqlite3_sql(g.pStmt), -1, &pNew, 0); |
| 422 | sqlite3_finalize(g.pStmt); |
| 423 | g.pStmt = pNew; |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 424 | }else |
| 425 | #endif |
| 426 | { |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 427 | sqlite3_reset(g.pStmt); |
| 428 | } |
drh | 0d84718 | 2015-07-02 01:38:39 +0000 | [diff] [blame] | 429 | speedtest1_shrink_memory(); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 430 | } |
| 431 | |
drh | 98ef26b | 2016-09-21 23:58:49 +0000 | [diff] [blame] | 432 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 433 | /* The sqlite3_trace() callback function */ |
| 434 | static void traceCallback(void *NotUsed, const char *zSql){ |
| 435 | int n = (int)strlen(zSql); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 436 | while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ) n--; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 437 | fprintf(stderr,"%.*s;\n", n, zSql); |
| 438 | } |
drh | 98ef26b | 2016-09-21 23:58:49 +0000 | [diff] [blame] | 439 | #endif /* SQLITE_OMIT_DEPRECATED */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 440 | |
| 441 | /* Substitute random() function that gives the same random |
| 442 | ** sequence on each run, for repeatability. */ |
| 443 | static void randomFunc( |
| 444 | sqlite3_context *context, |
| 445 | int NotUsed, |
| 446 | sqlite3_value **NotUsed2 |
| 447 | ){ |
| 448 | sqlite3_result_int64(context, (sqlite3_int64)speedtest1_random()); |
| 449 | } |
| 450 | |
drh | ae28d6e | 2013-12-21 00:04:37 +0000 | [diff] [blame] | 451 | /* Estimate the square root of an integer */ |
| 452 | static int est_square_root(int x){ |
| 453 | int y0 = x/2; |
| 454 | int y1; |
| 455 | int n; |
| 456 | for(n=0; y0>0 && n<10; n++){ |
| 457 | y1 = (y0 + x/y0)/2; |
| 458 | if( y1==y0 ) break; |
| 459 | y0 = y1; |
| 460 | } |
| 461 | return y0; |
| 462 | } |
| 463 | |
drh | 89500dc | 2016-11-21 18:15:35 +0000 | [diff] [blame] | 464 | |
| 465 | #if SQLITE_VERSION_NUMBER<3005004 |
| 466 | /* |
| 467 | ** An implementation of group_concat(). Used only when testing older |
| 468 | ** versions of SQLite that lack the built-in group_concat(). |
| 469 | */ |
| 470 | struct groupConcat { |
| 471 | char *z; |
| 472 | int nAlloc; |
| 473 | int nUsed; |
| 474 | }; |
| 475 | static void groupAppend(struct groupConcat *p, const char *z, int n){ |
| 476 | if( p->nUsed+n >= p->nAlloc ){ |
| 477 | int n2 = (p->nAlloc+n+1)*2; |
| 478 | char *z2 = sqlite3_realloc(p->z, n2); |
| 479 | if( z2==0 ) return; |
| 480 | p->z = z2; |
| 481 | p->nAlloc = n2; |
| 482 | } |
| 483 | memcpy(p->z+p->nUsed, z, n); |
| 484 | p->nUsed += n; |
| 485 | } |
| 486 | static void groupStep( |
| 487 | sqlite3_context *context, |
| 488 | int argc, |
| 489 | sqlite3_value **argv |
| 490 | ){ |
| 491 | const char *zVal; |
| 492 | struct groupConcat *p; |
| 493 | const char *zSep; |
| 494 | int nVal, nSep; |
| 495 | assert( argc==1 || argc==2 ); |
| 496 | if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; |
| 497 | p= (struct groupConcat*)sqlite3_aggregate_context(context, sizeof(*p)); |
| 498 | |
| 499 | if( p ){ |
| 500 | int firstTerm = p->nUsed==0; |
| 501 | if( !firstTerm ){ |
| 502 | if( argc==2 ){ |
| 503 | zSep = (char*)sqlite3_value_text(argv[1]); |
| 504 | nSep = sqlite3_value_bytes(argv[1]); |
| 505 | }else{ |
| 506 | zSep = ","; |
| 507 | nSep = 1; |
| 508 | } |
| 509 | if( nSep ) groupAppend(p, zSep, nSep); |
| 510 | } |
| 511 | zVal = (char*)sqlite3_value_text(argv[0]); |
| 512 | nVal = sqlite3_value_bytes(argv[0]); |
| 513 | if( zVal ) groupAppend(p, zVal, nVal); |
| 514 | } |
| 515 | } |
| 516 | static void groupFinal(sqlite3_context *context){ |
| 517 | struct groupConcat *p; |
| 518 | p = sqlite3_aggregate_context(context, 0); |
| 519 | if( p && p->z ){ |
| 520 | p->z[p->nUsed] = 0; |
| 521 | sqlite3_result_text(context, p->z, p->nUsed, sqlite3_free); |
| 522 | } |
| 523 | } |
| 524 | #endif |
| 525 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 526 | /* |
| 527 | ** The main and default testset |
| 528 | */ |
| 529 | void testset_main(void){ |
| 530 | int i; /* Loop counter */ |
| 531 | int n; /* iteration count */ |
| 532 | int sz; /* Size of the tables */ |
| 533 | int maxb; /* Maximum swizzled value */ |
drh | ba69061 | 2016-10-18 15:29:57 +0000 | [diff] [blame] | 534 | unsigned x1 = 0, x2 = 0; /* Parameters */ |
| 535 | int len = 0; /* Length of the zNum[] string */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 536 | char zNum[2000]; /* A number name */ |
| 537 | |
| 538 | sz = n = g.szTest*500; |
drh | ba69061 | 2016-10-18 15:29:57 +0000 | [diff] [blame] | 539 | zNum[0] = 0; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 540 | maxb = roundup_allones(sz); |
| 541 | speedtest1_begin_test(100, "%d INSERTs into table with no index", n); |
| 542 | speedtest1_exec("BEGIN"); |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 543 | speedtest1_exec("CREATE%s TABLE t1(a INTEGER %s, b INTEGER %s, c TEXT %s);", |
| 544 | isTemp(9), g.zNN, g.zNN, g.zNN); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 545 | speedtest1_prepare("INSERT INTO t1 VALUES(?1,?2,?3); -- %d times", n); |
| 546 | for(i=1; i<=n; i++){ |
| 547 | x1 = swizzle(i,maxb); |
| 548 | speedtest1_numbername(x1, zNum, sizeof(zNum)); |
| 549 | sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1); |
| 550 | sqlite3_bind_int(g.pStmt, 2, i); |
| 551 | sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC); |
| 552 | speedtest1_run(); |
| 553 | } |
| 554 | speedtest1_exec("COMMIT"); |
| 555 | speedtest1_end_test(); |
| 556 | |
| 557 | |
| 558 | n = sz; |
| 559 | speedtest1_begin_test(110, "%d ordered INSERTS with one index/PK", n); |
| 560 | speedtest1_exec("BEGIN"); |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 561 | speedtest1_exec( |
| 562 | "CREATE%s TABLE t2(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s", |
| 563 | isTemp(5), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 564 | speedtest1_prepare("INSERT INTO t2 VALUES(?1,?2,?3); -- %d times", n); |
| 565 | for(i=1; i<=n; i++){ |
| 566 | x1 = swizzle(i,maxb); |
| 567 | speedtest1_numbername(x1, zNum, sizeof(zNum)); |
| 568 | sqlite3_bind_int(g.pStmt, 1, i); |
| 569 | sqlite3_bind_int64(g.pStmt, 2, (sqlite3_int64)x1); |
| 570 | sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC); |
| 571 | speedtest1_run(); |
| 572 | } |
| 573 | speedtest1_exec("COMMIT"); |
| 574 | speedtest1_end_test(); |
| 575 | |
| 576 | |
| 577 | n = sz; |
| 578 | speedtest1_begin_test(120, "%d unordered INSERTS with one index/PK", n); |
| 579 | speedtest1_exec("BEGIN"); |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 580 | speedtest1_exec( |
| 581 | "CREATE%s TABLE t3(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s", |
| 582 | isTemp(3), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 583 | speedtest1_prepare("INSERT INTO t3 VALUES(?1,?2,?3); -- %d times", n); |
| 584 | for(i=1; i<=n; i++){ |
| 585 | x1 = swizzle(i,maxb); |
| 586 | speedtest1_numbername(x1, zNum, sizeof(zNum)); |
| 587 | sqlite3_bind_int(g.pStmt, 2, i); |
| 588 | sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1); |
| 589 | sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC); |
| 590 | speedtest1_run(); |
| 591 | } |
| 592 | speedtest1_exec("COMMIT"); |
| 593 | speedtest1_end_test(); |
| 594 | |
drh | 89500dc | 2016-11-21 18:15:35 +0000 | [diff] [blame] | 595 | #if SQLITE_VERSION_NUMBER<3005004 |
| 596 | sqlite3_create_function(g.db, "group_concat", 1, SQLITE_UTF8, 0, |
| 597 | 0, groupStep, groupFinal); |
| 598 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 599 | |
drh | 5e8980d | 2014-03-25 20:28:38 +0000 | [diff] [blame] | 600 | n = 25; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 601 | speedtest1_begin_test(130, "%d SELECTS, numeric BETWEEN, unindexed", n); |
| 602 | speedtest1_exec("BEGIN"); |
| 603 | speedtest1_prepare( |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 604 | "SELECT count(*), avg(b), sum(length(c)), group_concat(c) FROM t1\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 605 | " WHERE b BETWEEN ?1 AND ?2; -- %d times", n |
| 606 | ); |
| 607 | for(i=1; i<=n; i++){ |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 608 | if( (i-1)%g.nRepeat==0 ){ |
| 609 | x1 = speedtest1_random()%maxb; |
| 610 | x2 = speedtest1_random()%10 + sz/5000 + x1; |
| 611 | } |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 612 | sqlite3_bind_int(g.pStmt, 1, x1); |
| 613 | sqlite3_bind_int(g.pStmt, 2, x2); |
| 614 | speedtest1_run(); |
| 615 | } |
| 616 | speedtest1_exec("COMMIT"); |
| 617 | speedtest1_end_test(); |
| 618 | |
| 619 | |
drh | 5e8980d | 2014-03-25 20:28:38 +0000 | [diff] [blame] | 620 | n = 10; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 621 | speedtest1_begin_test(140, "%d SELECTS, LIKE, unindexed", n); |
| 622 | speedtest1_exec("BEGIN"); |
| 623 | speedtest1_prepare( |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 624 | "SELECT count(*), avg(b), sum(length(c)), group_concat(c) FROM t1\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 625 | " WHERE c LIKE ?1; -- %d times", n |
| 626 | ); |
| 627 | for(i=1; i<=n; i++){ |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 628 | if( (i-1)%g.nRepeat==0 ){ |
| 629 | x1 = speedtest1_random()%maxb; |
| 630 | zNum[0] = '%'; |
| 631 | len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); |
| 632 | zNum[len] = '%'; |
| 633 | zNum[len+1] = 0; |
| 634 | } |
drh | 0a22c86 | 2016-11-08 16:27:59 +0000 | [diff] [blame] | 635 | sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 636 | speedtest1_run(); |
| 637 | } |
| 638 | speedtest1_exec("COMMIT"); |
| 639 | speedtest1_end_test(); |
| 640 | |
| 641 | |
drh | 5e8980d | 2014-03-25 20:28:38 +0000 | [diff] [blame] | 642 | n = 10; |
drh | c872966 | 2014-03-25 17:45:49 +0000 | [diff] [blame] | 643 | speedtest1_begin_test(142, "%d SELECTS w/ORDER BY, unindexed", n); |
| 644 | speedtest1_exec("BEGIN"); |
| 645 | speedtest1_prepare( |
| 646 | "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n" |
| 647 | " ORDER BY a; -- %d times", n |
| 648 | ); |
| 649 | for(i=1; i<=n; i++){ |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 650 | if( (i-1)%g.nRepeat==0 ){ |
| 651 | x1 = speedtest1_random()%maxb; |
| 652 | zNum[0] = '%'; |
| 653 | len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); |
| 654 | zNum[len] = '%'; |
| 655 | zNum[len+1] = 0; |
| 656 | } |
drh | 0a22c86 | 2016-11-08 16:27:59 +0000 | [diff] [blame] | 657 | sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); |
drh | c872966 | 2014-03-25 17:45:49 +0000 | [diff] [blame] | 658 | speedtest1_run(); |
| 659 | } |
| 660 | speedtest1_exec("COMMIT"); |
| 661 | speedtest1_end_test(); |
| 662 | |
dan | 54fc214 | 2015-03-05 16:21:20 +0000 | [diff] [blame] | 663 | n = 10; /* g.szTest/5; */ |
drh | c872966 | 2014-03-25 17:45:49 +0000 | [diff] [blame] | 664 | speedtest1_begin_test(145, "%d SELECTS w/ORDER BY and LIMIT, unindexed", n); |
drh | 0c60c1f | 2014-03-25 14:54:36 +0000 | [diff] [blame] | 665 | speedtest1_exec("BEGIN"); |
| 666 | speedtest1_prepare( |
| 667 | "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n" |
| 668 | " ORDER BY a LIMIT 10; -- %d times", n |
| 669 | ); |
| 670 | for(i=1; i<=n; i++){ |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 671 | if( (i-1)%g.nRepeat==0 ){ |
| 672 | x1 = speedtest1_random()%maxb; |
| 673 | zNum[0] = '%'; |
| 674 | len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); |
| 675 | zNum[len] = '%'; |
| 676 | zNum[len+1] = 0; |
| 677 | } |
drh | 0a22c86 | 2016-11-08 16:27:59 +0000 | [diff] [blame] | 678 | sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); |
drh | 0c60c1f | 2014-03-25 14:54:36 +0000 | [diff] [blame] | 679 | speedtest1_run(); |
| 680 | } |
| 681 | speedtest1_exec("COMMIT"); |
| 682 | speedtest1_end_test(); |
| 683 | |
| 684 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 685 | speedtest1_begin_test(150, "CREATE INDEX five times"); |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 686 | speedtest1_exec("BEGIN;"); |
| 687 | speedtest1_exec("CREATE UNIQUE INDEX t1b ON t1(b);"); |
| 688 | speedtest1_exec("CREATE INDEX t1c ON t1(c);"); |
| 689 | speedtest1_exec("CREATE UNIQUE INDEX t2b ON t2(b);"); |
| 690 | speedtest1_exec("CREATE INDEX t2c ON t2(c DESC);"); |
| 691 | speedtest1_exec("CREATE INDEX t3bc ON t3(b,c);"); |
| 692 | speedtest1_exec("COMMIT;"); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 693 | speedtest1_end_test(); |
| 694 | |
| 695 | |
| 696 | n = sz/5; |
| 697 | speedtest1_begin_test(160, "%d SELECTS, numeric BETWEEN, indexed", n); |
| 698 | speedtest1_exec("BEGIN"); |
| 699 | speedtest1_prepare( |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 700 | "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM t1\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 701 | " WHERE b BETWEEN ?1 AND ?2; -- %d times", n |
| 702 | ); |
| 703 | for(i=1; i<=n; i++){ |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 704 | if( (i-1)%g.nRepeat==0 ){ |
| 705 | x1 = speedtest1_random()%maxb; |
| 706 | x2 = speedtest1_random()%10 + sz/5000 + x1; |
| 707 | } |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 708 | sqlite3_bind_int(g.pStmt, 1, x1); |
| 709 | sqlite3_bind_int(g.pStmt, 2, x2); |
| 710 | speedtest1_run(); |
| 711 | } |
| 712 | speedtest1_exec("COMMIT"); |
| 713 | speedtest1_end_test(); |
| 714 | |
| 715 | |
| 716 | n = sz/5; |
| 717 | speedtest1_begin_test(161, "%d SELECTS, numeric BETWEEN, PK", n); |
| 718 | speedtest1_exec("BEGIN"); |
| 719 | speedtest1_prepare( |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 720 | "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM t2\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 721 | " WHERE a BETWEEN ?1 AND ?2; -- %d times", n |
| 722 | ); |
| 723 | for(i=1; i<=n; i++){ |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 724 | if( (i-1)%g.nRepeat==0 ){ |
| 725 | x1 = speedtest1_random()%maxb; |
| 726 | x2 = speedtest1_random()%10 + sz/5000 + x1; |
| 727 | } |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 728 | sqlite3_bind_int(g.pStmt, 1, x1); |
| 729 | sqlite3_bind_int(g.pStmt, 2, x2); |
| 730 | speedtest1_run(); |
| 731 | } |
| 732 | speedtest1_exec("COMMIT"); |
| 733 | speedtest1_end_test(); |
| 734 | |
| 735 | |
| 736 | n = sz/5; |
| 737 | speedtest1_begin_test(170, "%d SELECTS, text BETWEEN, indexed", n); |
| 738 | speedtest1_exec("BEGIN"); |
| 739 | speedtest1_prepare( |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 740 | "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM t1\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 741 | " WHERE c BETWEEN ?1 AND (?1||'~'); -- %d times", n |
| 742 | ); |
| 743 | for(i=1; i<=n; i++){ |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 744 | if( (i-1)%g.nRepeat==0 ){ |
| 745 | x1 = swizzle(i, maxb); |
| 746 | len = speedtest1_numbername(x1, zNum, sizeof(zNum)-1); |
| 747 | } |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 748 | sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC); |
| 749 | speedtest1_run(); |
| 750 | } |
| 751 | speedtest1_exec("COMMIT"); |
| 752 | speedtest1_end_test(); |
| 753 | |
| 754 | n = sz; |
| 755 | speedtest1_begin_test(180, "%d INSERTS with three indexes", n); |
| 756 | speedtest1_exec("BEGIN"); |
| 757 | speedtest1_exec( |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 758 | "CREATE%s TABLE t4(\n" |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 759 | " a INTEGER %s %s,\n" |
| 760 | " b INTEGER %s,\n" |
| 761 | " c TEXT %s\n" |
| 762 | ") %s", |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 763 | isTemp(1), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 764 | speedtest1_exec("CREATE INDEX t4b ON t4(b)"); |
| 765 | speedtest1_exec("CREATE INDEX t4c ON t4(c)"); |
| 766 | speedtest1_exec("INSERT INTO t4 SELECT * FROM t1"); |
| 767 | speedtest1_exec("COMMIT"); |
| 768 | speedtest1_end_test(); |
| 769 | |
| 770 | n = sz; |
| 771 | speedtest1_begin_test(190, "DELETE and REFILL one table", n); |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 772 | speedtest1_exec("DELETE FROM t2;"); |
| 773 | speedtest1_exec("INSERT INTO t2 SELECT * FROM t1;"); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 774 | speedtest1_end_test(); |
| 775 | |
| 776 | |
| 777 | speedtest1_begin_test(200, "VACUUM"); |
| 778 | speedtest1_exec("VACUUM"); |
| 779 | speedtest1_end_test(); |
| 780 | |
| 781 | |
| 782 | speedtest1_begin_test(210, "ALTER TABLE ADD COLUMN, and query"); |
| 783 | speedtest1_exec("ALTER TABLE t2 ADD COLUMN d DEFAULT 123"); |
| 784 | speedtest1_exec("SELECT sum(d) FROM t2"); |
| 785 | speedtest1_end_test(); |
| 786 | |
| 787 | |
| 788 | n = sz/5; |
| 789 | speedtest1_begin_test(230, "%d UPDATES, numeric BETWEEN, indexed", n); |
| 790 | speedtest1_exec("BEGIN"); |
| 791 | speedtest1_prepare( |
| 792 | "UPDATE t2 SET d=b*2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n |
| 793 | ); |
| 794 | for(i=1; i<=n; i++){ |
| 795 | x1 = speedtest1_random()%maxb; |
| 796 | x2 = speedtest1_random()%10 + sz/5000 + x1; |
| 797 | sqlite3_bind_int(g.pStmt, 1, x1); |
| 798 | sqlite3_bind_int(g.pStmt, 2, x2); |
| 799 | speedtest1_run(); |
| 800 | } |
| 801 | speedtest1_exec("COMMIT"); |
| 802 | speedtest1_end_test(); |
| 803 | |
| 804 | |
| 805 | n = sz; |
| 806 | speedtest1_begin_test(240, "%d UPDATES of individual rows", n); |
| 807 | speedtest1_exec("BEGIN"); |
| 808 | speedtest1_prepare( |
| 809 | "UPDATE t2 SET d=b*3 WHERE a=?1; -- %d times", n |
| 810 | ); |
| 811 | for(i=1; i<=n; i++){ |
| 812 | x1 = speedtest1_random()%sz + 1; |
| 813 | sqlite3_bind_int(g.pStmt, 1, x1); |
| 814 | speedtest1_run(); |
| 815 | } |
| 816 | speedtest1_exec("COMMIT"); |
| 817 | speedtest1_end_test(); |
| 818 | |
| 819 | speedtest1_begin_test(250, "One big UPDATE of the whole %d-row table", sz); |
| 820 | speedtest1_exec("UPDATE t2 SET d=b*4"); |
| 821 | speedtest1_end_test(); |
| 822 | |
| 823 | |
| 824 | speedtest1_begin_test(260, "Query added column after filling"); |
| 825 | speedtest1_exec("SELECT sum(d) FROM t2"); |
| 826 | speedtest1_end_test(); |
| 827 | |
| 828 | |
| 829 | |
| 830 | n = sz/5; |
| 831 | speedtest1_begin_test(270, "%d DELETEs, numeric BETWEEN, indexed", n); |
| 832 | speedtest1_exec("BEGIN"); |
| 833 | speedtest1_prepare( |
| 834 | "DELETE FROM t2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n |
| 835 | ); |
| 836 | for(i=1; i<=n; i++){ |
| 837 | x1 = speedtest1_random()%maxb + 1; |
| 838 | x2 = speedtest1_random()%10 + sz/5000 + x1; |
| 839 | sqlite3_bind_int(g.pStmt, 1, x1); |
| 840 | sqlite3_bind_int(g.pStmt, 2, x2); |
| 841 | speedtest1_run(); |
| 842 | } |
| 843 | speedtest1_exec("COMMIT"); |
| 844 | speedtest1_end_test(); |
| 845 | |
| 846 | |
| 847 | n = sz; |
| 848 | speedtest1_begin_test(280, "%d DELETEs of individual rows", n); |
| 849 | speedtest1_exec("BEGIN"); |
| 850 | speedtest1_prepare( |
| 851 | "DELETE FROM t3 WHERE a=?1; -- %d times", n |
| 852 | ); |
| 853 | for(i=1; i<=n; i++){ |
| 854 | x1 = speedtest1_random()%sz + 1; |
| 855 | sqlite3_bind_int(g.pStmt, 1, x1); |
| 856 | speedtest1_run(); |
| 857 | } |
| 858 | speedtest1_exec("COMMIT"); |
| 859 | speedtest1_end_test(); |
| 860 | |
| 861 | |
| 862 | speedtest1_begin_test(290, "Refill two %d-row tables using REPLACE", sz); |
drh | e19f832 | 2013-11-23 11:45:58 +0000 | [diff] [blame] | 863 | speedtest1_exec("REPLACE INTO t2(a,b,c) SELECT a,b,c FROM t1"); |
| 864 | speedtest1_exec("REPLACE INTO t3(a,b,c) SELECT a,b,c FROM t1"); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 865 | speedtest1_end_test(); |
| 866 | |
drh | 039468e | 2013-12-18 16:27:48 +0000 | [diff] [blame] | 867 | speedtest1_begin_test(300, "Refill a %d-row table using (b&1)==(a&1)", sz); |
| 868 | speedtest1_exec("DELETE FROM t2;"); |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 869 | speedtest1_exec("INSERT INTO t2(a,b,c)\n" |
| 870 | " SELECT a,b,c FROM t1 WHERE (b&1)==(a&1);"); |
| 871 | speedtest1_exec("INSERT INTO t2(a,b,c)\n" |
| 872 | " SELECT a,b,c FROM t1 WHERE (b&1)<>(a&1);"); |
drh | 039468e | 2013-12-18 16:27:48 +0000 | [diff] [blame] | 873 | speedtest1_end_test(); |
| 874 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 875 | |
| 876 | n = sz/5; |
drh | 039468e | 2013-12-18 16:27:48 +0000 | [diff] [blame] | 877 | speedtest1_begin_test(310, "%d four-ways joins", n); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 878 | speedtest1_exec("BEGIN"); |
| 879 | speedtest1_prepare( |
| 880 | "SELECT t1.c FROM t1, t2, t3, t4\n" |
| 881 | " WHERE t4.a BETWEEN ?1 AND ?2\n" |
| 882 | " AND t3.a=t4.b\n" |
| 883 | " AND t2.a=t3.b\n" |
| 884 | " AND t1.c=t2.c" |
| 885 | ); |
| 886 | for(i=1; i<=n; i++){ |
| 887 | x1 = speedtest1_random()%sz + 1; |
| 888 | x2 = speedtest1_random()%10 + x1 + 4; |
| 889 | sqlite3_bind_int(g.pStmt, 1, x1); |
| 890 | sqlite3_bind_int(g.pStmt, 2, x2); |
| 891 | speedtest1_run(); |
| 892 | } |
| 893 | speedtest1_exec("COMMIT"); |
| 894 | speedtest1_end_test(); |
| 895 | |
drh | ae28d6e | 2013-12-21 00:04:37 +0000 | [diff] [blame] | 896 | speedtest1_begin_test(320, "subquery in result set", n); |
| 897 | speedtest1_prepare( |
| 898 | "SELECT sum(a), max(c),\n" |
| 899 | " avg((SELECT a FROM t2 WHERE 5+t2.b=t1.b) AND rowid<?1), max(c)\n" |
| 900 | " FROM t1 WHERE rowid<?1;" |
| 901 | ); |
| 902 | sqlite3_bind_int(g.pStmt, 1, est_square_root(g.szTest)*50); |
| 903 | speedtest1_run(); |
| 904 | speedtest1_end_test(); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 905 | |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 906 | sz = n = g.szTest*700; |
| 907 | zNum[0] = 0; |
| 908 | maxb = roundup_allones(sz/3); |
drh | 088c086 | 2016-11-09 01:07:10 +0000 | [diff] [blame] | 909 | speedtest1_begin_test(400, "%d REPLACE ops on an IPK", n); |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 910 | speedtest1_exec("BEGIN"); |
| 911 | speedtest1_exec("CREATE%s TABLE t5(a INTEGER PRIMARY KEY, b %s);", |
| 912 | isTemp(9), g.zNN); |
drh | 088c086 | 2016-11-09 01:07:10 +0000 | [diff] [blame] | 913 | speedtest1_prepare("REPLACE INTO t5 VALUES(?1,?2); -- %d times",n); |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 914 | for(i=1; i<=n; i++){ |
| 915 | x1 = swizzle(i,maxb); |
| 916 | speedtest1_numbername(i, zNum, sizeof(zNum)); |
| 917 | sqlite3_bind_int(g.pStmt, 1, (sqlite3_int64)x1); |
| 918 | sqlite3_bind_text(g.pStmt, 2, zNum, -1, SQLITE_STATIC); |
| 919 | speedtest1_run(); |
| 920 | } |
| 921 | speedtest1_exec("COMMIT"); |
| 922 | speedtest1_end_test(); |
| 923 | speedtest1_begin_test(410, "%d SELECTS on an IPK", n); |
| 924 | speedtest1_prepare("SELECT b FROM t5 WHERE a=?1; -- %d times",n); |
| 925 | for(i=1; i<=n; i++){ |
| 926 | x1 = swizzle(i,maxb); |
| 927 | sqlite3_bind_int(g.pStmt, 1, (sqlite3_int64)x1); |
| 928 | speedtest1_run(); |
| 929 | } |
| 930 | speedtest1_end_test(); |
| 931 | |
| 932 | sz = n = g.szTest*700; |
| 933 | zNum[0] = 0; |
| 934 | maxb = roundup_allones(sz/3); |
drh | 088c086 | 2016-11-09 01:07:10 +0000 | [diff] [blame] | 935 | speedtest1_begin_test(500, "%d REPLACE on TEXT PK", n); |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 936 | speedtest1_exec("BEGIN"); |
| 937 | speedtest1_exec("CREATE%s TABLE t6(a TEXT PRIMARY KEY, b %s)%s;", |
| 938 | isTemp(9), g.zNN, |
| 939 | sqlite3_libversion_number()>=3008002 ? "WITHOUT ROWID" : ""); |
drh | 088c086 | 2016-11-09 01:07:10 +0000 | [diff] [blame] | 940 | speedtest1_prepare("REPLACE INTO t6 VALUES(?1,?2); -- %d times",n); |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 941 | for(i=1; i<=n; i++){ |
| 942 | x1 = swizzle(i,maxb); |
| 943 | speedtest1_numbername(x1, zNum, sizeof(zNum)); |
| 944 | sqlite3_bind_int(g.pStmt, 2, i); |
| 945 | sqlite3_bind_text(g.pStmt, 1, zNum, -1, SQLITE_STATIC); |
| 946 | speedtest1_run(); |
| 947 | } |
| 948 | speedtest1_exec("COMMIT"); |
| 949 | speedtest1_end_test(); |
drh | 088c086 | 2016-11-09 01:07:10 +0000 | [diff] [blame] | 950 | speedtest1_begin_test(510, "%d SELECTS on a TEXT PK", n); |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 951 | speedtest1_prepare("SELECT b FROM t6 WHERE a=?1; -- %d times",n); |
| 952 | for(i=1; i<=n; i++){ |
| 953 | x1 = swizzle(i,maxb); |
| 954 | speedtest1_numbername(x1, zNum, sizeof(zNum)); |
| 955 | sqlite3_bind_text(g.pStmt, 1, zNum, -1, SQLITE_STATIC); |
| 956 | speedtest1_run(); |
| 957 | } |
| 958 | speedtest1_end_test(); |
drh | 088c086 | 2016-11-09 01:07:10 +0000 | [diff] [blame] | 959 | speedtest1_begin_test(520, "%d SELECT DISTINCT", n); |
| 960 | speedtest1_exec("SELECT DISTINCT b FROM t5;"); |
| 961 | speedtest1_exec("SELECT DISTINCT b FROM t6;"); |
| 962 | speedtest1_end_test(); |
drh | 662389b | 2016-11-08 00:30:11 +0000 | [diff] [blame] | 963 | |
| 964 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 965 | speedtest1_begin_test(980, "PRAGMA integrity_check"); |
| 966 | speedtest1_exec("PRAGMA integrity_check"); |
| 967 | speedtest1_end_test(); |
| 968 | |
| 969 | |
| 970 | speedtest1_begin_test(990, "ANALYZE"); |
| 971 | speedtest1_exec("ANALYZE"); |
| 972 | speedtest1_end_test(); |
| 973 | } |
| 974 | |
| 975 | /* |
drh | c475480 | 2014-02-09 00:18:21 +0000 | [diff] [blame] | 976 | ** A testset for common table expressions. This exercises code |
| 977 | ** for views, subqueries, co-routines, etc. |
| 978 | */ |
| 979 | void testset_cte(void){ |
| 980 | static const char *azPuzzle[] = { |
| 981 | /* Easy */ |
| 982 | "534...9.." |
| 983 | "67.195..." |
| 984 | ".98....6." |
| 985 | "8...6...3" |
| 986 | "4..8.3..1" |
| 987 | "....2...6" |
| 988 | ".6....28." |
| 989 | "...419..5" |
| 990 | "...28..79", |
| 991 | |
| 992 | /* Medium */ |
| 993 | "53....9.." |
| 994 | "6..195..." |
| 995 | ".98....6." |
| 996 | "8...6...3" |
| 997 | "4..8.3..1" |
| 998 | "....2...6" |
| 999 | ".6....28." |
| 1000 | "...419..5" |
| 1001 | "....8..79", |
| 1002 | |
| 1003 | /* Hard */ |
| 1004 | "53......." |
| 1005 | "6..195..." |
| 1006 | ".98....6." |
| 1007 | "8...6...3" |
| 1008 | "4..8.3..1" |
| 1009 | "....2...6" |
| 1010 | ".6....28." |
| 1011 | "...419..5" |
| 1012 | "....8..79", |
| 1013 | }; |
| 1014 | const char *zPuz; |
drh | fa46bfb | 2014-02-09 00:52:53 +0000 | [diff] [blame] | 1015 | double rSpacing; |
drh | 5574e3f | 2014-02-09 23:59:28 +0000 | [diff] [blame] | 1016 | int nElem; |
drh | c475480 | 2014-02-09 00:18:21 +0000 | [diff] [blame] | 1017 | |
| 1018 | if( g.szTest<25 ){ |
| 1019 | zPuz = azPuzzle[0]; |
| 1020 | }else if( g.szTest<70 ){ |
| 1021 | zPuz = azPuzzle[1]; |
| 1022 | }else{ |
| 1023 | zPuz = azPuzzle[2]; |
| 1024 | } |
| 1025 | speedtest1_begin_test(100, "Sudoku with recursive 'digits'"); |
| 1026 | speedtest1_prepare( |
| 1027 | "WITH RECURSIVE\n" |
| 1028 | " input(sud) AS (VALUES(?1)),\n" |
| 1029 | " digits(z,lp) AS (\n" |
| 1030 | " VALUES('1', 1)\n" |
| 1031 | " UNION ALL\n" |
| 1032 | " SELECT CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9\n" |
| 1033 | " ),\n" |
| 1034 | " x(s, ind) AS (\n" |
| 1035 | " SELECT sud, instr(sud, '.') FROM input\n" |
| 1036 | " UNION ALL\n" |
| 1037 | " SELECT\n" |
| 1038 | " substr(s, 1, ind-1) || z || substr(s, ind+1),\n" |
| 1039 | " instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n" |
| 1040 | " FROM x, digits AS z\n" |
| 1041 | " WHERE ind>0\n" |
| 1042 | " AND NOT EXISTS (\n" |
| 1043 | " SELECT 1\n" |
| 1044 | " FROM digits AS lp\n" |
| 1045 | " WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n" |
| 1046 | " OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n" |
| 1047 | " OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n" |
| 1048 | " + ((ind-1)/27) * 27 + lp\n" |
| 1049 | " + ((lp-1) / 3) * 6, 1)\n" |
| 1050 | " )\n" |
| 1051 | " )\n" |
| 1052 | "SELECT s FROM x WHERE ind=0;" |
| 1053 | ); |
| 1054 | sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC); |
| 1055 | speedtest1_run(); |
| 1056 | speedtest1_end_test(); |
| 1057 | |
| 1058 | speedtest1_begin_test(200, "Sudoku with VALUES 'digits'"); |
| 1059 | speedtest1_prepare( |
| 1060 | "WITH RECURSIVE\n" |
| 1061 | " input(sud) AS (VALUES(?1)),\n" |
| 1062 | " digits(z,lp) AS (VALUES('1',1),('2',2),('3',3),('4',4),('5',5),\n" |
| 1063 | " ('6',6),('7',7),('8',8),('9',9)),\n" |
| 1064 | " x(s, ind) AS (\n" |
| 1065 | " SELECT sud, instr(sud, '.') FROM input\n" |
| 1066 | " UNION ALL\n" |
| 1067 | " SELECT\n" |
| 1068 | " substr(s, 1, ind-1) || z || substr(s, ind+1),\n" |
| 1069 | " instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n" |
| 1070 | " FROM x, digits AS z\n" |
| 1071 | " WHERE ind>0\n" |
| 1072 | " AND NOT EXISTS (\n" |
| 1073 | " SELECT 1\n" |
| 1074 | " FROM digits AS lp\n" |
| 1075 | " WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n" |
| 1076 | " OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n" |
| 1077 | " OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n" |
| 1078 | " + ((ind-1)/27) * 27 + lp\n" |
| 1079 | " + ((lp-1) / 3) * 6, 1)\n" |
| 1080 | " )\n" |
| 1081 | " )\n" |
| 1082 | "SELECT s FROM x WHERE ind=0;" |
| 1083 | ); |
| 1084 | sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC); |
| 1085 | speedtest1_run(); |
| 1086 | speedtest1_end_test(); |
drh | fa46bfb | 2014-02-09 00:52:53 +0000 | [diff] [blame] | 1087 | |
| 1088 | rSpacing = 5.0/g.szTest; |
| 1089 | speedtest1_begin_test(300, "Mandelbrot Set with spacing=%f", rSpacing); |
| 1090 | speedtest1_prepare( |
| 1091 | "WITH RECURSIVE \n" |
| 1092 | " xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+?1 FROM xaxis WHERE x<1.2),\n" |
| 1093 | " yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+?2 FROM yaxis WHERE y<1.0),\n" |
| 1094 | " m(iter, cx, cy, x, y) AS (\n" |
| 1095 | " SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis\n" |
| 1096 | " UNION ALL\n" |
| 1097 | " SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m \n" |
| 1098 | " WHERE (x*x + y*y) < 4.0 AND iter<28\n" |
| 1099 | " ),\n" |
| 1100 | " m2(iter, cx, cy) AS (\n" |
| 1101 | " SELECT max(iter), cx, cy FROM m GROUP BY cx, cy\n" |
| 1102 | " ),\n" |
| 1103 | " a(t) AS (\n" |
| 1104 | " SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') \n" |
| 1105 | " FROM m2 GROUP BY cy\n" |
| 1106 | " )\n" |
| 1107 | "SELECT group_concat(rtrim(t),x'0a') FROM a;" |
| 1108 | ); |
| 1109 | sqlite3_bind_double(g.pStmt, 1, rSpacing*.05); |
| 1110 | sqlite3_bind_double(g.pStmt, 2, rSpacing); |
| 1111 | speedtest1_run(); |
| 1112 | speedtest1_end_test(); |
| 1113 | |
drh | 5574e3f | 2014-02-09 23:59:28 +0000 | [diff] [blame] | 1114 | nElem = 10000*g.szTest; |
| 1115 | speedtest1_begin_test(400, "EXCEPT operator on %d-element tables", nElem); |
| 1116 | speedtest1_prepare( |
| 1117 | "WITH RECURSIVE \n" |
| 1118 | " t1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM t1 WHERE x<%d),\n" |
| 1119 | " t2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM t2 WHERE y<%d)\n" |
| 1120 | "SELECT count(x), avg(x) FROM (\n" |
| 1121 | " SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1\n" |
| 1122 | ");", |
| 1123 | nElem, nElem |
| 1124 | ); |
| 1125 | speedtest1_run(); |
| 1126 | speedtest1_end_test(); |
| 1127 | |
drh | c475480 | 2014-02-09 00:18:21 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
drh | 8683e08 | 2014-10-11 10:52:54 +0000 | [diff] [blame] | 1130 | #ifdef SQLITE_ENABLE_RTREE |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1131 | /* Generate two numbers between 1 and mx. The first number is less than |
| 1132 | ** the second. Usually the numbers are near each other but can sometimes |
| 1133 | ** be far apart. |
| 1134 | */ |
| 1135 | static void twoCoords( |
| 1136 | int p1, int p2, /* Parameters adjusting sizes */ |
| 1137 | unsigned mx, /* Range of 1..mx */ |
| 1138 | unsigned *pX0, unsigned *pX1 /* OUT: write results here */ |
| 1139 | ){ |
| 1140 | unsigned d, x0, x1, span; |
| 1141 | |
| 1142 | span = mx/100 + 1; |
| 1143 | if( speedtest1_random()%3==0 ) span *= p1; |
| 1144 | if( speedtest1_random()%p2==0 ) span = mx/2; |
| 1145 | d = speedtest1_random()%span + 1; |
| 1146 | x0 = speedtest1_random()%(mx-d) + 1; |
| 1147 | x1 = x0 + d; |
| 1148 | *pX0 = x0; |
| 1149 | *pX1 = x1; |
| 1150 | } |
drh | 8683e08 | 2014-10-11 10:52:54 +0000 | [diff] [blame] | 1151 | #endif |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1152 | |
drh | 8683e08 | 2014-10-11 10:52:54 +0000 | [diff] [blame] | 1153 | #ifdef SQLITE_ENABLE_RTREE |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1154 | /* The following routine is an R-Tree geometry callback. It returns |
| 1155 | ** true if the object overlaps a slice on the Y coordinate between the |
| 1156 | ** two values given as arguments. In other words |
| 1157 | ** |
| 1158 | ** SELECT count(*) FROM rt1 WHERE id MATCH xslice(10,20); |
| 1159 | ** |
| 1160 | ** Is the same as saying: |
| 1161 | ** |
| 1162 | ** SELECT count(*) FROM rt1 WHERE y1>=10 AND y0<=20; |
| 1163 | */ |
| 1164 | static int xsliceGeometryCallback( |
| 1165 | sqlite3_rtree_geometry *p, |
| 1166 | int nCoord, |
| 1167 | double *aCoord, |
| 1168 | int *pRes |
| 1169 | ){ |
| 1170 | *pRes = aCoord[3]>=p->aParam[0] && aCoord[2]<=p->aParam[1]; |
| 1171 | return SQLITE_OK; |
| 1172 | } |
drh | 8683e08 | 2014-10-11 10:52:54 +0000 | [diff] [blame] | 1173 | #endif /* SQLITE_ENABLE_RTREE */ |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1174 | |
drh | 8683e08 | 2014-10-11 10:52:54 +0000 | [diff] [blame] | 1175 | #ifdef SQLITE_ENABLE_RTREE |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1176 | /* |
| 1177 | ** A testset for the R-Tree virtual table |
| 1178 | */ |
| 1179 | void testset_rtree(int p1, int p2){ |
| 1180 | unsigned i, n; |
| 1181 | unsigned mxCoord; |
| 1182 | unsigned x0, x1, y0, y1, z0, z1; |
| 1183 | unsigned iStep; |
| 1184 | int *aCheck = sqlite3_malloc( sizeof(int)*g.szTest*100 ); |
| 1185 | |
| 1186 | mxCoord = 15000; |
| 1187 | n = g.szTest*100; |
| 1188 | speedtest1_begin_test(100, "%d INSERTs into an r-tree", n); |
| 1189 | speedtest1_exec("BEGIN"); |
| 1190 | speedtest1_exec("CREATE VIRTUAL TABLE rt1 USING rtree(id,x0,x1,y0,y1,z0,z1)"); |
| 1191 | speedtest1_prepare("INSERT INTO rt1(id,x0,x1,y0,y1,z0,z1)" |
| 1192 | "VALUES(?1,?2,?3,?4,?5,?6,?7)"); |
| 1193 | for(i=1; i<=n; i++){ |
| 1194 | twoCoords(p1, p2, mxCoord, &x0, &x1); |
| 1195 | twoCoords(p1, p2, mxCoord, &y0, &y1); |
| 1196 | twoCoords(p1, p2, mxCoord, &z0, &z1); |
| 1197 | sqlite3_bind_int(g.pStmt, 1, i); |
| 1198 | sqlite3_bind_int(g.pStmt, 2, x0); |
| 1199 | sqlite3_bind_int(g.pStmt, 3, x1); |
| 1200 | sqlite3_bind_int(g.pStmt, 4, y0); |
| 1201 | sqlite3_bind_int(g.pStmt, 5, y1); |
| 1202 | sqlite3_bind_int(g.pStmt, 6, z0); |
| 1203 | sqlite3_bind_int(g.pStmt, 7, z1); |
| 1204 | speedtest1_run(); |
| 1205 | } |
| 1206 | speedtest1_exec("COMMIT"); |
| 1207 | speedtest1_end_test(); |
| 1208 | |
| 1209 | speedtest1_begin_test(101, "Copy from rtree to a regular table"); |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 1210 | speedtest1_exec(" TABLE t1(id INTEGER PRIMARY KEY,x0,x1,y0,y1,z0,z1)"); |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1211 | speedtest1_exec("INSERT INTO t1 SELECT * FROM rt1"); |
| 1212 | speedtest1_end_test(); |
| 1213 | |
| 1214 | n = g.szTest*20; |
| 1215 | speedtest1_begin_test(110, "%d one-dimensional intersect slice queries", n); |
| 1216 | speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x0>=?1 AND x1<=?2"); |
| 1217 | iStep = mxCoord/n; |
| 1218 | for(i=0; i<n; i++){ |
| 1219 | sqlite3_bind_int(g.pStmt, 1, i*iStep); |
| 1220 | sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep); |
| 1221 | speedtest1_run(); |
| 1222 | aCheck[i] = atoi(g.zResult); |
| 1223 | } |
| 1224 | speedtest1_end_test(); |
| 1225 | |
| 1226 | if( g.bVerify ){ |
| 1227 | n = g.szTest*20; |
| 1228 | speedtest1_begin_test(111, "Verify result from 1-D intersect slice queries"); |
| 1229 | speedtest1_prepare("SELECT count(*) FROM t1 WHERE x0>=?1 AND x1<=?2"); |
| 1230 | iStep = mxCoord/n; |
| 1231 | for(i=0; i<n; i++){ |
| 1232 | sqlite3_bind_int(g.pStmt, 1, i*iStep); |
| 1233 | sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep); |
| 1234 | speedtest1_run(); |
| 1235 | if( aCheck[i]!=atoi(g.zResult) ){ |
| 1236 | fatal_error("Count disagree step %d: %d..%d. %d vs %d", |
| 1237 | i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult)); |
| 1238 | } |
| 1239 | } |
| 1240 | speedtest1_end_test(); |
| 1241 | } |
| 1242 | |
| 1243 | n = g.szTest*20; |
| 1244 | speedtest1_begin_test(120, "%d one-dimensional overlap slice queries", n); |
| 1245 | speedtest1_prepare("SELECT count(*) FROM rt1 WHERE y1>=?1 AND y0<=?2"); |
| 1246 | iStep = mxCoord/n; |
| 1247 | for(i=0; i<n; i++){ |
| 1248 | sqlite3_bind_int(g.pStmt, 1, i*iStep); |
| 1249 | sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep); |
| 1250 | speedtest1_run(); |
| 1251 | aCheck[i] = atoi(g.zResult); |
| 1252 | } |
| 1253 | speedtest1_end_test(); |
| 1254 | |
| 1255 | if( g.bVerify ){ |
| 1256 | n = g.szTest*20; |
| 1257 | speedtest1_begin_test(121, "Verify result from 1-D overlap slice queries"); |
| 1258 | speedtest1_prepare("SELECT count(*) FROM t1 WHERE y1>=?1 AND y0<=?2"); |
| 1259 | iStep = mxCoord/n; |
| 1260 | for(i=0; i<n; i++){ |
| 1261 | sqlite3_bind_int(g.pStmt, 1, i*iStep); |
| 1262 | sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep); |
| 1263 | speedtest1_run(); |
| 1264 | if( aCheck[i]!=atoi(g.zResult) ){ |
| 1265 | fatal_error("Count disagree step %d: %d..%d. %d vs %d", |
| 1266 | i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult)); |
| 1267 | } |
| 1268 | } |
| 1269 | speedtest1_end_test(); |
| 1270 | } |
| 1271 | |
| 1272 | |
| 1273 | n = g.szTest*20; |
| 1274 | speedtest1_begin_test(125, "%d custom geometry callback queries", n); |
| 1275 | sqlite3_rtree_geometry_callback(g.db, "xslice", xsliceGeometryCallback, 0); |
| 1276 | speedtest1_prepare("SELECT count(*) FROM rt1 WHERE id MATCH xslice(?1,?2)"); |
| 1277 | iStep = mxCoord/n; |
| 1278 | for(i=0; i<n; i++){ |
| 1279 | sqlite3_bind_int(g.pStmt, 1, i*iStep); |
| 1280 | sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep); |
| 1281 | speedtest1_run(); |
| 1282 | if( aCheck[i]!=atoi(g.zResult) ){ |
| 1283 | fatal_error("Count disagree step %d: %d..%d. %d vs %d", |
| 1284 | i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult)); |
| 1285 | } |
| 1286 | } |
| 1287 | speedtest1_end_test(); |
| 1288 | |
| 1289 | n = g.szTest*80; |
| 1290 | speedtest1_begin_test(130, "%d three-dimensional intersect box queries", n); |
| 1291 | speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x1>=?1 AND x0<=?2" |
| 1292 | " AND y1>=?1 AND y0<=?2 AND z1>=?1 AND z0<=?2"); |
| 1293 | iStep = mxCoord/n; |
| 1294 | for(i=0; i<n; i++){ |
| 1295 | sqlite3_bind_int(g.pStmt, 1, i*iStep); |
| 1296 | sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep); |
| 1297 | speedtest1_run(); |
| 1298 | aCheck[i] = atoi(g.zResult); |
| 1299 | } |
| 1300 | speedtest1_end_test(); |
| 1301 | |
| 1302 | n = g.szTest*100; |
| 1303 | speedtest1_begin_test(140, "%d rowid queries", n); |
| 1304 | speedtest1_prepare("SELECT * FROM rt1 WHERE id=?1"); |
| 1305 | for(i=1; i<=n; i++){ |
| 1306 | sqlite3_bind_int(g.pStmt, 1, i); |
| 1307 | speedtest1_run(); |
| 1308 | } |
| 1309 | speedtest1_end_test(); |
| 1310 | } |
drh | 8683e08 | 2014-10-11 10:52:54 +0000 | [diff] [blame] | 1311 | #endif /* SQLITE_ENABLE_RTREE */ |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1312 | |
drh | c475480 | 2014-02-09 00:18:21 +0000 | [diff] [blame] | 1313 | /* |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1314 | ** A testset used for debugging speedtest1 itself. |
| 1315 | */ |
| 1316 | void testset_debug1(void){ |
| 1317 | unsigned i, n; |
| 1318 | unsigned x1, x2; |
| 1319 | char zNum[2000]; /* A number name */ |
| 1320 | |
| 1321 | n = g.szTest; |
| 1322 | for(i=1; i<=n; i++){ |
| 1323 | x1 = swizzle(i, n); |
| 1324 | x2 = swizzle(x1, n); |
| 1325 | speedtest1_numbername(x1, zNum, sizeof(zNum)); |
| 1326 | printf("%5d %5d %5d %s\n", i, x1, x2, zNum); |
| 1327 | } |
| 1328 | } |
| 1329 | |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 1330 | #ifdef __linux__ |
| 1331 | #include <sys/types.h> |
| 1332 | #include <unistd.h> |
| 1333 | |
| 1334 | /* |
| 1335 | ** Attempt to display I/O stats on Linux using /proc/PID/io |
| 1336 | */ |
| 1337 | static void displayLinuxIoStats(FILE *out){ |
| 1338 | FILE *in; |
| 1339 | char z[200]; |
| 1340 | sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); |
| 1341 | in = fopen(z, "rb"); |
| 1342 | if( in==0 ) return; |
| 1343 | while( fgets(z, sizeof(z), in)!=0 ){ |
| 1344 | static const struct { |
| 1345 | const char *zPattern; |
| 1346 | const char *zDesc; |
| 1347 | } aTrans[] = { |
| 1348 | { "rchar: ", "Bytes received by read():" }, |
| 1349 | { "wchar: ", "Bytes sent to write():" }, |
| 1350 | { "syscr: ", "Read() system calls:" }, |
| 1351 | { "syscw: ", "Write() system calls:" }, |
| 1352 | { "read_bytes: ", "Bytes rcvd from storage:" }, |
| 1353 | { "write_bytes: ", "Bytes sent to storage:" }, |
| 1354 | { "cancelled_write_bytes: ", "Cancelled write bytes:" }, |
| 1355 | }; |
| 1356 | int i; |
| 1357 | for(i=0; i<sizeof(aTrans)/sizeof(aTrans[0]); i++){ |
| 1358 | int n = (int)strlen(aTrans[i].zPattern); |
| 1359 | if( strncmp(aTrans[i].zPattern, z, n)==0 ){ |
| 1360 | fprintf(out, "-- %-28s %s", aTrans[i].zDesc, &z[n]); |
| 1361 | break; |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | fclose(in); |
| 1366 | } |
| 1367 | #endif |
| 1368 | |
drh | 30e3fdf | 2016-06-04 16:33:48 +0000 | [diff] [blame] | 1369 | #if SQLITE_VERSION_NUMBER<3006018 |
| 1370 | # define sqlite3_sourceid(X) "(before 3.6.18)" |
| 1371 | #endif |
| 1372 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1373 | int main(int argc, char **argv){ |
| 1374 | int doAutovac = 0; /* True for --autovacuum */ |
| 1375 | int cacheSize = 0; /* Desired cache size. 0 means default */ |
| 1376 | int doExclusive = 0; /* True for --exclusive */ |
| 1377 | int nHeap = 0, mnHeap = 0; /* Heap size from --heap */ |
| 1378 | int doIncrvac = 0; /* True for --incrvacuum */ |
| 1379 | const char *zJMode = 0; /* Journal mode */ |
| 1380 | const char *zKey = 0; /* Encryption key */ |
drh | aa430bf | 2016-12-31 18:37:50 +0000 | [diff] [blame] | 1381 | int nLook = -1, szLook = 0; /* --lookaside configuration */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1382 | int noSync = 0; /* True for --nosync */ |
| 1383 | int pageSize = 0; /* Desired page size. 0 means default */ |
| 1384 | int nPCache = 0, szPCache = 0;/* --pcache configuration */ |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 1385 | int doPCache = 0; /* True if --pcache is seen */ |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1386 | int nScratch = 0, szScratch=0;/* --scratch configuration */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1387 | int showStats = 0; /* True for --stats */ |
drh | 46a06bb | 2014-04-18 13:57:39 +0000 | [diff] [blame] | 1388 | int nThread = 0; /* --threads value */ |
drh | 26f4198 | 2016-12-12 23:24:08 +0000 | [diff] [blame] | 1389 | int mmapSize = 0; /* How big of a memory map to use */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1390 | const char *zTSet = "main"; /* Which --testset torun */ |
| 1391 | int doTrace = 0; /* True for --trace */ |
| 1392 | const char *zEncoding = 0; /* --utf16be or --utf16le */ |
| 1393 | const char *zDbName = 0; /* Name of the test database */ |
| 1394 | |
| 1395 | void *pHeap = 0; /* Allocated heap space */ |
| 1396 | void *pLook = 0; /* Allocated lookaside space */ |
| 1397 | void *pPCache = 0; /* Allocated storage for pcache */ |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1398 | void *pScratch = 0; /* Allocated storage for scratch */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1399 | int iCur, iHi; /* Stats values, current and "highwater" */ |
drh | e19f832 | 2013-11-23 11:45:58 +0000 | [diff] [blame] | 1400 | int i; /* Loop counter */ |
| 1401 | int rc; /* API return code */ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1402 | |
drh | b719e3a | 2016-02-19 16:19:23 +0000 | [diff] [blame] | 1403 | /* Display the version of SQLite being tested */ |
drh | fbcd313 | 2016-03-30 12:20:24 +0000 | [diff] [blame] | 1404 | printf("-- Speedtest1 for SQLite %s %.50s\n", |
| 1405 | sqlite3_libversion(), sqlite3_sourceid()); |
drh | b719e3a | 2016-02-19 16:19:23 +0000 | [diff] [blame] | 1406 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1407 | /* Process command-line arguments */ |
| 1408 | g.zWR = ""; |
| 1409 | g.zNN = ""; |
| 1410 | g.zPK = "UNIQUE"; |
| 1411 | g.szTest = 100; |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 1412 | g.nRepeat = 1; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1413 | for(i=1; i<argc; i++){ |
| 1414 | const char *z = argv[i]; |
| 1415 | if( z[0]=='-' ){ |
| 1416 | do{ z++; }while( z[0]=='-' ); |
| 1417 | if( strcmp(z,"autovacuum")==0 ){ |
| 1418 | doAutovac = 1; |
| 1419 | }else if( strcmp(z,"cachesize")==0 ){ |
| 1420 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
| 1421 | i++; |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1422 | cacheSize = integerValue(argv[i]); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1423 | }else if( strcmp(z,"exclusive")==0 ){ |
| 1424 | doExclusive = 1; |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 1425 | }else if( strcmp(z,"explain")==0 ){ |
| 1426 | g.bSqlOnly = 1; |
| 1427 | g.bExplain = 1; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1428 | }else if( strcmp(z,"heap")==0 ){ |
| 1429 | if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]); |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1430 | nHeap = integerValue(argv[i+1]); |
| 1431 | mnHeap = integerValue(argv[i+2]); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1432 | i += 2; |
| 1433 | }else if( strcmp(z,"incrvacuum")==0 ){ |
| 1434 | doIncrvac = 1; |
| 1435 | }else if( strcmp(z,"journal")==0 ){ |
| 1436 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
| 1437 | zJMode = argv[++i]; |
| 1438 | }else if( strcmp(z,"key")==0 ){ |
| 1439 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
| 1440 | zKey = argv[++i]; |
| 1441 | }else if( strcmp(z,"lookaside")==0 ){ |
| 1442 | if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]); |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1443 | nLook = integerValue(argv[i+1]); |
| 1444 | szLook = integerValue(argv[i+2]); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1445 | i += 2; |
drh | 89500dc | 2016-11-21 18:15:35 +0000 | [diff] [blame] | 1446 | #if SQLITE_VERSION_NUMBER>=3006000 |
drh | 2e43e96 | 2015-07-03 14:34:25 +0000 | [diff] [blame] | 1447 | }else if( strcmp(z,"multithread")==0 ){ |
| 1448 | sqlite3_config(SQLITE_CONFIG_MULTITHREAD); |
| 1449 | }else if( strcmp(z,"nomemstat")==0 ){ |
| 1450 | sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0); |
drh | 89500dc | 2016-11-21 18:15:35 +0000 | [diff] [blame] | 1451 | #endif |
drh | 26f4198 | 2016-12-12 23:24:08 +0000 | [diff] [blame] | 1452 | #if SQLITE_VERSION_NUMBER>=3007017 |
| 1453 | }else if( strcmp(z, "mmap")==0 ){ |
| 1454 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
| 1455 | mmapSize = integerValue(argv[++i]); |
| 1456 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1457 | }else if( strcmp(z,"nosync")==0 ){ |
| 1458 | noSync = 1; |
| 1459 | }else if( strcmp(z,"notnull")==0 ){ |
| 1460 | g.zNN = "NOT NULL"; |
drh | cfb8f8d | 2015-07-23 20:44:49 +0000 | [diff] [blame] | 1461 | #ifdef SQLITE_ENABLE_RBU |
| 1462 | }else if( strcmp(z,"rbu")==0 ){ |
| 1463 | sqlite3ota_create_vfs("rbu", 0); |
| 1464 | sqlite3_vfs_register(sqlite3_vfs_find("rbu"), 1); |
dan | 54fc214 | 2015-03-05 16:21:20 +0000 | [diff] [blame] | 1465 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1466 | }else if( strcmp(z,"pagesize")==0 ){ |
| 1467 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1468 | pageSize = integerValue(argv[++i]); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1469 | }else if( strcmp(z,"pcache")==0 ){ |
| 1470 | if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]); |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1471 | nPCache = integerValue(argv[i+1]); |
| 1472 | szPCache = integerValue(argv[i+2]); |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 1473 | doPCache = 1; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1474 | i += 2; |
| 1475 | }else if( strcmp(z,"primarykey")==0 ){ |
| 1476 | g.zPK = "PRIMARY KEY"; |
drh | f8a89ca | 2016-10-18 14:35:55 +0000 | [diff] [blame] | 1477 | }else if( strcmp(z,"repeat")==0 ){ |
| 1478 | if( i>=argc-1 ) fatal_error("missing arguments on %s\n", argv[i]); |
| 1479 | g.nRepeat = integerValue(argv[i+1]); |
| 1480 | i += 1; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1481 | }else if( strcmp(z,"reprepare")==0 ){ |
| 1482 | g.bReprepare = 1; |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1483 | }else if( strcmp(z,"scratch")==0 ){ |
| 1484 | if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]); |
| 1485 | nScratch = integerValue(argv[i+1]); |
| 1486 | szScratch = integerValue(argv[i+2]); |
| 1487 | i += 2; |
drh | 89500dc | 2016-11-21 18:15:35 +0000 | [diff] [blame] | 1488 | #if SQLITE_VERSION_NUMBER>=3006000 |
drh | 2e43e96 | 2015-07-03 14:34:25 +0000 | [diff] [blame] | 1489 | }else if( strcmp(z,"serialized")==0 ){ |
| 1490 | sqlite3_config(SQLITE_CONFIG_SERIALIZED); |
| 1491 | }else if( strcmp(z,"singlethread")==0 ){ |
| 1492 | sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); |
drh | 89500dc | 2016-11-21 18:15:35 +0000 | [diff] [blame] | 1493 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1494 | }else if( strcmp(z,"sqlonly")==0 ){ |
| 1495 | g.bSqlOnly = 1; |
drh | 0d84718 | 2015-07-02 01:38:39 +0000 | [diff] [blame] | 1496 | }else if( strcmp(z,"shrink-memory")==0 ){ |
| 1497 | g.bMemShrink = 1; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1498 | }else if( strcmp(z,"size")==0 ){ |
| 1499 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1500 | g.szTest = integerValue(argv[++i]); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1501 | }else if( strcmp(z,"stats")==0 ){ |
| 1502 | showStats = 1; |
drh | 2160ca5 | 2016-04-12 16:59:39 +0000 | [diff] [blame] | 1503 | }else if( strcmp(z,"temp")==0 ){ |
| 1504 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
| 1505 | i++; |
| 1506 | if( argv[i][0]<'0' || argv[i][0]>'9' || argv[i][1]!=0 ){ |
| 1507 | fatal_error("argument to --temp should be integer between 0 and 9"); |
| 1508 | } |
| 1509 | g.eTemp = argv[i][0] - '0'; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1510 | }else if( strcmp(z,"testset")==0 ){ |
| 1511 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
| 1512 | zTSet = argv[++i]; |
| 1513 | }else if( strcmp(z,"trace")==0 ){ |
| 1514 | doTrace = 1; |
drh | 46a06bb | 2014-04-18 13:57:39 +0000 | [diff] [blame] | 1515 | }else if( strcmp(z,"threads")==0 ){ |
| 1516 | if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]); |
| 1517 | nThread = integerValue(argv[++i]); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1518 | }else if( strcmp(z,"utf16le")==0 ){ |
| 1519 | zEncoding = "utf16le"; |
| 1520 | }else if( strcmp(z,"utf16be")==0 ){ |
| 1521 | zEncoding = "utf16be"; |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1522 | }else if( strcmp(z,"verify")==0 ){ |
| 1523 | g.bVerify = 1; |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1524 | }else if( strcmp(z,"without-rowid")==0 ){ |
| 1525 | g.zWR = "WITHOUT ROWID"; |
| 1526 | g.zPK = "PRIMARY KEY"; |
| 1527 | }else if( strcmp(z, "help")==0 || strcmp(z,"?")==0 ){ |
| 1528 | printf(zHelp, argv[0]); |
| 1529 | exit(0); |
| 1530 | }else{ |
| 1531 | fatal_error("unknown option: %s\nUse \"%s -?\" for help\n", |
| 1532 | argv[i], argv[0]); |
| 1533 | } |
| 1534 | }else if( zDbName==0 ){ |
| 1535 | zDbName = argv[i]; |
| 1536 | }else{ |
| 1537 | fatal_error("surplus argument: %s\nUse \"%s -?\" for help\n", |
| 1538 | argv[i], argv[0]); |
| 1539 | } |
| 1540 | } |
drh | 741c277 | 2016-04-12 17:13:33 +0000 | [diff] [blame] | 1541 | if( zDbName!=0 ) unlink(zDbName); |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 1542 | #if SQLITE_VERSION_NUMBER>=3006001 |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1543 | if( nHeap>0 ){ |
| 1544 | pHeap = malloc( nHeap ); |
| 1545 | if( pHeap==0 ) fatal_error("cannot allocate %d-byte heap\n", nHeap); |
drh | e19f832 | 2013-11-23 11:45:58 +0000 | [diff] [blame] | 1546 | rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap); |
drh | 7b65ad3 | 2013-11-23 21:29:07 +0000 | [diff] [blame] | 1547 | if( rc ) fatal_error("heap configuration failed: %d\n", rc); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1548 | } |
drh | ee70a84 | 2015-07-06 18:54:52 +0000 | [diff] [blame] | 1549 | if( doPCache ){ |
| 1550 | if( nPCache>0 && szPCache>0 ){ |
| 1551 | pPCache = malloc( nPCache*(sqlite3_int64)szPCache ); |
| 1552 | if( pPCache==0 ) fatal_error("cannot allocate %lld-byte pcache\n", |
| 1553 | nPCache*(sqlite3_int64)szPCache); |
| 1554 | } |
drh | e19f832 | 2013-11-23 11:45:58 +0000 | [diff] [blame] | 1555 | rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache); |
drh | 7b65ad3 | 2013-11-23 21:29:07 +0000 | [diff] [blame] | 1556 | if( rc ) fatal_error("pcache configuration failed: %d\n", rc); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1557 | } |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1558 | if( nScratch>0 && szScratch>0 ){ |
| 1559 | pScratch = malloc( nScratch*(sqlite3_int64)szScratch ); |
| 1560 | if( pScratch==0 ) fatal_error("cannot allocate %lld-byte scratch\n", |
| 1561 | nScratch*(sqlite3_int64)szScratch); |
| 1562 | rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch); |
| 1563 | if( rc ) fatal_error("scratch configuration failed: %d\n", rc); |
| 1564 | } |
drh | aa430bf | 2016-12-31 18:37:50 +0000 | [diff] [blame] | 1565 | if( nLook>=0 ){ |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1566 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); |
| 1567 | } |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 1568 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1569 | |
| 1570 | /* Open the database and the input file */ |
| 1571 | if( sqlite3_open(zDbName, &g.db) ){ |
| 1572 | fatal_error("Cannot open database file: %s\n", zDbName); |
| 1573 | } |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 1574 | #if SQLITE_VERSION_NUMBER>=3006001 |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1575 | if( nLook>0 && szLook>0 ){ |
| 1576 | pLook = malloc( nLook*szLook ); |
drh | e19f832 | 2013-11-23 11:45:58 +0000 | [diff] [blame] | 1577 | rc = sqlite3_db_config(g.db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook,nLook); |
drh | 7b65ad3 | 2013-11-23 21:29:07 +0000 | [diff] [blame] | 1578 | if( rc ) fatal_error("lookaside configuration failed: %d\n", rc); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1579 | } |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 1580 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1581 | |
| 1582 | /* Set database connection options */ |
| 1583 | sqlite3_create_function(g.db, "random", 0, SQLITE_UTF8, 0, randomFunc, 0, 0); |
drh | 98ef26b | 2016-09-21 23:58:49 +0000 | [diff] [blame] | 1584 | #ifndef SQLITE_OMIT_DEPRECATED |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1585 | if( doTrace ) sqlite3_trace(g.db, traceCallback, 0); |
drh | 98ef26b | 2016-09-21 23:58:49 +0000 | [diff] [blame] | 1586 | #endif |
drh | 26f4198 | 2016-12-12 23:24:08 +0000 | [diff] [blame] | 1587 | if( mmapSize>0 ){ |
| 1588 | speedtest1_exec("PRAGMA mmap_size=%d", mmapSize); |
| 1589 | } |
drh | 43cbe14 | 2014-08-29 18:06:33 +0000 | [diff] [blame] | 1590 | speedtest1_exec("PRAGMA threads=%d", nThread); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1591 | if( zKey ){ |
| 1592 | speedtest1_exec("PRAGMA key('%s')", zKey); |
| 1593 | } |
| 1594 | if( zEncoding ){ |
| 1595 | speedtest1_exec("PRAGMA encoding=%s", zEncoding); |
| 1596 | } |
| 1597 | if( doAutovac ){ |
| 1598 | speedtest1_exec("PRAGMA auto_vacuum=FULL"); |
| 1599 | }else if( doIncrvac ){ |
| 1600 | speedtest1_exec("PRAGMA auto_vacuum=INCREMENTAL"); |
| 1601 | } |
| 1602 | if( pageSize ){ |
| 1603 | speedtest1_exec("PRAGMA page_size=%d", pageSize); |
| 1604 | } |
| 1605 | if( cacheSize ){ |
| 1606 | speedtest1_exec("PRAGMA cache_size=%d", cacheSize); |
| 1607 | } |
| 1608 | if( noSync ) speedtest1_exec("PRAGMA synchronous=OFF"); |
| 1609 | if( doExclusive ){ |
| 1610 | speedtest1_exec("PRAGMA locking_mode=EXCLUSIVE"); |
| 1611 | } |
| 1612 | if( zJMode ){ |
| 1613 | speedtest1_exec("PRAGMA journal_mode=%s", zJMode); |
| 1614 | } |
| 1615 | |
drh | 849a9d9 | 2013-12-21 15:46:06 +0000 | [diff] [blame] | 1616 | if( g.bExplain ) printf(".explain\n.echo on\n"); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1617 | if( strcmp(zTSet,"main")==0 ){ |
| 1618 | testset_main(); |
| 1619 | }else if( strcmp(zTSet,"debug1")==0 ){ |
| 1620 | testset_debug1(); |
drh | c475480 | 2014-02-09 00:18:21 +0000 | [diff] [blame] | 1621 | }else if( strcmp(zTSet,"cte")==0 ){ |
| 1622 | testset_cte(); |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1623 | }else if( strcmp(zTSet,"rtree")==0 ){ |
drh | 8683e08 | 2014-10-11 10:52:54 +0000 | [diff] [blame] | 1624 | #ifdef SQLITE_ENABLE_RTREE |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1625 | testset_rtree(6, 147); |
drh | 8683e08 | 2014-10-11 10:52:54 +0000 | [diff] [blame] | 1626 | #else |
| 1627 | fatal_error("compile with -DSQLITE_ENABLE_RTREE to enable " |
| 1628 | "the R-Tree tests\n"); |
| 1629 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1630 | }else{ |
drh | 65e6b0d | 2014-04-28 17:56:19 +0000 | [diff] [blame] | 1631 | fatal_error("unknown testset: \"%s\"\nChoices: main debug1 cte rtree\n", |
| 1632 | zTSet); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1633 | } |
| 1634 | speedtest1_final(); |
| 1635 | |
| 1636 | /* Database connection statistics printed after both prepared statements |
| 1637 | ** have been finalized */ |
drh | 290ea40 | 2013-12-01 18:10:01 +0000 | [diff] [blame] | 1638 | #if SQLITE_VERSION_NUMBER>=3007009 |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1639 | if( showStats ){ |
| 1640 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHi, 0); |
| 1641 | printf("-- Lookaside Slots Used: %d (max %d)\n", iCur,iHi); |
| 1642 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHi, 0); |
| 1643 | printf("-- Successful lookasides: %d\n", iHi); |
| 1644 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur,&iHi,0); |
| 1645 | printf("-- Lookaside size faults: %d\n", iHi); |
| 1646 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur,&iHi,0); |
| 1647 | printf("-- Lookaside OOM faults: %d\n", iHi); |
| 1648 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHi, 0); |
| 1649 | printf("-- Pager Heap Usage: %d bytes\n", iCur); |
| 1650 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHi, 1); |
| 1651 | printf("-- Page cache hits: %d\n", iCur); |
| 1652 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHi, 1); |
drh | 2a702db | 2013-12-02 21:25:40 +0000 | [diff] [blame] | 1653 | printf("-- Page cache misses: %d\n", iCur); |
| 1654 | #if SQLITE_VERSION_NUMBER>=3007012 |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1655 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHi, 1); |
| 1656 | printf("-- Page cache writes: %d\n", iCur); |
drh | 2a702db | 2013-12-02 21:25:40 +0000 | [diff] [blame] | 1657 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1658 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHi, 0); |
| 1659 | printf("-- Schema Heap Usage: %d bytes\n", iCur); |
| 1660 | sqlite3_db_status(g.db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHi, 0); |
| 1661 | printf("-- Statement Heap Usage: %d bytes\n", iCur); |
| 1662 | } |
drh | 290ea40 | 2013-12-01 18:10:01 +0000 | [diff] [blame] | 1663 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1664 | |
| 1665 | sqlite3_close(g.db); |
| 1666 | |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 1667 | #if SQLITE_VERSION_NUMBER>=3006001 |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1668 | /* Global memory usage statistics printed after the database connection |
| 1669 | ** has closed. Memory usage should be zero at this point. */ |
| 1670 | if( showStats ){ |
| 1671 | sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHi, 0); |
| 1672 | printf("-- Memory Used (bytes): %d (max %d)\n", iCur,iHi); |
drh | d79e9c5 | 2013-12-02 01:24:05 +0000 | [diff] [blame] | 1673 | #if SQLITE_VERSION_NUMBER>=3007000 |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1674 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHi, 0); |
| 1675 | printf("-- Outstanding Allocations: %d (max %d)\n", iCur,iHi); |
drh | d79e9c5 | 2013-12-02 01:24:05 +0000 | [diff] [blame] | 1676 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1677 | sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHi, 0); |
| 1678 | printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur,iHi); |
| 1679 | sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHi, 0); |
| 1680 | printf("-- Scratch Overflow Bytes: %d (max %d)\n", iCur,iHi); |
| 1681 | sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHi, 0); |
| 1682 | printf("-- Largest Allocation: %d bytes\n",iHi); |
| 1683 | sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHi, 0); |
| 1684 | printf("-- Largest Pcache Allocation: %d bytes\n",iHi); |
| 1685 | sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHi, 0); |
| 1686 | printf("-- Largest Scratch Allocation: %d bytes\n", iHi); |
| 1687 | } |
drh | 5995e29 | 2015-06-18 12:37:32 +0000 | [diff] [blame] | 1688 | #endif |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1689 | |
drh | fc1a84c | 2016-02-27 19:19:22 +0000 | [diff] [blame] | 1690 | #ifdef __linux__ |
| 1691 | if( showStats ){ |
| 1692 | displayLinuxIoStats(stdout); |
| 1693 | } |
| 1694 | #endif |
| 1695 | |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1696 | /* Release memory */ |
| 1697 | free( pLook ); |
| 1698 | free( pPCache ); |
drh | 93307e9 | 2013-11-24 01:14:14 +0000 | [diff] [blame] | 1699 | free( pScratch ); |
drh | ad1ca9a | 2013-11-23 04:16:58 +0000 | [diff] [blame] | 1700 | free( pHeap ); |
| 1701 | return 0; |
| 1702 | } |