2 ** A program for performance testing.
4 ** The available command-line options are described below:
6 static const char zHelp
[] =
7 "Usage: %s [--options] DATABASE\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"
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
57 #ifdef SQLITE_ENABLE_RBU
58 # include "sqlite3rbu.h"
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 */
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
, ...){
94 vfprintf(stderr
, zMsg
, ap
);
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;
110 /* Provide an alternative to sqlite3_stricmp() in older versions of
112 #if SQLITE_VERSION_NUMBER<3007011
113 # define sqlite3_stricmp strcmp
117 ** Interpret zArg as an integer value, possibly with suffixes.
119 static int integerValue(const char *zArg
){
121 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
123 { "MiB", 1024*1024 },
124 { "GiB", 1024*1024*1024 },
127 { "GB", 1000000000 },
137 }else if( zArg
[0]=='+' ){
140 if( zArg
[0]=='0' && zArg
[1]=='x' ){
143 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
148 while( isdigit(zArg
[0]) ){
149 v
= v
*10 + zArg
[0] - '0';
153 for(i
=0; i
<sizeof(aMult
)/sizeof(aMult
[0]); i
++){
154 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
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
168 static sqlite3_vfs
*clockVfs
= 0;
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
);
178 clockVfs
->xCurrentTime(clockVfs
, &r
);
179 t
= (sqlite3_int64
)(r
*86400000.0);
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;
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
){
198 out
= (out
<<1) | (in
&1);
205 /* Round up a number so that it is a power of two minus one
207 unsigned roundup_allones(unsigned limit
){
209 while( m
<limit
) m
= (m
<<1)+1;
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().
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" };
231 i
+= speedtest1_numbername(n
/1000000000, zOut
+i
, nOut
-i
);
232 sqlite3_snprintf(nOut
-i
, zOut
+i
, " billion");
233 i
+= (int)strlen(zOut
+i
);
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
);
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
);
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
);
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
);
263 if( i
&& i
<nOut
-1 ) zOut
[i
++] = ' ';
264 sqlite3_snprintf(nOut
-i
, zOut
+i
, "%s", ones
[n
]);
265 i
+= (int)strlen(zOut
+i
);
268 sqlite3_snprintf(nOut
-i
, zOut
+i
, "zero");
269 i
+= (int)strlen(zOut
+i
);
275 /* Start a new test case */
277 static const char zDots
[] =
278 ".......................................................................";
279 void speedtest1_begin_test(int iTestNum
, const char *zTestName
, ...){
280 int n
= (int)strlen(zTestName
);
283 va_start(ap
, zTestName
);
284 zName
= sqlite3_vmprintf(zTestName
, ap
);
286 n
= (int)strlen(zName
);
288 zName
[NAMEWIDTH
] = 0;
292 printf("/* %4d - %s%.*s */\n", iTestNum
, zName
, NAMEWIDTH
-n
, zDots
);
294 printf("%4d - %s%.*s ", iTestNum
, zName
, NAMEWIDTH
-n
, zDots
);
299 g
.iStart
= speedtest1_timestamp();
304 /* Complete a test case */
305 void speedtest1_end_test(void){
306 sqlite3_int64 iElapseTime
= speedtest1_timestamp() - g
.iStart
;
308 g
.iTotal
+= iElapseTime
;
309 printf("%4d.%03ds\n", (int)(iElapseTime
/1000), (int)(iElapseTime
%1000));
312 sqlite3_finalize(g
.pStmt
);
317 /* Report end of testing */
318 void speedtest1_final(void){
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
);
332 #if SQLITE_VERSION_NUMBER>=3007017
333 && ( sqlite3_strglob("CREATE *", zSql
)==0
334 || sqlite3_strglob("DROP *", zSql
)==0
335 || sqlite3_strglob("ALTER *", zSql
)==0
339 printf("%.*s;\n", n
, zSql
);
343 /* Shrink memory used, if appropriate and if the SQLite version is capable
346 void speedtest1_shrink_memory(void){
347 #if SQLITE_VERSION_NUMBER>=3007010
348 if( g
.bMemShrink
) sqlite3_db_release_memory(g
.db
);
353 void speedtest1_exec(const char *zFormat
, ...){
356 va_start(ap
, zFormat
);
357 zSql
= sqlite3_vmprintf(zFormat
, ap
);
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
));
368 speedtest1_shrink_memory();
371 /* Prepare an SQL statement */
372 void speedtest1_prepare(const char *zFormat
, ...){
375 va_start(ap
, zFormat
);
376 zSql
= sqlite3_vmprintf(zFormat
, ap
);
382 if( g
.pStmt
) sqlite3_finalize(g
.pStmt
);
383 rc
= sqlite3_prepare_v2(g
.db
, zSql
, -1, &g
.pStmt
, 0);
385 fatal_error("SQL error: %s\n", sqlite3_errmsg(g
.db
));
391 /* Run an SQL statement previously prepared */
392 void speedtest1_run(void){
394 if( g
.bSqlOnly
) return;
397 while( sqlite3_step(g
.pStmt
)==SQLITE_ROW
){
398 n
= sqlite3_column_count(g
.pStmt
);
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);
410 #if SQLITE_VERSION_NUMBER>=3006001
413 sqlite3_prepare_v2(g
.db
, sqlite3_sql(g
.pStmt
), -1, &pNew
, 0);
414 sqlite3_finalize(g
.pStmt
);
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
,
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
){
446 for(n
=0; y0
>0 && n
<10; n
++){
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
);
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
);
481 speedtest1_exec("COMMIT");
482 speedtest1_end_test();
486 speedtest1_begin_test(110, "%d ordered INSERTS with one index/PK", n
);
487 speedtest1_exec("BEGIN");
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
);
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
);
500 speedtest1_exec("COMMIT");
501 speedtest1_end_test();
505 speedtest1_begin_test(120, "%d unordered INSERTS with one index/PK", n
);
506 speedtest1_exec("BEGIN");
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
);
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
);
519 speedtest1_exec("COMMIT");
520 speedtest1_end_test();
524 speedtest1_begin_test(130, "%d SELECTS, numeric BETWEEN, unindexed", n
);
525 speedtest1_exec("BEGIN");
527 "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
528 " WHERE b BETWEEN ?1 AND ?2; -- %d times", n
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
);
537 speedtest1_exec("COMMIT");
538 speedtest1_end_test();
542 speedtest1_begin_test(140, "%d SELECTS, LIKE, unindexed", n
);
543 speedtest1_exec("BEGIN");
545 "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
546 " WHERE c LIKE ?1; -- %d times", n
549 x1
= speedtest1_random()%maxb
;
551 len
= speedtest1_numbername(i
, zNum
+1, sizeof(zNum
)-2);
554 sqlite3_bind_text(g
.pStmt
, 1, zNum
, len
, SQLITE_STATIC
);
557 speedtest1_exec("COMMIT");
558 speedtest1_end_test();
562 speedtest1_begin_test(142, "%d SELECTS w/ORDER BY, unindexed", n
);
563 speedtest1_exec("BEGIN");
565 "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n"
566 " ORDER BY a; -- %d times", n
569 x1
= speedtest1_random()%maxb
;
571 len
= speedtest1_numbername(i
, zNum
+1, sizeof(zNum
)-2);
574 sqlite3_bind_text(g
.pStmt
, 1, zNum
, len
, SQLITE_STATIC
);
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");
584 "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n"
585 " ORDER BY a LIMIT 10; -- %d times", n
588 x1
= speedtest1_random()%maxb
;
590 len
= speedtest1_numbername(i
, zNum
+1, sizeof(zNum
)-2);
593 sqlite3_bind_text(g
.pStmt
, 1, zNum
, len
, SQLITE_STATIC
);
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();
612 speedtest1_begin_test(160, "%d SELECTS, numeric BETWEEN, indexed", n
);
613 speedtest1_exec("BEGIN");
615 "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
616 " WHERE b BETWEEN ?1 AND ?2; -- %d times", n
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
);
625 speedtest1_exec("COMMIT");
626 speedtest1_end_test();
630 speedtest1_begin_test(161, "%d SELECTS, numeric BETWEEN, PK", n
);
631 speedtest1_exec("BEGIN");
633 "SELECT count(*), avg(b), sum(length(c)) FROM t2\n"
634 " WHERE a BETWEEN ?1 AND ?2; -- %d times", n
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
);
643 speedtest1_exec("COMMIT");
644 speedtest1_end_test();
648 speedtest1_begin_test(170, "%d SELECTS, text BETWEEN, indexed", n
);
649 speedtest1_exec("BEGIN");
651 "SELECT count(*), avg(b), sum(length(c)) FROM t1\n"
652 " WHERE c BETWEEN ?1 AND (?1||'~'); -- %d times", n
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
);
660 speedtest1_exec("COMMIT");
661 speedtest1_end_test();
664 speedtest1_begin_test(180, "%d INSERTS with three indexes", n
);
665 speedtest1_exec("BEGIN");
667 "CREATE%s TABLE t4(\n"
668 " a INTEGER %s %s,\n"
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();
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();
698 speedtest1_begin_test(230, "%d UPDATES, numeric BETWEEN, indexed", n
);
699 speedtest1_exec("BEGIN");
701 "UPDATE t2 SET d=b*2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n
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
);
710 speedtest1_exec("COMMIT");
711 speedtest1_end_test();
715 speedtest1_begin_test(240, "%d UPDATES of individual rows", n
);
716 speedtest1_exec("BEGIN");
718 "UPDATE t2 SET d=b*3 WHERE a=?1; -- %d times", n
721 x1
= speedtest1_random()%sz
+ 1;
722 sqlite3_bind_int(g
.pStmt
, 1, x1
);
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();
740 speedtest1_begin_test(270, "%d DELETEs, numeric BETWEEN, indexed", n
);
741 speedtest1_exec("BEGIN");
743 "DELETE FROM t2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n
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
);
752 speedtest1_exec("COMMIT");
753 speedtest1_end_test();
757 speedtest1_begin_test(280, "%d DELETEs of individual rows", n
);
758 speedtest1_exec("BEGIN");
760 "DELETE FROM t3 WHERE a=?1; -- %d times", n
763 x1
= speedtest1_random()%sz
+ 1;
764 sqlite3_bind_int(g
.pStmt
, 1, x1
);
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();
786 speedtest1_begin_test(310, "%d four-ways joins", n
);
787 speedtest1_exec("BEGIN");
789 "SELECT t1.c FROM t1, t2, t3, t4\n"
790 " WHERE t4.a BETWEEN ?1 AND ?2\n"
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
);
802 speedtest1_exec("COMMIT");
803 speedtest1_end_test();
805 speedtest1_begin_test(320, "subquery in result set", n
);
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);
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
[] = {
870 }else if( g
.szTest
<70 ){
875 speedtest1_begin_test(100, "Sudoku with recursive 'digits'");
878 " input(sud) AS (VALUES(?1)),\n"
879 " digits(z,lp) AS (\n"
882 " SELECT CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9\n"
885 " SELECT sud, instr(sud, '.') FROM input\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"
892 " AND NOT EXISTS (\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"
902 "SELECT s FROM x WHERE ind=0;"
904 sqlite3_bind_text(g
.pStmt
, 1, zPuz
, -1, SQLITE_STATIC
);
906 speedtest1_end_test();
908 speedtest1_begin_test(200, "Sudoku with VALUES 'digits'");
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"
915 " SELECT sud, instr(sud, '.') FROM input\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"
922 " AND NOT EXISTS (\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"
932 "SELECT s FROM x WHERE ind=0;"
934 sqlite3_bind_text(g
.pStmt
, 1, zPuz
, -1, SQLITE_STATIC
);
936 speedtest1_end_test();
938 rSpacing
= 5.0/g
.szTest
;
939 speedtest1_begin_test(300, "Mandelbrot Set with spacing=%f", rSpacing
);
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"
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"
950 " m2(iter, cx, cy) AS (\n"
951 " SELECT max(iter), cx, cy FROM m GROUP BY cx, cy\n"
954 " SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') \n"
955 " FROM m2 GROUP BY cy\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
);
962 speedtest1_end_test();
964 nElem
= 10000*g
.szTest
;
965 speedtest1_begin_test(400, "EXCEPT operator on %d-element tables", nElem
);
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"
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
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
;
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;
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
,
1020 *pRes
= aCoord
[3]>=p
->aParam
[0] && aCoord
[2]<=p
->aParam
[1];
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
){
1032 unsigned x0
, x1
, y0
, y1
, z0
, z1
;
1034 int *aCheck
= sqlite3_malloc( sizeof(int)*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
);
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();
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");
1069 sqlite3_bind_int(g
.pStmt
, 1, i
*iStep
);
1070 sqlite3_bind_int(g
.pStmt
, 2, (i
+1)*iStep
);
1072 aCheck
[i
] = atoi(g
.zResult
);
1074 speedtest1_end_test();
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");
1082 sqlite3_bind_int(g
.pStmt
, 1, i
*iStep
);
1083 sqlite3_bind_int(g
.pStmt
, 2, (i
+1)*iStep
);
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();
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");
1098 sqlite3_bind_int(g
.pStmt
, 1, i
*iStep
);
1099 sqlite3_bind_int(g
.pStmt
, 2, (i
+1)*iStep
);
1101 aCheck
[i
] = atoi(g
.zResult
);
1103 speedtest1_end_test();
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");
1111 sqlite3_bind_int(g
.pStmt
, 1, i
*iStep
);
1112 sqlite3_bind_int(g
.pStmt
, 2, (i
+1)*iStep
);
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();
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)");
1129 sqlite3_bind_int(g
.pStmt
, 1, i
*iStep
);
1130 sqlite3_bind_int(g
.pStmt
, 2, (i
+1)*iStep
);
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();
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");
1145 sqlite3_bind_int(g
.pStmt
, 1, i
*iStep
);
1146 sqlite3_bind_int(g
.pStmt
, 2, (i
+1)*iStep
);
1148 aCheck
[i
] = atoi(g
.zResult
);
1150 speedtest1_end_test();
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
);
1159 speedtest1_end_test();
1161 #endif /* SQLITE_ENABLE_RTREE */
1164 ** A testset used for debugging speedtest1 itself.
1166 void testset_debug1(void){
1169 char zNum
[2000]; /* A number name */
1172 for(i
=1; i
<=n
; i
++){
1174 x2
= swizzle(x1
, n
);
1175 speedtest1_numbername(x1
, zNum
, sizeof(zNum
));
1176 printf("%5d %5d %5d %s\n", i
, x1
, x2
, zNum
);
1181 #include <sys/types.h>
1185 ** Attempt to display I/O stats on Linux using /proc/PID/io
1187 static void displayLinuxIoStats(FILE *out
){
1190 sqlite3_snprintf(sizeof(z
), z
, "/proc/%d/io", getpid());
1191 in
= fopen(z
, "rb");
1193 while( fgets(z
, sizeof(z
), in
)!=0 ){
1194 static const struct {
1195 const char *zPattern
;
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:" },
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
]);
1219 #if SQLITE_VERSION_NUMBER<3006018
1220 # define sqlite3_sourceid(X) "(before 3.6.18)"
1223 int main(int argc
, char **argv
){
1224 int doAutovac
= 0; /* True for --autovacuum */
1225 int cacheSize
= 0; /* Desired cache size. 0 means default */
1226 int doExclusive
= 0; /* True for --exclusive */
1227 int nHeap
= 0, mnHeap
= 0; /* Heap size from --heap */
1228 int doIncrvac
= 0; /* True for --incrvacuum */
1229 const char *zJMode
= 0; /* Journal mode */
1230 const char *zKey
= 0; /* Encryption key */
1231 int nLook
= 0, szLook
= 0; /* --lookaside configuration */
1232 int noSync
= 0; /* True for --nosync */
1233 int pageSize
= 0; /* Desired page size. 0 means default */
1234 int nPCache
= 0, szPCache
= 0;/* --pcache configuration */
1235 int doPCache
= 0; /* True if --pcache is seen */
1236 int nScratch
= 0, szScratch
=0;/* --scratch configuration */
1237 int showStats
= 0; /* True for --stats */
1238 int nThread
= 0; /* --threads value */
1239 const char *zTSet
= "main"; /* Which --testset torun */
1240 int doTrace
= 0; /* True for --trace */
1241 const char *zEncoding
= 0; /* --utf16be or --utf16le */
1242 const char *zDbName
= 0; /* Name of the test database */
1244 void *pHeap
= 0; /* Allocated heap space */
1245 void *pLook
= 0; /* Allocated lookaside space */
1246 void *pPCache
= 0; /* Allocated storage for pcache */
1247 void *pScratch
= 0; /* Allocated storage for scratch */
1248 int iCur
, iHi
; /* Stats values, current and "highwater" */
1249 int i
; /* Loop counter */
1250 int rc
; /* API return code */
1252 /* Display the version of SQLite being tested */
1253 printf("-- Speedtest1 for SQLite %s %.50s\n",
1254 sqlite3_libversion(), sqlite3_sourceid());
1256 /* Process command-line arguments */
1261 for(i
=1; i
<argc
; i
++){
1262 const char *z
= argv
[i
];
1264 do{ z
++; }while( z
[0]=='-' );
1265 if( strcmp(z
,"autovacuum")==0 ){
1267 }else if( strcmp(z
,"cachesize")==0 ){
1268 if( i
>=argc
-1 ) fatal_error("missing argument on %s\n", argv
[i
]);
1270 cacheSize
= integerValue(argv
[i
]);
1271 }else if( strcmp(z
,"exclusive")==0 ){
1273 }else if( strcmp(z
,"explain")==0 ){
1276 }else if( strcmp(z
,"heap")==0 ){
1277 if( i
>=argc
-2 ) fatal_error("missing arguments on %s\n", argv
[i
]);
1278 nHeap
= integerValue(argv
[i
+1]);
1279 mnHeap
= integerValue(argv
[i
+2]);
1281 }else if( strcmp(z
,"incrvacuum")==0 ){
1283 }else if( strcmp(z
,"journal")==0 ){
1284 if( i
>=argc
-1 ) fatal_error("missing argument on %s\n", argv
[i
]);
1286 }else if( strcmp(z
,"key")==0 ){
1287 if( i
>=argc
-1 ) fatal_error("missing argument on %s\n", argv
[i
]);
1289 }else if( strcmp(z
,"lookaside")==0 ){
1290 if( i
>=argc
-2 ) fatal_error("missing arguments on %s\n", argv
[i
]);
1291 nLook
= integerValue(argv
[i
+1]);
1292 szLook
= integerValue(argv
[i
+2]);
1294 }else if( strcmp(z
,"multithread")==0 ){
1295 sqlite3_config(SQLITE_CONFIG_MULTITHREAD
);
1296 }else if( strcmp(z
,"nomemstat")==0 ){
1297 sqlite3_config(SQLITE_CONFIG_MEMSTATUS
, 0);
1298 }else if( strcmp(z
,"nosync")==0 ){
1300 }else if( strcmp(z
,"notnull")==0 ){
1302 #ifdef SQLITE_ENABLE_RBU
1303 }else if( strcmp(z
,"rbu")==0 ){
1304 sqlite3ota_create_vfs("rbu", 0);
1305 sqlite3_vfs_register(sqlite3_vfs_find("rbu"), 1);
1307 }else if( strcmp(z
,"pagesize")==0 ){
1308 if( i
>=argc
-1 ) fatal_error("missing argument on %s\n", argv
[i
]);
1309 pageSize
= integerValue(argv
[++i
]);
1310 }else if( strcmp(z
,"pcache")==0 ){
1311 if( i
>=argc
-2 ) fatal_error("missing arguments on %s\n", argv
[i
]);
1312 nPCache
= integerValue(argv
[i
+1]);
1313 szPCache
= integerValue(argv
[i
+2]);
1316 }else if( strcmp(z
,"primarykey")==0 ){
1317 g
.zPK
= "PRIMARY KEY";
1318 }else if( strcmp(z
,"reprepare")==0 ){
1320 }else if( strcmp(z
,"scratch")==0 ){
1321 if( i
>=argc
-2 ) fatal_error("missing arguments on %s\n", argv
[i
]);
1322 nScratch
= integerValue(argv
[i
+1]);
1323 szScratch
= integerValue(argv
[i
+2]);
1325 }else if( strcmp(z
,"serialized")==0 ){
1326 sqlite3_config(SQLITE_CONFIG_SERIALIZED
);
1327 }else if( strcmp(z
,"singlethread")==0 ){
1328 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD
);
1329 }else if( strcmp(z
,"sqlonly")==0 ){
1331 }else if( strcmp(z
,"shrink-memory")==0 ){
1333 }else if( strcmp(z
,"size")==0 ){
1334 if( i
>=argc
-1 ) fatal_error("missing argument on %s\n", argv
[i
]);
1335 g
.szTest
= integerValue(argv
[++i
]);
1336 }else if( strcmp(z
,"stats")==0 ){
1338 }else if( strcmp(z
,"temp")==0 ){
1339 if( i
>=argc
-1 ) fatal_error("missing argument on %s\n", argv
[i
]);
1341 if( argv
[i
][0]<'0' || argv
[i
][0]>'9' || argv
[i
][1]!=0 ){
1342 fatal_error("argument to --temp should be integer between 0 and 9");
1344 g
.eTemp
= argv
[i
][0] - '0';
1345 }else if( strcmp(z
,"testset")==0 ){
1346 if( i
>=argc
-1 ) fatal_error("missing argument on %s\n", argv
[i
]);
1348 }else if( strcmp(z
,"trace")==0 ){
1350 }else if( strcmp(z
,"threads")==0 ){
1351 if( i
>=argc
-1 ) fatal_error("missing argument on %s\n", argv
[i
]);
1352 nThread
= integerValue(argv
[++i
]);
1353 }else if( strcmp(z
,"utf16le")==0 ){
1354 zEncoding
= "utf16le";
1355 }else if( strcmp(z
,"utf16be")==0 ){
1356 zEncoding
= "utf16be";
1357 }else if( strcmp(z
,"verify")==0 ){
1359 }else if( strcmp(z
,"without-rowid")==0 ){
1360 g
.zWR
= "WITHOUT ROWID";
1361 g
.zPK
= "PRIMARY KEY";
1362 }else if( strcmp(z
, "help")==0 || strcmp(z
,"?")==0 ){
1363 printf(zHelp
, argv
[0]);
1366 fatal_error("unknown option: %s\nUse \"%s -?\" for help\n",
1369 }else if( zDbName
==0 ){
1372 fatal_error("surplus argument: %s\nUse \"%s -?\" for help\n",
1376 if( zDbName
!=0 ) unlink(zDbName
);
1377 #if SQLITE_VERSION_NUMBER>=3006001
1379 pHeap
= malloc( nHeap
);
1380 if( pHeap
==0 ) fatal_error("cannot allocate %d-byte heap\n", nHeap
);
1381 rc
= sqlite3_config(SQLITE_CONFIG_HEAP
, pHeap
, nHeap
, mnHeap
);
1382 if( rc
) fatal_error("heap configuration failed: %d\n", rc
);
1385 if( nPCache
>0 && szPCache
>0 ){
1386 pPCache
= malloc( nPCache
*(sqlite3_int64
)szPCache
);
1387 if( pPCache
==0 ) fatal_error("cannot allocate %lld-byte pcache\n",
1388 nPCache
*(sqlite3_int64
)szPCache
);
1390 rc
= sqlite3_config(SQLITE_CONFIG_PAGECACHE
, pPCache
, szPCache
, nPCache
);
1391 if( rc
) fatal_error("pcache configuration failed: %d\n", rc
);
1393 if( nScratch
>0 && szScratch
>0 ){
1394 pScratch
= malloc( nScratch
*(sqlite3_int64
)szScratch
);
1395 if( pScratch
==0 ) fatal_error("cannot allocate %lld-byte scratch\n",
1396 nScratch
*(sqlite3_int64
)szScratch
);
1397 rc
= sqlite3_config(SQLITE_CONFIG_SCRATCH
, pScratch
, szScratch
, nScratch
);
1398 if( rc
) fatal_error("scratch configuration failed: %d\n", rc
);
1401 sqlite3_config(SQLITE_CONFIG_LOOKASIDE
, 0, 0);
1405 /* Open the database and the input file */
1406 if( sqlite3_open(zDbName
, &g
.db
) ){
1407 fatal_error("Cannot open database file: %s\n", zDbName
);
1409 #if SQLITE_VERSION_NUMBER>=3006001
1410 if( nLook
>0 && szLook
>0 ){
1411 pLook
= malloc( nLook
*szLook
);
1412 rc
= sqlite3_db_config(g
.db
, SQLITE_DBCONFIG_LOOKASIDE
, pLook
, szLook
,nLook
);
1413 if( rc
) fatal_error("lookaside configuration failed: %d\n", rc
);
1417 /* Set database connection options */
1418 sqlite3_create_function(g
.db
, "random", 0, SQLITE_UTF8
, 0, randomFunc
, 0, 0);
1419 if( doTrace
) sqlite3_trace(g
.db
, traceCallback
, 0);
1420 speedtest1_exec("PRAGMA threads=%d", nThread
);
1422 speedtest1_exec("PRAGMA key('%s')", zKey
);
1425 speedtest1_exec("PRAGMA encoding=%s", zEncoding
);
1428 speedtest1_exec("PRAGMA auto_vacuum=FULL");
1429 }else if( doIncrvac
){
1430 speedtest1_exec("PRAGMA auto_vacuum=INCREMENTAL");
1433 speedtest1_exec("PRAGMA page_size=%d", pageSize
);
1436 speedtest1_exec("PRAGMA cache_size=%d", cacheSize
);
1438 if( noSync
) speedtest1_exec("PRAGMA synchronous=OFF");
1440 speedtest1_exec("PRAGMA locking_mode=EXCLUSIVE");
1443 speedtest1_exec("PRAGMA journal_mode=%s", zJMode
);
1446 if( g
.bExplain
) printf(".explain\n.echo on\n");
1447 if( strcmp(zTSet
,"main")==0 ){
1449 }else if( strcmp(zTSet
,"debug1")==0 ){
1451 }else if( strcmp(zTSet
,"cte")==0 ){
1453 }else if( strcmp(zTSet
,"rtree")==0 ){
1454 #ifdef SQLITE_ENABLE_RTREE
1455 testset_rtree(6, 147);
1457 fatal_error("compile with -DSQLITE_ENABLE_RTREE to enable "
1458 "the R-Tree tests\n");
1461 fatal_error("unknown testset: \"%s\"\nChoices: main debug1 cte rtree\n",
1466 /* Database connection statistics printed after both prepared statements
1467 ** have been finalized */
1468 #if SQLITE_VERSION_NUMBER>=3007009
1470 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_LOOKASIDE_USED
, &iCur
, &iHi
, 0);
1471 printf("-- Lookaside Slots Used: %d (max %d)\n", iCur
,iHi
);
1472 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_LOOKASIDE_HIT
, &iCur
, &iHi
, 0);
1473 printf("-- Successful lookasides: %d\n", iHi
);
1474 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE
, &iCur
,&iHi
,0);
1475 printf("-- Lookaside size faults: %d\n", iHi
);
1476 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL
, &iCur
,&iHi
,0);
1477 printf("-- Lookaside OOM faults: %d\n", iHi
);
1478 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_CACHE_USED
, &iCur
, &iHi
, 0);
1479 printf("-- Pager Heap Usage: %d bytes\n", iCur
);
1480 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_CACHE_HIT
, &iCur
, &iHi
, 1);
1481 printf("-- Page cache hits: %d\n", iCur
);
1482 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_CACHE_MISS
, &iCur
, &iHi
, 1);
1483 printf("-- Page cache misses: %d\n", iCur
);
1484 #if SQLITE_VERSION_NUMBER>=3007012
1485 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_CACHE_WRITE
, &iCur
, &iHi
, 1);
1486 printf("-- Page cache writes: %d\n", iCur
);
1488 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_SCHEMA_USED
, &iCur
, &iHi
, 0);
1489 printf("-- Schema Heap Usage: %d bytes\n", iCur
);
1490 sqlite3_db_status(g
.db
, SQLITE_DBSTATUS_STMT_USED
, &iCur
, &iHi
, 0);
1491 printf("-- Statement Heap Usage: %d bytes\n", iCur
);
1495 sqlite3_close(g
.db
);
1497 #if SQLITE_VERSION_NUMBER>=3006001
1498 /* Global memory usage statistics printed after the database connection
1499 ** has closed. Memory usage should be zero at this point. */
1501 sqlite3_status(SQLITE_STATUS_MEMORY_USED
, &iCur
, &iHi
, 0);
1502 printf("-- Memory Used (bytes): %d (max %d)\n", iCur
,iHi
);
1503 #if SQLITE_VERSION_NUMBER>=3007000
1504 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT
, &iCur
, &iHi
, 0);
1505 printf("-- Outstanding Allocations: %d (max %d)\n", iCur
,iHi
);
1507 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW
, &iCur
, &iHi
, 0);
1508 printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur
,iHi
);
1509 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW
, &iCur
, &iHi
, 0);
1510 printf("-- Scratch Overflow Bytes: %d (max %d)\n", iCur
,iHi
);
1511 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE
, &iCur
, &iHi
, 0);
1512 printf("-- Largest Allocation: %d bytes\n",iHi
);
1513 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE
, &iCur
, &iHi
, 0);
1514 printf("-- Largest Pcache Allocation: %d bytes\n",iHi
);
1515 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE
, &iCur
, &iHi
, 0);
1516 printf("-- Largest Scratch Allocation: %d bytes\n", iHi
);
1522 displayLinuxIoStats(stdout
);
1526 /* Release memory */