Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/speedtest1.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / speedtest1.c` 3551 lines ยท 118844 bytes Raw Blame History 1 /* 2 ** A program for performance testing. 3 ** 4 ** To build this program against an historical version of SQLite for comparison 5 ** testing: 6 ** 7 ** Unix: 8 ** 9 ** ./configure --all 10 ** make clean speedtest1 11 ** mv speedtest1 speedtest1-current 12 ** cp $HISTORICAL_SQLITE3_C_H . 13 ** touch sqlite3.c sqlite3.h .target_source 14 ** make speedtest1 15 ** mv speedtest1 speedtest1-baseline 16 ** 17 ** Windows: 18 ** 19 ** nmake /f Makefile.msc clean speedtest1.exe 20 ** mv speedtest1.exe speedtest1-current.exe 21 ** cp $HISTORICAL_SQLITE_C_H . 22 ** touch sqlite3.c sqlite3.h .target_source 23 ** nmake /f Makefile.msc speedtest1.exe 24 ** mv speedtest1.exe speedtest1-baseline.exe 25 ** 26 ** The available command-line options are described below: 27 */ 28 static const char zHelp[] = 29 "Usage: %s [--options] DATABASE\n" 30 "Options:\n" 31 " --autovacuum Enable AUTOVACUUM mode\n" 32 " --big-transactions Add BEGIN/END around all large tests\n" 33 " --cachesize N Set PRAGMA cache_size=N. Note: N is pages, not bytes\n" 34 " --checkpoint Run PRAGMA wal_checkpoint after each test case\n" 35 " --exclusive Enable locking_mode=EXCLUSIVE\n" 36 " --explain Like --sqlonly but with added EXPLAIN keywords\n" 37 " --fullfsync Enable fullfsync=TRUE\n" 38 " --hard-heap-limit N The hard limit on the maximum heap size\n" 39 " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n" 40 " --incrvacuum Enable incremenatal vacuum mode\n" 41 " --journal M Set the journal_mode to M\n" 42 " --key KEY Set the encryption key to KEY\n" 43 " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n" 44 " --memdb Use an in-memory database\n" 45 " --mmap SZ MMAP the first SZ bytes of the database file\n" 46 " --multithread Set multithreaded mode\n" 47 " --nomemstat Disable memory statistics\n" 48 " --nomutex Open db with SQLITE_OPEN_NOMUTEX\n" 49 " --nosync Set PRAGMA synchronous=OFF\n" 50 " --notnull Add NOT NULL constraints to table columns\n" 51 " --output FILE Store SQL output in FILE\n" 52 " --pagesize N Set the page size to N\n" 53 " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n" 54 " --primarykey Use PRIMARY KEY instead of UNIQUE where appropriate\n" 55 " --repeat N Repeat each SELECT N times (default: 1)\n" 56 " --reprepare Reprepare each statement upon every invocation\n" 57 " --reserve N Reserve N bytes on each database page\n" 58 " --script FILE Write an SQL script for the test into FILE\n" 59 " --serialized Set serialized threading mode\n" 60 " --singlethread Set single-threaded mode - disables all mutexing\n" 61 " --sqlonly No-op. Only show the SQL that would have been run.\n" 62 " --shrink-memory Invoke sqlite3_db_release_memory() frequently.\n" 63 " --size N Relative test size. Default=100\n" 64 " --soft-heap-limit N The soft limit on the maximum heap size\n" 65 " --strict Use STRICT table where appropriate\n" 66 " --stats Show statistics at the end\n" 67 " --stmtscanstatus Activate SQLITE_DBCONFIG_STMT_SCANSTATUS\n" 68 " --temp N N from 0 to 9. 0: no temp table. 9: all temp tables\n" 69 " --testset T Run test-set T (main, cte, rtree, orm, fp, json,\n" 70 " star, app, debug). Can be a comma-separated list\n" 71 " of values, with /SCALE suffixes or macro \"mix1\"\n" 72 " --trace Turn on SQL tracing\n" 73 " --threads N Use up to N threads for sorting\n" 74 " --utf16be Set text encoding to UTF-16BE\n" 75 " --utf16le Set text encoding to UTF-16LE\n" 76 " --verify Run additional verification steps\n" 77 " --vfs NAME Use the given (preinstalled) VFS\n" 78 " --without-rowid Use WITHOUT ROWID where appropriate\n" 79 ; 80 81 #include "sqlite3.h" 82 #include 83 #include 84 #include 85 #include 86 #include 87 #include 88 #ifndef _WIN32 89 # include 90 #else 91 # include 92 #endif 93 #define ISSPACE(X) isspace((unsigned char)(X)) 94 #define ISDIGIT(X) isdigit((unsigned char)(X)) 95 96 #if SQLITE_VERSION_NUMBER<`3005000` 97 # define sqlite3_int64 sqlite_int64 98 #endif 99 100 typedef sqlite3_uint64 u64; 101 102 /* 103 ** State structure for a Hash hash in progress 104 */ 105 typedef struct HashContext HashContext; 106 struct HashContext { 107 unsigned char isInit; /* True if initialized */ 108 unsigned char i, j; /* State variables */ 109 unsigned char s[256]; /* State variables */ 110 unsigned char r[32]; /* Result */ 111 }; 112 113 114 /* All global state is held in this structure */ 115 static struct Global { 116 sqlite3 *db; /* The open database connection */ 117 const char *zDbName; /* Name of the database file */ 118 const char *zVfs; /* --vfs NAME */ 119 sqlite3_stmt *pStmt; /* Current SQL statement */ 120 sqlite3_int64 iStart; /* Start-time for the current test */ 121 sqlite3_int64 iTotal; /* Total time */ 122 int bWithoutRowid; /* True for --without-rowid */ 123 int bReprepare; /* True to reprepare the SQL on each rerun */ 124 int bSqlOnly; /* True to print the SQL once only */ 125 int bExplain; /* Print SQL with EXPLAIN prefix */ 126 int bVerify; /* Try to verify that results are correct */ 127 int bMemShrink; /* Call sqlite3_db_release_memory() often */ 128 int eTemp; /* 0: no TEMP. 9: always TEMP. */ 129 int szTest; /* Scale factor for test iterations */ 130 int szBase; /* Base size prior to testset scaling */ 131 int nRepeat; /* Repeat selects this many times */ 132 int doCheckpoint; /* Run PRAGMA wal_checkpoint after each trans */ 133 int nReserve; /* Reserve bytes */ 134 int stmtScanStatus; /* True to activate Stmt ScanStatus reporting */ 135 int doBigTransactions; /* Enable transactions on tests 410 and 510 */ 136 const char *zWR; /* Might be WITHOUT ROWID */ 137 const char *zNN; /* Might be NOT NULL */ 138 const char *zPK; /* Might be UNIQUE or PRIMARY KEY */ 139 unsigned int x, y; /* Pseudo-random number generator state */ 140 u64 nResByte; /* Total number of result bytes */ 141 int nResult; /* Size of the current result */ 142 char zResult[3000]; /* Text of the current result */ 143 FILE *pScript; /* Write an SQL script into this file */ 144 #ifndef SPEEDTEST_OMIT_HASH 145 FILE *hashFile; /* Store all hash results in this file */ 146 HashContext hash; /* Hash of all output */ 147 #endif 148 } g; 149 150 /* Return " TEMP" or "", as appropriate for creating a table. 151 */ 152 static const char *isTemp(int N){ 153 return g.eTemp>=N ? " TEMP" : ""; 154 } 155 156 /* Print an error message and exit */ 157 static void fatal_error(const char *zMsg, ...){ 158 va_list ap; 159 va_start(ap, zMsg); 160 vfprintf(stderr, zMsg, ap); 161 va_end(ap); 162 #ifdef SQLITE_SPEEDTEST1_WASM 163 /* Emscripten complains when exit() is called and anything is left 164 in the I/O buffers. */ 165 fflush(stdout); 166 fflush(stderr); 167 #endif 168 exit(1); 169 } 170 171 #ifndef SPEEDTEST_OMIT_HASH 172 /**************************************************************************** 173 ** Hash algorithm used to verify that compilation is not miscompiled 174 ** in such a was as to generate an incorrect result. 175 */ 176 177 /* 178 ** Initialize a new hash. iSize determines the size of the hash 179 ** in bits and should be one of 224, 256, 384, or 512. Or iSize 180 ** can be zero to use the default hash size of 256 bits. 181 */ 182 static void HashInit(void){ 183 unsigned int k; 184 g.hash.i = 0; 185 g.hash.j = 0; 186 for(k=0; k<256; k++) g.hash.s[k] = k; 187 } 188 189 /* 190 ** Make consecutive calls to the HashUpdate function to add new content 191 ** to the hash 192 */ 193 static void HashUpdate( 194 const unsigned char *aData, 195 unsigned int nData 196 ){ 197 unsigned char t; 198 unsigned char i = g.hash.i; 199 unsigned char j = g.hash.j; 200 unsigned int k; 201 if( g.hashFile ) fwrite(aData, 1, nData, g.hashFile); 202 for(k=0; k='0' && c<='9' ) return c - '0'; 243 if( c>='a' && c<='f' ) return c - 'a' + 10; 244 if( c>='A' && c<='F' ) return c - 'A' + 10; 245 return -1; 246 } 247 248 /* Provide an alternative to sqlite3_stricmp() in older versions of 249 ** SQLite */ 250 #if SQLITE_VERSION_NUMBER<`3007011` 251 # define sqlite3_stricmp strcmp 252 #endif 253 254 /* 255 ** Interpret zArg as an integer value, possibly with suffixes. 256 */ 257 static int integerValue(const char *zArg){ 258 sqlite3_int64 v = 0; 259 static const struct { char *zSuffix; int iMult; } aMult[] = { 260 { "KiB", 1024 }, 261 { "MiB", 1024*1024 }, 262 { "GiB", 1024*1024*1024 }, 263 { "KB", 1000 }, 264 { "MB", `1000000` }, 265 { "GB", `1000000000` }, 266 { "K", 1000 }, 267 { "M", `1000000` }, 268 { "G", `1000000000` }, 269 }; 270 int i; 271 int isNeg = 0; 272 if( zArg[0]=='-' ){ 273 isNeg = 1; 274 zArg++; 275 }else if( zArg[0]=='+' ){ 276 zArg++; 277 } 278 if( zArg[0]=='0' && zArg[1]=='x' ){ 279 int x; 280 zArg += 2; 281 while( (x = hexDigitValue(zArg[0]))>=0 ){ 282 v = (v<<4) + x; 283 zArg++; 284 } 285 }else{ 286 while( isdigit(zArg[0]) ){ 287 v = v*10 + zArg[0] - '0'; 288 zArg++; 289 } 290 } 291 for(i=0; i0x7fffffff ) fatal_error("parameter too large - max `2147483648`"); 298 return (int)(isNeg? -v : v); 299 } 300 301 /* Return the current wall-clock time, in milliseconds */ 302 sqlite3_int64 speedtest1_timestamp(void){ 303 #if SQLITE_VERSION_NUMBER<`3005000` 304 return 0; 305 #else 306 static sqlite3_vfs *clockVfs = 0; 307 sqlite3_int64 t; 308 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0); 309 #if SQLITE_VERSION_NUMBER>=`3007000` 310 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ 311 clockVfs->xCurrentTimeInt64(clockVfs, &t); 312 }else 313 #endif 314 { 315 double r; 316 clockVfs->xCurrentTime(clockVfs, &r); 317 t = (sqlite3_int64)(r*`86400000`.0); 318 } 319 return t; 320 #endif 321 } 322 323 /* Return a pseudo-random unsigned integer */ 324 unsigned int speedtest1_random(void){ 325 g.x = (g.x>>1) ^ ((1+~(g.x&1)) & 0xd0000001); 326 g.y = g.y*`1103515245` + 12345; 327 return g.x ^ g.y; 328 } 329 330 /* Map the value in within the range of 1...limit into another 331 ** number in a way that is chatic and invertable. 332 */ 333 unsigned swizzle(unsigned in, unsigned limit){ 334 unsigned out = 0; 335 while( limit ){ 336 out = (out<<1) | (in&1); 337 in >>= 1; 338 limit >>= 1; 339 } 340 return out; 341 } 342 343 /* Round up a number so that it is a power of two minus one 344 */ 345 unsigned roundup_allones(unsigned limit){ 346 unsigned m = 1; 347 while( m "one hundred twenty three" 358 */ 359 int speedtest1_numbername(unsigned int n, char *zOut, int nOut){ 360 static const char *ones[] = { "zero", "one", "two", "three", "four", "five", 361 "six", "seven", "eight", "nine", "ten", "eleven", "twelve", 362 "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", 363 "eighteen", "nineteen" }; 364 static const char *tens[] = { "", "ten", "twenty", "thirty", "forty", 365 "fifty", "sixty", "seventy", "eighty", "ninety" }; 366 int i = 0; 367 368 if( n>=`1000000000` ){ 369 i += speedtest1_numbername(n/`1000000000`, zOut+i, nOut-i); 370 sqlite3_snprintf(nOut-i, zOut+i, " billion"); 371 i += (int)strlen(zOut+i); 372 n = n % `1000000000`; 373 } 374 if( n>=`1000000` ){ 375 if( i && i=1000 ){ 382 if( i && i=100 ){ 389 if( i && i=20 ){ 395 if( i && i0 ){ 401 if( i && iNAMEWIDTH ){ 428 zName[NAMEWIDTH] = 0; 429 n = NAMEWIDTH; 430 } 431 if( g.pScript ){ 432 fprintf(g.pScript,"-- begin test %d %.*s\n", iTestNumber, n, zName) 433 /* maintenance reminder: ^^^ code in ext/wasm expects %d to be 434 ** field #4 (as in: cut -d' ' -f4). */; 435 } 436 if( g.bSqlOnly ){ 437 printf("/* %4d - %s%.*s */\n", iTestNum, zName, NAMEWIDTH-n, zDots); 438 }else{ 439 printf("%4d - %s%.*s ", iTestNum, zName, NAMEWIDTH-n, zDots); 440 fflush(stdout); 441 } 442 sqlite3_free(zName); 443 g.nResult = 0; 444 g.iStart = speedtest1_timestamp(); 445 g.x = 0xad131d0b; 446 g.y = 0x44f9eac8; 447 } 448 449 /* Forward reference */ 450 void speedtest1_exec(const char*,...); 451 452 /* Complete a test case */ 453 void speedtest1_end_test(void){ 454 sqlite3_int64 iElapseTime = speedtest1_timestamp() - g.iStart; 455 if( g.doCheckpoint ) speedtest1_exec("PRAGMA wal_checkpoint;"); 456 assert( iTestNumber > 0 ); 457 if( g.pScript ){ 458 fprintf(g.pScript,"-- end test %d\n", iTestNumber); 459 } 460 if( !g.bSqlOnly ){ 461 g.iTotal += iElapseTime; 462 printf("%4d.%03ds\n", (int)(iElapseTime/1000), (int)(iElapseTime%1000)); 463 } 464 if( g.pStmt ){ 465 sqlite3_finalize(g.pStmt); 466 g.pStmt = 0; 467 } 468 iTestNumber = 0; 469 } 470 471 /* Report end of testing */ 472 void speedtest1_final(void){ 473 if( !g.bSqlOnly ){ 474 printf(" TOTAL%.*s %4d.%03ds\n", NAMEWIDTH-5, zDots, 475 (int)(g.iTotal/1000), (int)(g.iTotal%1000)); 476 } 477 if( g.bVerify ){ 478 #ifndef SPEEDTEST_OMIT_HASH 479 int i; 480 #endif 481 printf("Verification Hash: %llu ", g.nResByte); 482 #ifndef SPEEDTEST_OMIT_HASH 483 HashUpdate((const unsigned char*)"\n", 1); 484 HashFinal(); 485 for(i=0; i<24; i++){ 486 printf("%02x", g.hash.r[i]); 487 } 488 if( g.hashFile && g.hashFile!=stdout ) fclose(g.hashFile); 489 #endif 490 printf("\n"); 491 } 492 } 493 494 /* Print an SQL statement to standard output */ 495 static void printSql(const char *zSql){ 496 int n = (int)strlen(zSql); 497 while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ){ n--; } 498 if( g.bExplain ) printf("EXPLAIN "); 499 printf("%.*s;\n", n, zSql); 500 if( g.bExplain 501 #if SQLITE_VERSION_NUMBER>=`3007017` 502 && ( sqlite3_strglob("CREATE *", zSql)==0 503 || sqlite3_strglob("DROP *", zSql)==0 504 || sqlite3_strglob("ALTER *", zSql)==0 505 ) 506 #endif 507 ){ 508 printf("%.*s;\n", n, zSql); 509 } 510 } 511 512 /* Shrink memory used, if appropriate and if the SQLite version is capable 513 ** of doing so. 514 */ 515 void speedtest1_shrink_memory(void){ 516 #if SQLITE_VERSION_NUMBER>=`3007010` 517 if( g.bMemShrink ) sqlite3_db_release_memory(g.db); 518 #endif 519 } 520 521 /* Run SQL */ 522 void speedtest1_exec(const char *zFormat, ...){ 523 va_list ap; 524 char *zSql; 525 va_start(ap, zFormat); 526 zSql = sqlite3_vmprintf(zFormat, ap); 527 va_end(ap); 528 if( g.bSqlOnly ){ 529 printSql(zSql); 530 }else{ 531 char *zErrMsg = 0; 532 int rc; 533 if( g.pScript ){ 534 fprintf(g.pScript,"%s;\n",zSql); 535 } 536 rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg); 537 if( zErrMsg ) fatal_error("SQL error: %s\n%s\n", zErrMsg, zSql); 538 if( rc!=SQLITE_OK ) fatal_error("exec error: %s\n", sqlite3_errmsg(g.db)); 539 } 540 sqlite3_free(zSql); 541 speedtest1_shrink_memory(); 542 } 543 544 /* Run SQL and return the first column of the first row as a string. The 545 ** returned string is obtained from sqlite_malloc() and must be freed by 546 ** the caller. 547 */ 548 char *speedtest1_once(const char *zFormat, ...){ 549 va_list ap; 550 char *zSql; 551 sqlite3_stmt *pStmt; 552 char *zResult = 0; 553 va_start(ap, zFormat); 554 zSql = sqlite3_vmprintf(zFormat, ap); 555 va_end(ap); 556 if( g.bSqlOnly ){ 557 printSql(zSql); 558 }else{ 559 int rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0); 560 if( rc ){ 561 fatal_error("SQL error: %s\n", sqlite3_errmsg(g.db)); 562 } 563 if( g.pScript ){ 564 char *z = sqlite3_expanded_sql(pStmt); 565 fprintf(g.pScript,"%s\n",z); 566 sqlite3_free(z); 567 } 568 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 569 const char *z = (const char*)sqlite3_column_text(pStmt, 0); 570 if( z ) zResult = sqlite3_mprintf("%s", z); 571 } 572 rc = sqlite3_reset(pStmt); 573 if( rc!=SQLITE_OK ){ 574 fatal_error("%s\nError code %d: %s\n", 575 sqlite3_sql(pStmt), rc, sqlite3_errmsg(g.db)); 576 } 577 sqlite3_finalize(pStmt); 578 } 579 sqlite3_free(zSql); 580 speedtest1_shrink_memory(); 581 return zResult; 582 } 583 584 /* Prepare an SQL statement */ 585 void speedtest1_prepare(const char *zFormat, ...){ 586 va_list ap; 587 char *zSql; 588 va_start(ap, zFormat); 589 zSql = sqlite3_vmprintf(zFormat, ap); 590 va_end(ap); 591 if( g.bSqlOnly ){ 592 printSql(zSql); 593 }else{ 594 int rc; 595 if( g.pStmt ) sqlite3_finalize(g.pStmt); 596 rc = sqlite3_prepare_v2(g.db, zSql, -1, &g.pStmt, 0); 597 if( rc ){ 598 fatal_error("SQL error: %s\n", sqlite3_errmsg(g.db)); 599 } 600 } 601 sqlite3_free(zSql); 602 } 603 604 /* Run an SQL statement previously prepared */ 605 void speedtest1_run(void){ 606 int i, n, len, rc; 607 if( g.bSqlOnly ) return; 608 assert( g.pStmt ); 609 g.nResult = 0; 610 if( g.pScript ){ 611 char *z = sqlite3_expanded_sql(g.pStmt); 612 fprintf(g.pScript,"%s\n",z); 613 sqlite3_free(z); 614 } 615 while( sqlite3_step(g.pStmt)==SQLITE_ROW ){ 616 n = sqlite3_column_count(g.pStmt); 617 for(i=0; i>4]; 644 zChar[1] = "`0123456789abcdef`"[aBlob[iBlob]&15]; 645 HashUpdate(zChar,2); 646 } 647 g.nResByte += nBlob*2 + 2; 648 }else{ 649 HashUpdate((unsigned char*)z, len); 650 g.nResByte += len + 2; 651 } 652 } 653 #endif 654 if( g.nResult+len0 ) g.zResult[g.nResult++] = ' '; 656 memcpy(g.zResult + g.nResult, z, len+1); 657 g.nResult += len; 658 } 659 } 660 } 661 #if SQLITE_VERSION_NUMBER>=`3006001` 662 if( g.bReprepare ){ 663 sqlite3_stmt *pNew; 664 sqlite3_prepare_v2(g.db, sqlite3_sql(g.pStmt), -1, &pNew, 0); 665 rc = sqlite3_finalize(g.pStmt); 666 if( rc!=SQLITE_OK ){ 667 fatal_error("%s\nError code %d: %s\n", 668 sqlite3_sql(pNew), rc, sqlite3_errmsg(g.db)); 669 } 670 g.pStmt = pNew; 671 }else 672 #endif 673 { 674 rc = sqlite3_reset(g.pStmt); 675 if( rc!=SQLITE_OK ){ 676 fatal_error("%s\nError code %d: %s\n", 677 sqlite3_sql(g.pStmt), rc, sqlite3_errmsg(g.db)); 678 } 679 } 680 speedtest1_shrink_memory(); 681 } 682 683 #ifndef SQLITE_OMIT_DEPRECATED 684 /* The sqlite3_trace() callback function */ 685 static void traceCallback(void *NotUsed, const char *zSql){ 686 int n = (int)strlen(zSql); 687 while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ) n--; 688 fprintf(stderr,"%.*s;\n", n, zSql); 689 } 690 #endif /* SQLITE_OMIT_DEPRECATED */ 691 692 /* Substitute random() function that gives the same random 693 ** sequence on each run, for repeatability. */ 694 static void randomFunc( 695 sqlite3_context *context, 696 int NotUsed, 697 sqlite3_value **NotUsed2 698 ){ 699 sqlite3_result_int64(context, (sqlite3_int64)speedtest1_random()); 700 } 701 702 /* Estimate the square root of an integer */ 703 static int est_square_root(int x){ 704 int y0 = x/2; 705 int y1; 706 int n; 707 for(n=0; y0>0 && n<10; n++){ 708 y1 = (y0 + x/y0)/2; 709 if( y1==y0 ) break; 710 y0 = y1; 711 } 712 return y0; 713 } 714 715 716 #if SQLITE_VERSION_NUMBER<`3005004` 717 /* 718 ** An implementation of group_concat(). Used only when testing older 719 ** versions of SQLite that lack the built-in group_concat(). 720 */ 721 struct groupConcat { 722 char *z; 723 int nAlloc; 724 int nUsed; 725 }; 726 static void groupAppend(struct groupConcat *p, const char *z, int n){ 727 if( p->nUsed+n >= p->nAlloc ){ 728 int n2 = (p->nAlloc+n+1)*2; 729 char *z2 = sqlite3_realloc(p->z, n2); 730 if( z2==0 ) return; 731 p->z = z2; 732 p->nAlloc = n2; 733 } 734 memcpy(p->z+p->nUsed, z, n); 735 p->nUsed += n; 736 } 737 static void groupStep( 738 sqlite3_context *context, 739 int argc, 740 sqlite3_value **argv 741 ){ 742 const char *zVal; 743 struct groupConcat *p; 744 const char *zSep; 745 int nVal, nSep; 746 assert( argc==1 || argc==2 ); 747 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return; 748 p= (struct groupConcat*)sqlite3_aggregate_context(context, sizeof(*p)); 749 750 if( p ){ 751 int firstTerm = p->nUsed==0; 752 if( !firstTerm ){ 753 if( argc==2 ){ 754 zSep = (char*)sqlite3_value_text(argv[1]); 755 nSep = sqlite3_value_bytes(argv[1]); 756 }else{ 757 zSep = ","; 758 nSep = 1; 759 } 760 if( nSep ) groupAppend(p, zSep, nSep); 761 } 762 zVal = (char*)sqlite3_value_text(argv[0]); 763 nVal = sqlite3_value_bytes(argv[0]); 764 if( zVal ) groupAppend(p, zVal, nVal); 765 } 766 } 767 static void groupFinal(sqlite3_context *context){ 768 struct groupConcat *p; 769 p = sqlite3_aggregate_context(context, 0); 770 if( p && p->z ){ 771 p->z[p->nUsed] = 0; 772 sqlite3_result_text(context, p->z, p->nUsed, sqlite3_free); 773 } 774 } 775 #endif 776 777 /* 778 ** The main and default testset 779 */ 780 void testset_main(void){ 781 int i; /* Loop counter */ 782 int n; /* iteration count */ 783 int sz; /* Size of the tables */ 784 int maxb; /* Maximum swizzled value */ 785 unsigned x1 = 0, x2 = 0; /* Parameters */ 786 int len = 0; /* Length of the zNum[] string */ 787 char zNum[2000]; /* A number name */ 788 789 sz = n = g.szTest*500; 790 zNum[0] = 0; 791 maxb = roundup_allones(sz); 792 speedtest1_begin_test(100, "%d INSERTs into table with no index", n); 793 speedtest1_exec("BEGIN"); 794 speedtest1_exec("CREATE%s TABLE z1(a INTEGER %s, b INTEGER %s, c TEXT %s);", 795 isTemp(9), g.zNN, g.zNN, g.zNN); 796 speedtest1_prepare("INSERT INTO z1 VALUES(?1,?2,?3); -- %d times", n); 797 for(i=1; i<=n; i++){ 798 x1 = swizzle(i,maxb); 799 speedtest1_numbername(x1, zNum, sizeof(zNum)); 800 sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1); 801 sqlite3_bind_int(g.pStmt, 2, i); 802 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC); 803 speedtest1_run(); 804 } 805 speedtest1_exec("COMMIT"); 806 speedtest1_end_test(); 807 808 809 n = sz; 810 speedtest1_begin_test(110, "%d ordered INSERTS with one index/PK", n); 811 speedtest1_exec("BEGIN"); 812 speedtest1_exec( 813 "CREATE%s TABLE z2(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s", 814 isTemp(5), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); 815 speedtest1_prepare("INSERT INTO z2 VALUES(?1,?2,?3); -- %d times", n); 816 for(i=1; i<=n; i++){ 817 x1 = swizzle(i,maxb); 818 speedtest1_numbername(x1, zNum, sizeof(zNum)); 819 sqlite3_bind_int(g.pStmt, 1, i); 820 sqlite3_bind_int64(g.pStmt, 2, (sqlite3_int64)x1); 821 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC); 822 speedtest1_run(); 823 } 824 speedtest1_exec("COMMIT"); 825 speedtest1_end_test(); 826 827 828 n = sz; 829 speedtest1_begin_test(120, "%d unordered INSERTS with one index/PK", n); 830 speedtest1_exec("BEGIN"); 831 speedtest1_exec( 832 "CREATE%s TABLE t3(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s", 833 isTemp(3), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); 834 speedtest1_prepare("INSERT INTO t3 VALUES(?1,?2,?3); -- %d times", n); 835 for(i=1; i<=n; i++){ 836 x1 = swizzle(i,maxb); 837 speedtest1_numbername(x1, zNum, sizeof(zNum)); 838 sqlite3_bind_int(g.pStmt, 2, i); 839 sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1); 840 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC); 841 speedtest1_run(); 842 } 843 speedtest1_exec("COMMIT"); 844 speedtest1_end_test(); 845 846 #if SQLITE_VERSION_NUMBER<`3005004` 847 sqlite3_create_function(g.db, "group_concat", 1, SQLITE_UTF8, 0, 848 0, groupStep, groupFinal); 849 #endif 850 851 n = 25; 852 speedtest1_begin_test(130, "%d SELECTS, numeric BETWEEN, unindexed", n); 853 speedtest1_exec("BEGIN"); 854 speedtest1_prepare( 855 "SELECT count(*), avg(b), sum(length(c)), group_concat(c) FROM z1\n" 856 " WHERE b BETWEEN ?1 AND ?2; -- %d times", n 857 ); 858 for(i=1; i<=n; i++){ 859 if( (i-1)%g.nRepeat==0 ){ 860 x1 = speedtest1_random()%maxb; 861 x2 = speedtest1_random()%10 + sz/5000 + x1; 862 } 863 sqlite3_bind_int(g.pStmt, 1, x1); 864 sqlite3_bind_int(g.pStmt, 2, x2); 865 speedtest1_run(); 866 } 867 speedtest1_exec("COMMIT"); 868 speedtest1_end_test(); 869 870 871 n = 10; 872 speedtest1_begin_test(140, "%d SELECTS, LIKE, unindexed", n); 873 speedtest1_exec("BEGIN"); 874 speedtest1_prepare( 875 "SELECT count(*), avg(b), sum(length(c)), group_concat(c) FROM z1\n" 876 " WHERE c LIKE ?1; -- %d times", n 877 ); 878 for(i=1; i<=n; i++){ 879 if( (i-1)%g.nRepeat==0 ){ 880 x1 = speedtest1_random()%maxb; 881 zNum[0] = '%'; 882 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); 883 zNum[len] = '%'; 884 zNum[len+1] = 0; 885 } 886 sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); 887 speedtest1_run(); 888 } 889 speedtest1_exec("COMMIT"); 890 speedtest1_end_test(); 891 892 893 n = 10; 894 speedtest1_begin_test(142, "%d SELECTS w/ORDER BY, unindexed", n); 895 speedtest1_exec("BEGIN"); 896 speedtest1_prepare( 897 "SELECT a, b, c FROM z1 WHERE c LIKE ?1\n" 898 " ORDER BY a; -- %d times", n 899 ); 900 for(i=1; i<=n; i++){ 901 if( (i-1)%g.nRepeat==0 ){ 902 x1 = speedtest1_random()%maxb; 903 zNum[0] = '%'; 904 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); 905 zNum[len] = '%'; 906 zNum[len+1] = 0; 907 } 908 sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); 909 speedtest1_run(); 910 } 911 speedtest1_exec("COMMIT"); 912 speedtest1_end_test(); 913 914 n = 10; /* g.szTest/5; */ 915 speedtest1_begin_test(145, "%d SELECTS w/ORDER BY and LIMIT, unindexed", n); 916 speedtest1_exec("BEGIN"); 917 speedtest1_prepare( 918 "SELECT a, b, c FROM z1 WHERE c LIKE ?1\n" 919 " ORDER BY a LIMIT 10; -- %d times", n 920 ); 921 for(i=1; i<=n; i++){ 922 if( (i-1)%g.nRepeat==0 ){ 923 x1 = speedtest1_random()%maxb; 924 zNum[0] = '%'; 925 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2); 926 zNum[len] = '%'; 927 zNum[len+1] = 0; 928 } 929 sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC); 930 speedtest1_run(); 931 } 932 speedtest1_exec("COMMIT"); 933 speedtest1_end_test(); 934 935 936 speedtest1_begin_test(150, "CREATE INDEX five times"); 937 speedtest1_exec("BEGIN;"); 938 speedtest1_exec("CREATE UNIQUE INDEX t1b ON z1(b);"); 939 speedtest1_exec("CREATE INDEX t1c ON z1(c);"); 940 speedtest1_exec("CREATE UNIQUE INDEX t2b ON z2(b);"); 941 speedtest1_exec("CREATE INDEX t2c ON z2(c DESC);"); 942 speedtest1_exec("CREATE INDEX t3bc ON t3(b,c);"); 943 speedtest1_exec("COMMIT;"); 944 speedtest1_end_test(); 945 946 947 n = sz/5; 948 speedtest1_begin_test(160, "%d SELECTS, numeric BETWEEN, indexed", n); 949 speedtest1_exec("BEGIN"); 950 speedtest1_prepare( 951 "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM z1\n" 952 " WHERE b BETWEEN ?1 AND ?2; -- %d times", n 953 ); 954 for(i=1; i<=n; i++){ 955 if( (i-1)%g.nRepeat==0 ){ 956 x1 = speedtest1_random()%maxb; 957 x2 = speedtest1_random()%10 + sz/5000 + x1; 958 } 959 sqlite3_bind_int(g.pStmt, 1, x1); 960 sqlite3_bind_int(g.pStmt, 2, x2); 961 speedtest1_run(); 962 } 963 speedtest1_exec("COMMIT"); 964 speedtest1_end_test(); 965 966 967 n = sz/5; 968 speedtest1_begin_test(161, "%d SELECTS, numeric BETWEEN, PK", n); 969 speedtest1_exec("BEGIN"); 970 speedtest1_prepare( 971 "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM z2\n" 972 " WHERE a BETWEEN ?1 AND ?2; -- %d times", n 973 ); 974 for(i=1; i<=n; i++){ 975 if( (i-1)%g.nRepeat==0 ){ 976 x1 = speedtest1_random()%maxb; 977 x2 = speedtest1_random()%10 + sz/5000 + x1; 978 } 979 sqlite3_bind_int(g.pStmt, 1, x1); 980 sqlite3_bind_int(g.pStmt, 2, x2); 981 speedtest1_run(); 982 } 983 speedtest1_exec("COMMIT"); 984 speedtest1_end_test(); 985 986 987 n = sz/5; 988 speedtest1_begin_test(170, "%d SELECTS, text BETWEEN, indexed", n); 989 speedtest1_exec("BEGIN"); 990 speedtest1_prepare( 991 "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM z1\n" 992 " WHERE c BETWEEN ?1 AND (?1||'~'); -- %d times", n 993 ); 994 for(i=1; i<=n; i++){ 995 if( (i-1)%g.nRepeat==0 ){ 996 x1 = swizzle(i, maxb); 997 len = speedtest1_numbername(x1, zNum, sizeof(zNum)-1); 998 } 999 sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC); 1000 speedtest1_run(); 1001 } 1002 speedtest1_exec("COMMIT"); 1003 speedtest1_end_test(); 1004 1005 n = sz; 1006 speedtest1_begin_test(180, "%d INSERTS with three indexes", n); 1007 speedtest1_exec("BEGIN"); 1008 speedtest1_exec( 1009 "CREATE%s TABLE t4(\n" 1010 " a INTEGER %s %s,\n" 1011 " b INTEGER %s,\n" 1012 " c TEXT %s\n" 1013 ") %s", 1014 isTemp(1), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR); 1015 speedtest1_exec("CREATE INDEX t4b ON t4(b)"); 1016 speedtest1_exec("CREATE INDEX t4c ON t4(c)"); 1017 speedtest1_exec("INSERT INTO t4 SELECT * FROM z1"); 1018 speedtest1_exec("COMMIT"); 1019 speedtest1_end_test(); 1020 1021 n = sz; 1022 speedtest1_begin_test(190, "DELETE and REFILL one table", n); 1023 speedtest1_exec("DELETE FROM z2;"); 1024 speedtest1_exec("INSERT INTO z2 SELECT * FROM z1;"); 1025 speedtest1_end_test(); 1026 1027 1028 speedtest1_begin_test(200, "VACUUM"); 1029 speedtest1_exec("VACUUM"); 1030 speedtest1_end_test(); 1031 1032 1033 speedtest1_begin_test(210, "ALTER TABLE ADD COLUMN, and query"); 1034 speedtest1_exec("ALTER TABLE z2 ADD COLUMN d INT DEFAULT 123"); 1035 speedtest1_exec("SELECT sum(d) FROM z2"); 1036 speedtest1_end_test(); 1037 1038 1039 n = sz/5; 1040 speedtest1_begin_test(230, "%d UPDATES, numeric BETWEEN, indexed", n); 1041 speedtest1_exec("BEGIN"); 1042 speedtest1_prepare( 1043 "UPDATE z2 SET d=b*2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n 1044 ); 1045 for(i=1; i<=n; i++){ 1046 x1 = speedtest1_random()%maxb; 1047 x2 = speedtest1_random()%10 + sz/5000 + x1; 1048 sqlite3_bind_int(g.pStmt, 1, x1); 1049 sqlite3_bind_int(g.pStmt, 2, x2); 1050 speedtest1_run(); 1051 } 1052 speedtest1_exec("COMMIT"); 1053 speedtest1_end_test(); 1054 1055 1056 n = sz; 1057 speedtest1_begin_test(240, "%d UPDATES of individual rows", n); 1058 speedtest1_exec("BEGIN"); 1059 speedtest1_prepare( 1060 "UPDATE z2 SET d=b*3 WHERE a=?1; -- %d times", n 1061 ); 1062 for(i=1; i<=n; i++){ 1063 x1 = speedtest1_random()%sz + 1; 1064 sqlite3_bind_int(g.pStmt, 1, x1); 1065 speedtest1_run(); 1066 } 1067 speedtest1_exec("COMMIT"); 1068 speedtest1_end_test(); 1069 1070 speedtest1_begin_test(250, "One big UPDATE of the whole %d-row table", sz); 1071 speedtest1_exec("UPDATE z2 SET d=b*4"); 1072 speedtest1_end_test(); 1073 1074 1075 speedtest1_begin_test(260, "Query added column after filling"); 1076 speedtest1_exec("SELECT sum(d) FROM z2"); 1077 speedtest1_end_test(); 1078 1079 1080 1081 n = sz/5; 1082 speedtest1_begin_test(270, "%d DELETEs, numeric BETWEEN, indexed", n); 1083 speedtest1_exec("BEGIN"); 1084 speedtest1_prepare( 1085 "DELETE FROM z2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n 1086 ); 1087 for(i=1; i<=n; i++){ 1088 x1 = speedtest1_random()%maxb + 1; 1089 x2 = speedtest1_random()%10 + sz/5000 + x1; 1090 sqlite3_bind_int(g.pStmt, 1, x1); 1091 sqlite3_bind_int(g.pStmt, 2, x2); 1092 speedtest1_run(); 1093 } 1094 speedtest1_exec("COMMIT"); 1095 speedtest1_end_test(); 1096 1097 1098 n = sz; 1099 speedtest1_begin_test(280, "%d DELETEs of individual rows", n); 1100 speedtest1_exec("BEGIN"); 1101 speedtest1_prepare( 1102 "DELETE FROM t3 WHERE a=?1; -- %d times", n 1103 ); 1104 for(i=1; i<=n; i++){ 1105 x1 = speedtest1_random()%sz + 1; 1106 sqlite3_bind_int(g.pStmt, 1, x1); 1107 speedtest1_run(); 1108 } 1109 speedtest1_exec("COMMIT"); 1110 speedtest1_end_test(); 1111 1112 1113 speedtest1_begin_test(290, "Refill two %d-row tables using REPLACE", sz); 1114 speedtest1_exec("REPLACE INTO z2(a,b,c) SELECT a,b,c FROM z1"); 1115 speedtest1_exec("REPLACE INTO t3(a,b,c) SELECT a,b,c FROM z1"); 1116 speedtest1_end_test(); 1117 1118 speedtest1_begin_test(300, "Refill a %d-row table using (b&1)==(a&1)", sz); 1119 speedtest1_exec("DELETE FROM z2;"); 1120 speedtest1_exec("INSERT INTO z2(a,b,c)\n" 1121 " SELECT a,b,c FROM z1 WHERE (b&1)==(a&1);"); 1122 speedtest1_exec("INSERT INTO z2(a,b,c)\n" 1123 " SELECT a,b,c FROM z1 WHERE (b&1)<>(a&1);"); 1124 speedtest1_end_test(); 1125 1126 1127 n = sz/5; 1128 speedtest1_begin_test(310, "%d four-ways joins", n); 1129 speedtest1_exec("BEGIN"); 1130 speedtest1_prepare( 1131 "SELECT z1.c FROM z1, z2, t3, t4\n" 1132 " WHERE t4.a BETWEEN ?1 AND ?2\n" 1133 " AND t3.a=t4.b\n" 1134 " AND z2.a=t3.b\n" 1135 " AND z1.c=z2.c;" 1136 ); 1137 for(i=1; i<=n; i++){ 1138 x1 = speedtest1_random()%sz + 1; 1139 x2 = speedtest1_random()%10 + x1 + 4; 1140 sqlite3_bind_int(g.pStmt, 1, x1); 1141 sqlite3_bind_int(g.pStmt, 2, x2); 1142 speedtest1_run(); 1143 } 1144 speedtest1_exec("COMMIT"); 1145 speedtest1_end_test(); 1146 1147 speedtest1_begin_test(320, "subquery in result set", n); 1148 speedtest1_prepare( 1149 "SELECT sum(a), max(c),\n" 1150 " avg((SELECT a FROM z2 WHERE 5+z2.b=z1.b) AND rowid=`3008002` ? "WITHOUT ROWID" : ""); 1203 speedtest1_prepare("REPLACE INTO t6 VALUES(?1,?2); -- %d times",n); 1204 for(i=1; i<=n; i++){ 1205 x1 = swizzle(i,maxb); 1206 speedtest1_numbername(x1, zNum, sizeof(zNum)); 1207 sqlite3_bind_int(g.pStmt, 2, i); 1208 sqlite3_bind_text(g.pStmt, 1, zNum, -1, SQLITE_STATIC); 1209 speedtest1_run(); 1210 } 1211 speedtest1_exec("COMMIT"); 1212 speedtest1_end_test(); 1213 speedtest1_begin_test(510, "%d SELECTS on a TEXT PK", n); 1214 if( g.doBigTransactions ){ 1215 /* See notes for test 410. */ 1216 speedtest1_exec("BEGIN"); 1217 } 1218 speedtest1_prepare("SELECT b FROM t6 WHERE a=?1; -- %d times",n); 1219 for(i=1; i<=n; i++){ 1220 x1 = swizzle(i,maxb); 1221 speedtest1_numbername(x1, zNum, sizeof(zNum)); 1222 sqlite3_bind_text(g.pStmt, 1, zNum, -1, SQLITE_STATIC); 1223 speedtest1_run(); 1224 } 1225 if( g.doBigTransactions ){ 1226 speedtest1_exec("COMMIT"); 1227 } 1228 speedtest1_end_test(); 1229 speedtest1_begin_test(520, "%d SELECT DISTINCT", n); 1230 speedtest1_exec("SELECT DISTINCT b FROM t5;"); 1231 speedtest1_exec("SELECT DISTINCT b FROM t6;"); 1232 speedtest1_end_test(); 1233 1234 1235 speedtest1_begin_test(980, "PRAGMA integrity_check"); 1236 speedtest1_exec("PRAGMA integrity_check"); 1237 speedtest1_end_test(); 1238 1239 1240 speedtest1_begin_test(990, "ANALYZE"); 1241 speedtest1_exec("ANALYZE"); 1242 speedtest1_end_test(); 1243 } 1244 1245 /* 1246 ** A testset for common table expressions. This exercises code 1247 ** for views, subqueries, co-routines, etc. 1248 */ 1249 void testset_cte(void){ 1250 static const char *azPuzzle[] = { 1251 /* Easy */ 1252 "534...9.." 1253 "67.195..." 1254 ".98....6." 1255 "8...6...3" 1256 "4..8.3..1" 1257 "....2...6" 1258 ".6....28." 1259 "...419..5" 1260 "...28..79", 1261 1262 /* Medium */ 1263 "53....9.." 1264 "6..195..." 1265 ".98....6." 1266 "8...6...3" 1267 "4..8.3..1" 1268 "....2...6" 1269 ".6....28." 1270 "...419..5" 1271 "....8..79", 1272 1273 /* Hard */ 1274 "53......." 1275 "6..195..." 1276 ".98....6." 1277 "8...6...3" 1278 "4..8.3..1" 1279 "....2...6" 1280 ".6....28." 1281 "...419..5" 1282 "....8..79", 1283 }; 1284 const char *zPuz; 1285 double rSpacing; 1286 int nElem; 1287 1288 if( g.szTest<25 ){ 1289 zPuz = azPuzzle[0]; 1290 }else if( g.szTest<70 ){ 1291 zPuz = azPuzzle[1]; 1292 }else{ 1293 zPuz = azPuzzle[2]; 1294 } 1295 speedtest1_begin_test(100, "Sudoku with recursive 'digits'"); 1296 speedtest1_prepare( 1297 "WITH RECURSIVE\n" 1298 " input(sud) AS (VALUES(?1)),\n" 1299 " digits(z,lp) AS (\n" 1300 " VALUES('1', 1)\n" 1301 " UNION ALL\n" 1302 " SELECT CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9\n" 1303 " ),\n" 1304 " x(s, ind) AS (\n" 1305 " SELECT sud, instr(sud, '.') FROM input\n" 1306 " UNION ALL\n" 1307 " SELECT\n" 1308 " substr(s, 1, ind-1) || z || substr(s, ind+1),\n" 1309 " instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n" 1310 " FROM x, digits AS z\n" 1311 " WHERE ind>0\n" 1312 " AND NOT EXISTS (\n" 1313 " SELECT 1\n" 1314 " FROM digits AS lp\n" 1315 " WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n" 1316 " OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n" 1317 " OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n" 1318 " + ((ind-1)/27) * 27 + lp\n" 1319 " + ((lp-1) / 3) * 6, 1)\n" 1320 " )\n" 1321 " )\n" 1322 "SELECT s FROM x WHERE ind=0;" 1323 ); 1324 sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC); 1325 speedtest1_run(); 1326 speedtest1_end_test(); 1327 1328 speedtest1_begin_test(200, "Sudoku with VALUES 'digits'"); 1329 speedtest1_prepare( 1330 "WITH RECURSIVE\n" 1331 " input(sud) AS (VALUES(?1)),\n" 1332 " digits(z,lp) AS (VALUES('1',1),('2',2),('3',3),('4',4),('5',5),\n" 1333 " ('6',6),('7',7),('8',8),('9',9)),\n" 1334 " x(s, ind) AS (\n" 1335 " SELECT sud, instr(sud, '.') FROM input\n" 1336 " UNION ALL\n" 1337 " SELECT\n" 1338 " substr(s, 1, ind-1) || z || substr(s, ind+1),\n" 1339 " instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n" 1340 " FROM x, digits AS z\n" 1341 " WHERE ind>0\n" 1342 " AND NOT EXISTS (\n" 1343 " SELECT 1\n" 1344 " FROM digits AS lp\n" 1345 " WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n" 1346 " OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n" 1347 " OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n" 1348 " + ((ind-1)/27) * 27 + lp\n" 1349 " + ((lp-1) / 3) * 6, 1)\n" 1350 " )\n" 1351 " )\n" 1352 "SELECT s FROM x WHERE ind=0;" 1353 ); 1354 sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC); 1355 speedtest1_run(); 1356 speedtest1_end_test(); 1357 1358 rSpacing = 5.0/g.szTest; 1359 speedtest1_begin_test(300, "Mandelbrot Set with spacing=%f", rSpacing); 1360 speedtest1_prepare( 1361 "WITH RECURSIVE \n" 1362 " xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+?1 FROM xaxis WHERE x<1.2),\n" 1363 " yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+?2 FROM yaxis WHERE y<1.0),\n" 1364 " m(iter, cx, cy, x, y) AS (\n" 1365 " SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis\n" 1366 " UNION ALL\n" 1367 " SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m \n" 1368 " WHERE (x*x + y*y) < 4.0 AND iter<28\n" 1369 " ),\n" 1370 " m2(iter, cx, cy) AS (\n" 1371 " SELECT max(iter), cx, cy FROM m GROUP BY cx, cy\n" 1372 " ),\n" 1373 " a(t) AS (\n" 1374 " SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') \n" 1375 " FROM m2 GROUP BY cy\n" 1376 " )\n" 1377 "SELECT group_concat(rtrim(t),x'0a') FROM a;" 1378 ); 1379 sqlite3_bind_double(g.pStmt, 1, rSpacing*.05); 1380 sqlite3_bind_double(g.pStmt, 2, rSpacing); 1381 speedtest1_run(); 1382 speedtest1_end_test(); 1383 1384 nElem = 10000*g.szTest; 1385 speedtest1_begin_test(400, "EXCEPT operator on %d-element tables", nElem); 1386 speedtest1_prepare( 1387 "WITH RECURSIVE \n" 1388 " z1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM z1 WHERE x<%d),\n" 1389 " z2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM z2 WHERE y<%d)\n" 1390 "SELECT count(x), avg(x) FROM (\n" 1391 " SELECT x FROM z1 EXCEPT SELECT y FROM z2 ORDER BY 1\n" 1392 ");", 1393 nElem, nElem 1394 ); 1395 speedtest1_run(); 1396 speedtest1_end_test(); 1397 } 1398 1399 /* 1400 ** Compute a pseudo-random floating point ascii number. 1401 */ 1402 void speedtest1_random_ascii_fp(char *zFP){ 1403 int x = speedtest1_random(); 1404 int y = speedtest1_random(); 1405 int z; 1406 z = y%10; 1407 if( z<0 ) z = -z; 1408 y /= 10; 1409 sqlite3_snprintf(100,zFP,"%d.%de%d",y,z,x%200); 1410 } 1411 1412 /* 1413 ** A testset for floating-point numbers. 1414 */ 1415 void testset_fp(void){ 1416 int n; 1417 int i; 1418 char zFP1[100]; 1419 char zFP2[100]; 1420 1421 n = g.szTest*5000; 1422 speedtest1_begin_test(100, "Fill a table with %d FP values", n*2); 1423 speedtest1_exec("BEGIN"); 1424 speedtest1_exec("CREATE%s TABLE z1(a REAL %s, b REAL %s);", 1425 isTemp(1), g.zNN, g.zNN); 1426 speedtest1_prepare("INSERT INTO z1 VALUES(?1,?2); -- %d times", n); 1427 for(i=1; i<=n; i++){ 1428 speedtest1_random_ascii_fp(zFP1); 1429 speedtest1_random_ascii_fp(zFP2); 1430 sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC); 1431 sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC); 1432 speedtest1_run(); 1433 } 1434 speedtest1_exec("COMMIT"); 1435 speedtest1_end_test(); 1436 1437 n = g.szTest/25 + 2; 1438 speedtest1_begin_test(110, "%d range queries", n); 1439 speedtest1_prepare("SELECT sum(b) FROM z1 WHERE a BETWEEN ?1 AND ?2"); 1440 for(i=1; i<=n; i++){ 1441 speedtest1_random_ascii_fp(zFP1); 1442 speedtest1_random_ascii_fp(zFP2); 1443 sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC); 1444 sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC); 1445 speedtest1_run(); 1446 } 1447 speedtest1_end_test(); 1448 1449 speedtest1_begin_test(120, "CREATE INDEX three times"); 1450 speedtest1_exec("BEGIN;"); 1451 speedtest1_exec("CREATE INDEX t1a ON z1(a);"); 1452 speedtest1_exec("CREATE INDEX t1b ON z1(b);"); 1453 speedtest1_exec("CREATE INDEX t1ab ON z1(a,b);"); 1454 speedtest1_exec("COMMIT;"); 1455 speedtest1_end_test(); 1456 1457 n = g.szTest/3 + 2; 1458 speedtest1_begin_test(130, "%d indexed range queries", n); 1459 speedtest1_prepare("SELECT sum(b) FROM z1 WHERE a BETWEEN ?1 AND ?2"); 1460 for(i=1; i<=n; i++){ 1461 speedtest1_random_ascii_fp(zFP1); 1462 speedtest1_random_ascii_fp(zFP2); 1463 sqlite3_bind_text(g.pStmt, 1, zFP1, -1, SQLITE_STATIC); 1464 sqlite3_bind_text(g.pStmt, 2, zFP2, -1, SQLITE_STATIC); 1465 speedtest1_run(); 1466 } 1467 speedtest1_end_test(); 1468 1469 n = g.szTest*5000; 1470 speedtest1_begin_test(140, "%d calls to round()", n); 1471 speedtest1_exec("SELECT sum(round(a,2)+round(b,4)) FROM z1;"); 1472 speedtest1_end_test(); 1473 1474 1475 speedtest1_begin_test(150, "%d printf() calls", n*4); 1476 speedtest1_exec( 1477 "WITH c(fmt) AS (VALUES('%%g'),('%%e'),('%%!g'),('%%.20f'))" 1478 "SELECT sum(printf(fmt,a)) FROM z1, c" 1479 ); 1480 speedtest1_end_test(); 1481 } 1482 1483 /* 1484 ** A testset for star-schema queries. 1485 */ 1486 void testset_star(void){ 1487 int n; 1488 int i; 1489 n = g.szTest*50; 1490 speedtest1_begin_test(100, "Create a fact table with %d entries", n); 1491 speedtest1_exec( 1492 "CREATE TABLE facttab(" 1493 " attr01 INT," 1494 " attr02 INT," 1495 " attr03 INT," 1496 " data01 TEXT," 1497 " attr04 INT," 1498 " attr05 INT," 1499 " attr06 INT," 1500 " attr07 INT," 1501 " attr08 INT," 1502 " factid INTEGER PRIMARY KEY," 1503 " data02 TEXT" 1504 ");" 1505 ); 1506 speedtest1_exec( 1507 "WITH RECURSIVE counter(nnn) AS" 1508 "(VALUES(1) UNION ALL SELECT nnn+1 FROM counter WHERE nnn<%d)" 1509 "INSERT INTO facttab(attr01,attr02,attr03,attr04,attr05," 1510 "attr06,attr07,attr08,data01,data02)" 1511 "SELECT random()%%12, random()%%13, random()%%14, random()%%15," 1512 "random()%%16, random()%%17, random()%%18, random()%%19," 1513 "concat('data-',nnn), format('%%x',random()) FROM counter;", 1514 n 1515 ); 1516 speedtest1_end_test(); 1517 1518 speedtest1_begin_test(110, "Create indexes on all attributes columns"); 1519 for(i=1; i<=8; i++){ 1520 speedtest1_exec( 1521 "CREATE INDEX fact_attr%02d ON facttab(attr%02d)", i, i 1522 ); 1523 } 1524 speedtest1_end_test(); 1525 1526 speedtest1_begin_test(120, "Create dimension tables"); 1527 for(i=1; i<=8; i++){ 1528 speedtest1_exec( 1529 "CREATE TABLE dimension%02d(" 1530 "beta%02d INT, " 1531 "content%02d TEXT, " 1532 "rate%02d REAL)", 1533 i, i, i, i 1534 ); 1535 speedtest1_exec( 1536 "WITH RECURSIVE ctr(nn) AS" 1537 " (VALUES(1) UNION ALL SELECT nn+1 FROM ctr WHERE nn<%d)" 1538 " INSERT INTO dimension%02d" 1539 " SELECT nn%%(%d), concat('content-%02d-',nn)," 1540 " (random()%%10000)*0.125 FROM ctr;", 1541 4*(i+1), i, 2*(i+1), i 1542 ); 1543 if( i&2 ){ 1544 speedtest1_exec( 1545 "CREATE INDEX dim%02d ON dimension%02d(beta%02d);", 1546 i, i, i 1547 ); 1548 }else{ 1549 speedtest1_exec( 1550 "CREATE INDEX dim%02d ON dimension%02d(beta%02d,content%02d);", 1551 i, i, i, i 1552 ); 1553 } 1554 } 1555 speedtest1_end_test(); 1556 1557 speedtest1_begin_test(130, "Star query over the entire fact table"); 1558 speedtest1_exec( 1559 "SELECT count(*), max(content04), min(content03), sum(rate04), avg(rate05)" 1560 " FROM facttab, dimension01, dimension02, dimension03, dimension04," 1561 " dimension05, dimension06, dimension07, dimension08" 1562 " WHERE attr01=beta01" 1563 " AND attr02=beta02" 1564 " AND attr03=beta03" 1565 " AND attr04=beta04" 1566 " AND attr05=beta05" 1567 " AND attr06=beta06" 1568 " AND attr07=beta07" 1569 " AND attr08=beta08" 1570 ";" 1571 ); 1572 speedtest1_end_test(); 1573 1574 speedtest1_begin_test(140, "Star query with LEFT JOINs"); 1575 speedtest1_exec( 1576 "SELECT count(*), max(content04), min(content03), sum(rate04), avg(rate05)" 1577 " FROM facttab LEFT JOIN dimension01 ON attr01=beta01" 1578 " LEFT JOIN dimension02 ON attr02=beta02" 1579 " JOIN dimension03 ON attr03=beta03" 1580 " JOIN dimension04 ON attr04=beta04" 1581 " JOIN dimension05 ON attr05=beta05" 1582 " LEFT JOIN dimension06 ON attr06=beta06" 1583 " JOIN dimension07 ON attr07=beta07" 1584 " JOIN dimension08 ON attr08=beta08" 1585 " WHERE facttab.data01 LIKE 'data-9%%'" 1586 ";" 1587 ); 1588 speedtest1_end_test(); 1589 } 1590 1591 /* 1592 ** Tests that simulate an application opening and closing an SQLite database 1593 ** frequently. Fossil is used as the model. The focus here is on rapidly 1594 ** parsing the database schema and rapidly generating prepared statements, 1595 ** in other words, rapid start-up of Fossil-like applications. 1596 ** 1597 ** The same database has no data, so the performance of sqlite3_step() is 1598 ** not significant to this testset. 1599 */ 1600 static void testset_app(void){ 1601 int i, n; 1602 speedtest1_begin_test(100, "Generate a Fossil-like database schema"); 1603 speedtest1_exec( 1604 "BEGIN;" 1605 "CREATE TABLE blob(\n" 1606 " rid INTEGER PRIMARY KEY,\n" 1607 " rcvid INTEGER,\n" 1608 " size INTEGER,\n" 1609 " uuid TEXT UNIQUE NOT NULL,\n" 1610 " content BLOB,\n" 1611 " CHECK( length(uuid)>=40 AND rid>0 )\n" 1612 ");\n" 1613 "CREATE TABLE delta(\n" 1614 " rid INTEGER PRIMARY KEY,\n" 1615 " srcid INTEGER NOT NULL REFERENCES blob\n" 1616 ");\n" 1617 "CREATE TABLE rcvfrom(\n" 1618 " rcvid INTEGER PRIMARY KEY,\n" 1619 " uid INTEGER REFERENCES user,\n" 1620 " mtime DATETIME,\n" 1621 " nonce TEXT UNIQUE,\n" 1622 " ipaddr TEXT\n" 1623 ");\n" 1624 "CREATE TABLE private(rid INTEGER PRIMARY KEY);\n" 1625 "CREATE TABLE accesslog(\n" 1626 " uname TEXT,\n" 1627 " ipaddr TEXT,\n" 1628 " success BOOLEAN,\n" 1629 " mtime TIMESTAMP\n" 1630 ");\n" 1631 "CREATE TABLE user(\n" 1632 " uid INTEGER PRIMARY KEY,\n" 1633 " login TEXT UNIQUE,\n" 1634 " pw TEXT,\n" 1635 " cap TEXT,\n" 1636 " cookie TEXT,\n" 1637 " ipaddr TEXT,\n" 1638 " cexpire DATETIME,\n" 1639 " info TEXT,\n" 1640 " mtime DATE,\n" 1641 " photo BLOB\n" 1642 ", jx TEXT DEFAULT '{}');\n" 1643 "CREATE TABLE reportfmt(\n" 1644 " rn INTEGER PRIMARY KEY,\n" 1645 " owner TEXT,\n" 1646 " title TEXT UNIQUE,\n" 1647 " mtime INTEGER,\n" 1648 " cols TEXT,\n" 1649 " sqlcode TEXT\n" 1650 ", jx TEXT DEFAULT '{}');\n" 1651 "CREATE TABLE config(\n" 1652 " name TEXT PRIMARY KEY NOT NULL,\n" 1653 " value CLOB, mtime INTEGER,\n" 1654 " CHECK( typeof(name)='text' AND length(name)>=1 )\n" 1655 ") WITHOUT ROWID;\n" 1656 "CREATE TABLE shun(uuid PRIMARY KEY, mtime INTEGER, scom TEXT)\n" 1657 " WITHOUT ROWID;\n" 1658 "CREATE TABLE concealed(\n" 1659 " hash TEXT PRIMARY KEY,\n" 1660 " content TEXT\n" 1661 ", mtime INTEGER) WITHOUT ROWID;\n" 1662 "CREATE TABLE admin_log(\n" 1663 " id INTEGER PRIMARY KEY,\n" 1664 " time INTEGER, -- Seconds since 1970\n" 1665 " page TEXT, -- path of page\n" 1666 " who TEXT, -- User who made the change\n" 1667 " what TEXT -- What changed\n" 1668 ");\n" 1669 "CREATE TABLE unversioned(\n" 1670 " name TEXT PRIMARY KEY,\n" 1671 " rcvid INTEGER,\n" 1672 " mtime DATETIME,\n" 1673 " hash TEXT,\n" 1674 " sz INTEGER,\n" 1675 " encoding INT,\n" 1676 " content BLOB\n" 1677 ") WITHOUT ROWID;\n" 1678 "CREATE TABLE subscriber(\n" 1679 " subscriberId INTEGER PRIMARY KEY,\n" 1680 " subscriberCode BLOB DEFAULT (randomblob(32)) UNIQUE,\n" 1681 " semail TEXT UNIQUE COLLATE nocase,\n" 1682 " suname TEXT,\n" 1683 " sverified BOOLEAN DEFAULT true,\n" 1684 " sdonotcall BOOLEAN,\n" 1685 " sdigest BOOLEAN,\n" 1686 " ssub TEXT,\n" 1687 " sctime INTDATE,\n" 1688 " mtime INTDATE,\n" 1689 " smip TEXT\n" 1690 ", lastContact INT);\n" 1691 "CREATE TABLE pending_alert(\n" 1692 " eventid TEXT PRIMARY KEY,\n" 1693 " sentSep BOOLEAN DEFAULT false,\n" 1694 " sentDigest BOOLEAN DEFAULT false\n" 1695 ", sentMod BOOLEAN DEFAULT false) WITHOUT ROWID;\n" 1696 "CREATE TABLE filename(\n" 1697 " fnid INTEGER PRIMARY KEY,\n" 1698 " name TEXT UNIQUE\n" 1699 ") STRICT;\n" 1700 "CREATE TABLE mlink(\n" 1701 " mid INTEGER,\n" 1702 " fid INTEGER,\n" 1703 " pmid INTEGER,\n" 1704 " pid INTEGER,\n" 1705 " fnid INTEGER REFERENCES filename,\n" 1706 " pfnid INTEGER,\n" 1707 " mperm INTEGER,\n" 1708 " isaux INT DEFAULT 0\n" 1709 ") STRICT;\n" 1710 "CREATE TABLE plink(\n" 1711 " pid INTEGER REFERENCES blob,\n" 1712 " cid INTEGER REFERENCES blob,\n" 1713 " isprim INT,\n" 1714 " mtime REAL,\n" 1715 " baseid INTEGER REFERENCES blob,\n" 1716 " UNIQUE(pid, cid)\n" 1717 ") STRICT;\n" 1718 "CREATE TABLE leaf(rid INTEGER PRIMARY KEY);\n" 1719 "CREATE TABLE event(\n" 1720 " type TEXT,\n" 1721 " mtime REAL,\n" 1722 " objid INTEGER PRIMARY KEY,\n" 1723 " tagid INTEGER,\n" 1724 " uid INTEGER REFERENCES user,\n" 1725 " bgcolor TEXT,\n" 1726 " euser TEXT,\n" 1727 " user TEXT,\n" 1728 " ecomment TEXT,\n" 1729 " comment TEXT,\n" 1730 " brief TEXT,\n" 1731 " omtime REAL\n" 1732 ") STRICT;\n" 1733 "CREATE TABLE phantom(\n" 1734 " rid INTEGER PRIMARY KEY\n" 1735 ");\n" 1736 "CREATE TABLE orphan(\n" 1737 " rid INTEGER PRIMARY KEY,\n" 1738 " baseline INTEGER\n" 1739 ") STRICT;\n" 1740 "CREATE TABLE unclustered(\n" 1741 " rid INTEGER PRIMARY KEY\n" 1742 ");\n" 1743 "CREATE TABLE unsent(\n" 1744 " rid INTEGER PRIMARY KEY\n" 1745 ");\n" 1746 "CREATE TABLE tag(\n" 1747 " tagid INTEGER PRIMARY KEY,\n" 1748 " tagname TEXT UNIQUE\n" 1749 ") STRICT;\n" 1750 "CREATE TABLE tagxref(\n" 1751 " tagid INTEGER REFERENCES tag,\n" 1752 " tagtype INTEGER,\n" 1753 " srcid INTEGER REFERENCES blob,\n" 1754 " origid INTEGER REFERENCES blob,\n" 1755 " value TEXT,\n" 1756 " mtime REAL,\n" 1757 " rid INTEGER REFERENCES blob,\n" 1758 " UNIQUE(rid, tagid)\n" 1759 ") STRICT;\n" 1760 "CREATE TABLE backlink(\n" 1761 " target TEXT,\n" 1762 " srctype INT,\n" 1763 " srcid INT,\n" 1764 " mtime REAL,\n" 1765 " UNIQUE(target, srctype, srcid)\n" 1766 ") STRICT;\n" 1767 "CREATE TABLE attachment(\n" 1768 " attachid INTEGER PRIMARY KEY,\n" 1769 " isLatest INT DEFAULT 0,\n" 1770 " mtime REAL,\n" 1771 " src TEXT,\n" 1772 " target TEXT,\n" 1773 " filename TEXT,\n" 1774 " comment TEXT,\n" 1775 " user TEXT\n" 1776 ") STRICT;\n" 1777 "CREATE TABLE cherrypick(\n" 1778 " parentid INT,\n" 1779 " childid INT,\n" 1780 " isExclude INT DEFAULT false,\n" 1781 " PRIMARY KEY(parentid, childid)\n" 1782 ") WITHOUT ROWID, STRICT;\n" 1783 "CREATE TABLE vcache(\n" 1784 " vid INTEGER, -- check-in ID\n" 1785 " fname TEXT, -- filename\n" 1786 " rid INTEGER, -- artifact ID\n" 1787 " PRIMARY KEY(vid,fname)\n" 1788 ") WITHOUT ROWID;\n" 1789 "CREATE TABLE synclog(\n" 1790 " sfrom TEXT,\n" 1791 " sto TEXT,\n" 1792 " stime INT NOT NULL,\n" 1793 " stype TEXT,\n" 1794 " PRIMARY KEY(sfrom,sto)\n" 1795 ") WITHOUT ROWID;\n" 1796 "CREATE TABLE chat(\n" 1797 " msgid INTEGER PRIMARY KEY AUTOINCREMENT,\n" 1798 " mtime JULIANDAY,\n" 1799 " lmtime TEXT,\n" 1800 " xfrom TEXT,\n" 1801 " xmsg TEXT,\n" 1802 " fname TEXT,\n" 1803 " fmime TEXT,\n" 1804 " mdel INT,\n" 1805 " file BLOB\n" 1806 ");\n" 1807 "CREATE TABLE ftsdocs(\n" 1808 " rowid INTEGER PRIMARY KEY,\n" 1809 " type CHAR(1),\n" 1810 " rid INTEGER,\n" 1811 " name TEXT,\n" 1812 " idxed BOOLEAN,\n" 1813 " label TEXT,\n" 1814 " url TEXT,\n" 1815 " mtime DATE,\n" 1816 " bx TEXT,\n" 1817 " UNIQUE(type,rid)\n" 1818 ");\n" 1819 "CREATE TABLE ticket(\n" 1820 " -- Do not change any column that begins with tkt_\n" 1821 " tkt_id INTEGER PRIMARY KEY,\n" 1822 " tkt_uuid TEXT UNIQUE,\n" 1823 " tkt_mtime DATE,\n" 1824 " tkt_ctime DATE,\n" 1825 " -- Add as many fields as required below this line\n" 1826 " type TEXT,\n" 1827 " status TEXT,\n" 1828 " subsystem TEXT,\n" 1829 " priority TEXT,\n" 1830 " severity TEXT,\n" 1831 " foundin TEXT,\n" 1832 " private_contact TEXT,\n" 1833 " resolution TEXT,\n" 1834 " title TEXT,\n" 1835 " comment TEXT\n" 1836 ");\n" 1837 "CREATE TABLE ticketchng(\n" 1838 " -- Do not change any column that begins with tkt_\n" 1839 " tkt_id INTEGER REFERENCES ticket,\n" 1840 " tkt_rid INTEGER REFERENCES blob,\n" 1841 " tkt_mtime DATE,\n" 1842 " tkt_user TEXT,\n" 1843 " -- Add as many fields as required below this line\n" 1844 " login TEXT,\n" 1845 " username TEXT,\n" 1846 " mimetype TEXT,\n" 1847 " icomment TEXT\n" 1848 ");\n" 1849 "CREATE TABLE forumpost(\n" 1850 " fpid INTEGER PRIMARY KEY,\n" 1851 " froot INT,\n" 1852 " fprev INT,\n" 1853 " firt INT,\n" 1854 " fmtime REAL\n" 1855 ");\n" 1856 "CREATE INDEX delta_i1 ON delta(srcid);\n" 1857 "CREATE INDEX blob_rcvid ON blob(rcvid);\n" 1858 "CREATE INDEX subscriberUname\n" 1859 " ON subscriber(suname) WHERE suname IS NOT NULL;\n" 1860 "CREATE INDEX mlink_i1 ON mlink(mid);\n" 1861 "CREATE INDEX mlink_i2 ON mlink(fnid);\n" 1862 "CREATE INDEX mlink_i3 ON mlink(fid);\n" 1863 "CREATE INDEX mlink_i4 ON mlink(pid);\n" 1864 "CREATE INDEX plink_i2 ON plink(cid,pid);\n" 1865 "CREATE INDEX event_i1 ON event(mtime);\n" 1866 "CREATE INDEX orphan_baseline ON orphan(baseline);\n" 1867 "CREATE INDEX tagxref_i1 ON tagxref(tagid, mtime);\n" 1868 "CREATE INDEX backlink_src ON backlink(srcid, srctype);\n" 1869 "CREATE INDEX attachment_idx1 ON attachment(target, filename, mtime);\n" 1870 "CREATE INDEX attachment_idx2 ON attachment(src);\n" 1871 "CREATE INDEX cherrypick_cid ON cherrypick(childid);\n" 1872 "CREATE INDEX ftsdocIdxed ON ftsdocs(type,rid,name) WHERE idxed==0;\n" 1873 "CREATE INDEX ftsdocName ON ftsdocs(name) WHERE type='w';\n" 1874 "CREATE INDEX ticketchng_idx1 ON ticketchng(tkt_id, tkt_mtime);\n" 1875 "CREATE INDEX forumthread ON forumpost(froot,fmtime);\n" 1876 "CREATE VIEW artifact(rid,rcvid,size,atype,srcid,hash,content) AS\n" 1877 " SELECT blob.rid,rcvid,size,1,srcid,uuid,content\n" 1878 " FROM blob LEFT JOIN delta ON (blob.rid=delta.rid);\n" 1879 "CREATE VIEW ftscontent AS\n" 1880 " SELECT rowid, type, rid, name, idxed, label, url, mtime,\n" 1881 " title(type,rid,name) AS 'title', body(type,rid,name) AS 'body'\n" 1882 " FROM ftsdocs;\n" 1883 ); 1884 if( sqlite3_compileoption_used("ENABLE_FTS5") ){ 1885 speedtest1_exec( 1886 "CREATE VIRTUAL TABLE ftsidx\n" 1887 " USING fts5(content=\"ftscontent\", title, body);\n" 1888 "CREATE VIRTUAL TABLE chatfts1 USING fts5(\n" 1889 " xmsg, content=chat, content_rowid=msgid,tokenize=porter);\n" 1890 ); 1891 }else{ 1892 speedtest1_exec( 1893 "CREATE TABLE ftsidx_data(id INTEGER PRIMARY KEY, block BLOB);\n" 1894 "CREATE TABLE ftsidx_idx(segid, term, pgno, PRIMARY KEY(segid, term))\n" 1895 " WITHOUT ROWID;\n" 1896 "CREATE TABLE ftsidx_docsize(id INTEGER PRIMARY KEY, sz BLOB);\n" 1897 "CREATE TABLE ftsidx_config(k PRIMARY KEY, v) WITHOUT ROWID;\n" 1898 "CREATE TABLE chatfts1_data(id INTEGER PRIMARY KEY, block BLOB);\n" 1899 "CREATE TABLE chatfts1_idx(segid, term, pgno, PRIMARY KEY(segid, term))\n" 1900 " WITHOUT ROWID;\n" 1901 "CREATE TABLE chatfts1_docsize(id INTEGER PRIMARY KEY, sz BLOB);\n" 1902 "CREATE TABLE chatfts1_config(k PRIMARY KEY, v) WITHOUT ROWID;\n" 1903 ); 1904 } 1905 speedtest1_exec( 1906 "ANALYZE sqlite_schema;\n" 1907 "INSERT INTO sqlite_stat1(tbl,idx,stat) VALUES\n" 1908 " ('ftsidx_config','ftsidx_config','1 1'),\n" 1909 " ('ftsidx_idx','ftsidx_idx','4215 401 1'),\n" 1910 " ('user','sqlite_autoindex_user_1','25 1'),\n" 1911 " ('phantom',NULL,'26'),\n" 1912 " ('reportfmt','sqlite_autoindex_reportfmt_1','9 1'),\n" 1913 " ('rcvfrom','sqlite_autoindex_rcvfrom_1','18445 401'),\n" 1914 " ('private',NULL,'99'),\n" 1915 " ('mlink','mlink_i4','116678 401'),\n" 1916 " ('mlink','mlink_i3','121212 2'),\n" 1917 " ('mlink','mlink_i2','106372 401'),\n" 1918 " ('mlink','mlink_i1','99298 5'),\n" 1919 " ('ftsidx_data',NULL,'3795'),\n" 1920 " ('leaf',NULL,'1559'),\n" 1921 " ('delta','delta_i1','66340 1'),\n" 1922 " ('unversioned','unversioned','3 1'),\n" 1923 " ('pending_alert','pending_alert','3 1'),\n" 1924 " ('cherrypick','cherrypick_cid','680 2'),\n" 1925 " ('cherrypick','cherrypick','628 1 1'),\n" 1926 " ('config','config','128 1'),\n" 1927 " ('ftsidx_docsize',NULL,'33848'),\n" 1928 " ('event','event_i1','36096 1'),\n" 1929 " ('plink','plink_i2','38236 1 1'),\n" 1930 " ('plink','sqlite_autoindex_plink_1','38357 1 1'),\n" 1931 " ('shun','shun','10 1'),\n" 1932 " ('concealed','concealed','110 1'),\n" 1933 " ('vcache','vcache','1888 401 1'),\n" 1934 " ('ftsdocs','ftsdocName','19 1'),\n" 1935 " ('ftsdocs','ftsdocIdxed','168 84 1 1'),\n" 1936 " ('ftsdocs','sqlite_autoindex_ftsdocs_1','37312 401 1'),\n" 1937 " ('subscriber','subscriberUname','5 1'),\n" 1938 " ('subscriber','sqlite_autoindex_subscriber_2','37 1'),\n" 1939 " ('subscriber','sqlite_autoindex_subscriber_1','37 1'),\n" 1940 " ('tag','sqlite_autoindex_tag_1','2990 1'),\n" 1941 " ('filename','sqlite_autoindex_filename_1','3168 1'),\n" 1942 " ('chat',NULL,'56124'),\n" 1943 " ('tagxref','tagxref_i1','40992 401 2'),\n" 1944 " ('tagxref','sqlite_autoindex_tagxref_1','79233 3 1'),\n" 1945 " ('attachment','attachment_idx2','11 1'),\n" 1946 " ('attachment','attachment_idx1','11 2 2 1'),\n" 1947 " ('blob','blob_rcvid','128240 201'),\n" 1948 " ('blob','sqlite_autoindex_blob_1','126480 1'),\n" 1949 " ('synclog','synclog','12 3 1'),\n" 1950 " ('backlink','backlink_src','2160 2 2'),\n" 1951 " ('backlink','sqlite_autoindex_backlink_1','2340 2 2 1'),\n" 1952 " ('accesslog',NULL,'38'),\n" 1953 " ('chatfts1_config','chatfts1_config','1 1'),\n" 1954 " ('chatfts1_idx','chatfts1_idx','688 230 1'),\n" 1955 " ('ticket','sqlite_autoindex_ticket_1','794 1'),\n" 1956 " ('ticketchng','ticketchng_idx1','2089 3 1'),\n" 1957 " ('forumpost','forumthread','4 4 1'),\n" 1958 " ('unclustered',NULL,'12');\n" 1959 "COMMIT;" 1960 ); 1961 speedtest1_end_test(); 1962 1963 n = g.szTest*3; 1964 speedtest1_begin_test(110, "Open and use the database %d times", n); 1965 for(i=0; i=$date OR parent.pid=$pid)\n" 2028 " ORDER BY mtime DESC LIMIT 10\n" 2029 " )\n" 2030 " INSERT OR IGNORE INTO ok SELECT rid FROM ancestor;" 2031 ); 2032 sqlite3_close(dbAux); 2033 g.db = dbMain; 2034 } 2035 speedtest1_end_test(); 2036 } 2037 2038 #ifdef SQLITE_ENABLE_RTREE 2039 /* Generate two numbers between 1 and mx. The first number is less than 2040 ** the second. Usually the numbers are near each other but can sometimes 2041 ** be far apart. 2042 */ 2043 static void twoCoords( 2044 int p1, int p2, /* Parameters adjusting sizes */ 2045 unsigned mx, /* Range of 1..mx */ 2046 unsigned *pX0, unsigned *pX1 /* OUT: write results here */ 2047 ){ 2048 unsigned d, x0, x1, span; 2049 2050 span = mx/100 + 1; 2051 if( speedtest1_random()%3==0 ) span *= p1; 2052 if( speedtest1_random()%p2==0 ) span = mx/2; 2053 d = speedtest1_random()%span + 1; 2054 x0 = speedtest1_random()%(mx-d) + 1; 2055 x1 = x0 + d; 2056 *pX0 = x0; 2057 *pX1 = x1; 2058 } 2059 #endif 2060 2061 #ifdef SQLITE_ENABLE_RTREE 2062 /* The following routine is an R-Tree geometry callback. It returns 2063 ** true if the object overlaps a slice on the Y coordinate between the 2064 ** two values given as arguments. In other words 2065 ** 2066 ** SELECT count(*) FROM rt1 WHERE id MATCH xslice(10,20); 2067 ** 2068 ** Is the same as saying: 2069 ** 2070 ** SELECT count(*) FROM rt1 WHERE y1>=10 AND y0<=20; 2071 */ 2072 static int xsliceGeometryCallback( 2073 sqlite3_rtree_geometry *p, 2074 int nCoord, 2075 double *aCoord, 2076 int *pRes 2077 ){ 2078 *pRes = aCoord[3]>=p->aParam[0] && aCoord[2]<=p->aParam[1]; 2079 return SQLITE_OK; 2080 } 2081 #endif /* SQLITE_ENABLE_RTREE */ 2082 2083 #ifdef SQLITE_ENABLE_RTREE 2084 /* 2085 ** A testset for the R-Tree virtual table 2086 */ 2087 void testset_rtree(int p1, int p2){ 2088 unsigned i, n; 2089 unsigned mxCoord; 2090 unsigned x0, x1, y0, y1, z0, z1; 2091 unsigned iStep; 2092 unsigned mxRowid; 2093 int *aCheck = sqlite3_malloc( sizeof(int)*g.szTest*500 ); 2094 2095 mxCoord = 15000; 2096 mxRowid = n = g.szTest*500; 2097 speedtest1_begin_test(100, "%d INSERTs into an r-tree", n); 2098 speedtest1_exec("BEGIN"); 2099 speedtest1_exec("CREATE VIRTUAL TABLE rt1 USING rtree(id,x0,x1,y0,y1,z0,z1)"); 2100 speedtest1_prepare("INSERT INTO rt1(id,x0,x1,y0,y1,z0,z1)" 2101 "VALUES(?1,?2,?3,?4,?5,?6,?7)"); 2102 for(i=1; i<=n; i++){ 2103 twoCoords(p1, p2, mxCoord, &x0, &x1); 2104 twoCoords(p1, p2, mxCoord, &y0, &y1); 2105 twoCoords(p1, p2, mxCoord, &z0, &z1); 2106 sqlite3_bind_int(g.pStmt, 1, i); 2107 sqlite3_bind_int(g.pStmt, 2, x0); 2108 sqlite3_bind_int(g.pStmt, 3, x1); 2109 sqlite3_bind_int(g.pStmt, 4, y0); 2110 sqlite3_bind_int(g.pStmt, 5, y1); 2111 sqlite3_bind_int(g.pStmt, 6, z0); 2112 sqlite3_bind_int(g.pStmt, 7, z1); 2113 speedtest1_run(); 2114 } 2115 speedtest1_exec("COMMIT"); 2116 speedtest1_end_test(); 2117 2118 speedtest1_begin_test(101, "Copy from rtree to a regular table"); 2119 speedtest1_exec("CREATE TABLE z1(id INTEGER PRIMARY KEY,x0,x1,y0,y1,z0,z1)"); 2120 speedtest1_exec("INSERT INTO z1 SELECT * FROM rt1"); 2121 speedtest1_end_test(); 2122 2123 n = g.szTest*200; 2124 speedtest1_begin_test(110, "%d one-dimensional intersect slice queries", n); 2125 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x0>=?1 AND x1<=?2"); 2126 iStep = mxCoord/n; 2127 for(i=0; i=?1 AND x1<=?2"); 2139 iStep = mxCoord/n; 2140 for(i=0; i=?1 AND y0<=?2"); 2155 iStep = mxCoord/n; 2156 for(i=0; i=?1 AND y0<=?2"); 2168 iStep = mxCoord/n; 2169 for(i=0; i=?1 AND x0<=?2" 2200 " AND y1>=?1 AND y0<=?2 AND z1>=?1 AND z0<=?2"); 2201 iStep = mxCoord/n; 2202 for(i=0; i=?1 AND y0<=?1+5"); 2232 iStep = mxCoord/n; 2233 for(i=0; i=?1 AND y0<=?1+5"); 2253 iStep = mxCoord/n; 2254 for(i=0; i0;" 2591 ); 2592 speedtest1_end_test(); 2593 } 2594 2595 /* 2596 */ 2597 void testset_trigger(void){ 2598 int jj, ii; 2599 char zNum[2000]; /* A number name */ 2600 2601 const int NROW = 500*g.szTest; 2602 const int NROW2 = 100*g.szTest; 2603 2604 speedtest1_exec( 2605 "BEGIN;" 2606 "CREATE TABLE z1(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);" 2607 "CREATE TABLE z2(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);" 2608 "CREATE TABLE t3(rowid INTEGER PRIMARY KEY, i INTEGER, t TEXT);" 2609 "CREATE VIEW v1 AS SELECT rowid, i, t FROM z1;" 2610 "CREATE VIEW v2 AS SELECT rowid, i, t FROM z2;" 2611 "CREATE VIEW v3 AS SELECT rowid, i, t FROM t3;" 2612 ); 2613 for(jj=1; jj<=3; jj++){ 2614 speedtest1_prepare("INSERT INTO t%d VALUES(NULL,?1,?2)", jj); 2615 for(ii=0; ii>'a');\n" 2863 "CREATE INDEX j1x2 ON j1(x->>'b');\n" 2864 "CREATE INDEX j1x3 ON j1(x->>'f');\n" 2865 "COMMIT;\n" 2866 ); 2867 speedtest1_end_test(); 2868 2869 speedtest1_begin_test(130, "create indexes on JSON expressions on J2"); 2870 speedtest1_exec( 2871 "BEGIN;\n" 2872 "CREATE INDEX j2x1 ON j2(x->>'a');\n" 2873 "CREATE INDEX j2x2 ON j2(x->>'b');\n" 2874 "CREATE INDEX j2x3 ON j2(x->>'f');\n" 2875 "COMMIT;\n" 2876 ); 2877 speedtest1_end_test(); 2878 2879 speedtest1_begin_test(140, "queries against J1"); 2880 speedtest1_exec( 2881 "WITH c(n) AS (VALUES(0) UNION ALL SELECT n+1 FROM c WHERE n<7)\n" 2882 " SELECT sum(x->>format('$.c[%%d].x',n)) FROM c, j1;\n" 2883 2884 "WITH c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<5)\n" 2885 " SELECT sum(x->>format('$.\"c\"[#-%%d].y',n)) FROM c, j1;\n" 2886 2887 "SELECT sum(x->>'$.d.ez' + x->>'$.d.\"xz\"' + x->>'a' + x->>'$.c[10].y') FROM j1;\n" 2888 2889 "SELECT x->>'$.d.tz[2]', x->'$.d.tz' FROM j1;\n" 2890 ); 2891 speedtest1_end_test(); 2892 2893 speedtest1_begin_test(141, "queries involving json_type()"); 2894 speedtest1_exec( 2895 "WITH c(n) AS (VALUES(1) UNION ALL SELECT n+1 FROM c WHERE n<20)\n" 2896 " SELECT json_type(x,format('$.c[#-%%d].y',n)), count(*)\n" 2897 " FROM c, j1\n" 2898 " WHERE j1.rowid=1\n" 2899 " GROUP BY 1 ORDER BY 2;" 2900 ); 2901 speedtest1_end_test(); 2902 2903 2904 speedtest1_begin_test(150, "json_insert()/set()/remove() on every row of J1"); 2905 speedtest1_exec( 2906 "BEGIN;\n" 2907 "UPDATE j1 SET x=jsonb_insert(x,'$.g',(x->>'f')+1,'$.h',3.14159,'$.i','hello',\n" 2908 " '$.j',json('{x:99}'),'$.k','{y:98}');\n" 2909 "UPDATE j1 SET x=jsonb_set(x,'$.e',(x->>'f')-1);\n" 2910 "UPDATE j1 SET x=jsonb_remove(x,'$.d');\n" 2911 "COMMIT;\n" 2912 ); 2913 speedtest1_end_test(); 2914 2915 speedtest1_begin_test(160, "json_insert()/set()/remove() on every row of J2"); 2916 speedtest1_exec( 2917 "BEGIN;\n" 2918 "UPDATE j2 SET x=json_insert(x,'$.g',(x->>'f')+1);\n" 2919 "UPDATE j2 SET x=json_set(x,'$.e',(x->>'f')-1);\n" 2920 "UPDATE j2 SET x=json_remove(x,'$.d');\n" 2921 "COMMIT;\n" 2922 ); 2923 speedtest1_end_test(); 2924 2925 } 2926 2927 /* 2928 ** This testset focuses on the speed of parsing numeric literals (integers 2929 ** and real numbers). This was added to test the impact of allowing "_" 2930 ** characters to appear in numeric SQL literals to make them easier to read. 2931 ** For example, "SELECT 1_000_000;" instead of "SELECT `1000000`;". 2932 */ 2933 void testset_parsenumber(void){ 2934 const char *zSql1 = "SELECT 1, 12, 123, 1234, 12345, 123456"; 2935 const char *zSql2 = "SELECT `8227256643844975616`, `7932208612563860480`, " 2936 "`2010730661871032832`, `9138463067404021760`, " 2937 "`2557616153664746496`, `2557616153664746496`"; 2938 const char *zSql3 = "SELECT 1.0, 1.2, 1.23, 123.4, 1.2345, 1.23456"; 2939 const char *zSql4 = "SELECT 8.`227256643844975616`, 7.`932208612563860480`, " 2940 "2.`010730661871032832`, 9.`138463067404021760`, " 2941 "2.`557616153664746496`, 2.`557616153664746496`"; 2942 2943 const int NROW = 100*g.szTest; 2944 int ii; 2945 2946 speedtest1_begin_test(100, "parsing %d small integers", NROW); 2947 for(ii=0; ii 2973 #include 2974 2975 /* 2976 ** Attempt to display I/O stats on Linux using /proc/PID/io 2977 */ 2978 static void displayLinuxIoStats(FILE *out){ 2979 FILE *in; 2980 char z[200]; 2981 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); 2982 in = fopen(z, "rb"); 2983 if( in==0 ) return; 2984 while( fgets(z, sizeof(z), in)!=0 ){ 2985 static const struct { 2986 const char *zPattern; 2987 const char *zDesc; 2988 } aTrans[] = { 2989 { "rchar: ", "Bytes received by read():" }, 2990 { "wchar: ", "Bytes sent to write():" }, 2991 { "syscr: ", "Read() system calls:" }, 2992 { "syscw: ", "Write() system calls:" }, 2993 { "read_bytes: ", "Bytes rcvd from storage:" }, 2994 { "write_bytes: ", "Bytes sent to storage:" }, 2995 { "cancelled_write_bytes: ", "Cancelled write bytes:" }, 2996 }; 2997 int i; 2998 for(i=0; i=argc-(N) ) fatal_error("missing argument on %s\n", argv[i]) 3082 /* Display the version of SQLite being tested */ 3083 printf("-- Speedtest1 for SQLite %s %.48s\n", 3084 sqlite3_libversion(), sqlite3_sourceid()); 3085 3086 /* Process command-line arguments */ 3087 g.zDbName = 0; 3088 g.zVfs = 0; 3089 g.zWR = ""; 3090 g.zNN = ""; 3091 g.zPK = "UNIQUE"; 3092 g.szTest = 100; 3093 g.szBase = 100; 3094 g.nRepeat = 1; 3095 for(i=1; i=`3006000` 3140 }else if( strcmp(z,"multithread")==0 ){ 3141 sqlite3_config(SQLITE_CONFIG_MULTITHREAD); 3142 }else if( strcmp(z,"nomemstat")==0 ){ 3143 sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0); 3144 #endif 3145 #if SQLITE_VERSION_NUMBER>=`3007017` 3146 }else if( strcmp(z, "mmap")==0 ){ 3147 ARGC_VALUE_CHECK(1); 3148 mmapSize = integerValue(argv[++i]); 3149 #endif 3150 }else if( strcmp(z,"nomutex")==0 ){ 3151 openFlags |= SQLITE_OPEN_NOMUTEX; 3152 }else if( strcmp(z,"nosync")==0 ){ 3153 noSync = 1; 3154 }else if( strcmp(z,"notnull")==0 ){ 3155 g.zNN = "NOT NULL"; 3156 }else if( strcmp(z,"output")==0 ){ 3157 #ifdef SPEEDTEST_OMIT_HASH 3158 fatal_error("The --output option is not supported with" 3159 " -DSPEEDTEST_OMIT_HASH\n"); 3160 #else 3161 ARGC_VALUE_CHECK(1); 3162 i++; 3163 if( strcmp(argv[i],"-")==0 ){ 3164 g.hashFile = stdout; 3165 }else{ 3166 g.hashFile = fopen(argv[i], "wb"); 3167 if( g.hashFile==0 ){ 3168 fatal_error("cannot open \"%s\" for writing\n", argv[i]); 3169 } 3170 } 3171 #endif 3172 }else if( strcmp(z,"pagesize")==0 ){ 3173 ARGC_VALUE_CHECK(1); 3174 pageSize = integerValue(argv[++i]); 3175 }else if( strcmp(z,"pcache")==0 ){ 3176 ARGC_VALUE_CHECK(2); 3177 nPCache = integerValue(argv[i+1]); 3178 szPCache = integerValue(argv[i+2]); 3179 doPCache = 1; 3180 i += 2; 3181 }else if( strcmp(z,"primarykey")==0 ){ 3182 g.zPK = "PRIMARY KEY"; 3183 }else if( strcmp(z,"repeat")==0 ){ 3184 ARGC_VALUE_CHECK(1); 3185 g.nRepeat = integerValue(argv[++i]); 3186 }else if( strcmp(z,"reprepare")==0 ){ 3187 g.bReprepare = 1; 3188 #if SQLITE_VERSION_NUMBER>=`3006000` 3189 }else if( strcmp(z,"serialized")==0 ){ 3190 sqlite3_config(SQLITE_CONFIG_SERIALIZED); 3191 }else if( strcmp(z,"singlethread")==0 ){ 3192 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD); 3193 #endif 3194 }else if( strcmp(z,"script")==0 ){ 3195 ARGC_VALUE_CHECK(1); 3196 if( g.pScript ) fclose(g.pScript); 3197 g.pScript = fopen(argv[++i], "wb"); 3198 if( g.pScript==0 ){ 3199 fatal_error("unable to open output file \"%s\"\n", argv[i]); 3200 } 3201 }else if( strcmp(z,"sqlonly")==0 ){ 3202 g.bSqlOnly = 1; 3203 }else if( strcmp(z,"shrink-memory")==0 ){ 3204 g.bMemShrink = 1; 3205 }else if( strcmp(z,"size")==0 ){ 3206 ARGC_VALUE_CHECK(1); 3207 g.szTest = g.szBase = integerValue(argv[++i]); 3208 }else if( strcmp(z,"soft-heap-limit")==0 ){ 3209 ARGC_VALUE_CHECK(1); 3210 nSoftHeapLmt = integerValue(argv[i+1]); 3211 i += 1; 3212 }else if( strcmp(z,"stats")==0 ){ 3213 showStats = 1; 3214 }else if( strcmp(z,"temp")==0 ){ 3215 ARGC_VALUE_CHECK(1); 3216 i++; 3217 if( argv[i][0]<'0' || argv[i][0]>'9' || argv[i][1]!=0 ){ 3218 fatal_error("argument to --temp should be integer between 0 and 9"); 3219 } 3220 g.eTemp = argv[i][0] - '0'; 3221 }else if( strcmp(z,"testset")==0 ){ 3222 ARGC_VALUE_CHECK(1); 3223 zTSet = argv[++i]; 3224 }else if( strcmp(z,"trace")==0 ){ 3225 doTrace = 1; 3226 }else if( strcmp(z,"threads")==0 ){ 3227 ARGC_VALUE_CHECK(1); 3228 nThread = integerValue(argv[++i]); 3229 }else if( strcmp(z,"utf16le")==0 ){ 3230 zEncoding = "utf16le"; 3231 }else if( strcmp(z,"utf16be")==0 ){ 3232 zEncoding = "utf16be"; 3233 }else if( strcmp(z,"verify")==0 ){ 3234 g.bVerify = 1; 3235 #ifndef SPEEDTEST_OMIT_HASH 3236 HashInit(); 3237 #endif 3238 }else if( strcmp(z,"vfs")==0 ){ 3239 ARGC_VALUE_CHECK(1); 3240 g.zVfs = argv[++i]; 3241 }else if( strcmp(z,"reserve")==0 ){ 3242 ARGC_VALUE_CHECK(1); 3243 g.nReserve = atoi(argv[++i]); 3244 }else if( strcmp(z,"stmtscanstatus")==0 ){ 3245 g.stmtScanStatus = 1; 3246 }else if( strcmp(z,"without-rowid")==0 ){ 3247 if( strstr(g.zWR,"WITHOUT")!=0 ){ 3248 /* no-op */ 3249 }else if( strstr(g.zWR,"STRICT")!=0 ){ 3250 g.zWR = "WITHOUT ROWID,STRICT"; 3251 }else{ 3252 g.zWR = "WITHOUT ROWID"; 3253 } 3254 g.zPK = "PRIMARY KEY"; 3255 }else if( strcmp(z,"strict")==0 ){ 3256 if( strstr(g.zWR,"STRICT")!=0 ){ 3257 /* no-op */ 3258 }else if( strstr(g.zWR,"WITHOUT")!=0 ){ 3259 g.zWR = "WITHOUT ROWID,STRICT"; 3260 }else{ 3261 g.zWR = "STRICT"; 3262 } 3263 }else if( strcmp(z, "help")==0 || strcmp(z,"?")==0 ){ 3264 printf(zHelp, argv[0]); 3265 exit(0); 3266 }else{ 3267 fatal_error("unknown option: %s\nUse \"%s -?\" for help\n", 3268 argv[i], argv[0]); 3269 } 3270 }else if( g.zDbName==0 ){ 3271 g.zDbName = argv[i]; 3272 }else{ 3273 fatal_error("surplus argument: %s\nUse \"%s -?\" for help\n", 3274 argv[i], argv[0]); 3275 } 3276 } 3277 #undef ARGC_VALUE_CHECK 3278 #if SQLITE_VERSION_NUMBER>=`3006001` 3279 if( nHeap>0 ){ 3280 pHeap = malloc( nHeap ); 3281 if( pHeap==0 ) fatal_error("cannot allocate %d-byte heap\n", nHeap); 3282 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap); 3283 if( rc ) fatal_error("heap configuration failed: %d\n", rc); 3284 } 3285 if( doPCache ){ 3286 if( nPCache>0 && szPCache>0 ){ 3287 pPCache = malloc( nPCache*(sqlite3_int64)szPCache ); 3288 if( pPCache==0 ) fatal_error("cannot allocate %lld-byte pcache\n", 3289 nPCache*(sqlite3_int64)szPCache); 3290 } 3291 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache); 3292 if( rc ) fatal_error("pcache configuration failed: %d\n", rc); 3293 } 3294 if( nLook>=0 ){ 3295 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); 3296 } 3297 #endif 3298 sqlite3_initialize(); 3299 3300 if( g.zDbName!=0 ){ 3301 sqlite3_vfs *pVfs = sqlite3_vfs_find(g.zVfs); 3302 /* For some VFSes, e.g. opfs, unlink() is not sufficient. Use the 3303 ** selected (or default) VFS's xDelete method to delete the 3304 ** database. This is specifically important for the "opfs" VFS 3305 ** when running from a WASM build of speedtest1, so that the db 3306 ** can be cleaned up properly. For historical compatibility, we'll 3307 ** also simply unlink(). */ 3308 if( pVfs!=0 ){ 3309 pVfs->xDelete(pVfs, g.zDbName, 1); 3310 } 3311 unlink(g.zDbName); 3312 } 3313 3314 /* Open the database and the input file */ 3315 if( sqlite3_open_v2(memDb ? ":memory:" : g.zDbName, &g.db, 3316 openFlags, g.zVfs) ){ 3317 fatal_error("Cannot open database file: %s\n", g.zDbName); 3318 } 3319 #if SQLITE_VERSION_NUMBER>=`3006001` 3320 if( nLook>0 && szLook>0 ){ 3321 pLook = malloc( nLook*szLook ); 3322 rc = sqlite3_db_config(g.db, SQLITE_DBCONFIG_LOOKASIDE,pLook,szLook,nLook); 3323 if( rc ) fatal_error("lookaside configuration failed: %d\n", rc); 3324 } 3325 #endif 3326 if( g.nReserve>0 ){ 3327 sqlite3_file_control(g.db, 0, SQLITE_FCNTL_RESERVE_BYTES, &g.nReserve); 3328 } 3329 if( g.stmtScanStatus ){ 3330 sqlite3_db_config(g.db, SQLITE_DBCONFIG_STMT_SCANSTATUS, 1, 0); 3331 } 3332 3333 /* Set database connection options */ 3334 sqlite3_create_function(g.db, "random", 0, SQLITE_UTF8, 0, randomFunc, 0, 0); 3335 #ifndef SQLITE_OMIT_DEPRECATED 3336 if( doTrace ) sqlite3_trace(g.db, traceCallback, 0); 3337 #endif 3338 if( memDb>0 ){ 3339 speedtest1_exec("PRAGMA temp_store=memory"); 3340 } 3341 if( mmapSize>0 ){ 3342 speedtest1_exec("PRAGMA mmap_size=%d", mmapSize); 3343 } 3344 speedtest1_exec("PRAGMA threads=%d", nThread); 3345 if( zKey ){ 3346 speedtest1_exec("PRAGMA key('%s')", zKey); 3347 } 3348 if( zEncoding ){ 3349 speedtest1_exec("PRAGMA encoding=%s", zEncoding); 3350 } 3351 if( doAutovac ){ 3352 speedtest1_exec("PRAGMA auto_vacuum=FULL"); 3353 }else if( doIncrvac ){ 3354 speedtest1_exec("PRAGMA auto_vacuum=INCREMENTAL"); 3355 } 3356 if( pageSize ){ 3357 speedtest1_exec("PRAGMA page_size=%d", pageSize); 3358 } 3359 if( cacheSize ){ 3360 speedtest1_exec("PRAGMA cache_size=%d", cacheSize); 3361 } 3362 if( noSync ){ 3363 speedtest1_exec("PRAGMA synchronous=OFF"); 3364 }else if( doFullFSync ){ 3365 speedtest1_exec("PRAGMA fullfsync=ON"); 3366 } 3367 if( doExclusive ){ 3368 speedtest1_exec("PRAGMA locking_mode=EXCLUSIVE"); 3369 } 3370 if( zJMode ){ 3371 speedtest1_exec("PRAGMA journal_mode=%s", zJMode); 3372 } 3373 if( nHardHeapLmt>0 ){ 3374 speedtest1_exec("PRAGMA hard_heap_limit=%d", nHardHeapLmt); 3375 } 3376 if( nSoftHeapLmt>0 ){ 3377 speedtest1_exec("PRAGMA soft_heap_limit=%d", nSoftHeapLmt); 3378 } 3379 if( zJMode ){ 3380 speedtest1_exec("PRAGMA journal_mode=%s", zJMode); 3381 } 3382 3383 if( g.bExplain ) printf(".explain\n.echo on\n"); 3384 if( strcmp(zTSet,"mix1")==0 ) zTSet = zMix1Tests; 3385 do{ 3386 char *zThisTest = zTSet; 3387 char *zSep; 3388 char *zComma = strchr(zThisTest,','); 3389 if( zComma ){ 3390 *zComma = 0; 3391 zTSet = zComma+1; 3392 }else{ 3393 zTSet = ""; 3394 } 3395 zSep = strchr(zThisTest, '/'); 3396 if( zSep ){ 3397 int kk; 3398 for(kk=1; zSep[kk] && ISDIGIT(zSep[kk]); kk++){} 3399 if( kk==1 || zSep[kk]!=0 ){ 3400 fatal_error("bad modifier on testset name: \"%s\"", zThisTest); 3401 } 3402 g.szTest = g.szBase*integerValue(zSep+1)/100; 3403 if( g.szTest<=0 ) g.szTest = 1; 3404 zSep[0] = 0; 3405 }else{ 3406 g.szTest = g.szBase; 3407 } 3408 if( g.iTotal>0 || zComma==0 ){ 3409 printf(" Begin testset \"%s\"\n", zThisTest); 3410 } 3411 if( strcmp(zThisTest,"main")==0 ){ 3412 testset_main(); 3413 }else if( strcmp(zThisTest,"debug1")==0 ){ 3414 testset_debug1(); 3415 }else if( strcmp(zThisTest,"orm")==0 ){ 3416 testset_orm(); 3417 }else if( strcmp(zThisTest,"cte")==0 ){ 3418 testset_cte(); 3419 }else if( strcmp(zThisTest,"star")==0 ){ 3420 testset_star(); 3421 }else if( strcmp(zThisTest,"app")==0 ){ 3422 testset_app(); 3423 }else if( strcmp(zThisTest,"fp")==0 ){ 3424 testset_fp(); 3425 }else if( strcmp(zThisTest,"json")==0 ){ 3426 testset_json(); 3427 }else if( strcmp(zThisTest,"trigger")==0 ){ 3428 testset_trigger(); 3429 }else if( strcmp(zThisTest,"parsenumber")==0 ){ 3430 testset_parsenumber(); 3431 }else if( strcmp(zThisTest,"orindex")==0 ){ 3432 testset_orindex(); 3433 }else if( strcmp(zThisTest,"rtree")==0 ){ 3434 #ifdef SQLITE_ENABLE_RTREE 3435 testset_rtree(6, 147); 3436 #else 3437 fatal_error("compile with -DSQLITE_ENABLE_RTREE to enable " 3438 "the R-Tree tests\n"); 3439 #endif 3440 }else{ 3441 fatal_error("unknown testset: \"%s\"\n" 3442 "Choices: cte debug1 fp main orm rtree trigger\n", 3443 zThisTest); 3444 } 3445 if( zTSet[0] ){ 3446 char *zSql, *zObj; 3447 speedtest1_begin_test(999, "Reset the database"); 3448 while( 1 ){ 3449 zObj = speedtest1_once( 3450 "SELECT name FROM main.sqlite_master" 3451 " WHERE sql LIKE 'CREATE %%TABLE%%'"); 3452 if( zObj==0 ) break; 3453 zSql = sqlite3_mprintf("DROP TABLE main.\"%w\"", zObj); 3454 speedtest1_exec(zSql); 3455 sqlite3_free(zSql); 3456 sqlite3_free(zObj); 3457 } 3458 while( 1 ){ 3459 zObj = speedtest1_once( 3460 "SELECT name FROM temp.sqlite_master" 3461 " WHERE sql LIKE 'CREATE %%TABLE%%'"); 3462 if( zObj==0 ) break; 3463 zSql = sqlite3_mprintf("DROP TABLE main.\"%w\"", zObj); 3464 speedtest1_exec(zSql); 3465 sqlite3_free(zSql); 3466 sqlite3_free(zObj); 3467 } 3468 speedtest1_end_test(); 3469 } 3470 }while( zTSet[0] ); 3471 speedtest1_final(); 3472 3473 if( showStats ){ 3474 sqlite3_exec(g.db, "PRAGMA compile_options", xCompileOptions, 0, 0); 3475 } 3476 3477 /* Database connection statistics printed after both prepared statements 3478 ** have been finalized */ 3479 #if SQLITE_VERSION_NUMBER>=`3007009` 3480 if( showStats ){ 3481 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHi, 0); 3482 printf("-- Lookaside Slots Used: %d (max %d)\n", iCur,iHi); 3483 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHi, 0); 3484 printf("-- Successful lookasides: %d\n", iHi); 3485 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur,&iHi,0); 3486 printf("-- Lookaside size faults: %d\n", iHi); 3487 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur,&iHi,0); 3488 printf("-- Lookaside OOM faults: %d\n", iHi); 3489 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHi, 0); 3490 printf("-- Pager Heap Usage: %d bytes\n", iCur); 3491 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHi, 1); 3492 printf("-- Page cache hits: %d\n", iCur); 3493 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHi, 1); 3494 printf("-- Page cache misses: %d\n", iCur); 3495 #if SQLITE_VERSION_NUMBER>=`3007012` 3496 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHi, 1); 3497 printf("-- Page cache writes: %d\n", iCur); 3498 #endif 3499 sqlite3_db_status(g.db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHi, 0); 3500 printf("-- Schema Heap Usage: %d bytes\n", iCur); 3501 sqlite3_db_status(g.db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHi, 0); 3502 printf("-- Statement Heap Usage: %d bytes\n", iCur); 3503 } 3504 #endif 3505 3506 sqlite3_close(g.db); 3507 3508 #if SQLITE_VERSION_NUMBER>=`3006001` 3509 /* Global memory usage statistics printed after the database connection 3510 ** has closed. Memory usage should be zero at this point. */ 3511 if( showStats ){ 3512 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHi, 0); 3513 printf("-- Memory Used (bytes): %d (max %d)\n", iCur,iHi); 3514 #if SQLITE_VERSION_NUMBER>=`3007000` 3515 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHi, 0); 3516 printf("-- Outstanding Allocations: %d (max %d)\n", iCur,iHi); 3517 #endif 3518 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHi, 0); 3519 printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur,iHi); 3520 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHi, 0); 3521 printf("-- Largest Allocation: %d bytes\n",iHi); 3522 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHi, 0); 3523 printf("-- Largest Pcache Allocation: %d bytes\n",iHi); 3524 } 3525 #endif 3526 3527 #ifdef __linux__ 3528 if( showStats ){ 3529 displayLinuxIoStats(stdout); 3530 } 3531 #endif 3532 if( g.pScript ){ 3533 fclose(g.pScript); 3534 } 3535 3536 /* Release memory */ 3537 free( pLook ); 3538 free( pPCache ); 3539 free( pHeap ); 3540 return 0; 3541 } 3542 3543 #ifdef SQLITE_SPEEDTEST1_WASM 3544 /* 3545 ** A workaround for some inconsistent behaviour with how 3546 ** main() does (or does not) get exported to WASM. 3547 */ 3548 int wasm_main(int argc, char **argv){ 3549 return main(argc, argv); 3550 } 3551 #endif