blob: 02d55d5974f251e9f6b1a01f290425a05f47b39a [file] [log] [blame]
drhad1ca9a2013-11-23 04:16:58 +00001/*
2** A program for performance testing.
3**
4** The available command-line options are described below:
5*/
6static 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"
drh849a9d92013-12-21 15:46:06 +000012 " --explain Like --sqlonly but with added EXPLAIN keywords\n"
drh93307e92013-11-24 01:14:14 +000013 " --heap SZ MIN Memory allocator uses SZ bytes & min allocation MIN\n"
drhad1ca9a2013-11-23 04:16:58 +000014 " --incrvacuum Enable incremenatal vacuum mode\n"
drh1dae26b2015-02-03 19:20:03 +000015 " --journal M Set the journal_mode to M\n"
drhad1ca9a2013-11-23 04:16:58 +000016 " --key KEY Set the encryption key to KEY\n"
17 " --lookaside N SZ Configure lookaside for N slots of SZ bytes each\n"
drh26f41982016-12-12 23:24:08 +000018 " --mmap SZ MMAP the first SZ bytes of the database file\n"
drh2e43e962015-07-03 14:34:25 +000019 " --multithread Set multithreaded mode\n"
20 " --nomemstat Disable memory statistics\n"
drhad1ca9a2013-11-23 04:16:58 +000021 " --nosync Set PRAGMA synchronous=OFF\n"
22 " --notnull Add NOT NULL constraints to table columns\n"
23 " --pagesize N Set the page size to N\n"
24 " --pcache N SZ Configure N pages of pagecache each of size SZ bytes\n"
25 " --primarykey Use PRIMARY KEY instead of UNIQUE where appropriate\n"
drhf8a89ca2016-10-18 14:35:55 +000026 " --repeat N Repeat each SELECT N times (default: 1)\n"
drhad1ca9a2013-11-23 04:16:58 +000027 " --reprepare Reprepare each statement upon every invocation\n"
drh93307e92013-11-24 01:14:14 +000028 " --scratch N SZ Configure scratch memory for N slots of SZ bytes each\n"
drh2e43e962015-07-03 14:34:25 +000029 " --serialized Set serialized threading mode\n"
30 " --singlethread Set single-threaded mode - disables all mutexing\n"
drhad1ca9a2013-11-23 04:16:58 +000031 " --sqlonly No-op. Only show the SQL that would have been run.\n"
drh0d847182015-07-02 01:38:39 +000032 " --shrink-memory Invoke sqlite3_db_release_memory() frequently.\n"
drhad1ca9a2013-11-23 04:16:58 +000033 " --size N Relative test size. Default=100\n"
34 " --stats Show statistics at the end\n"
drh2160ca52016-04-12 16:59:39 +000035 " --temp N N from 0 to 9. 0: no temp table. 9: all temp tables\n"
drhad1ca9a2013-11-23 04:16:58 +000036 " --testset T Run test-set T\n"
37 " --trace Turn on SQL tracing\n"
drh46a06bb2014-04-18 13:57:39 +000038 " --threads N Use up to N threads for sorting\n"
drhad1ca9a2013-11-23 04:16:58 +000039 " --utf16be Set text encoding to UTF-16BE\n"
40 " --utf16le Set text encoding to UTF-16LE\n"
drh65e6b0d2014-04-28 17:56:19 +000041 " --verify Run additional verification steps.\n"
drhad1ca9a2013-11-23 04:16:58 +000042 " --without-rowid Use WITHOUT ROWID where appropriate\n"
43;
44
45
46#include "sqlite3.h"
47#include <assert.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <stdarg.h>
51#include <string.h>
52#include <ctype.h>
drheb257132016-12-31 14:33:05 +000053#ifndef _WIN32
54# include <unistd.h>
55#else
56# include <io.h>
57#endif
drhc56fac72015-10-29 13:48:15 +000058#define ISSPACE(X) isspace((unsigned char)(X))
59#define ISDIGIT(X) isdigit((unsigned char)(X))
drhad1ca9a2013-11-23 04:16:58 +000060
drh43076902015-06-18 15:26:09 +000061#if SQLITE_VERSION_NUMBER<3005000
62# define sqlite3_int64 sqlite_int64
63#endif
drhcfb8f8d2015-07-23 20:44:49 +000064#ifdef SQLITE_ENABLE_RBU
65# include "sqlite3rbu.h"
dan54fc2142015-03-05 16:21:20 +000066#endif
67
drhad1ca9a2013-11-23 04:16:58 +000068/* All global state is held in this structure */
69static struct Global {
70 sqlite3 *db; /* The open database connection */
71 sqlite3_stmt *pStmt; /* Current SQL statement */
72 sqlite3_int64 iStart; /* Start-time for the current test */
73 sqlite3_int64 iTotal; /* Total time */
74 int bWithoutRowid; /* True for --without-rowid */
75 int bReprepare; /* True to reprepare the SQL on each rerun */
76 int bSqlOnly; /* True to print the SQL once only */
drh849a9d92013-12-21 15:46:06 +000077 int bExplain; /* Print SQL with EXPLAIN prefix */
drh65e6b0d2014-04-28 17:56:19 +000078 int bVerify; /* Try to verify that results are correct */
drh0d847182015-07-02 01:38:39 +000079 int bMemShrink; /* Call sqlite3_db_release_memory() often */
drh2160ca52016-04-12 16:59:39 +000080 int eTemp; /* 0: no TEMP. 9: always TEMP. */
drhad1ca9a2013-11-23 04:16:58 +000081 int szTest; /* Scale factor for test iterations */
drhf8a89ca2016-10-18 14:35:55 +000082 int nRepeat; /* Repeat selects this many times */
drhad1ca9a2013-11-23 04:16:58 +000083 const char *zWR; /* Might be WITHOUT ROWID */
84 const char *zNN; /* Might be NOT NULL */
85 const char *zPK; /* Might be UNIQUE or PRIMARY KEY */
86 unsigned int x, y; /* Pseudo-random number generator state */
87 int nResult; /* Size of the current result */
88 char zResult[3000]; /* Text of the current result */
89} g;
90
drh2160ca52016-04-12 16:59:39 +000091/* Return " TEMP" or "", as appropriate for creating a table.
92*/
93static const char *isTemp(int N){
94 return g.eTemp>=N ? " TEMP" : "";
95}
96
drhad1ca9a2013-11-23 04:16:58 +000097
drh93307e92013-11-24 01:14:14 +000098/* Print an error message and exit */
99static void fatal_error(const char *zMsg, ...){
100 va_list ap;
101 va_start(ap, zMsg);
102 vfprintf(stderr, zMsg, ap);
103 va_end(ap);
104 exit(1);
drhad1ca9a2013-11-23 04:16:58 +0000105}
106
107/*
108** Return the value of a hexadecimal digit. Return -1 if the input
109** is not a hex digit.
110*/
111static int hexDigitValue(char c){
112 if( c>='0' && c<='9' ) return c - '0';
113 if( c>='a' && c<='f' ) return c - 'a' + 10;
114 if( c>='A' && c<='F' ) return c - 'A' + 10;
115 return -1;
116}
117
drh290ea402013-12-01 18:10:01 +0000118/* Provide an alternative to sqlite3_stricmp() in older versions of
119** SQLite */
120#if SQLITE_VERSION_NUMBER<3007011
121# define sqlite3_stricmp strcmp
122#endif
123
drhad1ca9a2013-11-23 04:16:58 +0000124/*
125** Interpret zArg as an integer value, possibly with suffixes.
126*/
drh93307e92013-11-24 01:14:14 +0000127static int integerValue(const char *zArg){
drhad1ca9a2013-11-23 04:16:58 +0000128 sqlite3_int64 v = 0;
129 static const struct { char *zSuffix; int iMult; } aMult[] = {
130 { "KiB", 1024 },
131 { "MiB", 1024*1024 },
132 { "GiB", 1024*1024*1024 },
133 { "KB", 1000 },
134 { "MB", 1000000 },
135 { "GB", 1000000000 },
136 { "K", 1000 },
137 { "M", 1000000 },
138 { "G", 1000000000 },
139 };
140 int i;
141 int isNeg = 0;
142 if( zArg[0]=='-' ){
143 isNeg = 1;
144 zArg++;
145 }else if( zArg[0]=='+' ){
146 zArg++;
147 }
148 if( zArg[0]=='0' && zArg[1]=='x' ){
149 int x;
150 zArg += 2;
151 while( (x = hexDigitValue(zArg[0]))>=0 ){
152 v = (v<<4) + x;
153 zArg++;
154 }
155 }else{
156 while( isdigit(zArg[0]) ){
157 v = v*10 + zArg[0] - '0';
158 zArg++;
159 }
160 }
161 for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
162 if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
163 v *= aMult[i].iMult;
164 break;
165 }
166 }
mistachkinb87875a2013-11-27 18:00:20 +0000167 if( v>0x7fffffff ) fatal_error("parameter too large - max 2147483648");
drhdcb5fa02013-11-27 14:50:51 +0000168 return (int)(isNeg? -v : v);
drhad1ca9a2013-11-23 04:16:58 +0000169}
170
171/* Return the current wall-clock time, in milliseconds */
172sqlite3_int64 speedtest1_timestamp(void){
drh43076902015-06-18 15:26:09 +0000173#if SQLITE_VERSION_NUMBER<3005000
174 return 0;
175#else
drhad1ca9a2013-11-23 04:16:58 +0000176 static sqlite3_vfs *clockVfs = 0;
177 sqlite3_int64 t;
178 if( clockVfs==0 ) clockVfs = sqlite3_vfs_find(0);
drhd79e9c52013-12-02 01:24:05 +0000179#if SQLITE_VERSION_NUMBER>=3007000
drh290ea402013-12-01 18:10:01 +0000180 if( clockVfs->iVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){
drhad1ca9a2013-11-23 04:16:58 +0000181 clockVfs->xCurrentTimeInt64(clockVfs, &t);
drhd79e9c52013-12-02 01:24:05 +0000182 }else
183#endif
184 {
drhad1ca9a2013-11-23 04:16:58 +0000185 double r;
186 clockVfs->xCurrentTime(clockVfs, &r);
187 t = (sqlite3_int64)(r*86400000.0);
188 }
189 return t;
drh43076902015-06-18 15:26:09 +0000190#endif
drhad1ca9a2013-11-23 04:16:58 +0000191}
192
193/* Return a pseudo-random unsigned integer */
194unsigned int speedtest1_random(void){
195 g.x = (g.x>>1) ^ ((1+~(g.x&1)) & 0xd0000001);
196 g.y = g.y*1103515245 + 12345;
197 return g.x ^ g.y;
198}
199
200/* Map the value in within the range of 1...limit into another
201** number in a way that is chatic and invertable.
202*/
203unsigned swizzle(unsigned in, unsigned limit){
204 unsigned out = 0;
205 while( limit ){
206 out = (out<<1) | (in&1);
207 in >>= 1;
208 limit >>= 1;
209 }
210 return out;
211}
212
213/* Round up a number so that it is a power of two minus one
214*/
215unsigned roundup_allones(unsigned limit){
216 unsigned m = 1;
217 while( m<limit ) m = (m<<1)+1;
218 return m;
219}
220
221/* The speedtest1_numbername procedure below converts its argment (an integer)
222** into a string which is the English-language name for that number.
223** The returned string should be freed with sqlite3_free().
224**
225** Example:
226**
227** speedtest1_numbername(123) -> "one hundred twenty three"
228*/
229int speedtest1_numbername(unsigned int n, char *zOut, int nOut){
230 static const char *ones[] = { "zero", "one", "two", "three", "four", "five",
231 "six", "seven", "eight", "nine", "ten", "eleven", "twelve",
232 "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
233 "eighteen", "nineteen" };
234 static const char *tens[] = { "", "ten", "twenty", "thirty", "forty",
235 "fifty", "sixty", "seventy", "eighty", "ninety" };
236 int i = 0;
237
238 if( n>=1000000000 ){
239 i += speedtest1_numbername(n/1000000000, zOut+i, nOut-i);
240 sqlite3_snprintf(nOut-i, zOut+i, " billion");
241 i += (int)strlen(zOut+i);
242 n = n % 1000000000;
243 }
244 if( n>=1000000 ){
245 if( i && i<nOut-1 ) zOut[i++] = ' ';
246 i += speedtest1_numbername(n/1000000, zOut+i, nOut-i);
247 sqlite3_snprintf(nOut-i, zOut+i, " million");
248 i += (int)strlen(zOut+i);
249 n = n % 1000000;
250 }
251 if( n>=1000 ){
252 if( i && i<nOut-1 ) zOut[i++] = ' ';
253 i += speedtest1_numbername(n/1000, zOut+i, nOut-i);
254 sqlite3_snprintf(nOut-i, zOut+i, " thousand");
255 i += (int)strlen(zOut+i);
256 n = n % 1000;
257 }
258 if( n>=100 ){
259 if( i && i<nOut-1 ) zOut[i++] = ' ';
260 sqlite3_snprintf(nOut-i, zOut+i, "%s hundred", ones[n/100]);
261 i += (int)strlen(zOut+i);
262 n = n % 100;
263 }
264 if( n>=20 ){
265 if( i && i<nOut-1 ) zOut[i++] = ' ';
266 sqlite3_snprintf(nOut-i, zOut+i, "%s", tens[n/10]);
267 i += (int)strlen(zOut+i);
268 n = n % 10;
269 }
270 if( n>0 ){
271 if( i && i<nOut-1 ) zOut[i++] = ' ';
272 sqlite3_snprintf(nOut-i, zOut+i, "%s", ones[n]);
273 i += (int)strlen(zOut+i);
274 }
275 if( i==0 ){
276 sqlite3_snprintf(nOut-i, zOut+i, "zero");
277 i += (int)strlen(zOut+i);
278 }
279 return i;
280}
281
282
283/* Start a new test case */
284#define NAMEWIDTH 60
285static const char zDots[] =
286 ".......................................................................";
287void speedtest1_begin_test(int iTestNum, const char *zTestName, ...){
288 int n = (int)strlen(zTestName);
289 char *zName;
290 va_list ap;
291 va_start(ap, zTestName);
292 zName = sqlite3_vmprintf(zTestName, ap);
293 va_end(ap);
294 n = (int)strlen(zName);
295 if( n>NAMEWIDTH ){
296 zName[NAMEWIDTH] = 0;
297 n = NAMEWIDTH;
298 }
299 if( g.bSqlOnly ){
300 printf("/* %4d - %s%.*s */\n", iTestNum, zName, NAMEWIDTH-n, zDots);
301 }else{
302 printf("%4d - %s%.*s ", iTestNum, zName, NAMEWIDTH-n, zDots);
303 fflush(stdout);
304 }
305 sqlite3_free(zName);
306 g.nResult = 0;
307 g.iStart = speedtest1_timestamp();
drhdcb5fa02013-11-27 14:50:51 +0000308 g.x = 0xad131d0b;
309 g.y = 0x44f9eac8;
drhad1ca9a2013-11-23 04:16:58 +0000310}
311
312/* Complete a test case */
313void speedtest1_end_test(void){
314 sqlite3_int64 iElapseTime = speedtest1_timestamp() - g.iStart;
315 if( !g.bSqlOnly ){
316 g.iTotal += iElapseTime;
317 printf("%4d.%03ds\n", (int)(iElapseTime/1000), (int)(iElapseTime%1000));
318 }
319 if( g.pStmt ){
320 sqlite3_finalize(g.pStmt);
321 g.pStmt = 0;
322 }
323}
324
325/* Report end of testing */
326void speedtest1_final(void){
327 if( !g.bSqlOnly ){
328 printf(" TOTAL%.*s %4d.%03ds\n", NAMEWIDTH-5, zDots,
329 (int)(g.iTotal/1000), (int)(g.iTotal%1000));
330 }
331}
332
drh849a9d92013-12-21 15:46:06 +0000333/* Print an SQL statement to standard output */
334static void printSql(const char *zSql){
335 int n = (int)strlen(zSql);
drhc56fac72015-10-29 13:48:15 +0000336 while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ){ n--; }
drh849a9d92013-12-21 15:46:06 +0000337 if( g.bExplain ) printf("EXPLAIN ");
338 printf("%.*s;\n", n, zSql);
339 if( g.bExplain
drh5995e292015-06-18 12:37:32 +0000340#if SQLITE_VERSION_NUMBER>=3007017
drh25555502013-12-21 17:14:58 +0000341 && ( sqlite3_strglob("CREATE *", zSql)==0
342 || sqlite3_strglob("DROP *", zSql)==0
343 || sqlite3_strglob("ALTER *", zSql)==0
drh849a9d92013-12-21 15:46:06 +0000344 )
drh25555502013-12-21 17:14:58 +0000345#endif
drh849a9d92013-12-21 15:46:06 +0000346 ){
347 printf("%.*s;\n", n, zSql);
348 }
349}
350
drh0d847182015-07-02 01:38:39 +0000351/* Shrink memory used, if appropriate and if the SQLite version is capable
352** of doing so.
353*/
354void speedtest1_shrink_memory(void){
355#if SQLITE_VERSION_NUMBER>=3007010
356 if( g.bMemShrink ) sqlite3_db_release_memory(g.db);
357#endif
358}
359
drhad1ca9a2013-11-23 04:16:58 +0000360/* Run SQL */
361void speedtest1_exec(const char *zFormat, ...){
362 va_list ap;
363 char *zSql;
364 va_start(ap, zFormat);
365 zSql = sqlite3_vmprintf(zFormat, ap);
366 va_end(ap);
367 if( g.bSqlOnly ){
drh849a9d92013-12-21 15:46:06 +0000368 printSql(zSql);
drhad1ca9a2013-11-23 04:16:58 +0000369 }else{
drhe19f8322013-11-23 11:45:58 +0000370 char *zErrMsg = 0;
371 int rc = sqlite3_exec(g.db, zSql, 0, 0, &zErrMsg);
372 if( zErrMsg ) fatal_error("SQL error: %s\n%s\n", zErrMsg, zSql);
373 if( rc!=SQLITE_OK ) fatal_error("exec error: %s\n", sqlite3_errmsg(g.db));
drhad1ca9a2013-11-23 04:16:58 +0000374 }
375 sqlite3_free(zSql);
drh0d847182015-07-02 01:38:39 +0000376 speedtest1_shrink_memory();
drhad1ca9a2013-11-23 04:16:58 +0000377}
378
379/* Prepare an SQL statement */
380void speedtest1_prepare(const char *zFormat, ...){
381 va_list ap;
382 char *zSql;
383 va_start(ap, zFormat);
384 zSql = sqlite3_vmprintf(zFormat, ap);
385 va_end(ap);
386 if( g.bSqlOnly ){
drh849a9d92013-12-21 15:46:06 +0000387 printSql(zSql);
drhad1ca9a2013-11-23 04:16:58 +0000388 }else{
389 int rc;
390 if( g.pStmt ) sqlite3_finalize(g.pStmt);
391 rc = sqlite3_prepare_v2(g.db, zSql, -1, &g.pStmt, 0);
392 if( rc ){
393 fatal_error("SQL error: %s\n", sqlite3_errmsg(g.db));
394 }
395 }
396 sqlite3_free(zSql);
397}
398
399/* Run an SQL statement previously prepared */
400void speedtest1_run(void){
401 int i, n, len;
402 if( g.bSqlOnly ) return;
403 assert( g.pStmt );
404 g.nResult = 0;
405 while( sqlite3_step(g.pStmt)==SQLITE_ROW ){
406 n = sqlite3_column_count(g.pStmt);
407 for(i=0; i<n; i++){
408 const char *z = (const char*)sqlite3_column_text(g.pStmt, i);
409 if( z==0 ) z = "nil";
410 len = (int)strlen(z);
411 if( g.nResult+len<sizeof(g.zResult)-2 ){
412 if( g.nResult>0 ) g.zResult[g.nResult++] = ' ';
413 memcpy(g.zResult + g.nResult, z, len+1);
414 g.nResult += len;
415 }
416 }
417 }
drh5995e292015-06-18 12:37:32 +0000418#if SQLITE_VERSION_NUMBER>=3006001
drhad1ca9a2013-11-23 04:16:58 +0000419 if( g.bReprepare ){
420 sqlite3_stmt *pNew;
421 sqlite3_prepare_v2(g.db, sqlite3_sql(g.pStmt), -1, &pNew, 0);
422 sqlite3_finalize(g.pStmt);
423 g.pStmt = pNew;
drh5995e292015-06-18 12:37:32 +0000424 }else
425#endif
426 {
drhad1ca9a2013-11-23 04:16:58 +0000427 sqlite3_reset(g.pStmt);
428 }
drh0d847182015-07-02 01:38:39 +0000429 speedtest1_shrink_memory();
drhad1ca9a2013-11-23 04:16:58 +0000430}
431
drh98ef26b2016-09-21 23:58:49 +0000432#ifndef SQLITE_OMIT_DEPRECATED
drhad1ca9a2013-11-23 04:16:58 +0000433/* The sqlite3_trace() callback function */
434static void traceCallback(void *NotUsed, const char *zSql){
435 int n = (int)strlen(zSql);
drhc56fac72015-10-29 13:48:15 +0000436 while( n>0 && (zSql[n-1]==';' || ISSPACE(zSql[n-1])) ) n--;
drhad1ca9a2013-11-23 04:16:58 +0000437 fprintf(stderr,"%.*s;\n", n, zSql);
438}
drh98ef26b2016-09-21 23:58:49 +0000439#endif /* SQLITE_OMIT_DEPRECATED */
drhad1ca9a2013-11-23 04:16:58 +0000440
441/* Substitute random() function that gives the same random
442** sequence on each run, for repeatability. */
443static void randomFunc(
444 sqlite3_context *context,
445 int NotUsed,
446 sqlite3_value **NotUsed2
447){
448 sqlite3_result_int64(context, (sqlite3_int64)speedtest1_random());
449}
450
drhae28d6e2013-12-21 00:04:37 +0000451/* Estimate the square root of an integer */
452static int est_square_root(int x){
453 int y0 = x/2;
454 int y1;
455 int n;
456 for(n=0; y0>0 && n<10; n++){
457 y1 = (y0 + x/y0)/2;
458 if( y1==y0 ) break;
459 y0 = y1;
460 }
461 return y0;
462}
463
drh89500dc2016-11-21 18:15:35 +0000464
465#if SQLITE_VERSION_NUMBER<3005004
466/*
467** An implementation of group_concat(). Used only when testing older
468** versions of SQLite that lack the built-in group_concat().
469*/
470struct groupConcat {
471 char *z;
472 int nAlloc;
473 int nUsed;
474};
475static void groupAppend(struct groupConcat *p, const char *z, int n){
476 if( p->nUsed+n >= p->nAlloc ){
477 int n2 = (p->nAlloc+n+1)*2;
478 char *z2 = sqlite3_realloc(p->z, n2);
479 if( z2==0 ) return;
480 p->z = z2;
481 p->nAlloc = n2;
482 }
483 memcpy(p->z+p->nUsed, z, n);
484 p->nUsed += n;
485}
486static void groupStep(
487 sqlite3_context *context,
488 int argc,
489 sqlite3_value **argv
490){
491 const char *zVal;
492 struct groupConcat *p;
493 const char *zSep;
494 int nVal, nSep;
495 assert( argc==1 || argc==2 );
496 if( sqlite3_value_type(argv[0])==SQLITE_NULL ) return;
497 p= (struct groupConcat*)sqlite3_aggregate_context(context, sizeof(*p));
498
499 if( p ){
500 int firstTerm = p->nUsed==0;
501 if( !firstTerm ){
502 if( argc==2 ){
503 zSep = (char*)sqlite3_value_text(argv[1]);
504 nSep = sqlite3_value_bytes(argv[1]);
505 }else{
506 zSep = ",";
507 nSep = 1;
508 }
509 if( nSep ) groupAppend(p, zSep, nSep);
510 }
511 zVal = (char*)sqlite3_value_text(argv[0]);
512 nVal = sqlite3_value_bytes(argv[0]);
513 if( zVal ) groupAppend(p, zVal, nVal);
514 }
515}
516static void groupFinal(sqlite3_context *context){
517 struct groupConcat *p;
518 p = sqlite3_aggregate_context(context, 0);
519 if( p && p->z ){
520 p->z[p->nUsed] = 0;
521 sqlite3_result_text(context, p->z, p->nUsed, sqlite3_free);
522 }
523}
524#endif
525
drhad1ca9a2013-11-23 04:16:58 +0000526/*
527** The main and default testset
528*/
529void testset_main(void){
530 int i; /* Loop counter */
531 int n; /* iteration count */
532 int sz; /* Size of the tables */
533 int maxb; /* Maximum swizzled value */
drhba690612016-10-18 15:29:57 +0000534 unsigned x1 = 0, x2 = 0; /* Parameters */
535 int len = 0; /* Length of the zNum[] string */
drhad1ca9a2013-11-23 04:16:58 +0000536 char zNum[2000]; /* A number name */
537
538 sz = n = g.szTest*500;
drhba690612016-10-18 15:29:57 +0000539 zNum[0] = 0;
drhad1ca9a2013-11-23 04:16:58 +0000540 maxb = roundup_allones(sz);
541 speedtest1_begin_test(100, "%d INSERTs into table with no index", n);
542 speedtest1_exec("BEGIN");
drh2160ca52016-04-12 16:59:39 +0000543 speedtest1_exec("CREATE%s TABLE t1(a INTEGER %s, b INTEGER %s, c TEXT %s);",
544 isTemp(9), g.zNN, g.zNN, g.zNN);
drhad1ca9a2013-11-23 04:16:58 +0000545 speedtest1_prepare("INSERT INTO t1 VALUES(?1,?2,?3); -- %d times", n);
546 for(i=1; i<=n; i++){
547 x1 = swizzle(i,maxb);
548 speedtest1_numbername(x1, zNum, sizeof(zNum));
549 sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1);
550 sqlite3_bind_int(g.pStmt, 2, i);
551 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
552 speedtest1_run();
553 }
554 speedtest1_exec("COMMIT");
555 speedtest1_end_test();
556
557
558 n = sz;
559 speedtest1_begin_test(110, "%d ordered INSERTS with one index/PK", n);
560 speedtest1_exec("BEGIN");
drh2160ca52016-04-12 16:59:39 +0000561 speedtest1_exec(
562 "CREATE%s TABLE t2(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s",
563 isTemp(5), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
drhad1ca9a2013-11-23 04:16:58 +0000564 speedtest1_prepare("INSERT INTO t2 VALUES(?1,?2,?3); -- %d times", n);
565 for(i=1; i<=n; i++){
566 x1 = swizzle(i,maxb);
567 speedtest1_numbername(x1, zNum, sizeof(zNum));
568 sqlite3_bind_int(g.pStmt, 1, i);
569 sqlite3_bind_int64(g.pStmt, 2, (sqlite3_int64)x1);
570 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
571 speedtest1_run();
572 }
573 speedtest1_exec("COMMIT");
574 speedtest1_end_test();
575
576
577 n = sz;
578 speedtest1_begin_test(120, "%d unordered INSERTS with one index/PK", n);
579 speedtest1_exec("BEGIN");
drh2160ca52016-04-12 16:59:39 +0000580 speedtest1_exec(
581 "CREATE%s TABLE t3(a INTEGER %s %s, b INTEGER %s, c TEXT %s) %s",
582 isTemp(3), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
drhad1ca9a2013-11-23 04:16:58 +0000583 speedtest1_prepare("INSERT INTO t3 VALUES(?1,?2,?3); -- %d times", n);
584 for(i=1; i<=n; i++){
585 x1 = swizzle(i,maxb);
586 speedtest1_numbername(x1, zNum, sizeof(zNum));
587 sqlite3_bind_int(g.pStmt, 2, i);
588 sqlite3_bind_int64(g.pStmt, 1, (sqlite3_int64)x1);
589 sqlite3_bind_text(g.pStmt, 3, zNum, -1, SQLITE_STATIC);
590 speedtest1_run();
591 }
592 speedtest1_exec("COMMIT");
593 speedtest1_end_test();
594
drh89500dc2016-11-21 18:15:35 +0000595#if SQLITE_VERSION_NUMBER<3005004
596 sqlite3_create_function(g.db, "group_concat", 1, SQLITE_UTF8, 0,
597 0, groupStep, groupFinal);
598#endif
drhad1ca9a2013-11-23 04:16:58 +0000599
drh5e8980d2014-03-25 20:28:38 +0000600 n = 25;
drhad1ca9a2013-11-23 04:16:58 +0000601 speedtest1_begin_test(130, "%d SELECTS, numeric BETWEEN, unindexed", n);
602 speedtest1_exec("BEGIN");
603 speedtest1_prepare(
drh662389b2016-11-08 00:30:11 +0000604 "SELECT count(*), avg(b), sum(length(c)), group_concat(c) FROM t1\n"
drhad1ca9a2013-11-23 04:16:58 +0000605 " WHERE b BETWEEN ?1 AND ?2; -- %d times", n
606 );
607 for(i=1; i<=n; i++){
drhf8a89ca2016-10-18 14:35:55 +0000608 if( (i-1)%g.nRepeat==0 ){
609 x1 = speedtest1_random()%maxb;
610 x2 = speedtest1_random()%10 + sz/5000 + x1;
611 }
drhad1ca9a2013-11-23 04:16:58 +0000612 sqlite3_bind_int(g.pStmt, 1, x1);
613 sqlite3_bind_int(g.pStmt, 2, x2);
614 speedtest1_run();
615 }
616 speedtest1_exec("COMMIT");
617 speedtest1_end_test();
618
619
drh5e8980d2014-03-25 20:28:38 +0000620 n = 10;
drhad1ca9a2013-11-23 04:16:58 +0000621 speedtest1_begin_test(140, "%d SELECTS, LIKE, unindexed", n);
622 speedtest1_exec("BEGIN");
623 speedtest1_prepare(
drh662389b2016-11-08 00:30:11 +0000624 "SELECT count(*), avg(b), sum(length(c)), group_concat(c) FROM t1\n"
drhad1ca9a2013-11-23 04:16:58 +0000625 " WHERE c LIKE ?1; -- %d times", n
626 );
627 for(i=1; i<=n; i++){
drhf8a89ca2016-10-18 14:35:55 +0000628 if( (i-1)%g.nRepeat==0 ){
629 x1 = speedtest1_random()%maxb;
630 zNum[0] = '%';
631 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2);
632 zNum[len] = '%';
633 zNum[len+1] = 0;
634 }
drh0a22c862016-11-08 16:27:59 +0000635 sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC);
drhad1ca9a2013-11-23 04:16:58 +0000636 speedtest1_run();
637 }
638 speedtest1_exec("COMMIT");
639 speedtest1_end_test();
640
641
drh5e8980d2014-03-25 20:28:38 +0000642 n = 10;
drhc8729662014-03-25 17:45:49 +0000643 speedtest1_begin_test(142, "%d SELECTS w/ORDER BY, unindexed", n);
644 speedtest1_exec("BEGIN");
645 speedtest1_prepare(
646 "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n"
647 " ORDER BY a; -- %d times", n
648 );
649 for(i=1; i<=n; i++){
drhf8a89ca2016-10-18 14:35:55 +0000650 if( (i-1)%g.nRepeat==0 ){
651 x1 = speedtest1_random()%maxb;
652 zNum[0] = '%';
653 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2);
654 zNum[len] = '%';
655 zNum[len+1] = 0;
656 }
drh0a22c862016-11-08 16:27:59 +0000657 sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC);
drhc8729662014-03-25 17:45:49 +0000658 speedtest1_run();
659 }
660 speedtest1_exec("COMMIT");
661 speedtest1_end_test();
662
dan54fc2142015-03-05 16:21:20 +0000663 n = 10; /* g.szTest/5; */
drhc8729662014-03-25 17:45:49 +0000664 speedtest1_begin_test(145, "%d SELECTS w/ORDER BY and LIMIT, unindexed", n);
drh0c60c1f2014-03-25 14:54:36 +0000665 speedtest1_exec("BEGIN");
666 speedtest1_prepare(
667 "SELECT a, b, c FROM t1 WHERE c LIKE ?1\n"
668 " ORDER BY a LIMIT 10; -- %d times", n
669 );
670 for(i=1; i<=n; i++){
drhf8a89ca2016-10-18 14:35:55 +0000671 if( (i-1)%g.nRepeat==0 ){
672 x1 = speedtest1_random()%maxb;
673 zNum[0] = '%';
674 len = speedtest1_numbername(i, zNum+1, sizeof(zNum)-2);
675 zNum[len] = '%';
676 zNum[len+1] = 0;
677 }
drh0a22c862016-11-08 16:27:59 +0000678 sqlite3_bind_text(g.pStmt, 1, zNum, len+1, SQLITE_STATIC);
drh0c60c1f2014-03-25 14:54:36 +0000679 speedtest1_run();
680 }
681 speedtest1_exec("COMMIT");
682 speedtest1_end_test();
683
684
drhad1ca9a2013-11-23 04:16:58 +0000685 speedtest1_begin_test(150, "CREATE INDEX five times");
drh849a9d92013-12-21 15:46:06 +0000686 speedtest1_exec("BEGIN;");
687 speedtest1_exec("CREATE UNIQUE INDEX t1b ON t1(b);");
688 speedtest1_exec("CREATE INDEX t1c ON t1(c);");
689 speedtest1_exec("CREATE UNIQUE INDEX t2b ON t2(b);");
690 speedtest1_exec("CREATE INDEX t2c ON t2(c DESC);");
691 speedtest1_exec("CREATE INDEX t3bc ON t3(b,c);");
692 speedtest1_exec("COMMIT;");
drhad1ca9a2013-11-23 04:16:58 +0000693 speedtest1_end_test();
694
695
696 n = sz/5;
697 speedtest1_begin_test(160, "%d SELECTS, numeric BETWEEN, indexed", n);
698 speedtest1_exec("BEGIN");
699 speedtest1_prepare(
drh662389b2016-11-08 00:30:11 +0000700 "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM t1\n"
drhad1ca9a2013-11-23 04:16:58 +0000701 " WHERE b BETWEEN ?1 AND ?2; -- %d times", n
702 );
703 for(i=1; i<=n; i++){
drhf8a89ca2016-10-18 14:35:55 +0000704 if( (i-1)%g.nRepeat==0 ){
705 x1 = speedtest1_random()%maxb;
706 x2 = speedtest1_random()%10 + sz/5000 + x1;
707 }
drhad1ca9a2013-11-23 04:16:58 +0000708 sqlite3_bind_int(g.pStmt, 1, x1);
709 sqlite3_bind_int(g.pStmt, 2, x2);
710 speedtest1_run();
711 }
712 speedtest1_exec("COMMIT");
713 speedtest1_end_test();
714
715
716 n = sz/5;
717 speedtest1_begin_test(161, "%d SELECTS, numeric BETWEEN, PK", n);
718 speedtest1_exec("BEGIN");
719 speedtest1_prepare(
drh662389b2016-11-08 00:30:11 +0000720 "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM t2\n"
drhad1ca9a2013-11-23 04:16:58 +0000721 " WHERE a BETWEEN ?1 AND ?2; -- %d times", n
722 );
723 for(i=1; i<=n; i++){
drhf8a89ca2016-10-18 14:35:55 +0000724 if( (i-1)%g.nRepeat==0 ){
725 x1 = speedtest1_random()%maxb;
726 x2 = speedtest1_random()%10 + sz/5000 + x1;
727 }
drhad1ca9a2013-11-23 04:16:58 +0000728 sqlite3_bind_int(g.pStmt, 1, x1);
729 sqlite3_bind_int(g.pStmt, 2, x2);
730 speedtest1_run();
731 }
732 speedtest1_exec("COMMIT");
733 speedtest1_end_test();
734
735
736 n = sz/5;
737 speedtest1_begin_test(170, "%d SELECTS, text BETWEEN, indexed", n);
738 speedtest1_exec("BEGIN");
739 speedtest1_prepare(
drh662389b2016-11-08 00:30:11 +0000740 "SELECT count(*), avg(b), sum(length(c)), group_concat(a) FROM t1\n"
drhad1ca9a2013-11-23 04:16:58 +0000741 " WHERE c BETWEEN ?1 AND (?1||'~'); -- %d times", n
742 );
743 for(i=1; i<=n; i++){
drhf8a89ca2016-10-18 14:35:55 +0000744 if( (i-1)%g.nRepeat==0 ){
745 x1 = swizzle(i, maxb);
746 len = speedtest1_numbername(x1, zNum, sizeof(zNum)-1);
747 }
drhad1ca9a2013-11-23 04:16:58 +0000748 sqlite3_bind_text(g.pStmt, 1, zNum, len, SQLITE_STATIC);
749 speedtest1_run();
750 }
751 speedtest1_exec("COMMIT");
752 speedtest1_end_test();
753
754 n = sz;
755 speedtest1_begin_test(180, "%d INSERTS with three indexes", n);
756 speedtest1_exec("BEGIN");
757 speedtest1_exec(
drh2160ca52016-04-12 16:59:39 +0000758 "CREATE%s TABLE t4(\n"
drhad1ca9a2013-11-23 04:16:58 +0000759 " a INTEGER %s %s,\n"
760 " b INTEGER %s,\n"
761 " c TEXT %s\n"
762 ") %s",
drh2160ca52016-04-12 16:59:39 +0000763 isTemp(1), g.zNN, g.zPK, g.zNN, g.zNN, g.zWR);
drhad1ca9a2013-11-23 04:16:58 +0000764 speedtest1_exec("CREATE INDEX t4b ON t4(b)");
765 speedtest1_exec("CREATE INDEX t4c ON t4(c)");
766 speedtest1_exec("INSERT INTO t4 SELECT * FROM t1");
767 speedtest1_exec("COMMIT");
768 speedtest1_end_test();
769
770 n = sz;
771 speedtest1_begin_test(190, "DELETE and REFILL one table", n);
drh849a9d92013-12-21 15:46:06 +0000772 speedtest1_exec("DELETE FROM t2;");
773 speedtest1_exec("INSERT INTO t2 SELECT * FROM t1;");
drhad1ca9a2013-11-23 04:16:58 +0000774 speedtest1_end_test();
775
776
777 speedtest1_begin_test(200, "VACUUM");
778 speedtest1_exec("VACUUM");
779 speedtest1_end_test();
780
781
782 speedtest1_begin_test(210, "ALTER TABLE ADD COLUMN, and query");
783 speedtest1_exec("ALTER TABLE t2 ADD COLUMN d DEFAULT 123");
784 speedtest1_exec("SELECT sum(d) FROM t2");
785 speedtest1_end_test();
786
787
788 n = sz/5;
789 speedtest1_begin_test(230, "%d UPDATES, numeric BETWEEN, indexed", n);
790 speedtest1_exec("BEGIN");
791 speedtest1_prepare(
792 "UPDATE t2 SET d=b*2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n
793 );
794 for(i=1; i<=n; i++){
795 x1 = speedtest1_random()%maxb;
796 x2 = speedtest1_random()%10 + sz/5000 + x1;
797 sqlite3_bind_int(g.pStmt, 1, x1);
798 sqlite3_bind_int(g.pStmt, 2, x2);
799 speedtest1_run();
800 }
801 speedtest1_exec("COMMIT");
802 speedtest1_end_test();
803
804
805 n = sz;
806 speedtest1_begin_test(240, "%d UPDATES of individual rows", n);
807 speedtest1_exec("BEGIN");
808 speedtest1_prepare(
809 "UPDATE t2 SET d=b*3 WHERE a=?1; -- %d times", n
810 );
811 for(i=1; i<=n; i++){
812 x1 = speedtest1_random()%sz + 1;
813 sqlite3_bind_int(g.pStmt, 1, x1);
814 speedtest1_run();
815 }
816 speedtest1_exec("COMMIT");
817 speedtest1_end_test();
818
819 speedtest1_begin_test(250, "One big UPDATE of the whole %d-row table", sz);
820 speedtest1_exec("UPDATE t2 SET d=b*4");
821 speedtest1_end_test();
822
823
824 speedtest1_begin_test(260, "Query added column after filling");
825 speedtest1_exec("SELECT sum(d) FROM t2");
826 speedtest1_end_test();
827
828
829
830 n = sz/5;
831 speedtest1_begin_test(270, "%d DELETEs, numeric BETWEEN, indexed", n);
832 speedtest1_exec("BEGIN");
833 speedtest1_prepare(
834 "DELETE FROM t2 WHERE b BETWEEN ?1 AND ?2; -- %d times", n
835 );
836 for(i=1; i<=n; i++){
837 x1 = speedtest1_random()%maxb + 1;
838 x2 = speedtest1_random()%10 + sz/5000 + x1;
839 sqlite3_bind_int(g.pStmt, 1, x1);
840 sqlite3_bind_int(g.pStmt, 2, x2);
841 speedtest1_run();
842 }
843 speedtest1_exec("COMMIT");
844 speedtest1_end_test();
845
846
847 n = sz;
848 speedtest1_begin_test(280, "%d DELETEs of individual rows", n);
849 speedtest1_exec("BEGIN");
850 speedtest1_prepare(
851 "DELETE FROM t3 WHERE a=?1; -- %d times", n
852 );
853 for(i=1; i<=n; i++){
854 x1 = speedtest1_random()%sz + 1;
855 sqlite3_bind_int(g.pStmt, 1, x1);
856 speedtest1_run();
857 }
858 speedtest1_exec("COMMIT");
859 speedtest1_end_test();
860
861
862 speedtest1_begin_test(290, "Refill two %d-row tables using REPLACE", sz);
drhe19f8322013-11-23 11:45:58 +0000863 speedtest1_exec("REPLACE INTO t2(a,b,c) SELECT a,b,c FROM t1");
864 speedtest1_exec("REPLACE INTO t3(a,b,c) SELECT a,b,c FROM t1");
drhad1ca9a2013-11-23 04:16:58 +0000865 speedtest1_end_test();
866
drh039468e2013-12-18 16:27:48 +0000867 speedtest1_begin_test(300, "Refill a %d-row table using (b&1)==(a&1)", sz);
868 speedtest1_exec("DELETE FROM t2;");
drh849a9d92013-12-21 15:46:06 +0000869 speedtest1_exec("INSERT INTO t2(a,b,c)\n"
870 " SELECT a,b,c FROM t1 WHERE (b&1)==(a&1);");
871 speedtest1_exec("INSERT INTO t2(a,b,c)\n"
872 " SELECT a,b,c FROM t1 WHERE (b&1)<>(a&1);");
drh039468e2013-12-18 16:27:48 +0000873 speedtest1_end_test();
874
drhad1ca9a2013-11-23 04:16:58 +0000875
876 n = sz/5;
drh039468e2013-12-18 16:27:48 +0000877 speedtest1_begin_test(310, "%d four-ways joins", n);
drhad1ca9a2013-11-23 04:16:58 +0000878 speedtest1_exec("BEGIN");
879 speedtest1_prepare(
880 "SELECT t1.c FROM t1, t2, t3, t4\n"
881 " WHERE t4.a BETWEEN ?1 AND ?2\n"
882 " AND t3.a=t4.b\n"
883 " AND t2.a=t3.b\n"
884 " AND t1.c=t2.c"
885 );
886 for(i=1; i<=n; i++){
887 x1 = speedtest1_random()%sz + 1;
888 x2 = speedtest1_random()%10 + x1 + 4;
889 sqlite3_bind_int(g.pStmt, 1, x1);
890 sqlite3_bind_int(g.pStmt, 2, x2);
891 speedtest1_run();
892 }
893 speedtest1_exec("COMMIT");
894 speedtest1_end_test();
895
drhae28d6e2013-12-21 00:04:37 +0000896 speedtest1_begin_test(320, "subquery in result set", n);
897 speedtest1_prepare(
898 "SELECT sum(a), max(c),\n"
899 " avg((SELECT a FROM t2 WHERE 5+t2.b=t1.b) AND rowid<?1), max(c)\n"
900 " FROM t1 WHERE rowid<?1;"
901 );
902 sqlite3_bind_int(g.pStmt, 1, est_square_root(g.szTest)*50);
903 speedtest1_run();
904 speedtest1_end_test();
drhad1ca9a2013-11-23 04:16:58 +0000905
drh662389b2016-11-08 00:30:11 +0000906 sz = n = g.szTest*700;
907 zNum[0] = 0;
908 maxb = roundup_allones(sz/3);
drh088c0862016-11-09 01:07:10 +0000909 speedtest1_begin_test(400, "%d REPLACE ops on an IPK", n);
drh662389b2016-11-08 00:30:11 +0000910 speedtest1_exec("BEGIN");
911 speedtest1_exec("CREATE%s TABLE t5(a INTEGER PRIMARY KEY, b %s);",
912 isTemp(9), g.zNN);
drh088c0862016-11-09 01:07:10 +0000913 speedtest1_prepare("REPLACE INTO t5 VALUES(?1,?2); -- %d times",n);
drh662389b2016-11-08 00:30:11 +0000914 for(i=1; i<=n; i++){
915 x1 = swizzle(i,maxb);
916 speedtest1_numbername(i, zNum, sizeof(zNum));
917 sqlite3_bind_int(g.pStmt, 1, (sqlite3_int64)x1);
918 sqlite3_bind_text(g.pStmt, 2, zNum, -1, SQLITE_STATIC);
919 speedtest1_run();
920 }
921 speedtest1_exec("COMMIT");
922 speedtest1_end_test();
923 speedtest1_begin_test(410, "%d SELECTS on an IPK", n);
924 speedtest1_prepare("SELECT b FROM t5 WHERE a=?1; -- %d times",n);
925 for(i=1; i<=n; i++){
926 x1 = swizzle(i,maxb);
927 sqlite3_bind_int(g.pStmt, 1, (sqlite3_int64)x1);
928 speedtest1_run();
929 }
930 speedtest1_end_test();
931
932 sz = n = g.szTest*700;
933 zNum[0] = 0;
934 maxb = roundup_allones(sz/3);
drh088c0862016-11-09 01:07:10 +0000935 speedtest1_begin_test(500, "%d REPLACE on TEXT PK", n);
drh662389b2016-11-08 00:30:11 +0000936 speedtest1_exec("BEGIN");
937 speedtest1_exec("CREATE%s TABLE t6(a TEXT PRIMARY KEY, b %s)%s;",
938 isTemp(9), g.zNN,
939 sqlite3_libversion_number()>=3008002 ? "WITHOUT ROWID" : "");
drh088c0862016-11-09 01:07:10 +0000940 speedtest1_prepare("REPLACE INTO t6 VALUES(?1,?2); -- %d times",n);
drh662389b2016-11-08 00:30:11 +0000941 for(i=1; i<=n; i++){
942 x1 = swizzle(i,maxb);
943 speedtest1_numbername(x1, zNum, sizeof(zNum));
944 sqlite3_bind_int(g.pStmt, 2, i);
945 sqlite3_bind_text(g.pStmt, 1, zNum, -1, SQLITE_STATIC);
946 speedtest1_run();
947 }
948 speedtest1_exec("COMMIT");
949 speedtest1_end_test();
drh088c0862016-11-09 01:07:10 +0000950 speedtest1_begin_test(510, "%d SELECTS on a TEXT PK", n);
drh662389b2016-11-08 00:30:11 +0000951 speedtest1_prepare("SELECT b FROM t6 WHERE a=?1; -- %d times",n);
952 for(i=1; i<=n; i++){
953 x1 = swizzle(i,maxb);
954 speedtest1_numbername(x1, zNum, sizeof(zNum));
955 sqlite3_bind_text(g.pStmt, 1, zNum, -1, SQLITE_STATIC);
956 speedtest1_run();
957 }
958 speedtest1_end_test();
drh088c0862016-11-09 01:07:10 +0000959 speedtest1_begin_test(520, "%d SELECT DISTINCT", n);
960 speedtest1_exec("SELECT DISTINCT b FROM t5;");
961 speedtest1_exec("SELECT DISTINCT b FROM t6;");
962 speedtest1_end_test();
drh662389b2016-11-08 00:30:11 +0000963
964
drhad1ca9a2013-11-23 04:16:58 +0000965 speedtest1_begin_test(980, "PRAGMA integrity_check");
966 speedtest1_exec("PRAGMA integrity_check");
967 speedtest1_end_test();
968
969
970 speedtest1_begin_test(990, "ANALYZE");
971 speedtest1_exec("ANALYZE");
972 speedtest1_end_test();
973}
974
975/*
drhc4754802014-02-09 00:18:21 +0000976** A testset for common table expressions. This exercises code
977** for views, subqueries, co-routines, etc.
978*/
979void testset_cte(void){
980 static const char *azPuzzle[] = {
981 /* Easy */
982 "534...9.."
983 "67.195..."
984 ".98....6."
985 "8...6...3"
986 "4..8.3..1"
987 "....2...6"
988 ".6....28."
989 "...419..5"
990 "...28..79",
991
992 /* Medium */
993 "53....9.."
994 "6..195..."
995 ".98....6."
996 "8...6...3"
997 "4..8.3..1"
998 "....2...6"
999 ".6....28."
1000 "...419..5"
1001 "....8..79",
1002
1003 /* Hard */
1004 "53......."
1005 "6..195..."
1006 ".98....6."
1007 "8...6...3"
1008 "4..8.3..1"
1009 "....2...6"
1010 ".6....28."
1011 "...419..5"
1012 "....8..79",
1013 };
1014 const char *zPuz;
drhfa46bfb2014-02-09 00:52:53 +00001015 double rSpacing;
drh5574e3f2014-02-09 23:59:28 +00001016 int nElem;
drhc4754802014-02-09 00:18:21 +00001017
1018 if( g.szTest<25 ){
1019 zPuz = azPuzzle[0];
1020 }else if( g.szTest<70 ){
1021 zPuz = azPuzzle[1];
1022 }else{
1023 zPuz = azPuzzle[2];
1024 }
1025 speedtest1_begin_test(100, "Sudoku with recursive 'digits'");
1026 speedtest1_prepare(
1027 "WITH RECURSIVE\n"
1028 " input(sud) AS (VALUES(?1)),\n"
1029 " digits(z,lp) AS (\n"
1030 " VALUES('1', 1)\n"
1031 " UNION ALL\n"
1032 " SELECT CAST(lp+1 AS TEXT), lp+1 FROM digits WHERE lp<9\n"
1033 " ),\n"
1034 " x(s, ind) AS (\n"
1035 " SELECT sud, instr(sud, '.') FROM input\n"
1036 " UNION ALL\n"
1037 " SELECT\n"
1038 " substr(s, 1, ind-1) || z || substr(s, ind+1),\n"
1039 " instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n"
1040 " FROM x, digits AS z\n"
1041 " WHERE ind>0\n"
1042 " AND NOT EXISTS (\n"
1043 " SELECT 1\n"
1044 " FROM digits AS lp\n"
1045 " WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n"
1046 " OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n"
1047 " OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n"
1048 " + ((ind-1)/27) * 27 + lp\n"
1049 " + ((lp-1) / 3) * 6, 1)\n"
1050 " )\n"
1051 " )\n"
1052 "SELECT s FROM x WHERE ind=0;"
1053 );
1054 sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC);
1055 speedtest1_run();
1056 speedtest1_end_test();
1057
1058 speedtest1_begin_test(200, "Sudoku with VALUES 'digits'");
1059 speedtest1_prepare(
1060 "WITH RECURSIVE\n"
1061 " input(sud) AS (VALUES(?1)),\n"
1062 " digits(z,lp) AS (VALUES('1',1),('2',2),('3',3),('4',4),('5',5),\n"
1063 " ('6',6),('7',7),('8',8),('9',9)),\n"
1064 " x(s, ind) AS (\n"
1065 " SELECT sud, instr(sud, '.') FROM input\n"
1066 " UNION ALL\n"
1067 " SELECT\n"
1068 " substr(s, 1, ind-1) || z || substr(s, ind+1),\n"
1069 " instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' )\n"
1070 " FROM x, digits AS z\n"
1071 " WHERE ind>0\n"
1072 " AND NOT EXISTS (\n"
1073 " SELECT 1\n"
1074 " FROM digits AS lp\n"
1075 " WHERE z.z = substr(s, ((ind-1)/9)*9 + lp, 1)\n"
1076 " OR z.z = substr(s, ((ind-1)%%9) + (lp-1)*9 + 1, 1)\n"
1077 " OR z.z = substr(s, (((ind-1)/3) %% 3) * 3\n"
1078 " + ((ind-1)/27) * 27 + lp\n"
1079 " + ((lp-1) / 3) * 6, 1)\n"
1080 " )\n"
1081 " )\n"
1082 "SELECT s FROM x WHERE ind=0;"
1083 );
1084 sqlite3_bind_text(g.pStmt, 1, zPuz, -1, SQLITE_STATIC);
1085 speedtest1_run();
1086 speedtest1_end_test();
drhfa46bfb2014-02-09 00:52:53 +00001087
1088 rSpacing = 5.0/g.szTest;
1089 speedtest1_begin_test(300, "Mandelbrot Set with spacing=%f", rSpacing);
1090 speedtest1_prepare(
1091 "WITH RECURSIVE \n"
1092 " xaxis(x) AS (VALUES(-2.0) UNION ALL SELECT x+?1 FROM xaxis WHERE x<1.2),\n"
1093 " yaxis(y) AS (VALUES(-1.0) UNION ALL SELECT y+?2 FROM yaxis WHERE y<1.0),\n"
1094 " m(iter, cx, cy, x, y) AS (\n"
1095 " SELECT 0, x, y, 0.0, 0.0 FROM xaxis, yaxis\n"
1096 " UNION ALL\n"
1097 " SELECT iter+1, cx, cy, x*x-y*y + cx, 2.0*x*y + cy FROM m \n"
1098 " WHERE (x*x + y*y) < 4.0 AND iter<28\n"
1099 " ),\n"
1100 " m2(iter, cx, cy) AS (\n"
1101 " SELECT max(iter), cx, cy FROM m GROUP BY cx, cy\n"
1102 " ),\n"
1103 " a(t) AS (\n"
1104 " SELECT group_concat( substr(' .+*#', 1+min(iter/7,4), 1), '') \n"
1105 " FROM m2 GROUP BY cy\n"
1106 " )\n"
1107 "SELECT group_concat(rtrim(t),x'0a') FROM a;"
1108 );
1109 sqlite3_bind_double(g.pStmt, 1, rSpacing*.05);
1110 sqlite3_bind_double(g.pStmt, 2, rSpacing);
1111 speedtest1_run();
1112 speedtest1_end_test();
1113
drh5574e3f2014-02-09 23:59:28 +00001114 nElem = 10000*g.szTest;
1115 speedtest1_begin_test(400, "EXCEPT operator on %d-element tables", nElem);
1116 speedtest1_prepare(
1117 "WITH RECURSIVE \n"
1118 " t1(x) AS (VALUES(2) UNION ALL SELECT x+2 FROM t1 WHERE x<%d),\n"
1119 " t2(y) AS (VALUES(3) UNION ALL SELECT y+3 FROM t2 WHERE y<%d)\n"
1120 "SELECT count(x), avg(x) FROM (\n"
1121 " SELECT x FROM t1 EXCEPT SELECT y FROM t2 ORDER BY 1\n"
1122 ");",
1123 nElem, nElem
1124 );
1125 speedtest1_run();
1126 speedtest1_end_test();
1127
drhc4754802014-02-09 00:18:21 +00001128}
1129
drh8683e082014-10-11 10:52:54 +00001130#ifdef SQLITE_ENABLE_RTREE
drh65e6b0d2014-04-28 17:56:19 +00001131/* Generate two numbers between 1 and mx. The first number is less than
1132** the second. Usually the numbers are near each other but can sometimes
1133** be far apart.
1134*/
1135static void twoCoords(
1136 int p1, int p2, /* Parameters adjusting sizes */
1137 unsigned mx, /* Range of 1..mx */
1138 unsigned *pX0, unsigned *pX1 /* OUT: write results here */
1139){
1140 unsigned d, x0, x1, span;
1141
1142 span = mx/100 + 1;
1143 if( speedtest1_random()%3==0 ) span *= p1;
1144 if( speedtest1_random()%p2==0 ) span = mx/2;
1145 d = speedtest1_random()%span + 1;
1146 x0 = speedtest1_random()%(mx-d) + 1;
1147 x1 = x0 + d;
1148 *pX0 = x0;
1149 *pX1 = x1;
1150}
drh8683e082014-10-11 10:52:54 +00001151#endif
drh65e6b0d2014-04-28 17:56:19 +00001152
drh8683e082014-10-11 10:52:54 +00001153#ifdef SQLITE_ENABLE_RTREE
drh65e6b0d2014-04-28 17:56:19 +00001154/* The following routine is an R-Tree geometry callback. It returns
1155** true if the object overlaps a slice on the Y coordinate between the
1156** two values given as arguments. In other words
1157**
1158** SELECT count(*) FROM rt1 WHERE id MATCH xslice(10,20);
1159**
1160** Is the same as saying:
1161**
1162** SELECT count(*) FROM rt1 WHERE y1>=10 AND y0<=20;
1163*/
1164static int xsliceGeometryCallback(
1165 sqlite3_rtree_geometry *p,
1166 int nCoord,
1167 double *aCoord,
1168 int *pRes
1169){
1170 *pRes = aCoord[3]>=p->aParam[0] && aCoord[2]<=p->aParam[1];
1171 return SQLITE_OK;
1172}
drh8683e082014-10-11 10:52:54 +00001173#endif /* SQLITE_ENABLE_RTREE */
drh65e6b0d2014-04-28 17:56:19 +00001174
drh8683e082014-10-11 10:52:54 +00001175#ifdef SQLITE_ENABLE_RTREE
drh65e6b0d2014-04-28 17:56:19 +00001176/*
1177** A testset for the R-Tree virtual table
1178*/
1179void testset_rtree(int p1, int p2){
1180 unsigned i, n;
1181 unsigned mxCoord;
1182 unsigned x0, x1, y0, y1, z0, z1;
1183 unsigned iStep;
1184 int *aCheck = sqlite3_malloc( sizeof(int)*g.szTest*100 );
1185
1186 mxCoord = 15000;
1187 n = g.szTest*100;
1188 speedtest1_begin_test(100, "%d INSERTs into an r-tree", n);
1189 speedtest1_exec("BEGIN");
1190 speedtest1_exec("CREATE VIRTUAL TABLE rt1 USING rtree(id,x0,x1,y0,y1,z0,z1)");
1191 speedtest1_prepare("INSERT INTO rt1(id,x0,x1,y0,y1,z0,z1)"
1192 "VALUES(?1,?2,?3,?4,?5,?6,?7)");
1193 for(i=1; i<=n; i++){
1194 twoCoords(p1, p2, mxCoord, &x0, &x1);
1195 twoCoords(p1, p2, mxCoord, &y0, &y1);
1196 twoCoords(p1, p2, mxCoord, &z0, &z1);
1197 sqlite3_bind_int(g.pStmt, 1, i);
1198 sqlite3_bind_int(g.pStmt, 2, x0);
1199 sqlite3_bind_int(g.pStmt, 3, x1);
1200 sqlite3_bind_int(g.pStmt, 4, y0);
1201 sqlite3_bind_int(g.pStmt, 5, y1);
1202 sqlite3_bind_int(g.pStmt, 6, z0);
1203 sqlite3_bind_int(g.pStmt, 7, z1);
1204 speedtest1_run();
1205 }
1206 speedtest1_exec("COMMIT");
1207 speedtest1_end_test();
1208
1209 speedtest1_begin_test(101, "Copy from rtree to a regular table");
drh2160ca52016-04-12 16:59:39 +00001210 speedtest1_exec(" TABLE t1(id INTEGER PRIMARY KEY,x0,x1,y0,y1,z0,z1)");
drh65e6b0d2014-04-28 17:56:19 +00001211 speedtest1_exec("INSERT INTO t1 SELECT * FROM rt1");
1212 speedtest1_end_test();
1213
1214 n = g.szTest*20;
1215 speedtest1_begin_test(110, "%d one-dimensional intersect slice queries", n);
1216 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x0>=?1 AND x1<=?2");
1217 iStep = mxCoord/n;
1218 for(i=0; i<n; i++){
1219 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1220 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1221 speedtest1_run();
1222 aCheck[i] = atoi(g.zResult);
1223 }
1224 speedtest1_end_test();
1225
1226 if( g.bVerify ){
1227 n = g.szTest*20;
1228 speedtest1_begin_test(111, "Verify result from 1-D intersect slice queries");
1229 speedtest1_prepare("SELECT count(*) FROM t1 WHERE x0>=?1 AND x1<=?2");
1230 iStep = mxCoord/n;
1231 for(i=0; i<n; i++){
1232 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1233 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1234 speedtest1_run();
1235 if( aCheck[i]!=atoi(g.zResult) ){
1236 fatal_error("Count disagree step %d: %d..%d. %d vs %d",
1237 i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult));
1238 }
1239 }
1240 speedtest1_end_test();
1241 }
1242
1243 n = g.szTest*20;
1244 speedtest1_begin_test(120, "%d one-dimensional overlap slice queries", n);
1245 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE y1>=?1 AND y0<=?2");
1246 iStep = mxCoord/n;
1247 for(i=0; i<n; i++){
1248 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1249 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1250 speedtest1_run();
1251 aCheck[i] = atoi(g.zResult);
1252 }
1253 speedtest1_end_test();
1254
1255 if( g.bVerify ){
1256 n = g.szTest*20;
1257 speedtest1_begin_test(121, "Verify result from 1-D overlap slice queries");
1258 speedtest1_prepare("SELECT count(*) FROM t1 WHERE y1>=?1 AND y0<=?2");
1259 iStep = mxCoord/n;
1260 for(i=0; i<n; i++){
1261 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1262 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1263 speedtest1_run();
1264 if( aCheck[i]!=atoi(g.zResult) ){
1265 fatal_error("Count disagree step %d: %d..%d. %d vs %d",
1266 i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult));
1267 }
1268 }
1269 speedtest1_end_test();
1270 }
1271
1272
1273 n = g.szTest*20;
1274 speedtest1_begin_test(125, "%d custom geometry callback queries", n);
1275 sqlite3_rtree_geometry_callback(g.db, "xslice", xsliceGeometryCallback, 0);
1276 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE id MATCH xslice(?1,?2)");
1277 iStep = mxCoord/n;
1278 for(i=0; i<n; i++){
1279 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1280 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1281 speedtest1_run();
1282 if( aCheck[i]!=atoi(g.zResult) ){
1283 fatal_error("Count disagree step %d: %d..%d. %d vs %d",
1284 i, i*iStep, (i+1)*iStep, aCheck[i], atoi(g.zResult));
1285 }
1286 }
1287 speedtest1_end_test();
1288
1289 n = g.szTest*80;
1290 speedtest1_begin_test(130, "%d three-dimensional intersect box queries", n);
1291 speedtest1_prepare("SELECT count(*) FROM rt1 WHERE x1>=?1 AND x0<=?2"
1292 " AND y1>=?1 AND y0<=?2 AND z1>=?1 AND z0<=?2");
1293 iStep = mxCoord/n;
1294 for(i=0; i<n; i++){
1295 sqlite3_bind_int(g.pStmt, 1, i*iStep);
1296 sqlite3_bind_int(g.pStmt, 2, (i+1)*iStep);
1297 speedtest1_run();
1298 aCheck[i] = atoi(g.zResult);
1299 }
1300 speedtest1_end_test();
1301
1302 n = g.szTest*100;
1303 speedtest1_begin_test(140, "%d rowid queries", n);
1304 speedtest1_prepare("SELECT * FROM rt1 WHERE id=?1");
1305 for(i=1; i<=n; i++){
1306 sqlite3_bind_int(g.pStmt, 1, i);
1307 speedtest1_run();
1308 }
1309 speedtest1_end_test();
1310}
drh8683e082014-10-11 10:52:54 +00001311#endif /* SQLITE_ENABLE_RTREE */
drh65e6b0d2014-04-28 17:56:19 +00001312
drhc4754802014-02-09 00:18:21 +00001313/*
drhad1ca9a2013-11-23 04:16:58 +00001314** A testset used for debugging speedtest1 itself.
1315*/
1316void testset_debug1(void){
1317 unsigned i, n;
1318 unsigned x1, x2;
1319 char zNum[2000]; /* A number name */
1320
1321 n = g.szTest;
1322 for(i=1; i<=n; i++){
1323 x1 = swizzle(i, n);
1324 x2 = swizzle(x1, n);
1325 speedtest1_numbername(x1, zNum, sizeof(zNum));
1326 printf("%5d %5d %5d %s\n", i, x1, x2, zNum);
1327 }
1328}
1329
drhfc1a84c2016-02-27 19:19:22 +00001330#ifdef __linux__
1331#include <sys/types.h>
1332#include <unistd.h>
1333
1334/*
1335** Attempt to display I/O stats on Linux using /proc/PID/io
1336*/
1337static void displayLinuxIoStats(FILE *out){
1338 FILE *in;
1339 char z[200];
1340 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid());
1341 in = fopen(z, "rb");
1342 if( in==0 ) return;
1343 while( fgets(z, sizeof(z), in)!=0 ){
1344 static const struct {
1345 const char *zPattern;
1346 const char *zDesc;
1347 } aTrans[] = {
1348 { "rchar: ", "Bytes received by read():" },
1349 { "wchar: ", "Bytes sent to write():" },
1350 { "syscr: ", "Read() system calls:" },
1351 { "syscw: ", "Write() system calls:" },
1352 { "read_bytes: ", "Bytes rcvd from storage:" },
1353 { "write_bytes: ", "Bytes sent to storage:" },
1354 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
1355 };
1356 int i;
1357 for(i=0; i<sizeof(aTrans)/sizeof(aTrans[0]); i++){
1358 int n = (int)strlen(aTrans[i].zPattern);
1359 if( strncmp(aTrans[i].zPattern, z, n)==0 ){
1360 fprintf(out, "-- %-28s %s", aTrans[i].zDesc, &z[n]);
1361 break;
1362 }
1363 }
1364 }
1365 fclose(in);
1366}
1367#endif
1368
drh30e3fdf2016-06-04 16:33:48 +00001369#if SQLITE_VERSION_NUMBER<3006018
1370# define sqlite3_sourceid(X) "(before 3.6.18)"
1371#endif
1372
drhad1ca9a2013-11-23 04:16:58 +00001373int main(int argc, char **argv){
1374 int doAutovac = 0; /* True for --autovacuum */
1375 int cacheSize = 0; /* Desired cache size. 0 means default */
1376 int doExclusive = 0; /* True for --exclusive */
1377 int nHeap = 0, mnHeap = 0; /* Heap size from --heap */
1378 int doIncrvac = 0; /* True for --incrvacuum */
1379 const char *zJMode = 0; /* Journal mode */
1380 const char *zKey = 0; /* Encryption key */
drhaa430bf2016-12-31 18:37:50 +00001381 int nLook = -1, szLook = 0; /* --lookaside configuration */
drhad1ca9a2013-11-23 04:16:58 +00001382 int noSync = 0; /* True for --nosync */
1383 int pageSize = 0; /* Desired page size. 0 means default */
1384 int nPCache = 0, szPCache = 0;/* --pcache configuration */
drhee70a842015-07-06 18:54:52 +00001385 int doPCache = 0; /* True if --pcache is seen */
drh93307e92013-11-24 01:14:14 +00001386 int nScratch = 0, szScratch=0;/* --scratch configuration */
drhad1ca9a2013-11-23 04:16:58 +00001387 int showStats = 0; /* True for --stats */
drh46a06bb2014-04-18 13:57:39 +00001388 int nThread = 0; /* --threads value */
drh26f41982016-12-12 23:24:08 +00001389 int mmapSize = 0; /* How big of a memory map to use */
drhad1ca9a2013-11-23 04:16:58 +00001390 const char *zTSet = "main"; /* Which --testset torun */
1391 int doTrace = 0; /* True for --trace */
1392 const char *zEncoding = 0; /* --utf16be or --utf16le */
1393 const char *zDbName = 0; /* Name of the test database */
1394
1395 void *pHeap = 0; /* Allocated heap space */
1396 void *pLook = 0; /* Allocated lookaside space */
1397 void *pPCache = 0; /* Allocated storage for pcache */
drh93307e92013-11-24 01:14:14 +00001398 void *pScratch = 0; /* Allocated storage for scratch */
drhad1ca9a2013-11-23 04:16:58 +00001399 int iCur, iHi; /* Stats values, current and "highwater" */
drhe19f8322013-11-23 11:45:58 +00001400 int i; /* Loop counter */
1401 int rc; /* API return code */
drhad1ca9a2013-11-23 04:16:58 +00001402
drhb719e3a2016-02-19 16:19:23 +00001403 /* Display the version of SQLite being tested */
drhfbcd3132016-03-30 12:20:24 +00001404 printf("-- Speedtest1 for SQLite %s %.50s\n",
1405 sqlite3_libversion(), sqlite3_sourceid());
drhb719e3a2016-02-19 16:19:23 +00001406
drhad1ca9a2013-11-23 04:16:58 +00001407 /* Process command-line arguments */
1408 g.zWR = "";
1409 g.zNN = "";
1410 g.zPK = "UNIQUE";
1411 g.szTest = 100;
drhf8a89ca2016-10-18 14:35:55 +00001412 g.nRepeat = 1;
drhad1ca9a2013-11-23 04:16:58 +00001413 for(i=1; i<argc; i++){
1414 const char *z = argv[i];
1415 if( z[0]=='-' ){
1416 do{ z++; }while( z[0]=='-' );
1417 if( strcmp(z,"autovacuum")==0 ){
1418 doAutovac = 1;
1419 }else if( strcmp(z,"cachesize")==0 ){
1420 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1421 i++;
drh93307e92013-11-24 01:14:14 +00001422 cacheSize = integerValue(argv[i]);
drhad1ca9a2013-11-23 04:16:58 +00001423 }else if( strcmp(z,"exclusive")==0 ){
1424 doExclusive = 1;
drh849a9d92013-12-21 15:46:06 +00001425 }else if( strcmp(z,"explain")==0 ){
1426 g.bSqlOnly = 1;
1427 g.bExplain = 1;
drhad1ca9a2013-11-23 04:16:58 +00001428 }else if( strcmp(z,"heap")==0 ){
1429 if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
drh93307e92013-11-24 01:14:14 +00001430 nHeap = integerValue(argv[i+1]);
1431 mnHeap = integerValue(argv[i+2]);
drhad1ca9a2013-11-23 04:16:58 +00001432 i += 2;
1433 }else if( strcmp(z,"incrvacuum")==0 ){
1434 doIncrvac = 1;
1435 }else if( strcmp(z,"journal")==0 ){
1436 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1437 zJMode = argv[++i];
1438 }else if( strcmp(z,"key")==0 ){
1439 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1440 zKey = argv[++i];
1441 }else if( strcmp(z,"lookaside")==0 ){
1442 if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
drh93307e92013-11-24 01:14:14 +00001443 nLook = integerValue(argv[i+1]);
1444 szLook = integerValue(argv[i+2]);
drhad1ca9a2013-11-23 04:16:58 +00001445 i += 2;
drh89500dc2016-11-21 18:15:35 +00001446#if SQLITE_VERSION_NUMBER>=3006000
drh2e43e962015-07-03 14:34:25 +00001447 }else if( strcmp(z,"multithread")==0 ){
1448 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
1449 }else if( strcmp(z,"nomemstat")==0 ){
1450 sqlite3_config(SQLITE_CONFIG_MEMSTATUS, 0);
drh89500dc2016-11-21 18:15:35 +00001451#endif
drh26f41982016-12-12 23:24:08 +00001452#if SQLITE_VERSION_NUMBER>=3007017
1453 }else if( strcmp(z, "mmap")==0 ){
1454 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1455 mmapSize = integerValue(argv[++i]);
1456 #endif
drhad1ca9a2013-11-23 04:16:58 +00001457 }else if( strcmp(z,"nosync")==0 ){
1458 noSync = 1;
1459 }else if( strcmp(z,"notnull")==0 ){
1460 g.zNN = "NOT NULL";
drhcfb8f8d2015-07-23 20:44:49 +00001461#ifdef SQLITE_ENABLE_RBU
1462 }else if( strcmp(z,"rbu")==0 ){
1463 sqlite3ota_create_vfs("rbu", 0);
1464 sqlite3_vfs_register(sqlite3_vfs_find("rbu"), 1);
dan54fc2142015-03-05 16:21:20 +00001465#endif
drhad1ca9a2013-11-23 04:16:58 +00001466 }else if( strcmp(z,"pagesize")==0 ){
1467 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
drh93307e92013-11-24 01:14:14 +00001468 pageSize = integerValue(argv[++i]);
drhad1ca9a2013-11-23 04:16:58 +00001469 }else if( strcmp(z,"pcache")==0 ){
1470 if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
drh93307e92013-11-24 01:14:14 +00001471 nPCache = integerValue(argv[i+1]);
1472 szPCache = integerValue(argv[i+2]);
drhee70a842015-07-06 18:54:52 +00001473 doPCache = 1;
drhad1ca9a2013-11-23 04:16:58 +00001474 i += 2;
1475 }else if( strcmp(z,"primarykey")==0 ){
1476 g.zPK = "PRIMARY KEY";
drhf8a89ca2016-10-18 14:35:55 +00001477 }else if( strcmp(z,"repeat")==0 ){
1478 if( i>=argc-1 ) fatal_error("missing arguments on %s\n", argv[i]);
1479 g.nRepeat = integerValue(argv[i+1]);
1480 i += 1;
drhad1ca9a2013-11-23 04:16:58 +00001481 }else if( strcmp(z,"reprepare")==0 ){
1482 g.bReprepare = 1;
drh93307e92013-11-24 01:14:14 +00001483 }else if( strcmp(z,"scratch")==0 ){
1484 if( i>=argc-2 ) fatal_error("missing arguments on %s\n", argv[i]);
1485 nScratch = integerValue(argv[i+1]);
1486 szScratch = integerValue(argv[i+2]);
1487 i += 2;
drh89500dc2016-11-21 18:15:35 +00001488#if SQLITE_VERSION_NUMBER>=3006000
drh2e43e962015-07-03 14:34:25 +00001489 }else if( strcmp(z,"serialized")==0 ){
1490 sqlite3_config(SQLITE_CONFIG_SERIALIZED);
1491 }else if( strcmp(z,"singlethread")==0 ){
1492 sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
drh89500dc2016-11-21 18:15:35 +00001493#endif
drhad1ca9a2013-11-23 04:16:58 +00001494 }else if( strcmp(z,"sqlonly")==0 ){
1495 g.bSqlOnly = 1;
drh0d847182015-07-02 01:38:39 +00001496 }else if( strcmp(z,"shrink-memory")==0 ){
1497 g.bMemShrink = 1;
drhad1ca9a2013-11-23 04:16:58 +00001498 }else if( strcmp(z,"size")==0 ){
1499 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
drh93307e92013-11-24 01:14:14 +00001500 g.szTest = integerValue(argv[++i]);
drhad1ca9a2013-11-23 04:16:58 +00001501 }else if( strcmp(z,"stats")==0 ){
1502 showStats = 1;
drh2160ca52016-04-12 16:59:39 +00001503 }else if( strcmp(z,"temp")==0 ){
1504 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1505 i++;
1506 if( argv[i][0]<'0' || argv[i][0]>'9' || argv[i][1]!=0 ){
1507 fatal_error("argument to --temp should be integer between 0 and 9");
1508 }
1509 g.eTemp = argv[i][0] - '0';
drhad1ca9a2013-11-23 04:16:58 +00001510 }else if( strcmp(z,"testset")==0 ){
1511 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1512 zTSet = argv[++i];
1513 }else if( strcmp(z,"trace")==0 ){
1514 doTrace = 1;
drh46a06bb2014-04-18 13:57:39 +00001515 }else if( strcmp(z,"threads")==0 ){
1516 if( i>=argc-1 ) fatal_error("missing argument on %s\n", argv[i]);
1517 nThread = integerValue(argv[++i]);
drhad1ca9a2013-11-23 04:16:58 +00001518 }else if( strcmp(z,"utf16le")==0 ){
1519 zEncoding = "utf16le";
1520 }else if( strcmp(z,"utf16be")==0 ){
1521 zEncoding = "utf16be";
drh65e6b0d2014-04-28 17:56:19 +00001522 }else if( strcmp(z,"verify")==0 ){
1523 g.bVerify = 1;
drhad1ca9a2013-11-23 04:16:58 +00001524 }else if( strcmp(z,"without-rowid")==0 ){
1525 g.zWR = "WITHOUT ROWID";
1526 g.zPK = "PRIMARY KEY";
1527 }else if( strcmp(z, "help")==0 || strcmp(z,"?")==0 ){
1528 printf(zHelp, argv[0]);
1529 exit(0);
1530 }else{
1531 fatal_error("unknown option: %s\nUse \"%s -?\" for help\n",
1532 argv[i], argv[0]);
1533 }
1534 }else if( zDbName==0 ){
1535 zDbName = argv[i];
1536 }else{
1537 fatal_error("surplus argument: %s\nUse \"%s -?\" for help\n",
1538 argv[i], argv[0]);
1539 }
1540 }
drh741c2772016-04-12 17:13:33 +00001541 if( zDbName!=0 ) unlink(zDbName);
drh5995e292015-06-18 12:37:32 +00001542#if SQLITE_VERSION_NUMBER>=3006001
drhad1ca9a2013-11-23 04:16:58 +00001543 if( nHeap>0 ){
1544 pHeap = malloc( nHeap );
1545 if( pHeap==0 ) fatal_error("cannot allocate %d-byte heap\n", nHeap);
drhe19f8322013-11-23 11:45:58 +00001546 rc = sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nHeap, mnHeap);
drh7b65ad32013-11-23 21:29:07 +00001547 if( rc ) fatal_error("heap configuration failed: %d\n", rc);
drhad1ca9a2013-11-23 04:16:58 +00001548 }
drhee70a842015-07-06 18:54:52 +00001549 if( doPCache ){
1550 if( nPCache>0 && szPCache>0 ){
1551 pPCache = malloc( nPCache*(sqlite3_int64)szPCache );
1552 if( pPCache==0 ) fatal_error("cannot allocate %lld-byte pcache\n",
1553 nPCache*(sqlite3_int64)szPCache);
1554 }
drhe19f8322013-11-23 11:45:58 +00001555 rc = sqlite3_config(SQLITE_CONFIG_PAGECACHE, pPCache, szPCache, nPCache);
drh7b65ad32013-11-23 21:29:07 +00001556 if( rc ) fatal_error("pcache configuration failed: %d\n", rc);
drhad1ca9a2013-11-23 04:16:58 +00001557 }
drh93307e92013-11-24 01:14:14 +00001558 if( nScratch>0 && szScratch>0 ){
1559 pScratch = malloc( nScratch*(sqlite3_int64)szScratch );
1560 if( pScratch==0 ) fatal_error("cannot allocate %lld-byte scratch\n",
1561 nScratch*(sqlite3_int64)szScratch);
1562 rc = sqlite3_config(SQLITE_CONFIG_SCRATCH, pScratch, szScratch, nScratch);
1563 if( rc ) fatal_error("scratch configuration failed: %d\n", rc);
1564 }
drhaa430bf2016-12-31 18:37:50 +00001565 if( nLook>=0 ){
drhad1ca9a2013-11-23 04:16:58 +00001566 sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
1567 }
drh5995e292015-06-18 12:37:32 +00001568#endif
drhad1ca9a2013-11-23 04:16:58 +00001569
1570 /* Open the database and the input file */
1571 if( sqlite3_open(zDbName, &g.db) ){
1572 fatal_error("Cannot open database file: %s\n", zDbName);
1573 }
drh5995e292015-06-18 12:37:32 +00001574#if SQLITE_VERSION_NUMBER>=3006001
drhad1ca9a2013-11-23 04:16:58 +00001575 if( nLook>0 && szLook>0 ){
1576 pLook = malloc( nLook*szLook );
drhe19f8322013-11-23 11:45:58 +00001577 rc = sqlite3_db_config(g.db, SQLITE_DBCONFIG_LOOKASIDE, pLook, szLook,nLook);
drh7b65ad32013-11-23 21:29:07 +00001578 if( rc ) fatal_error("lookaside configuration failed: %d\n", rc);
drhad1ca9a2013-11-23 04:16:58 +00001579 }
drh5995e292015-06-18 12:37:32 +00001580#endif
drhad1ca9a2013-11-23 04:16:58 +00001581
1582 /* Set database connection options */
1583 sqlite3_create_function(g.db, "random", 0, SQLITE_UTF8, 0, randomFunc, 0, 0);
drh98ef26b2016-09-21 23:58:49 +00001584#ifndef SQLITE_OMIT_DEPRECATED
drhad1ca9a2013-11-23 04:16:58 +00001585 if( doTrace ) sqlite3_trace(g.db, traceCallback, 0);
drh98ef26b2016-09-21 23:58:49 +00001586#endif
drh26f41982016-12-12 23:24:08 +00001587 if( mmapSize>0 ){
1588 speedtest1_exec("PRAGMA mmap_size=%d", mmapSize);
1589 }
drh43cbe142014-08-29 18:06:33 +00001590 speedtest1_exec("PRAGMA threads=%d", nThread);
drhad1ca9a2013-11-23 04:16:58 +00001591 if( zKey ){
1592 speedtest1_exec("PRAGMA key('%s')", zKey);
1593 }
1594 if( zEncoding ){
1595 speedtest1_exec("PRAGMA encoding=%s", zEncoding);
1596 }
1597 if( doAutovac ){
1598 speedtest1_exec("PRAGMA auto_vacuum=FULL");
1599 }else if( doIncrvac ){
1600 speedtest1_exec("PRAGMA auto_vacuum=INCREMENTAL");
1601 }
1602 if( pageSize ){
1603 speedtest1_exec("PRAGMA page_size=%d", pageSize);
1604 }
1605 if( cacheSize ){
1606 speedtest1_exec("PRAGMA cache_size=%d", cacheSize);
1607 }
1608 if( noSync ) speedtest1_exec("PRAGMA synchronous=OFF");
1609 if( doExclusive ){
1610 speedtest1_exec("PRAGMA locking_mode=EXCLUSIVE");
1611 }
1612 if( zJMode ){
1613 speedtest1_exec("PRAGMA journal_mode=%s", zJMode);
1614 }
1615
drh849a9d92013-12-21 15:46:06 +00001616 if( g.bExplain ) printf(".explain\n.echo on\n");
drhad1ca9a2013-11-23 04:16:58 +00001617 if( strcmp(zTSet,"main")==0 ){
1618 testset_main();
1619 }else if( strcmp(zTSet,"debug1")==0 ){
1620 testset_debug1();
drhc4754802014-02-09 00:18:21 +00001621 }else if( strcmp(zTSet,"cte")==0 ){
1622 testset_cte();
drh65e6b0d2014-04-28 17:56:19 +00001623 }else if( strcmp(zTSet,"rtree")==0 ){
drh8683e082014-10-11 10:52:54 +00001624#ifdef SQLITE_ENABLE_RTREE
drh65e6b0d2014-04-28 17:56:19 +00001625 testset_rtree(6, 147);
drh8683e082014-10-11 10:52:54 +00001626#else
1627 fatal_error("compile with -DSQLITE_ENABLE_RTREE to enable "
1628 "the R-Tree tests\n");
1629#endif
drhad1ca9a2013-11-23 04:16:58 +00001630 }else{
drh65e6b0d2014-04-28 17:56:19 +00001631 fatal_error("unknown testset: \"%s\"\nChoices: main debug1 cte rtree\n",
1632 zTSet);
drhad1ca9a2013-11-23 04:16:58 +00001633 }
1634 speedtest1_final();
1635
1636 /* Database connection statistics printed after both prepared statements
1637 ** have been finalized */
drh290ea402013-12-01 18:10:01 +00001638#if SQLITE_VERSION_NUMBER>=3007009
drhad1ca9a2013-11-23 04:16:58 +00001639 if( showStats ){
1640 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_USED, &iCur, &iHi, 0);
1641 printf("-- Lookaside Slots Used: %d (max %d)\n", iCur,iHi);
1642 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_HIT, &iCur, &iHi, 0);
1643 printf("-- Successful lookasides: %d\n", iHi);
1644 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE, &iCur,&iHi,0);
1645 printf("-- Lookaside size faults: %d\n", iHi);
1646 sqlite3_db_status(g.db, SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL, &iCur,&iHi,0);
1647 printf("-- Lookaside OOM faults: %d\n", iHi);
1648 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_USED, &iCur, &iHi, 0);
1649 printf("-- Pager Heap Usage: %d bytes\n", iCur);
1650 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_HIT, &iCur, &iHi, 1);
1651 printf("-- Page cache hits: %d\n", iCur);
1652 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_MISS, &iCur, &iHi, 1);
drh2a702db2013-12-02 21:25:40 +00001653 printf("-- Page cache misses: %d\n", iCur);
1654#if SQLITE_VERSION_NUMBER>=3007012
drhad1ca9a2013-11-23 04:16:58 +00001655 sqlite3_db_status(g.db, SQLITE_DBSTATUS_CACHE_WRITE, &iCur, &iHi, 1);
1656 printf("-- Page cache writes: %d\n", iCur);
drh2a702db2013-12-02 21:25:40 +00001657#endif
drhad1ca9a2013-11-23 04:16:58 +00001658 sqlite3_db_status(g.db, SQLITE_DBSTATUS_SCHEMA_USED, &iCur, &iHi, 0);
1659 printf("-- Schema Heap Usage: %d bytes\n", iCur);
1660 sqlite3_db_status(g.db, SQLITE_DBSTATUS_STMT_USED, &iCur, &iHi, 0);
1661 printf("-- Statement Heap Usage: %d bytes\n", iCur);
1662 }
drh290ea402013-12-01 18:10:01 +00001663#endif
drhad1ca9a2013-11-23 04:16:58 +00001664
1665 sqlite3_close(g.db);
1666
drh5995e292015-06-18 12:37:32 +00001667#if SQLITE_VERSION_NUMBER>=3006001
drhad1ca9a2013-11-23 04:16:58 +00001668 /* Global memory usage statistics printed after the database connection
1669 ** has closed. Memory usage should be zero at this point. */
1670 if( showStats ){
1671 sqlite3_status(SQLITE_STATUS_MEMORY_USED, &iCur, &iHi, 0);
1672 printf("-- Memory Used (bytes): %d (max %d)\n", iCur,iHi);
drhd79e9c52013-12-02 01:24:05 +00001673#if SQLITE_VERSION_NUMBER>=3007000
drhad1ca9a2013-11-23 04:16:58 +00001674 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &iCur, &iHi, 0);
1675 printf("-- Outstanding Allocations: %d (max %d)\n", iCur,iHi);
drhd79e9c52013-12-02 01:24:05 +00001676#endif
drhad1ca9a2013-11-23 04:16:58 +00001677 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW, &iCur, &iHi, 0);
1678 printf("-- Pcache Overflow Bytes: %d (max %d)\n", iCur,iHi);
1679 sqlite3_status(SQLITE_STATUS_SCRATCH_OVERFLOW, &iCur, &iHi, 0);
1680 printf("-- Scratch Overflow Bytes: %d (max %d)\n", iCur,iHi);
1681 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE, &iCur, &iHi, 0);
1682 printf("-- Largest Allocation: %d bytes\n",iHi);
1683 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE, &iCur, &iHi, 0);
1684 printf("-- Largest Pcache Allocation: %d bytes\n",iHi);
1685 sqlite3_status(SQLITE_STATUS_SCRATCH_SIZE, &iCur, &iHi, 0);
1686 printf("-- Largest Scratch Allocation: %d bytes\n", iHi);
1687 }
drh5995e292015-06-18 12:37:32 +00001688#endif
drhad1ca9a2013-11-23 04:16:58 +00001689
drhfc1a84c2016-02-27 19:19:22 +00001690#ifdef __linux__
1691 if( showStats ){
1692 displayLinuxIoStats(stdout);
1693 }
1694#endif
1695
drhad1ca9a2013-11-23 04:16:58 +00001696 /* Release memory */
1697 free( pLook );
1698 free( pPCache );
drh93307e92013-11-24 01:14:14 +00001699 free( pScratch );
drhad1ca9a2013-11-23 04:16:58 +00001700 free( pHeap );
1701 return 0;
1702}