4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file implements "key-value" performance test for SQLite. The
14 ** purpose is to compare the speed of SQLite for accessing large BLOBs
15 ** versus reading those same BLOB values out of individual files in the
18 ** Run "kvtest" with no arguments for on-line help, or see comments below.
22 ** (1) Gather this source file and a recent SQLite3 amalgamation with its
23 ** header into the working directory. You should have:
25 ** kvtest.c >--- this file
26 ** sqlite3.c \___ SQLite
27 ** sqlite3.h / amlagamation & header
29 ** (2) Run you compiler against the two C source code files.
31 ** (a) On linux or mac:
33 ** OPTS="-DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION"
34 ** gcc -Os -I. $OPTS kvtest.c sqlite3.c -o kvtest
36 ** The $OPTS options can be omitted. The $OPTS merely omit
37 ** the need to link against -ldl and -lpthread, or whatever
38 ** the equivalent libraries are called on your system.
40 ** (b) Windows with MSVC:
42 ** cl -I. kvtest.c sqlite3.c
46 ** (1) Create a test database by running "kvtest init" with appropriate
47 ** options. See the help message for available options.
49 ** (2) Construct the corresponding pile-of-files database on disk using
50 ** the "kvtest export" command.
52 ** (3) Run tests using "kvtest run" against either the SQLite database or
53 ** the pile-of-files database and with appropriate options.
57 ** ./kvtest init x1.db --count 100000 --size 10000
59 ** ./kvtest export x1.db x1
60 ** ./kvtest run x1.db --count 10000 --max-id 1000000
61 ** ./kvtest run x1 --count 10000 --max-id 1000000
63 static const char zHelp
[] =
64 "Usage: kvtest COMMAND ARGS...\n"
66 " kvtest init DBFILE --count N --size M --pagesize X\n"
68 " Generate a new test database file named DBFILE containing N\n"
69 " BLOBs each of size M bytes. The page size of the new database\n"
70 " file will be X. Additional options:\n"
72 " --variance V Randomly vary M by plus or minus V\n"
74 " kvtest export DBFILE DIRECTORY [--tree]\n"
76 " Export all the blobs in the kv table of DBFILE into separate\n"
77 " files in DIRECTORY. DIRECTORY is created if it does not previously\n"
78 " exist. If the --tree option is used, then the blobs are written\n"
79 " into a hierarchy of directories, using names like 00/00/00,\n"
80 " 00/00/01, 00/00/02, and so forth. Without the --tree option, all\n"
81 " files are in the top-level directory with names like 000000, 000001,\n"
82 " 000002, and so forth.\n"
84 " kvtest stat DBFILE [options]\n"
86 " Display summary information about DBFILE. Options:\n"
88 " --vacuum Run VACUUM on the database file\n"
90 " kvtest run DBFILE [options]\n"
92 " Run a performance test. DBFILE can be either the name of a\n"
93 " database or a directory containing sample files. Options:\n"
95 " --asc Read blobs in ascending order\n"
96 " --blob-api Use the BLOB API\n"
97 " --cache-size N Database cache size\n"
98 " --count N Read N blobs\n"
99 " --desc Read blobs in descending order\n"
100 " --fsync Synchronous file writes\n"
101 " --integrity-check Run \"PRAGMA integrity_check\" after test\n"
102 " --max-id N Maximum blob key to use\n"
103 " --mmap N Mmap as much as N bytes of DBFILE\n"
104 " --multitrans Each read or write in its own transaction\n"
105 " --nocheckpoint Omit the checkpoint on WAL mode writes\n"
106 " --nosync Set \"PRAGMA synchronous=OFF\"\n"
107 " --jmode MODE Set MODE journal mode prior to starting\n"
108 " --random Read blobs in a random order\n"
109 " --start N Start reading with this blob key\n"
110 " --stats Output operating stats before exiting\n"
111 " --update Do an overwrite test\n"
114 /* Reference resources used */
117 #include <sys/types.h>
118 #include <sys/stat.h>
126 /* Provide Windows equivalent for the needed parts of unistd.h */
130 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
131 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
132 # define access _access
138 ** The following macros are used to cast pointers to integers and
139 ** integers to pointers. The way you do this varies from one compiler
140 ** to the next, so we have developed the following set of #if statements
141 ** to generate appropriate macros for a wide range of compilers.
143 ** The correct "ANSI" way to do this is to use the intptr_t type.
144 ** Unfortunately, that typedef is not available on all compilers, or
145 ** if it is available, it requires an #include of specific headers
146 ** that vary from one machine to the next.
148 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on
149 ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)).
150 ** So we have to define the macros in different ways depending on the
153 #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */
154 # define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X))
155 # define SQLITE_PTR_TO_INT(X) ((sqlite3_int64)(__PTRDIFF_TYPE__)(X))
157 # define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X))
158 # define SQLITE_PTR_TO_INT(X) ((sqlite3_int64)(intptr_t)(X))
162 ** Show thqe help text and quit.
164 static void showHelp(void){
165 fprintf(stdout
, "%s", zHelp
);
170 ** Show an error message an quit.
172 static void fatalError(const char *zFormat
, ...){
174 fprintf(stdout
, "ERROR: ");
175 va_start(ap
, zFormat
);
176 vfprintf(stdout
, zFormat
, ap
);
178 fprintf(stdout
, "\n");
183 ** Return the value of a hexadecimal digit. Return -1 if the input
184 ** is not a hex digit.
186 static int hexDigitValue(char c
){
187 if( c
>='0' && c
<='9' ) return c
- '0';
188 if( c
>='a' && c
<='f' ) return c
- 'a' + 10;
189 if( c
>='A' && c
<='F' ) return c
- 'A' + 10;
194 ** Interpret zArg as an integer value, possibly with suffixes.
196 static int integerValue(const char *zArg
){
198 static const struct { char *zSuffix
; int iMult
; } aMult
[] = {
200 { "MiB", 1024*1024 },
201 { "GiB", 1024*1024*1024 },
204 { "GB", 1000000000 },
214 }else if( zArg
[0]=='+' ){
217 if( zArg
[0]=='0' && zArg
[1]=='x' ){
220 while( (x
= hexDigitValue(zArg
[0]))>=0 ){
225 while( zArg
[0]>='0' && zArg
[0]<='9' ){
226 v
= v
*10 + zArg
[0] - '0';
230 for(i
=0; i
<sizeof(aMult
)/sizeof(aMult
[0]); i
++){
231 if( sqlite3_stricmp(aMult
[i
].zSuffix
, zArg
)==0 ){
236 return isNeg
? -v
: v
;
241 ** Check the filesystem object zPath. Determine what it is:
243 ** PATH_DIR A single directory holding many files
244 ** PATH_TREE A directory hierarchy with files at the leaves
245 ** PATH_DB An SQLite database
246 ** PATH_NEXIST Does not exist
247 ** PATH_OTHER Something else
249 ** PATH_DIR means all of the separate files are grouped together
250 ** into a single directory with names like 000000, 000001, 000002, and
251 ** so forth. PATH_TREE means there is a hierarchy of directories so
252 ** that no single directory has too many entries. The files have names
253 ** like 00/00/00, 00/00/01, 00/00/02 and so forth. The decision between
254 ** PATH_DIR and PATH_TREE is determined by the presence of a subdirectory
255 ** named "00" at the top-level.
260 #define PATH_NEXIST 0
261 #define PATH_OTHER 99
262 static int pathType(const char *zPath
){
265 if( access(zPath
,R_OK
) ) return PATH_NEXIST
;
266 memset(&x
, 0, sizeof(x
));
267 rc
= stat(zPath
, &x
);
268 if( rc
<0 ) return PATH_OTHER
;
269 if( S_ISDIR(x
.st_mode
) ){
270 char *zLayer1
= sqlite3_mprintf("%s/00", zPath
);
271 memset(&x
, 0, sizeof(x
));
272 rc
= stat(zLayer1
, &x
);
273 sqlite3_free(zLayer1
);
274 if( rc
<0 ) return PATH_DIR
;
275 if( S_ISDIR(x
.st_mode
) ) return PATH_TREE
;
278 if( (x
.st_size
%512)==0 ) return PATH_DB
;
283 ** Return the size of a file in bytes. Or return -1 if the
284 ** named object is not a regular file or does not exist.
286 static sqlite3_int64
fileSize(const char *zPath
){
289 memset(&x
, 0, sizeof(x
));
290 rc
= stat(zPath
, &x
);
291 if( rc
<0 ) return -1;
292 if( !S_ISREG(x
.st_mode
) ) return -1;
297 ** A Pseudo-random number generator with a fixed seed. Use this so
298 ** that the same sequence of "random" numbers are generated on each
299 ** run, for repeatability.
301 static unsigned int randInt(void){
302 static unsigned int x
= 0x333a13cd;
303 static unsigned int y
= 0xecb2adea;
304 x
= (x
>>1) ^ ((1+~(x
&1)) & 0xd0000001);
305 y
= y
*1103515245 + 12345;
310 ** Do database initialization.
312 static int initMain(int argc
, char **argv
){
323 assert( strcmp(argv
[1],"init")==0 );
326 for(i
=3; i
<argc
; i
++){
328 if( z
[0]!='-' ) fatalError("unknown argument: \"%s\"", z
);
330 if( strcmp(z
, "-count")==0 ){
331 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
332 nCount
= integerValue(argv
[++i
]);
333 if( nCount
<1 ) fatalError("the --count must be positive");
336 if( strcmp(z
, "-size")==0 ){
337 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
338 sz
= integerValue(argv
[++i
]);
339 if( sz
<1 ) fatalError("the --size must be positive");
342 if( strcmp(z
, "-variance")==0 ){
343 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
344 iVariance
= integerValue(argv
[++i
]);
347 if( strcmp(z
, "-pagesize")==0 ){
348 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
349 pgsz
= integerValue(argv
[++i
]);
350 if( pgsz
<512 || pgsz
>65536 || ((pgsz
-1)&pgsz
)!=0 ){
351 fatalError("the --pagesize must be power of 2 between 512 and 65536");
355 fatalError("unknown option: \"%s\"", argv
[i
]);
357 rc
= sqlite3_open(zDb
, &db
);
359 fatalError("cannot open database \"%s\": %s", zDb
, sqlite3_errmsg(db
));
361 zSql
= sqlite3_mprintf(
362 "DROP TABLE IF EXISTS kv;\n"
363 "PRAGMA page_size=%d;\n"
366 "CREATE TABLE kv(k INTEGER PRIMARY KEY, v BLOB);\n"
367 "WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<%d)"
368 " INSERT INTO kv(k,v) SELECT x, randomblob(%d+(random()%%(%d))) FROM c;\n"
370 pgsz
, nCount
, sz
, iVariance
+1
372 rc
= sqlite3_exec(db
, zSql
, 0, 0, &zErrMsg
);
373 if( rc
) fatalError("database create failed: %s", zErrMsg
);
380 ** Analyze an existing database file. Report its content.
382 static int statMain(int argc
, char **argv
){
390 assert( strcmp(argv
[1],"stat")==0 );
393 for(i
=3; i
<argc
; i
++){
395 if( z
[0]!='-' ) fatalError("unknown argument: \"%s\"", z
);
397 if( strcmp(z
, "-vacuum")==0 ){
401 fatalError("unknown option: \"%s\"", argv
[i
]);
403 rc
= sqlite3_open(zDb
, &db
);
405 fatalError("cannot open database \"%s\": %s", zDb
, sqlite3_errmsg(db
));
408 printf("Vacuuming...."); fflush(stdout
);
409 sqlite3_exec(db
, "VACUUM", 0, 0, 0);
412 zSql
= sqlite3_mprintf(
413 "SELECT count(*), min(length(v)), max(length(v)), avg(length(v))"
416 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
417 if( rc
) fatalError("cannot prepare SQL [%s]: %s", zSql
, sqlite3_errmsg(db
));
419 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
420 printf("Number of entries: %8d\n", sqlite3_column_int(pStmt
, 0));
421 printf("Average value size: %8d\n", sqlite3_column_int(pStmt
, 3));
422 printf("Minimum value size: %8d\n", sqlite3_column_int(pStmt
, 1));
423 printf("Maximum value size: %8d\n", sqlite3_column_int(pStmt
, 2));
427 sqlite3_finalize(pStmt
);
428 zSql
= sqlite3_mprintf("PRAGMA page_size");
429 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
430 if( rc
) fatalError("cannot prepare SQL [%s]: %s", zSql
, sqlite3_errmsg(db
));
432 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
433 printf("Page-size: %8d\n", sqlite3_column_int(pStmt
, 0));
435 sqlite3_finalize(pStmt
);
436 zSql
= sqlite3_mprintf("PRAGMA page_count");
437 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
438 if( rc
) fatalError("cannot prepare SQL [%s]: %s", zSql
, sqlite3_errmsg(db
));
440 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
441 printf("Page-count: %8d\n", sqlite3_column_int(pStmt
, 0));
443 sqlite3_finalize(pStmt
);
444 zSql
= sqlite3_mprintf("PRAGMA freelist_count");
445 rc
= sqlite3_prepare_v2(db
, zSql
, -1, &pStmt
, 0);
446 if( rc
) fatalError("cannot prepare SQL [%s]: %s", zSql
, sqlite3_errmsg(db
));
448 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
449 printf("Freelist-count: %8d\n", sqlite3_column_int(pStmt
, 0));
451 sqlite3_finalize(pStmt
);
452 rc
= sqlite3_prepare_v2(db
, "PRAGMA integrity_check(10)", -1, &pStmt
, 0);
453 if( rc
) fatalError("cannot prepare integrity check: %s", sqlite3_errmsg(db
));
454 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
455 printf("Integrity-check: %s\n", sqlite3_column_text(pStmt
, 0));
457 sqlite3_finalize(pStmt
);
465 ** Return the integer value V. Also save the value of V in a
466 ** C-language variable whose address is PTR.
468 static void rememberFunc(
469 sqlite3_context
*pCtx
,
476 v
= sqlite3_value_int64(argv
[0]);
477 ptr
= sqlite3_value_int64(argv
[1]);
478 *(sqlite3_int64
*)SQLITE_INT_TO_PTR(ptr
) = v
;
479 sqlite3_result_int64(pCtx
, v
);
483 ** Make sure a directory named zDir exists.
485 static void kvtest_mkdir(const char *zDir
){
489 (void)mkdir(zDir
, 0755);
494 ** Export the kv table to individual files in the filesystem
496 static int exportMain(int argc
, char **argv
){
509 assert( strcmp(argv
[1],"export")==0 );
511 if( argc
<4 ) fatalError("Usage: kvtest export DATABASE DIRECTORY [OPTIONS]");
515 for(i
=4; i
<argc
; i
++){
516 const char *z
= argv
[i
];
517 if( z
[0]=='-' && z
[1]=='-' ) z
++;
518 if( strcmp(z
,"-tree")==0 ){
519 zFN
= sqlite3_mprintf("%s/00", zDir
);
524 fatalError("unknown argument: \"%s\"\n", argv
[i
]);
526 ePathType
= pathType(zDir
);
527 if( ePathType
!=PATH_DIR
&& ePathType
!=PATH_TREE
){
528 fatalError("object \"%s\" is not a directory", zDir
);
530 rc
= sqlite3_open(zDb
, &db
);
532 fatalError("cannot open database \"%s\": %s", zDb
, sqlite3_errmsg(db
));
534 rc
= sqlite3_prepare_v2(db
, "SELECT k, v FROM kv ORDER BY k", -1, &pStmt
, 0);
536 fatalError("prepare_v2 failed: %s\n", sqlite3_errmsg(db
));
538 nFN
= (int)strlen(zDir
);
539 zFN
= sqlite3_mprintf("%s/00/00/00.extra---------------------", zDir
);
541 fatalError("malloc failed\n");
543 zTail
= zFN
+ nFN
+ 1;
544 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
545 int iKey
= sqlite3_column_int(pStmt
, 0);
546 sqlite3_int64 nData
= sqlite3_column_bytes(pStmt
, 1);
547 const void *pData
= sqlite3_column_blob(pStmt
, 1);
549 if( ePathType
==PATH_DIR
){
550 sqlite3_snprintf(20, zTail
, "%06d", iKey
);
552 sqlite3_snprintf(20, zTail
, "%02d", iKey
/10000);
554 sqlite3_snprintf(20, zTail
, "%02d/%02d", iKey
/10000, (iKey
/100)%100);
556 sqlite3_snprintf(20, zTail
, "%02d/%02d/%02d",
557 iKey
/10000, (iKey
/100)%100, iKey
%100);
559 out
= fopen(zFN
, "wb");
560 nWrote
= fwrite(pData
, 1, nData
, out
);
562 printf("\r%s ", zTail
); fflush(stdout
);
564 fatalError("Wrote only %d of %d bytes to %s\n",
565 (int)nWrote
, nData
, zFN
);
568 sqlite3_finalize(pStmt
);
576 ** Read the content of file zName into memory obtained from sqlite3_malloc64()
577 ** and return a pointer to the buffer. The caller is responsible for freeing
580 ** If parameter pnByte is not NULL, (*pnByte) is set to the number of bytes
583 ** For convenience, a nul-terminator byte is always appended to the data read
584 ** from the file before the buffer is returned. This byte is not included in
585 ** the final value of (*pnByte), if applicable.
587 ** NULL is returned if any error is encountered. The final value of *pnByte
588 ** is undefined in this case.
590 static unsigned char *readFile(const char *zName
, sqlite3_int64
*pnByte
){
591 FILE *in
; /* FILE from which to read content of zName */
592 sqlite3_int64 nIn
; /* Size of zName in bytes */
593 size_t nRead
; /* Number of bytes actually read */
594 unsigned char *pBuf
; /* Content read from disk */
596 nIn
= fileSize(zName
);
597 if( nIn
<0 ) return 0;
598 in
= fopen(zName
, "rb");
599 if( in
==0 ) return 0;
600 pBuf
= sqlite3_malloc64( nIn
);
601 if( pBuf
==0 ) return 0;
602 nRead
= fread(pBuf
, (size_t)nIn
, 1, in
);
608 if( pnByte
) *pnByte
= nIn
;
613 ** Overwrite a file with randomness. Do not change the size of the
616 static void updateFile(const char *zName
, sqlite3_int64
*pnByte
, int doFsync
){
617 FILE *out
; /* FILE from which to read content of zName */
618 sqlite3_int64 sz
; /* Size of zName in bytes */
619 size_t nWritten
; /* Number of bytes actually read */
620 unsigned char *pBuf
; /* Content to store on disk */
621 const char *zMode
= "wb"; /* Mode for fopen() */
623 sz
= fileSize(zName
);
625 fatalError("No such file: \"%s\"", zName
);
629 pBuf
= sqlite3_malloc64( sz
);
631 fatalError("Cannot allocate %lld bytes\n", sz
);
633 sqlite3_randomness((int)sz
, pBuf
);
635 if( doFsync
) zMode
= "wbc";
637 out
= fopen(zName
, zMode
);
639 fatalError("Cannot open \"%s\" for writing\n", zName
);
641 nWritten
= fwrite(pBuf
, 1, (size_t)sz
, out
);
650 if( nWritten
!=(size_t)sz
){
651 fatalError("Wrote only %d of %d bytes to \"%s\"\n",
652 (int)nWritten
, (int)sz
, zName
);
658 ** Return the current time in milliseconds since the beginning of
661 static sqlite3_int64
timeOfDay(void){
662 static sqlite3_vfs
*clockVfs
= 0;
664 if( clockVfs
==0 ) clockVfs
= sqlite3_vfs_find(0);
665 if( clockVfs
->iVersion
>=2 && clockVfs
->xCurrentTimeInt64
!=0 ){
666 clockVfs
->xCurrentTimeInt64(clockVfs
, &t
);
669 clockVfs
->xCurrentTime(clockVfs
, &r
);
670 t
= (sqlite3_int64
)(r
*86400000.0);
677 ** Attempt to display I/O stats on Linux using /proc/PID/io
679 static void displayLinuxIoStats(FILE *out
){
682 sqlite3_snprintf(sizeof(z
), z
, "/proc/%d/io", getpid());
685 while( fgets(z
, sizeof(z
), in
)!=0 ){
686 static const struct {
687 const char *zPattern
;
690 { "rchar: ", "Bytes received by read():" },
691 { "wchar: ", "Bytes sent to write():" },
692 { "syscr: ", "Read() system calls:" },
693 { "syscw: ", "Write() system calls:" },
694 { "read_bytes: ", "Bytes read from storage:" },
695 { "write_bytes: ", "Bytes written to storage:" },
696 { "cancelled_write_bytes: ", "Cancelled write bytes:" },
699 for(i
=0; i
<sizeof(aTrans
)/sizeof(aTrans
[0]); i
++){
700 int n
= (int)strlen(aTrans
[i
].zPattern
);
701 if( strncmp(aTrans
[i
].zPattern
, z
, n
)==0 ){
702 fprintf(out
, "%-36s %s", aTrans
[i
].zDesc
, &z
[n
]);
712 ** Display memory stats.
714 static int display_stats(
715 sqlite3
*db
, /* Database to query */
716 int bReset
/* True to reset SQLite stats */
725 sqlite3_status(SQLITE_STATUS_MEMORY_USED
, &iCur
, &iHiwtr
, bReset
);
727 "Memory Used: %d (max %d) bytes\n",
730 sqlite3_status(SQLITE_STATUS_MALLOC_COUNT
, &iCur
, &iHiwtr
, bReset
);
731 fprintf(out
, "Number of Outstanding Allocations: %d (max %d)\n",
734 sqlite3_status(SQLITE_STATUS_PAGECACHE_USED
, &iCur
, &iHiwtr
, bReset
);
736 "Number of Pcache Pages Used: %d (max %d) pages\n",
739 sqlite3_status(SQLITE_STATUS_PAGECACHE_OVERFLOW
, &iCur
, &iHiwtr
, bReset
);
741 "Number of Pcache Overflow Bytes: %d (max %d) bytes\n",
744 sqlite3_status(SQLITE_STATUS_MALLOC_SIZE
, &iCur
, &iHiwtr
, bReset
);
745 fprintf(out
, "Largest Allocation: %d bytes\n",
748 sqlite3_status(SQLITE_STATUS_PAGECACHE_SIZE
, &iCur
, &iHiwtr
, bReset
);
749 fprintf(out
, "Largest Pcache Allocation: %d bytes\n",
753 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_USED
, &iCur
, &iHiwtr
, bReset
);
754 fprintf(out
, "Pager Heap Usage: %d bytes\n",
757 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_HIT
, &iCur
, &iHiwtr
, 1);
758 fprintf(out
, "Page cache hits: %d\n", iCur
);
760 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_MISS
, &iCur
, &iHiwtr
, 1);
761 fprintf(out
, "Page cache misses: %d\n", iCur
);
763 sqlite3_db_status(db
, SQLITE_DBSTATUS_CACHE_WRITE
, &iCur
, &iHiwtr
, 1);
764 fprintf(out
, "Page cache writes: %d\n", iCur
);
768 displayLinuxIoStats(out
);
774 /* Blob access order */
777 #define ORDER_RANDOM 3
781 ** Run a performance test
783 static int runMain(int argc
, char **argv
){
784 int eType
; /* Is zDb a database or a directory? */
785 char *zDb
; /* Database or directory name */
786 int i
; /* Loop counter */
787 int rc
; /* Return code from SQLite calls */
788 int nCount
= 1000; /* Number of blob fetch operations */
789 int nExtra
= 0; /* Extra cycles */
790 int iKey
= 1; /* Next blob key */
791 int iMax
= 0; /* Largest allowed key */
792 int iPagesize
= 0; /* Database page size */
793 int iCache
= 1000; /* Database cache size in kibibytes */
794 int bBlobApi
= 0; /* Use the incremental blob I/O API */
795 int bStats
= 0; /* Print stats before exiting */
796 int eOrder
= ORDER_ASC
; /* Access order */
797 int isUpdateTest
= 0; /* Do in-place updates rather than reads */
798 int doIntegrityCk
= 0; /* Run PRAGMA integrity_check after the test */
799 int noSync
= 0; /* Disable synchronous mode */
800 int doFsync
= 0; /* Update disk files synchronously */
801 int doMultiTrans
= 0; /* Each operation in its own transaction */
802 int noCheckpoint
= 0; /* Omit the checkpoint in WAL mode */
803 sqlite3
*db
= 0; /* Database connection */
804 sqlite3_stmt
*pStmt
= 0; /* Prepared statement for SQL access */
805 sqlite3_blob
*pBlob
= 0; /* Handle for incremental Blob I/O */
806 sqlite3_int64 tmStart
; /* Start time */
807 sqlite3_int64 tmElapsed
; /* Elapsed time */
808 int mmapSize
= 0; /* --mmap N argument */
809 sqlite3_int64 nData
= 0; /* Bytes of data */
810 sqlite3_int64 nTotal
= 0; /* Total data read */
811 unsigned char *pData
= 0; /* Content of the blob */
812 sqlite3_int64 nAlloc
= 0; /* Space allocated for pData[] */
813 const char *zJMode
= 0; /* Journal mode */
816 assert( strcmp(argv
[1],"run")==0 );
819 eType
= pathType(zDb
);
820 if( eType
==PATH_OTHER
) fatalError("unknown object type: \"%s\"", zDb
);
821 if( eType
==PATH_NEXIST
) fatalError("object does not exist: \"%s\"", zDb
);
822 for(i
=3; i
<argc
; i
++){
824 if( z
[0]!='-' ) fatalError("unknown argument: \"%s\"", z
);
826 if( strcmp(z
, "-asc")==0 ){
830 if( strcmp(z
, "-blob-api")==0 ){
834 if( strcmp(z
, "-cache-size")==0 ){
835 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
836 iCache
= integerValue(argv
[++i
]);
839 if( strcmp(z
, "-count")==0 ){
840 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
841 nCount
= integerValue(argv
[++i
]);
842 if( nCount
<1 ) fatalError("the --count must be positive");
845 if( strcmp(z
, "-desc")==0 ){
849 if( strcmp(z
, "-fsync")==0 ){
853 if( strcmp(z
, "-integrity-check")==0 ){
857 if( strcmp(z
, "-jmode")==0 ){
858 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
862 if( strcmp(z
, "-mmap")==0 ){
863 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
864 mmapSize
= integerValue(argv
[++i
]);
865 if( nCount
<0 ) fatalError("the --mmap must be non-negative");
868 if( strcmp(z
, "-max-id")==0 ){
869 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
870 iMax
= integerValue(argv
[++i
]);
873 if( strcmp(z
, "-multitrans")==0 ){
877 if( strcmp(z
, "-nocheckpoint")==0 ){
881 if( strcmp(z
, "-nosync")==0 ){
885 if( strcmp(z
, "-random")==0 ){
886 eOrder
= ORDER_RANDOM
;
889 if( strcmp(z
, "-start")==0 ){
890 if( i
==argc
-1 ) fatalError("missing argument on \"%s\"", argv
[i
]);
891 iKey
= integerValue(argv
[++i
]);
892 if( iKey
<1 ) fatalError("the --start must be positive");
895 if( strcmp(z
, "-stats")==0 ){
899 if( strcmp(z
, "-update")==0 ){
903 fatalError("unknown option: \"%s\"", argv
[i
]);
905 if( eType
==PATH_DB
){
906 /* Recover any prior crashes prior to starting the timer */
907 sqlite3_open(zDb
, &db
);
908 sqlite3_exec(db
, "SELECT rowid FROM sqlite_master LIMIT 1", 0, 0, 0);
912 tmStart
= timeOfDay();
913 if( eType
==PATH_DB
){
915 rc
= sqlite3_open(zDb
, &db
);
917 fatalError("cannot open database \"%s\": %s", zDb
, sqlite3_errmsg(db
));
919 zSql
= sqlite3_mprintf("PRAGMA mmap_size=%d", mmapSize
);
920 sqlite3_exec(db
, zSql
, 0, 0, 0);
922 zSql
= sqlite3_mprintf("PRAGMA cache_size=%d", iCache
);
923 sqlite3_exec(db
, zSql
, 0, 0, 0);
926 sqlite3_exec(db
, "PRAGMA synchronous=OFF", 0, 0, 0);
929 sqlite3_prepare_v2(db
, "PRAGMA page_size", -1, &pStmt
, 0);
930 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
931 iPagesize
= sqlite3_column_int(pStmt
, 0);
933 sqlite3_finalize(pStmt
);
934 sqlite3_prepare_v2(db
, "PRAGMA cache_size", -1, &pStmt
, 0);
935 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
936 iCache
= sqlite3_column_int(pStmt
, 0);
940 sqlite3_finalize(pStmt
);
943 zSql
= sqlite3_mprintf("PRAGMA journal_mode=%Q", zJMode
);
944 sqlite3_exec(db
, zSql
, 0, 0, 0);
947 sqlite3_exec(db
, "PRAGMA wal_autocheckpoint=0", 0, 0, 0);
950 sqlite3_prepare_v2(db
, "PRAGMA journal_mode", -1, &pStmt
, 0);
951 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
952 zJMode
= sqlite3_mprintf("%s", sqlite3_column_text(pStmt
, 0));
956 sqlite3_finalize(pStmt
);
958 sqlite3_prepare_v2(db
, "SELECT max(k) FROM kv", -1, &pStmt
, 0);
959 if( sqlite3_step(pStmt
)==SQLITE_ROW
){
960 iMax
= sqlite3_column_int(pStmt
, 0);
962 sqlite3_finalize(pStmt
);
965 if( !doMultiTrans
) sqlite3_exec(db
, "BEGIN", 0, 0, 0);
967 if( iMax
<=0 ) iMax
= 1000;
968 for(i
=0; i
<nCount
; i
++){
969 if( eType
==PATH_DIR
|| eType
==PATH_TREE
){
970 /* CASE 1: Reading or writing blobs out of separate files */
972 if( eType
==PATH_DIR
){
973 zKey
= sqlite3_mprintf("%s/%06d", zDb
, iKey
);
975 zKey
= sqlite3_mprintf("%s/%02d/%02d/%02d", zDb
, iKey
/10000,
976 (iKey
/100)%100, iKey
%100);
980 updateFile(zKey
, &nData
, doFsync
);
982 pData
= readFile(zKey
, &nData
);
986 }else if( bBlobApi
){
987 /* CASE 2: Reading from database using the incremental BLOB I/O API */
989 rc
= sqlite3_blob_open(db
, "main", "kv", "v", iKey
,
990 isUpdateTest
, &pBlob
);
992 fatalError("could not open sqlite3_blob handle: %s",
996 rc
= sqlite3_blob_reopen(pBlob
, iKey
);
999 nData
= sqlite3_blob_bytes(pBlob
);
1000 if( nAlloc
<nData
+1 ){
1002 pData
= sqlite3_realloc64(pData
, nAlloc
);
1004 if( pData
==0 ) fatalError("cannot allocate %d bytes", nData
+1);
1006 sqlite3_randomness((int)nData
, pData
);
1007 rc
= sqlite3_blob_write(pBlob
, pData
, (int)nData
, 0);
1008 if( rc
!=SQLITE_OK
){
1009 fatalError("could not write the blob at %d: %s", iKey
,
1010 sqlite3_errmsg(db
));
1013 rc
= sqlite3_blob_read(pBlob
, pData
, (int)nData
, 0);
1014 if( rc
!=SQLITE_OK
){
1015 fatalError("could not read the blob at %d: %s", iKey
,
1016 sqlite3_errmsg(db
));
1021 /* CASE 3: Reading from database using SQL */
1024 sqlite3_create_function(db
, "remember", 2, SQLITE_UTF8
, 0,
1025 rememberFunc
, 0, 0);
1027 rc
= sqlite3_prepare_v2(db
,
1028 "UPDATE kv SET v=randomblob(remember(length(v),?2))"
1029 " WHERE k=?1", -1, &pStmt
, 0);
1030 sqlite3_bind_int64(pStmt
, 2, SQLITE_PTR_TO_INT(&nData
));
1032 rc
= sqlite3_prepare_v2(db
,
1033 "SELECT v FROM kv WHERE k=?1", -1, &pStmt
, 0);
1036 fatalError("cannot prepare query: %s", sqlite3_errmsg(db
));
1039 sqlite3_reset(pStmt
);
1041 sqlite3_bind_int(pStmt
, 1, iKey
);
1043 rc
= sqlite3_step(pStmt
);
1044 if( rc
==SQLITE_ROW
){
1045 nData
= sqlite3_column_bytes(pStmt
, 0);
1046 pData
= (unsigned char*)sqlite3_column_blob(pStmt
, 0);
1049 if( eOrder
==ORDER_ASC
){
1051 if( iKey
>iMax
) iKey
= 1;
1052 }else if( eOrder
==ORDER_DESC
){
1054 if( iKey
<=0 ) iKey
= iMax
;
1056 iKey
= (randInt()%iMax
)+1;
1059 if( nData
==0 ){ nCount
++; nExtra
++; }
1061 if( nAlloc
) sqlite3_free(pData
);
1062 if( pStmt
) sqlite3_finalize(pStmt
);
1063 if( pBlob
) sqlite3_blob_close(pBlob
);
1065 display_stats(db
, 0);
1068 if( !doMultiTrans
) sqlite3_exec(db
, "COMMIT", 0, 0, 0);
1069 if( !noCheckpoint
){
1074 tmElapsed
= timeOfDay() - tmStart
;
1075 if( db
&& noCheckpoint
){
1080 printf("%d cycles due to %d misses\n", nCount
, nExtra
);
1082 if( eType
==PATH_DB
){
1083 printf("SQLite version: %s\n", sqlite3_libversion());
1084 if( doIntegrityCk
){
1085 sqlite3_open(zDb
, &db
);
1086 sqlite3_prepare_v2(db
, "PRAGMA integrity_check", -1, &pStmt
, 0);
1087 while( sqlite3_step(pStmt
)==SQLITE_ROW
){
1088 printf("integrity-check: %s\n", sqlite3_column_text(pStmt
, 0));
1090 sqlite3_finalize(pStmt
);
1095 printf("--count %d --max-id %d", nCount
-nExtra
, iMax
);
1097 case ORDER_RANDOM
: printf(" --random\n"); break;
1098 case ORDER_DESC
: printf(" --desc\n"); break;
1099 default: printf(" --asc\n"); break;
1101 if( eType
==PATH_DB
){
1102 printf("--cache-size %d --jmode %s\n", iCache
, zJMode
);
1103 printf("--mmap %d%s\n", mmapSize
, bBlobApi
? " --blob-api" : "");
1104 if( noSync
) printf("--nosync\n");
1106 if( iPagesize
) printf("Database page size: %d\n", iPagesize
);
1107 printf("Total elapsed time: %.3f\n", tmElapsed
/1000.0);
1109 printf("Microseconds per BLOB write: %.3f\n", tmElapsed
*1000.0/nCount
);
1110 printf("Content write rate: %.1f MB/s\n", nTotal
/(1000.0*tmElapsed
));
1112 printf("Microseconds per BLOB read: %.3f\n", tmElapsed
*1000.0/nCount
);
1113 printf("Content read rate: %.1f MB/s\n", nTotal
/(1000.0*tmElapsed
));
1119 int main(int argc
, char **argv
){
1120 if( argc
<3 ) showHelp();
1121 if( strcmp(argv
[1],"init")==0 ){
1122 return initMain(argc
, argv
);
1124 if( strcmp(argv
[1],"export")==0 ){
1125 return exportMain(argc
, argv
);
1127 if( strcmp(argv
[1],"run")==0 ){
1128 return runMain(argc
, argv
);
1130 if( strcmp(argv
[1],"stat")==0 ){
1131 return statMain(argc
, argv
);