Fix compilation issues with the VFS stat extension.
[sqlite.git] / test / speedtest1.c
blob2d337d470588d0fa7a06ae2b070275febcc6b23f
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"
12 " --explain Like --sqlonly but with added EXPLAIN keywords\n"
13 " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n"
14 " --incrvacuum Enable incremenatal vacuum mode\n"
15 " --journal M Set the journal_mode to M\n"
16 " --key KEY Set the encryption key to KEY\n"
17 " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n"
18 " --multithread Set multithreaded mode\n"
19 " --nomemstat Disable memory statistics\n"
20 " --nosync Set PRAGMA synchronous=OFF\n"
21 " --notnull Add NOT NULL constraints to table columns\n"
22 " --pagesize N Set the page size to N\n"
23 " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n"
24 " --primarykey Use PRIMARY KEY instead of UNIQUE where appropriate\n"
25 " --reprepare Reprepare each statement upon every invocation\n"
26 " --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n"
27 " --serialized Set serialized threading mode\n"
28 " --singlethread Set single-threaded mode - disables all mutexing\n"
29 " --sqlonly No-op. Only show the SQL that would have been run.\n"
30 " --shrink-memory Invoke sqlite3_db_release_memory() frequently.\n"
31 " --size N Relative test size. Default=100\n"
32 " --stats Show statistics at the end\n"
33 " --temp N N from 0 to 9. 0: no temp table. 9: all temp tables\n"
34 " --testset T Run test-set T\n"
35 " --trace Turn on SQL tracing\n"
36 " --threads N Use up to N threads for sorting\n"
37 " --utf16be Set text encoding to UTF-16BE\n"
38 " --utf16le Set text encoding to UTF-16LE\n"
39 " --verify Run additional verification steps.\n"
40 " --without-rowid Use WITHOUT ROWID where appropriate\n"
44 #include "sqlite3.h"
45 #include <assert.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdarg.h>
49 #include <string.h>
50 #include <ctype.h>
51 #define ISSPACE(X) isspace((unsigned char)(X))
52 #define ISDIGIT(X) isdigit((unsigned char)(X))
54 #if SQLITE_VERSION_NUMBER<3005000
55 # define sqlite3_int64 sqlite_int64
56 #endif
57 #ifdef SQLITE_ENABLE_RBU
58 # include "sqlite3rbu.h"
59 #endif
61 /* All global state is held in this structure */
62 static struct Global {
63 sqlite3 *db; /* The open database connection */
64 sqlite3_stmt *pStmt; /* Current SQL statement */
65 sqlite3_int64 iStart; /* Start-time for the current test */
66 sqlite3_int64 iTotal; /* Total time */
67 int bWithoutRowid; /* True for --without-rowid */
68 int bReprepare; /* True to reprepare the SQL on each rerun */
69 int bSqlOnly; /* True to print the SQL once only */
70 int bExplain; /* Print SQL with EXPLAIN prefix */
71 int bVerify; /* Try to verify that results are correct */
72 int bMemShrink; /* Call sqlite3_db_release_memory() often */
73 int eTemp; /* 0: no TEMP. 9: always TEMP. */
74 int szTest; /* Scale factor for test iterations */
75 const char *zWR; /* Might be WITHOUT ROWID */
76 const char *zNN; /* Might be NOT NULL */
77 const char *zPK; /* Might be UNIQUE or PRIMARY KEY */
78 unsigned int x, y; /* Pseudo-random number generator state */
79 int nResult; /* Size of the current result */
80 char zResult[3000]; /* Text of the current result */
81 } g;
83 /* Return " TEMP" or "", as appropriate for creating a table.
85 static const char *isTemp(int N){
86 return g.eTemp>=N ? " TEMP" : "";
90 /* Print an error message and exit */
91 static void fatal_error(const char *zMsg, ...){
92 va_list ap;
93 va_start(ap, zMsg);
94 vfprintf(stderr, zMsg, ap);
95 va_end(ap);
96 exit(1);
100 ** Return the value of a hexadecimal digit. Return -1 if the input
101 ** is not a hex digit.
103 static int hexDigitValue(char c){
104 if( c>='0' && c<='9' ) return c - '0';
105 if( c>='a' && c<='f' ) return c - 'a' + 10;
106 if( c>='A' && c<='F' ) return c - 'A' + 10;
107 return -1;
110 /* Provide an alternative to sqlite3_stricmp() in older versions of
111 ** SQLite */
112 #if SQLITE_VERSION_NUMBER<3007011
113 # define sqlite3_stricmp strcmp
114 #endif
117 ** Interpret zArg as an integer value, possibly with suffixes.
119 static int integerValue(const char *zArg){
120 sqlite3_int64 v = 0;
121 static const struct { char *zSuffix; int iMult; } aMult[] = {
122 { "KiB", 1024 },
123 { "MiB", 1024*1024 },
124 { "GiB", 1024*1024*1024 },
125 { "KB", 1000 },
126 { "MB", 1000000 },
127 { "GB", 1000000000 },
128 { "K", 1000 },
129 { "M", 1000000 },
130 { "G", 1000000000 },
132 int i;
133 int isNeg = 0;
134 if( zArg[0]=='-' ){
135 isNeg = 1;
136 zArg++;
137 }else if( zArg[0]=='+' ){
138 zArg++;
140 if( zArg[0]=='0' && zArg[1]=='x' ){
141 int x;
142 zArg += 2;
143 while( (x = hexDigitValue(zArg[0]))>=0 ){
144 v = (v<<4) + x;
145 zArg++;
147 }else{
148 while( isdigit(zArg[0]) ){
149 v = v*10 + zArg[0] - '0';
150 zArg++;
153 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
154 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
155 v *= aMult[i].iMult;
156 break;
159 if( v>0x7fffffff ) fatal_error("parameter too large - max 2147483648");
160 return (int)(isNeg? -v : v);
163 /* Return the current wall-clock time, in milliseconds */
164 sqlite3_int64 speedtest1_timestamp(void){
165 #if SQLITE_VERSION_NUMBER<3005000
166 return 0;
167 #else
168 static sqlite3_vfs *clockVfs = 0;
169 sqlite3_int64 t;
170 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
171 #if SQLITE_VERSION_NUMBER>=3007000
172 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
173 clockVfs->xCurrentTimeInt64(clockVfs, &t);
174 }else
175 #endif
177 double r;
178 clockVfs->xCurrentTime(clockVfs, &r);
179 t = (sqlite3_int64)(r*86400000.0);
181 return t;
182 #endif
185 /* Return a pseudo-random unsigned integer */
186 unsigned int speedtest1_random(void){
187 g.x = (g.x>>1) ^ ((1+~(g.x&1)) & 0xd0000001);
188 g.y = g.y*1103515245 + 12345;
189 return g.x ^ g.y;
192 /* Map the value in within the range of 1...limit into another
193 ** number in a way that is chatic and invertable.
195 unsigned swizzle(unsigned in, unsigned limit){
196 unsigned out = 0;
197 while( limit ){
198 out = (out<<1) | (in&1);
199 in >>= 1;
200 limit >>= 1;
202 return out;
205 /* Round up a number so that it is a power of two minus one
207 unsigned roundup_allones(unsigned limit){
208 unsigned m = 1;
209 while( m<limit ) m = (m<<1)+1;
210 return m;
213 /* The speedtest1_numbername procedure below converts its argment (an integer)
214 ** into a string which is the English-language name for that number.
215 ** The returned string should be freed with sqlite3_free().
217 ** Example:
219 ** speedtest1_numbername(123) -> "one hundred twenty three"
221 int speedtest1_numbername(unsigned int n, char *zOut, int nOut){
222 static const char *ones[] = { "zero", "one", "two", "three", "four", "five",
223 "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
224 "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
225 "eighteen", "nineteen" };
226 static const char *tens[] = { "", "ten", "twenty", "thirty", "forty",
227 "fifty", "sixty", "seventy", "eighty", "ninety" };
228 int i = 0;
230 if( n>=1000000000 ){
231 i += speedtest1_numbername(n/1000000000, zOut+i, nOut-i);
232 sqlite3_snprintf(nOut-i, zOut+i, " billion");
233 i += (int)strlen(zOut+i);
234 n = n % 1000000000;
236 if( n>=1000000 ){
237 if( i && i<nOut-1 ) zOut[i++] = ' ';
238 i += speedtest1_numbername(n/1000000, zOut+i, nOut-i);
239 sqlite3_snprintf(nOut-i, zOut+i, " million");
240 i += (int)strlen(zOut+i);
241 n = n % 1000000;
243 if( n>=1000 ){
244 if( i && i<nOut-1 ) zOut[i++] = ' ';
245 i += speedtest1_numbername(n/1000, zOut+i, nOut-i);
246 sqlite3_snprintf(nOut-i, zOut+i, " thousand");
247 i += (int)strlen(zOut+i);
248 n = n % 1000;
250 if( n>=100 ){
251 if( i && i<nOut-1 ) zOut[i++] = ' ';
252 sqlite3_snprintf(nOut-i, zOut+i, "%s hundred", ones[n/100]);
253 i += (int)strlen(zOut+i);
254 n = n % 100;
256 if( n>=20 ){
257 if( i && i<nOut-1 ) zOut[i++] = ' ';
258 sqlite3_snprintf(nOut-i, zOut+i, "%s", tens[n/10]);
259 i += (int)strlen(zOut+i);
260 n = n % 10;
262 if( n>0 ){
263 if( i && i<nOut-1 ) zOut[i++] = ' ';
264 sqlite3_snprintf(nOut-i, zOut+i, "%s", ones[n]);
265 i += (int)strlen(zOut+i);
267 if( i==0 ){
268 sqlite3_snprintf(nOut-i, zOut+i, "zero");
269 i += (int)strlen(zOut+i);
271 return i;
275 /* Start a new test case */
276 #define NAMEWIDTH 60
277 static const char zDots[] =
278 ".......................................................................";
279 void speedtest1_begin_test(int iTestNum, const char *zTestName, ...){
280 int n = (int)strlen(zTestName);
281 char *zName;
282 va_list ap;
283 va_start(ap, zTestName);
284 zName = sqlite3_vmprintf(zTestName, ap);
285 va_end(ap);
286 n = (int)strlen(zName);
287 if( n>NAMEWIDTH ){
288 zName[NAMEWIDTH] = 0;
289 n = NAMEWIDTH;
291 if( g.bSqlOnly ){
292 printf("/* %4d - %s%.*s */\n", iTestNum, zName, NAMEWIDTH-n, zDots);
293 }else{
294 printf("%4d - %s%.*s ", iTestNum, zName, NAMEWIDTH-n, zDots);
295 fflush(stdout);
297 sqlite3_free(zName);
298 g.nResult = 0;
299 g.iStart = speedtest1_timestamp();
300 g.x = 0xad131d0b;
301 g.y = 0x44f9eac8;
304 /* Complete a test case */
305 void speedtest1_end_test(void){
306 sqlite3_int64 iElapseTime = speedtest1_timestamp() - g.iStart;
307 if( !g.bSqlOnly ){
308 g.iTotal += iElapseTime;
309 printf("%4d.%03ds\n", (int)(iElapseTime/1000), (int)(iElapseTime%1000));
311 if( g.pStmt ){
312 sqlite3_finalize(g.pStmt);
313 g.pStmt = 0;
317 /* Report end of testing */
318 void speedtest1_final(void){
319 if( !g.bSqlOnly ){
320 printf(" TOTAL%.*s %4d.%03ds\n", NAMEWIDTH-5, zDots,
321 (int)(g.iTotal/1000), (int)(g.iTotal%1000));
325 /* Print an SQL statement to standard output */
326 static void printSql(const char *zSql){
327 int n = (int)strlen(zSql);
328 while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ){ n--; }
329 if( g.bExplain ) printf("EXPLAIN ");
330 printf("%.*s;\n", n, zSql);
331 if( g.bExplain
332 #if SQLITE_VERSION_NUMBER>=3007017
333 && ( sqlite3_strglob("CREATE *", zSql)==0
334 || sqlite3_strglob("DROP *", zSql)==0
335 || sqlite3_strglob("ALTER *", zSql)==0
337 #endif
339 printf("%.*s;\n", n, zSql);
343 /* Shrink memory used, if appropriate and if the SQLite version is capable
344 ** of doing so.
346 void speedtest1_shrink_memory(void){
347 #if SQLITE_VERSION_NUMBER>=3007010
348 if( g.bMemShrink ) sqlite3_db_release_memory(g.db);
349 #endif
352 /* Run SQL */
353 void speedtest1_exec(const char *zFormat, ...){
354 va_list ap;
355 char *zSql;
356 va_start(ap, zFormat);
357 zSql = sqlite3_vmprintf(zFormat, ap);
358 va_end(ap);
359 if( g.bSqlOnly ){
360 printSql(zSql);
361 }else{
362 char *zErrMsg = 0;
363 int rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
364 if( zErrMsg ) fatal_error("SQL error: %s\n%s\n", zErrMsg, zSql);
365 if( rc!=SQLITE_OK ) fatal_error("exec error: %s\n", sqlite3_errmsg(g.db));
367 sqlite3_free(zSql);
368 speedtest1_shrink_memory();
371 /* Prepare an SQL statement */
372 void speedtest1_prepare(const char *zFormat, ...){
373 va_list ap;
374 char *zSql;
375 va_start(ap, zFormat);
376 zSql = sqlite3_vmprintf(zFormat, ap);
377 va_end(ap);
378 if( g.bSqlOnly ){
379 printSql(zSql);
380 }else{
381 int rc;
382 if( g.pStmt ) sqlite3_finalize(g.pStmt);
383 rc = sqlite3_prepare_v2(g.db, zSql, -1, &g.pStmt, 0);
384 if( rc ){
385 fatal_error("SQL error: %s\n", sqlite3_errmsg(g.db));
388 sqlite3_free(zSql);
391 /* Run an SQL statement previously prepared */
392 void speedtest1_run(void){
393 int i, n, len;
394 if( g.bSqlOnly ) return;
395 assert( g.pStmt );
396 g.nResult = 0;
397 while( sqlite3_step(g.pStmt)==SQLITE_ROW ){
398 n = sqlite3_column_count(g.pStmt);
399 for(i=0; i<n; i++){
400 const char *z = (const char*)sqlite3_column_text(g.pStmt, i);
401 if( z==0 ) z = "nil";
402 len = (int)strlen(z);
403 if( g.nResult+len<sizeof(g.zResult)-2 ){
404 if( g.nResult>0 ) g.zResult[g.nResult++] = ' ';
405 memcpy(g.zResult + g.nResult, z, len+1);
406 g.nResult += len;
410 #if SQLITE_VERSION_NUMBER>=3006001
411 if( g.bReprepare ){
412 sqlite3_stmt *pNew;
413 sqlite3_prepare_v2(g.db, sqlite3_sql(g.pStmt), -1, &pNew, 0);
414 sqlite3_finalize(g.pStmt);
415 g.pStmt = pNew;
416 }else
417 #endif
419 sqlite3_reset(g.pStmt);
421 speedtest1_shrink_memory();
424 /* The sqlite3_trace() callback function */
425 static void traceCallback(void *NotUsed, const char *zSql){
426 int n = (int)strlen(zSql);
427 while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ) n--;
428 fprintf(stderr,"%.*s;\n", n, zSql);
431 /* Substitute random() function that gives the same random
432 ** sequence on each run, for repeatability. */
433 static void randomFunc(
434 sqlite3_context *context,
435 int NotUsed,
436 sqlite3_value **NotUsed2
438 sqlite3_result_int64(context, (sqlite3_int64)speedtest1_random());
441 /* Estimate the square root of an integer */
442 static int est_square_root(int x){
443 int y0 = x/2;
444 int y1;
445 int n;
446 for(n=0; y0>0 && n<10; n++){
447 y1 = (y0 + x/y0)/2;
448 if( y1==y0 ) break;
449 y0 = y1;
451 return y0;
455 ** The main and default testset
457 void testset_main(void){
458 int i; /* Loop counter */
459 int n; /* iteration count */
460 int sz; /* Size of the tables */
461 int maxb; /* Maximum swizzled value */
462 unsigned x1, x2; /* Parameters */
463 int len; /* Length of the zNum[] string */
464 char zNum[2000]; /* A number name */
466 sz = n = g.szTest*500;
467 maxb = roundup_allones(sz);
468 speedtest1_begin_test(100, "%d INSERTs into table with no index", n);
469 speedtest1_exec("BEGIN");
470 speedtest1_exec("CREATE%s TABLE t1(a INTEGER %s, b INTEGER %s, c TEXT %s);",
471 isTemp(9), g.zNN, g.zNN, g.zNN);
472 speedtest1_prepare("INSERT INTO t1 VALUES(?1,?2,?3); -- %d times", n);
473 for(i=1; i<=n; i++){
474 x1 = swizzle(i,maxb);
475 speedtest1_numbername(x1, zNum, sizeof(zNum));
476 sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1);
477 sqlite3_bind_int(g.pStmt, 2, i);
478 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
479 speedtest1_run();
481 speedtest1_exec("COMMIT");
482 speedtest1_end_test();
485 n = sz;
486 speedtest1_begin_test(110, "%d ordered INSERTS with one index/PK", n);
487 speedtest1_exec("BEGIN");
488 speedtest1_exec(
489 "CREATE%s TABLE t2(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s",
490 isTemp(5), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
491 speedtest1_prepare("INSERT INTO t2 VALUES(?1,?2,?3); -- %d times", n);
492 for(i=1; i<=n; i++){
493 x1 = swizzle(i,maxb);
494 speedtest1_numbername(x1, zNum, sizeof(zNum));
495 sqlite3_bind_int(g.pStmt, 1, i);
496 sqlite3_bind_int64(g.pStmt, 2, (sqlite3_int64)x1);
497 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
498 speedtest1_run();
500 speedtest1_exec("COMMIT");
501 speedtest1_end_test();
504 n = sz;
505 speedtest1_begin_test(120, "%d unordered INSERTS with one index/PK", n);
506 speedtest1_exec("BEGIN");
507 speedtest1_exec(
508 "CREATE%s TABLE t3(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s",
509 isTemp(3), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
510 speedtest1_prepare("INSERT INTO t3 VALUES(?1,?2,?3); -- %d times", n);
511 for(i=1; i<=n; i++){
512 x1 = swizzle(i,maxb);
513 speedtest1_numbername(x1, zNum, sizeof(zNum));
514 sqlite3_bind_int(g.pStmt, 2, i);
515 sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1);
516 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
517 speedtest1_run();
519 speedtest1_exec("COMMIT");
520 speedtest1_end_test();
523 n = 25;
524 speedtest1_begin_test(130, "%d SELECTS, numeric BETWEEN, unindexed", n);
525 speedtest1_exec("BEGIN");
526 speedtest1_prepare(
527 "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
528 " WHERE b BETWEEN ?1 AND ?2; -- %d times", n
530 for(i=1; i<=n; i++){
531 x1 = speedtest1_random()%maxb;
532 x2 = speedtest1_random()%10 + sz/5000 + x1;
533 sqlite3_bind_int(g.pStmt, 1, x1);
534 sqlite3_bind_int(g.pStmt, 2, x2);
535 speedtest1_run();
537 speedtest1_exec("COMMIT");
538 speedtest1_end_test();
541 n = 10;
542 speedtest1_begin_test(140, "%d SELECTS, LIKE, unindexed", n);
543 speedtest1_exec("BEGIN");
544 speedtest1_prepare(
545 "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
546 " WHERE c LIKE ?1; -- %d times", n
548 for(i=1; i<=n; i++){
549 x1 = speedtest1_random()%maxb;
550 zNum[0] = '%';
551 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2);
552 zNum[len] = '%';
553 zNum[len+1] = 0;
554 sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC);
555 speedtest1_run();
557 speedtest1_exec("COMMIT");
558 speedtest1_end_test();
561 n = 10;
562 speedtest1_begin_test(142, "%d SELECTS w/ORDER BY, unindexed", n);
563 speedtest1_exec("BEGIN");
564 speedtest1_prepare(
565 "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n"
566 " ORDER BY a; -- %d times", n
568 for(i=1; i<=n; i++){
569 x1 = speedtest1_random()%maxb;
570 zNum[0] = '%';
571 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2);
572 zNum[len] = '%';
573 zNum[len+1] = 0;
574 sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC);
575 speedtest1_run();
577 speedtest1_exec("COMMIT");
578 speedtest1_end_test();
580 n = 10; /* g.szTest/5; */
581 speedtest1_begin_test(145, "%d SELECTS w/ORDER BY and LIMIT, unindexed", n);
582 speedtest1_exec("BEGIN");
583 speedtest1_prepare(
584 "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n"
585 " ORDER BY a LIMIT 10; -- %d times", n
587 for(i=1; i<=n; i++){
588 x1 = speedtest1_random()%maxb;
589 zNum[0] = '%';
590 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2);
591 zNum[len] = '%';
592 zNum[len+1] = 0;
593 sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC);
594 speedtest1_run();
596 speedtest1_exec("COMMIT");
597 speedtest1_end_test();
600 speedtest1_begin_test(150, "CREATE INDEX five times");
601 speedtest1_exec("BEGIN;");
602 speedtest1_exec("CREATE UNIQUE INDEX t1b ON t1(b);");
603 speedtest1_exec("CREATE INDEX t1c ON t1(c);");
604 speedtest1_exec("CREATE UNIQUE INDEX t2b ON t2(b);");
605 speedtest1_exec("CREATE INDEX t2c ON t2(c DESC);");
606 speedtest1_exec("CREATE INDEX t3bc ON t3(b,c);");
607 speedtest1_exec("COMMIT;");
608 speedtest1_end_test();
611 n = sz/5;
612 speedtest1_begin_test(160, "%d SELECTS, numeric BETWEEN, indexed", n);
613 speedtest1_exec("BEGIN");
614 speedtest1_prepare(
615 "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
616 " WHERE b BETWEEN ?1 AND ?2; -- %d times", n
618 for(i=1; i<=n; i++){
619 x1 = speedtest1_random()%maxb;
620 x2 = speedtest1_random()%10 + sz/5000 + x1;
621 sqlite3_bind_int(g.pStmt, 1, x1);
622 sqlite3_bind_int(g.pStmt, 2, x2);
623 speedtest1_run();
625 speedtest1_exec("COMMIT");
626 speedtest1_end_test();
629 n = sz/5;
630 speedtest1_begin_test(161, "%d SELECTS, numeric BETWEEN, PK", n);
631 speedtest1_exec("BEGIN");
632 speedtest1_prepare(
633 "SELECT count(*), avg(b), sum(length(c)) FROM t2\n"
634 " WHERE a BETWEEN ?1 AND ?2; -- %d times", n
636 for(i=1; i<=n; i++){
637 x1 = speedtest1_random()%maxb;
638 x2 = speedtest1_random()%10 + sz/5000 + x1;
639 sqlite3_bind_int(g.pStmt, 1, x1);
640 sqlite3_bind_int(g.pStmt, 2, x2);
641 speedtest1_run();
643 speedtest1_exec("COMMIT");
644 speedtest1_end_test();
647 n = sz/5;
648 speedtest1_begin_test(170, "%d SELECTS, text BETWEEN, indexed", n);
649 speedtest1_exec("BEGIN");
650 speedtest1_prepare(
651 "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
652 " WHERE c BETWEEN ?1 AND (?1||'~'); -- %d times", n
654 for(i=1; i<=n; i++){
655 x1 = swizzle(i, maxb);
656 len = speedtest1_numbername(x1, zNum, sizeof(zNum)-1);
657 sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC);
658 speedtest1_run();
660 speedtest1_exec("COMMIT");
661 speedtest1_end_test();
663 n = sz;
664 speedtest1_begin_test(180, "%d INSERTS with three indexes", n);
665 speedtest1_exec("BEGIN");
666 speedtest1_exec(
667 "CREATE%s TABLE t4(\n"
668 " a INTEGER %s %s,\n"
669 " b INTEGER %s,\n"
670 " c TEXT %s\n"
671 ") %s",
672 isTemp(1), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
673 speedtest1_exec("CREATE INDEX t4b ON t4(b)");
674 speedtest1_exec("CREATE INDEX t4c ON t4(c)");
675 speedtest1_exec("INSERT INTO t4 SELECT * FROM t1");
676 speedtest1_exec("COMMIT");
677 speedtest1_end_test();
679 n = sz;
680 speedtest1_begin_test(190, "DELETE and REFILL one table", n);
681 speedtest1_exec("DELETE FROM t2;");
682 speedtest1_exec("INSERT INTO t2 SELECT * FROM t1;");
683 speedtest1_end_test();
686 speedtest1_begin_test(200, "VACUUM");
687 speedtest1_exec("VACUUM");
688 speedtest1_end_test();
691 speedtest1_begin_test(210, "ALTER TABLE ADD COLUMN, and query");
692 speedtest1_exec("ALTER TABLE t2 ADD COLUMN d DEFAULT 123");
693 speedtest1_exec("SELECT sum(d) FROM t2");
694 speedtest1_end_test();
697 n = sz/5;
698 speedtest1_begin_test(230, "%d UPDATES, numeric BETWEEN, indexed", n);
699 speedtest1_exec("BEGIN");
700 speedtest1_prepare(
701 "UPDATE t2 SET d=b*2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n
703 for(i=1; i<=n; i++){
704 x1 = speedtest1_random()%maxb;
705 x2 = speedtest1_random()%10 + sz/5000 + x1;
706 sqlite3_bind_int(g.pStmt, 1, x1);
707 sqlite3_bind_int(g.pStmt, 2, x2);
708 speedtest1_run();
710 speedtest1_exec("COMMIT");
711 speedtest1_end_test();
714 n = sz;
715 speedtest1_begin_test(240, "%d UPDATES of individual rows", n);
716 speedtest1_exec("BEGIN");
717 speedtest1_prepare(
718 "UPDATE t2 SET d=b*3 WHERE a=?1; -- %d times", n
720 for(i=1; i<=n; i++){
721 x1 = speedtest1_random()%sz + 1;
722 sqlite3_bind_int(g.pStmt, 1, x1);
723 speedtest1_run();
725 speedtest1_exec("COMMIT");
726 speedtest1_end_test();
728 speedtest1_begin_test(250, "One big UPDATE of the whole %d-row table", sz);
729 speedtest1_exec("UPDATE t2 SET d=b*4");
730 speedtest1_end_test();
733 speedtest1_begin_test(260, "Query added column after filling");
734 speedtest1_exec("SELECT sum(d) FROM t2");
735 speedtest1_end_test();
739 n = sz/5;
740 speedtest1_begin_test(270, "%d DELETEs, numeric BETWEEN, indexed", n);
741 speedtest1_exec("BEGIN");
742 speedtest1_prepare(
743 "DELETE FROM t2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n
745 for(i=1; i<=n; i++){
746 x1 = speedtest1_random()%maxb + 1;
747 x2 = speedtest1_random()%10 + sz/5000 + x1;
748 sqlite3_bind_int(g.pStmt, 1, x1);
749 sqlite3_bind_int(g.pStmt, 2, x2);
750 speedtest1_run();
752 speedtest1_exec("COMMIT");
753 speedtest1_end_test();
756 n = sz;
757 speedtest1_begin_test(280, "%d DELETEs of individual rows", n);
758 speedtest1_exec("BEGIN");
759 speedtest1_prepare(
760 "DELETE FROM t3 WHERE a=?1; -- %d times", n
762 for(i=1; i<=n; i++){
763 x1 = speedtest1_random()%sz + 1;
764 sqlite3_bind_int(g.pStmt, 1, x1);
765 speedtest1_run();
767 speedtest1_exec("COMMIT");
768 speedtest1_end_test();
771 speedtest1_begin_test(290, "Refill two %d-row tables using REPLACE", sz);
772 speedtest1_exec("REPLACE INTO t2(a,b,c) SELECT a,b,c FROM t1");
773 speedtest1_exec("REPLACE INTO t3(a,b,c) SELECT a,b,c FROM t1");
774 speedtest1_end_test();
776 speedtest1_begin_test(300, "Refill a %d-row table using (b&1)==(a&1)", sz);
777 speedtest1_exec("DELETE FROM t2;");
778 speedtest1_exec("INSERT INTO t2(a,b,c)\n"
779 " SELECT a,b,c FROM t1 WHERE (b&1)==(a&1);");
780 speedtest1_exec("INSERT INTO t2(a,b,c)\n"
781 " SELECT a,b,c FROM t1 WHERE (b&1)<>(a&1);");
782 speedtest1_end_test();
785 n = sz/5;
786 speedtest1_begin_test(310, "%d four-ways joins", n);
787 speedtest1_exec("BEGIN");
788 speedtest1_prepare(
789 "SELECT t1.c FROM t1, t2, t3, t4\n"
790 " WHERE t4.a BETWEEN ?1 AND ?2\n"
791 " AND t3.a=t4.b\n"
792 " AND t2.a=t3.b\n"
793 " AND t1.c=t2.c"
795 for(i=1; i<=n; i++){
796 x1 = speedtest1_random()%sz + 1;
797 x2 = speedtest1_random()%10 + x1 + 4;
798 sqlite3_bind_int(g.pStmt, 1, x1);
799 sqlite3_bind_int(g.pStmt, 2, x2);
800 speedtest1_run();
802 speedtest1_exec("COMMIT");
803 speedtest1_end_test();
805 speedtest1_begin_test(320, "subquery in result set", n);
806 speedtest1_prepare(
807 "SELECT sum(a), max(c),\n"
808 " avg((SELECT a FROM t2 WHERE 5+t2.b=t1.b) AND rowid<?1), max(c)\n"
809 " FROM t1 WHERE rowid<?1;"
811 sqlite3_bind_int(g.pStmt, 1, est_square_root(g.szTest)*50);
812 speedtest1_run();
813 speedtest1_end_test();
815 speedtest1_begin_test(980, "PRAGMA integrity_check");
816 speedtest1_exec("PRAGMA integrity_check");
817 speedtest1_end_test();
820 speedtest1_begin_test(990, "ANALYZE");
821 speedtest1_exec("ANALYZE");
822 speedtest1_end_test();
826 ** A testset for common table expressions. This exercises code
827 ** for views, subqueries, co-routines, etc.
829 void testset_cte(void){
830 static const char *azPuzzle[] = {
831 /* Easy */
832 "534...9.."
833 "67.195..."
834 ".98....6."
835 "8...6...3"
836 "4..8.3..1"
837 "....2...6"
838 ".6....28."
839 "...419..5"
840 "...28..79",
842 /* Medium */
843 "53....9.."
844 "6..195..."
845 ".98....6."
846 "8...6...3"
847 "4..8.3..1"
848 "....2...6"
849 ".6....28."
850 "...419..5"
851 "....8..79",
853 /* Hard */
854 "53......."
855 "6..195..."
856 ".98....6."
857 "8...6...3"
858 "4..8.3..1"
859 "....2...6"
860 ".6....28."
861 "...419..5"
862 "....8..79",
864 const char *zPuz;
865 double rSpacing;
866 int nElem;
868 if( g.szTest<25 ){
869 zPuz = azPuzzle[0];
870 }else if( g.szTest<70 ){
871 zPuz = azPuzzle[1];
872 }else{
873 zPuz = azPuzzle[2];
875 speedtest1_begin_test(100, "Sudoku with recursive 'digits'");
876 speedtest1_prepare(
877 "WITH RECURSIVE\n"
878 " input(sud) AS (VALUES(?1)),\n"
879 " digits(z,lp) AS (\n"
880 " VALUES('1', 1)\n"
881 " UNION ALL\n"
882 " SELECT CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9\n"
883 " ),\n"
884 " x(s, ind) AS (\n"
885 " SELECT sud, instr(sud, '.') FROM input\n"
886 " UNION ALL\n"
887 " SELECT\n"
888 " substr(s, 1, ind-1) || z || substr(s, ind+1),\n"
889 " instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n"
890 " FROM x, digits AS z\n"
891 " WHERE ind>0\n"
892 " AND NOT EXISTS (\n"
893 " SELECT 1\n"
894 " FROM digits AS lp\n"
895 " WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n"
896 " OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n"
897 " OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n"
898 " + ((ind-1)/27) * 27 + lp\n"
899 " + ((lp-1) / 3) * 6, 1)\n"
900 " )\n"
901 " )\n"
902 "SELECT s FROM x WHERE ind=0;"
904 sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC);
905 speedtest1_run();
906 speedtest1_end_test();
908 speedtest1_begin_test(200, "Sudoku with VALUES 'digits'");
909 speedtest1_prepare(
910 "WITH RECURSIVE\n"
911 " input(sud) AS (VALUES(?1)),\n"
912 " digits(z,lp) AS (VALUES('1',1),('2',2),('3',3),('4',4),('5',5),\n"
913 " ('6',6),('7',7),('8',8),('9',9)),\n"
914 " x(s, ind) AS (\n"
915 " SELECT sud, instr(sud, '.') FROM input\n"
916 " UNION ALL\n"
917 " SELECT\n"
918 " substr(s, 1, ind-1) || z || substr(s, ind+1),\n"
919 " instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n"
920 " FROM x, digits AS z\n"
921 " WHERE ind>0\n"
922 " AND NOT EXISTS (\n"
923 " SELECT 1\n"
924 " FROM digits AS lp\n"
925 " WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n"
926 " OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n"
927 " OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n"
928 " + ((ind-1)/27) * 27 + lp\n"
929 " + ((lp-1) / 3) * 6, 1)\n"
930 " )\n"
931 " )\n"
932 "SELECT s FROM x WHERE ind=0;"
934 sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC);
935 speedtest1_run();
936 speedtest1_end_test();
938 rSpacing = 5.0/g.szTest;
939 speedtest1_begin_test(300, "Mandelbrot Set with spacing=%f", rSpacing);
940 speedtest1_prepare(
941 "WITH RECURSIVE \n"
942 " xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+?1 FROM xaxis WHERE x<1.2),\n"
943 " yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+?2 FROM yaxis WHERE y<1.0),\n"
944 " m(iter, cx, cy, x, y) AS (\n"
945 " SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis\n"
946 " UNION ALL\n"
947 " SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m \n"
948 " WHERE (x*x + y*y) < 4.0 AND iter<28\n"
949 " ),\n"
950 " m2(iter, cx, cy) AS (\n"
951 " SELECT max(iter), cx, cy FROM m GROUP BY cx, cy\n"
952 " ),\n"
953 " a(t) AS (\n"
954 " SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') \n"
955 " FROM m2 GROUP BY cy\n"
956 " )\n"
957 "SELECT group_concat(rtrim(t),x'0a') FROM a;"
959 sqlite3_bind_double(g.pStmt, 1, rSpacing*.05);
960 sqlite3_bind_double(g.pStmt, 2, rSpacing);
961 speedtest1_run();
962 speedtest1_end_test();
964 nElem = 10000*g.szTest;
965 speedtest1_begin_test(400, "EXCEPT operator on %d-element tables", nElem);
966 speedtest1_prepare(
967 "WITH RECURSIVE \n"
968 " t1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM t1 WHERE x<%d),\n"
969 " t2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM t2 WHERE y<%d)\n"
970 "SELECT count(x), avg(x) FROM (\n"
971 " SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1\n"
972 ");",
973 nElem, nElem
975 speedtest1_run();
976 speedtest1_end_test();
980 #ifdef SQLITE_ENABLE_RTREE
981 /* Generate two numbers between 1 and mx. The first number is less than
982 ** the second. Usually the numbers are near each other but can sometimes
983 ** be far apart.
985 static void twoCoords(
986 int p1, int p2, /* Parameters adjusting sizes */
987 unsigned mx, /* Range of 1..mx */
988 unsigned *pX0, unsigned *pX1 /* OUT: write results here */
990 unsigned d, x0, x1, span;
992 span = mx/100 + 1;
993 if( speedtest1_random()%3==0 ) span *= p1;
994 if( speedtest1_random()%p2==0 ) span = mx/2;
995 d = speedtest1_random()%span + 1;
996 x0 = speedtest1_random()%(mx-d) + 1;
997 x1 = x0 + d;
998 *pX0 = x0;
999 *pX1 = x1;
1001 #endif
1003 #ifdef SQLITE_ENABLE_RTREE
1004 /* The following routine is an R-Tree geometry callback. It returns
1005 ** true if the object overlaps a slice on the Y coordinate between the
1006 ** two values given as arguments. In other words
1008 ** SELECT count(*) FROM rt1 WHERE id MATCH xslice(10,20);
1010 ** Is the same as saying:
1012 ** SELECT count(*) FROM rt1 WHERE y1>=10 AND y0<=20;
1014 static int xsliceGeometryCallback(
1015 sqlite3_rtree_geometry *p,
1016 int nCoord,
1017 double *aCoord,
1018 int *pRes
1020 *pRes = aCoord[3]>=p->aParam[0] && aCoord[2]<=p->aParam[1];
1021 return SQLITE_OK;
1023 #endif /* SQLITE_ENABLE_RTREE */
1025 #ifdef SQLITE_ENABLE_RTREE
1027 ** A testset for the R-Tree virtual table
1029 void testset_rtree(int p1, int p2){
1030 unsigned i, n;
1031 unsigned mxCoord;
1032 unsigned x0, x1, y0, y1, z0, z1;
1033 unsigned iStep;
1034 int *aCheck = sqlite3_malloc( sizeof(int)*g.szTest*100 );
1036 mxCoord = 15000;
1037 n = g.szTest*100;
1038 speedtest1_begin_test(100, "%d INSERTs into an r-tree", n);
1039 speedtest1_exec("BEGIN");
1040 speedtest1_exec("CREATE VIRTUAL TABLE rt1 USING rtree(id,x0,x1,y0,y1,z0,z1)");
1041 speedtest1_prepare("INSERT INTO rt1(id,x0,x1,y0,y1,z0,z1)"
1042 "VALUES(?1,?2,?3,?4,?5,?6,?7)");
1043 for(i=1; i<=n; i++){
1044 twoCoords(p1, p2, mxCoord, &x0, &x1);
1045 twoCoords(p1, p2, mxCoord, &y0, &y1);
1046 twoCoords(p1, p2, mxCoord, &z0, &z1);
1047 sqlite3_bind_int(g.pStmt, 1, i);
1048 sqlite3_bind_int(g.pStmt, 2, x0);
1049 sqlite3_bind_int(g.pStmt, 3, x1);
1050 sqlite3_bind_int(g.pStmt, 4, y0);
1051 sqlite3_bind_int(g.pStmt, 5, y1);
1052 sqlite3_bind_int(g.pStmt, 6, z0);
1053 sqlite3_bind_int(g.pStmt, 7, z1);
1054 speedtest1_run();
1056 speedtest1_exec("COMMIT");
1057 speedtest1_end_test();
1059 speedtest1_begin_test(101, "Copy from rtree to a regular table");
1060 speedtest1_exec(" TABLE t1(id INTEGER PRIMARY KEY,x0,x1,y0,y1,z0,z1)");
1061 speedtest1_exec("INSERT INTO t1 SELECT * FROM rt1");
1062 speedtest1_end_test();
1064 n = g.szTest*20;
1065 speedtest1_begin_test(110, "%d one-dimensional intersect slice queries", n);
1066 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x0>=?1 AND x1<=?2");
1067 iStep = mxCoord/n;
1068 for(i=0; i<n; i++){
1069 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1070 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1071 speedtest1_run();
1072 aCheck[i] = atoi(g.zResult);
1074 speedtest1_end_test();
1076 if( g.bVerify ){
1077 n = g.szTest*20;
1078 speedtest1_begin_test(111, "Verify result from 1-D intersect slice queries");
1079 speedtest1_prepare("SELECT count(*) FROM t1 WHERE x0>=?1 AND x1<=?2");
1080 iStep = mxCoord/n;
1081 for(i=0; i<n; i++){
1082 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1083 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1084 speedtest1_run();
1085 if( aCheck[i]!=atoi(g.zResult) ){
1086 fatal_error("Count disagree step %d: %d..%d. %d vs %d",
1087 i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult));
1090 speedtest1_end_test();
1093 n = g.szTest*20;
1094 speedtest1_begin_test(120, "%d one-dimensional overlap slice queries", n);
1095 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE y1>=?1 AND y0<=?2");
1096 iStep = mxCoord/n;
1097 for(i=0; i<n; i++){
1098 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1099 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1100 speedtest1_run();
1101 aCheck[i] = atoi(g.zResult);
1103 speedtest1_end_test();
1105 if( g.bVerify ){
1106 n = g.szTest*20;
1107 speedtest1_begin_test(121, "Verify result from 1-D overlap slice queries");
1108 speedtest1_prepare("SELECT count(*) FROM t1 WHERE y1>=?1 AND y0<=?2");
1109 iStep = mxCoord/n;
1110 for(i=0; i<n; i++){
1111 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1112 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1113 speedtest1_run();
1114 if( aCheck[i]!=atoi(g.zResult) ){
1115 fatal_error("Count disagree step %d: %d..%d. %d vs %d",
1116 i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult));
1119 speedtest1_end_test();
1123 n = g.szTest*20;
1124 speedtest1_begin_test(125, "%d custom geometry callback queries", n);
1125 sqlite3_rtree_geometry_callback(g.db, "xslice", xsliceGeometryCallback, 0);
1126 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE id MATCH xslice(?1,?2)");
1127 iStep = mxCoord/n;
1128 for(i=0; i<n; i++){
1129 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1130 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1131 speedtest1_run();
1132 if( aCheck[i]!=atoi(g.zResult) ){
1133 fatal_error("Count disagree step %d: %d..%d. %d vs %d",
1134 i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult));
1137 speedtest1_end_test();
1139 n = g.szTest*80;
1140 speedtest1_begin_test(130, "%d three-dimensional intersect box queries", n);
1141 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x1>=?1 AND x0<=?2"
1142 " AND y1>=?1 AND y0<=?2 AND z1>=?1 AND z0<=?2");
1143 iStep = mxCoord/n;
1144 for(i=0; i<n; i++){
1145 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1146 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1147 speedtest1_run();
1148 aCheck[i] = atoi(g.zResult);
1150 speedtest1_end_test();
1152 n = g.szTest*100;
1153 speedtest1_begin_test(140, "%d rowid queries", n);
1154 speedtest1_prepare("SELECT * FROM rt1 WHERE id=?1");
1155 for(i=1; i<=n; i++){
1156 sqlite3_bind_int(g.pStmt, 1, i);
1157 speedtest1_run();
1159 speedtest1_end_test();
1161 #endif /* SQLITE_ENABLE_RTREE */
1164 ** A testset used for debugging speedtest1 itself.
1166 void testset_debug1(void){
1167 unsigned i, n;
1168 unsigned x1, x2;
1169 char zNum[2000]; /* A number name */
1171 n = g.szTest;
1172 for(i=1; i<=n; i++){
1173 x1 = swizzle(i, n);
1174 x2 = swizzle(x1, n);
1175 speedtest1_numbername(x1, zNum, sizeof(zNum));
1176 printf("%5d %5d %5d %s\n", i, x1, x2, zNum);
1180 #ifdef __linux__
1181 #include <sys/types.h>
1182 #include <unistd.h>
1185 ** Attempt to display I/O stats on Linux using /proc/PID/io
1187 static void displayLinuxIoStats(FILE *out){
1188 FILE *in;
1189 char z[200];
1190 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
1191 in = fopen(z, "rb");
1192 if( in==0 ) return;
1193 while( fgets(z, sizeof(z), in)!=0 ){
1194 static const struct {
1195 const char *zPattern;
1196 const char *zDesc;
1197 } aTrans[] = {
1198 { "rchar: ", "Bytes received by read():" },
1199 { "wchar: ", "Bytes sent to write():" },
1200 { "syscr: ", "Read() system calls:" },
1201 { "syscw: ", "Write() system calls:" },
1202 { "read_bytes: ", "Bytes rcvd from storage:" },
1203 { "write_bytes: ", "Bytes sent to storage:" },
1204 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
1206 int i;
1207 for(i=0; i<sizeof(aTrans)/sizeof(aTrans[0]); i++){
1208 int n = (int)strlen(aTrans[i].zPattern);
1209 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
1210 fprintf(out, "-- %-28s %s", aTrans[i].zDesc, &z[n]);
1211 break;
1215 fclose(in);
1217 #endif
1219 int main(int argc, char **argv){
1220 int doAutovac = 0; /* True for --autovacuum */
1221 int cacheSize = 0; /* Desired cache size. 0 means default */
1222 int doExclusive = 0; /* True for --exclusive */
1223 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */
1224 int doIncrvac = 0; /* True for --incrvacuum */
1225 const char *zJMode = 0; /* Journal mode */
1226 const char *zKey = 0; /* Encryption key */
1227 int nLook = 0, szLook = 0; /* --lookaside configuration */
1228 int noSync = 0; /* True for --nosync */
1229 int pageSize = 0; /* Desired page size. 0 means default */
1230 int nPCache = 0, szPCache = 0;/* --pcache configuration */
1231 int doPCache = 0; /* True if --pcache is seen */
1232 int nScratch = 0, szScratch=0;/* --scratch configuration */
1233 int showStats = 0; /* True for --stats */
1234 int nThread = 0; /* --threads value */
1235 const char *zTSet = "main"; /* Which --testset torun */
1236 int doTrace = 0; /* True for --trace */
1237 const char *zEncoding = 0; /* --utf16be or --utf16le */
1238 const char *zDbName = 0; /* Name of the test database */
1240 void *pHeap = 0; /* Allocated heap space */
1241 void *pLook = 0; /* Allocated lookaside space */
1242 void *pPCache = 0; /* Allocated storage for pcache */
1243 void *pScratch = 0; /* Allocated storage for scratch */
1244 int iCur, iHi; /* Stats values, current and "highwater" */
1245 int i; /* Loop counter */
1246 int rc; /* API return code */
1248 /* Display the version of SQLite being tested */
1249 printf("-- Speedtest1 for SQLite %s %.50s\n",
1250 sqlite3_libversion(), sqlite3_sourceid());
1252 /* Process command-line arguments */
1253 g.zWR = "";
1254 g.zNN = "";
1255 g.zPK = "UNIQUE";
1256 g.szTest = 100;
1257 for(i=1; i<argc; i++){
1258 const char *z = argv[i];
1259 if( z[0]=='-' ){
1260 do{ z++; }while( z[0]=='-' );
1261 if( strcmp(z,"autovacuum")==0 ){
1262 doAutovac = 1;
1263 }else if( strcmp(z,"cachesize")==0 ){
1264 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1265 i++;
1266 cacheSize = integerValue(argv[i]);
1267 }else if( strcmp(z,"exclusive")==0 ){
1268 doExclusive = 1;
1269 }else if( strcmp(z,"explain")==0 ){
1270 g.bSqlOnly = 1;
1271 g.bExplain = 1;
1272 }else if( strcmp(z,"heap")==0 ){
1273 if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
1274 nHeap = integerValue(argv[i+1]);
1275 mnHeap = integerValue(argv[i+2]);
1276 i += 2;
1277 }else if( strcmp(z,"incrvacuum")==0 ){
1278 doIncrvac = 1;
1279 }else if( strcmp(z,"journal")==0 ){
1280 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1281 zJMode = argv[++i];
1282 }else if( strcmp(z,"key")==0 ){
1283 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1284 zKey = argv[++i];
1285 }else if( strcmp(z,"lookaside")==0 ){
1286 if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
1287 nLook = integerValue(argv[i+1]);
1288 szLook = integerValue(argv[i+2]);
1289 i += 2;
1290 }else if( strcmp(z,"multithread")==0 ){
1291 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
1292 }else if( strcmp(z,"nomemstat")==0 ){
1293 sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0);
1294 }else if( strcmp(z,"nosync")==0 ){
1295 noSync = 1;
1296 }else if( strcmp(z,"notnull")==0 ){
1297 g.zNN = "NOT NULL";
1298 #ifdef SQLITE_ENABLE_RBU
1299 }else if( strcmp(z,"rbu")==0 ){
1300 sqlite3ota_create_vfs("rbu", 0);
1301 sqlite3_vfs_register(sqlite3_vfs_find("rbu"), 1);
1302 #endif
1303 }else if( strcmp(z,"pagesize")==0 ){
1304 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1305 pageSize = integerValue(argv[++i]);
1306 }else if( strcmp(z,"pcache")==0 ){
1307 if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
1308 nPCache = integerValue(argv[i+1]);
1309 szPCache = integerValue(argv[i+2]);
1310 doPCache = 1;
1311 i += 2;
1312 }else if( strcmp(z,"primarykey")==0 ){
1313 g.zPK = "PRIMARY KEY";
1314 }else if( strcmp(z,"reprepare")==0 ){
1315 g.bReprepare = 1;
1316 }else if( strcmp(z,"scratch")==0 ){
1317 if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
1318 nScratch = integerValue(argv[i+1]);
1319 szScratch = integerValue(argv[i+2]);
1320 i += 2;
1321 }else if( strcmp(z,"serialized")==0 ){
1322 sqlite3_config(SQLITE_CONFIG_SERIALIZED);
1323 }else if( strcmp(z,"singlethread")==0 ){
1324 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
1325 }else if( strcmp(z,"sqlonly")==0 ){
1326 g.bSqlOnly = 1;
1327 }else if( strcmp(z,"shrink-memory")==0 ){
1328 g.bMemShrink = 1;
1329 }else if( strcmp(z,"size")==0 ){
1330 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1331 g.szTest = integerValue(argv[++i]);
1332 }else if( strcmp(z,"stats")==0 ){
1333 showStats = 1;
1334 }else if( strcmp(z,"temp")==0 ){
1335 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1336 i++;
1337 if( argv[i][0]<'0' || argv[i][0]>'9' || argv[i][1]!=0 ){
1338 fatal_error("argument to --temp should be integer between 0 and 9");
1340 g.eTemp = argv[i][0] - '0';
1341 }else if( strcmp(z,"testset")==0 ){
1342 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1343 zTSet = argv[++i];
1344 }else if( strcmp(z,"trace")==0 ){
1345 doTrace = 1;
1346 }else if( strcmp(z,"threads")==0 ){
1347 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1348 nThread = integerValue(argv[++i]);
1349 }else if( strcmp(z,"utf16le")==0 ){
1350 zEncoding = "utf16le";
1351 }else if( strcmp(z,"utf16be")==0 ){
1352 zEncoding = "utf16be";
1353 }else if( strcmp(z,"verify")==0 ){
1354 g.bVerify = 1;
1355 }else if( strcmp(z,"without-rowid")==0 ){
1356 g.zWR = "WITHOUT ROWID";
1357 g.zPK = "PRIMARY KEY";
1358 }else if( strcmp(z, "help")==0 || strcmp(z,"?")==0 ){
1359 printf(zHelp, argv[0]);
1360 exit(0);
1361 }else{
1362 fatal_error("unknown option: %s\nUse \"%s -?\" for help\n",
1363 argv[i], argv[0]);
1365 }else if( zDbName==0 ){
1366 zDbName = argv[i];
1367 }else{
1368 fatal_error("surplus argument: %s\nUse \"%s -?\" for help\n",
1369 argv[i], argv[0]);
1372 if( zDbName!=0 ) unlink(zDbName);
1373 #if SQLITE_VERSION_NUMBER>=3006001
1374 if( nHeap>0 ){
1375 pHeap = malloc( nHeap );
1376 if( pHeap==0 ) fatal_error("cannot allocate %d-byte heap\n", nHeap);
1377 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
1378 if( rc ) fatal_error("heap configuration failed: %d\n", rc);
1380 if( doPCache ){
1381 if( nPCache>0 && szPCache>0 ){
1382 pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
1383 if( pPCache==0 ) fatal_error("cannot allocate %lld-byte pcache\n",
1384 nPCache*(sqlite3_int64)szPCache);
1386 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
1387 if( rc ) fatal_error("pcache configuration failed: %d\n", rc);
1389 if( nScratch>0 && szScratch>0 ){
1390 pScratch = malloc( nScratch*(sqlite3_int64)szScratch );
1391 if( pScratch==0 ) fatal_error("cannot allocate %lld-byte scratch\n",
1392 nScratch*(sqlite3_int64)szScratch);
1393 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch);
1394 if( rc ) fatal_error("scratch configuration failed: %d\n", rc);
1396 if( nLook>0 ){
1397 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
1399 #endif
1401 /* Open the database and the input file */
1402 if( sqlite3_open(zDbName, &g.db) ){
1403 fatal_error("Cannot open database file: %s\n", zDbName);
1405 #if SQLITE_VERSION_NUMBER>=3006001
1406 if( nLook>0 && szLook>0 ){
1407 pLook = malloc( nLook*szLook );
1408 rc = sqlite3_db_config(g.db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook,nLook);
1409 if( rc ) fatal_error("lookaside configuration failed: %d\n", rc);
1411 #endif
1413 /* Set database connection options */
1414 sqlite3_create_function(g.db, "random", 0, SQLITE_UTF8, 0, randomFunc, 0, 0);
1415 if( doTrace ) sqlite3_trace(g.db, traceCallback, 0);
1416 speedtest1_exec("PRAGMA threads=%d", nThread);
1417 if( zKey ){
1418 speedtest1_exec("PRAGMA key('%s')", zKey);
1420 if( zEncoding ){
1421 speedtest1_exec("PRAGMA encoding=%s", zEncoding);
1423 if( doAutovac ){
1424 speedtest1_exec("PRAGMA auto_vacuum=FULL");
1425 }else if( doIncrvac ){
1426 speedtest1_exec("PRAGMA auto_vacuum=INCREMENTAL");
1428 if( pageSize ){
1429 speedtest1_exec("PRAGMA page_size=%d", pageSize);
1431 if( cacheSize ){
1432 speedtest1_exec("PRAGMA cache_size=%d", cacheSize);
1434 if( noSync ) speedtest1_exec("PRAGMA synchronous=OFF");
1435 if( doExclusive ){
1436 speedtest1_exec("PRAGMA locking_mode=EXCLUSIVE");
1438 if( zJMode ){
1439 speedtest1_exec("PRAGMA journal_mode=%s", zJMode);
1442 if( g.bExplain ) printf(".explain\n.echo on\n");
1443 if( strcmp(zTSet,"main")==0 ){
1444 testset_main();
1445 }else if( strcmp(zTSet,"debug1")==0 ){
1446 testset_debug1();
1447 }else if( strcmp(zTSet,"cte")==0 ){
1448 testset_cte();
1449 }else if( strcmp(zTSet,"rtree")==0 ){
1450 #ifdef SQLITE_ENABLE_RTREE
1451 testset_rtree(6, 147);
1452 #else
1453 fatal_error("compile with -DSQLITE_ENABLE_RTREE to enable "
1454 "the R-Tree tests\n");
1455 #endif
1456 }else{
1457 fatal_error("unknown testset: \"%s\"\nChoices: main debug1 cte rtree\n",
1458 zTSet);
1460 speedtest1_final();
1462 /* Database connection statistics printed after both prepared statements
1463 ** have been finalized */
1464 #if SQLITE_VERSION_NUMBER>=3007009
1465 if( showStats ){
1466 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHi, 0);
1467 printf("-- Lookaside Slots Used: %d (max %d)\n", iCur,iHi);
1468 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHi, 0);
1469 printf("-- Successful lookasides: %d\n", iHi);
1470 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur,&iHi,0);
1471 printf("-- Lookaside size faults: %d\n", iHi);
1472 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur,&iHi,0);
1473 printf("-- Lookaside OOM faults: %d\n", iHi);
1474 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHi, 0);
1475 printf("-- Pager Heap Usage: %d bytes\n", iCur);
1476 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHi, 1);
1477 printf("-- Page cache hits: %d\n", iCur);
1478 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHi, 1);
1479 printf("-- Page cache misses: %d\n", iCur);
1480 #if SQLITE_VERSION_NUMBER>=3007012
1481 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHi, 1);
1482 printf("-- Page cache writes: %d\n", iCur);
1483 #endif
1484 sqlite3_db_status(g.db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHi, 0);
1485 printf("-- Schema Heap Usage: %d bytes\n", iCur);
1486 sqlite3_db_status(g.db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHi, 0);
1487 printf("-- Statement Heap Usage: %d bytes\n", iCur);
1489 #endif
1491 sqlite3_close(g.db);
1493 #if SQLITE_VERSION_NUMBER>=3006001
1494 /* Global memory usage statistics printed after the database connection
1495 ** has closed. Memory usage should be zero at this point. */
1496 if( showStats ){
1497 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHi, 0);
1498 printf("-- Memory Used (bytes): %d (max %d)\n", iCur,iHi);
1499 #if SQLITE_VERSION_NUMBER>=3007000
1500 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHi, 0);
1501 printf("-- Outstanding Allocations: %d (max %d)\n", iCur,iHi);
1502 #endif
1503 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHi, 0);
1504 printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur,iHi);
1505 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHi, 0);
1506 printf("-- Scratch Overflow Bytes: %d (max %d)\n", iCur,iHi);
1507 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHi, 0);
1508 printf("-- Largest Allocation: %d bytes\n",iHi);
1509 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHi, 0);
1510 printf("-- Largest Pcache Allocation: %d bytes\n",iHi);
1511 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHi, 0);
1512 printf("-- Largest Scratch Allocation: %d bytes\n", iHi);
1514 #endif
1516 #ifdef __linux__
1517 if( showStats ){
1518 displayLinuxIoStats(stdout);
1520 #endif
1522 /* Release memory */
1523 free( pLook );
1524 free( pPCache );
1525 free( pScratch );
1526 free( pHeap );
1527 return 0;