Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/kvtest.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / kvtest.c` 1137 lines ยท 35993 bytes Raw Blame History 1 /* 2 ** 2016-12-28 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 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. 10 ** 11 ************************************************************************* 12 ** 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 16 ** filesystem. 17 ** 18 ** Run "kvtest" with no arguments for on-line help, or see comments below. 19 ** 20 ** HOW TO COMPILE: 21 ** 22 ** (1) Gather this source file and a recent SQLite3 amalgamation with its 23 ** header into the working directory. You should have: 24 ** 25 ** kvtest.c >--- this file 26 ** sqlite3.c \___ SQLite 27 ** sqlite3.h / amlagamation & header 28 ** 29 ** (2) Run you compiler against the two C source code files. 30 ** 31 ** (a) On linux or mac: 32 ** 33 ** OPTS="-DSQLITE_THREADSAFE=0 -DSQLITE_OMIT_LOAD_EXTENSION" 34 ** gcc -Os -I. $OPTS kvtest.c sqlite3.c -o kvtest 35 ** 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. 39 ** 40 ** (b) Windows with MSVC: 41 ** 42 ** cl -I. kvtest.c sqlite3.c 43 ** 44 ** USAGE: 45 ** 46 ** (1) Create a test database by running "kvtest init" with appropriate 47 ** options. See the help message for available options. 48 ** 49 ** (2) Construct the corresponding pile-of-files database on disk using 50 ** the "kvtest export" command. 51 ** 52 ** (3) Run tests using "kvtest run" against either the SQLite database or 53 ** the pile-of-files database and with appropriate options. 54 ** 55 ** For example: 56 ** 57 ** ./kvtest init x1.db --count 100000 --size 10000 58 ** mkdir x1 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` 62 */ 63 static const char zHelp[] = 64 "Usage: kvtest COMMAND ARGS...\n" 65 "\n" 66 " kvtest init DBFILE --count N --size M --pagesize X\n" 67 "\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" 71 "\n" 72 " --variance V Randomly vary M by plus or minus V\n" 73 "\n" 74 " kvtest export DBFILE DIRECTORY [--tree]\n" 75 "\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" 83 "\n" 84 " kvtest stat DBFILE [options]\n" 85 "\n" 86 " Display summary information about DBFILE. Options:\n" 87 "\n" 88 " --vacuum Run VACUUM on the database file\n" 89 "\n" 90 " kvtest run DBFILE [options]\n" 91 "\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" 94 "\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" 112 ; 113 114 /* Reference resources used */ 115 #include 116 #include 117 #include 118 #include 119 #include 120 #include 121 #include "sqlite3.h" 122 123 #ifndef _WIN32 124 # include 125 #else 126 /* Provide Windows equivalent for the needed parts of unistd.h */ 127 # include 128 # include 129 # define R_OK 2 130 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) 131 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) 132 # define access _access 133 #endif 134 135 #if !defined(_MSC_VER) 136 # include 137 #endif 138 139 /* 140 ** The following macros are used to cast pointers to integers and 141 ** integers to pointers. The way you do this varies from one compiler 142 ** to the next, so we have developed the following set of #if statements 143 ** to generate appropriate macros for a wide range of compilers. 144 ** 145 ** The correct "ANSI" way to do this is to use the intptr_t type. 146 ** Unfortunately, that typedef is not available on all compilers, or 147 ** if it is available, it requires an #include of specific headers 148 ** that vary from one machine to the next. 149 ** 150 ** Ticket #3860: The llvm-gcc-4.2 compiler from Apple chokes on 151 ** the ((void*)&((char*)0)[X]) construct. But MSVC chokes on ((void*)(X)). 152 ** So we have to define the macros in different ways depending on the 153 ** compiler. 154 */ 155 #if defined(__PTRDIFF_TYPE__) /* This case should work for GCC */ 156 # define SQLITE_INT_TO_PTR(X) ((void*)(__PTRDIFF_TYPE__)(X)) 157 # define SQLITE_PTR_TO_INT(X) ((sqlite3_int64)(__PTRDIFF_TYPE__)(X)) 158 #else 159 # define SQLITE_INT_TO_PTR(X) ((void*)(intptr_t)(X)) 160 # define SQLITE_PTR_TO_INT(X) ((sqlite3_int64)(intptr_t)(X)) 161 #endif 162 163 /* 164 ** Show the help text and quit. 165 */ 166 static void showHelp(void){ 167 fprintf(stdout, "%s", zHelp); 168 exit(1); 169 } 170 171 /* 172 ** Show an error message an quit. 173 */ 174 static void fatalError(const char *zFormat, ...){ 175 va_list ap; 176 fprintf(stdout, "ERROR: "); 177 va_start(ap, zFormat); 178 vfprintf(stdout, zFormat, ap); 179 va_end(ap); 180 fprintf(stdout, "\n"); 181 exit(1); 182 } 183 184 /* 185 ** Return the value of a hexadecimal digit. Return -1 if the input 186 ** is not a hex digit. 187 */ 188 static int hexDigitValue(char c){ 189 if( c>='0' && c<='9' ) return c - '0'; 190 if( c>='a' && c<='f' ) return c - 'a' + 10; 191 if( c>='A' && c<='F' ) return c - 'A' + 10; 192 return -1; 193 } 194 195 /* 196 ** Interpret zArg as an integer value, possibly with suffixes. 197 */ 198 static int integerValue(const char *zArg){ 199 int v = 0; 200 static const struct { char *zSuffix; int iMult; } aMult[] = { 201 { "KiB", 1024 }, 202 { "MiB", 1024*1024 }, 203 { "GiB", 1024*1024*1024 }, 204 { "KB", 1000 }, 205 { "MB", `1000000` }, 206 { "GB", `1000000000` }, 207 { "K", 1000 }, 208 { "M", `1000000` }, 209 { "G", `1000000000` }, 210 }; 211 int i; 212 int isNeg = 0; 213 if( zArg[0]=='-' ){ 214 isNeg = 1; 215 zArg++; 216 }else if( zArg[0]=='+' ){ 217 zArg++; 218 } 219 if( zArg[0]=='0' && zArg[1]=='x' ){ 220 int x; 221 zArg += 2; 222 while( (x = hexDigitValue(zArg[0]))>=0 ){ 223 v = (v<<4) + x; 224 zArg++; 225 } 226 }else{ 227 while( zArg[0]>='0' && zArg[0]<='9' ){ 228 v = v*10 + zArg[0] - '0'; 229 zArg++; 230 } 231 } 232 for(i=0; i>1) ^ ((1+~(x&1)) & 0xd0000001); 307 y = y*`1103515245` + 12345; 308 return x^y; 309 } 310 311 /* 312 ** Do database initialization. 313 */ 314 static int initMain(int argc, char **argv){ 315 char *zDb; 316 int i, rc; 317 int nCount = 1000; 318 int sz = 10000; 319 int iVariance = 0; 320 int pgsz = 4096; 321 sqlite3 *db; 322 char *zSql; 323 char *zErrMsg = 0; 324 325 assert( strcmp(argv[1],"init")==0 ); 326 assert( argc>=3 ); 327 zDb = argv[2]; 328 for(i=3; i65536 || ((pgsz-1)&pgsz)!=0 ){ 353 fatalError("the --pagesize must be power of 2 between 512 and 65536"); 354 } 355 continue; 356 } 357 fatalError("unknown option: \"%s\"", argv[i]); 358 } 359 rc = sqlite3_open(zDb, &db); 360 if( rc ){ 361 fatalError("cannot open database \"%s\": %s", zDb, sqlite3_errmsg(db)); 362 } 363 zSql = sqlite3_mprintf( 364 "DROP TABLE IF EXISTS kv;\n" 365 "PRAGMA page_size=%d;\n" 366 "VACUUM;\n" 367 "BEGIN;\n" 368 "CREATE TABLE kv(k INTEGER PRIMARY KEY, v BLOB);\n" 369 "WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<%d)" 370 " INSERT INTO kv(k,v) SELECT x, randomblob(%d+(random()%%(%d))) FROM c;\n" 371 "COMMIT;\n", 372 pgsz, nCount, sz, iVariance+1 373 ); 374 rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg); 375 if( rc ) fatalError("database create failed: %s", zErrMsg); 376 sqlite3_free(zSql); 377 sqlite3_close(db); 378 return 0; 379 } 380 381 /* 382 ** Analyze an existing database file. Report its content. 383 */ 384 static int statMain(int argc, char **argv){ 385 char *zDb; 386 int i, rc; 387 sqlite3 *db; 388 char *zSql; 389 sqlite3_stmt *pStmt; 390 int doVacuum = 0; 391 392 assert( strcmp(argv[1],"stat")==0 ); 393 assert( argc>=3 ); 394 zDb = argv[2]; 395 for(i=3; i=3 ); 513 if( argc<4 ) fatalError("Usage: kvtest export DATABASE DIRECTORY [OPTIONS]"); 514 zDb = argv[2]; 515 zDir = argv[3]; 516 kvtest_mkdir(zDir); 517 for(i=4; iiVersion>=2 && clockVfs->xCurrentTimeInt64!=0 ){ 668 clockVfs->xCurrentTimeInt64(clockVfs, &t); 669 }else{ 670 double r; 671 clockVfs->xCurrentTime(clockVfs, &r); 672 t = (sqlite3_int64)(r*`86400000`.0); 673 } 674 return t; 675 } 676 677 #ifdef __linux__ 678 /* 679 ** Attempt to display I/O stats on Linux using /proc/PID/io 680 */ 681 static void displayLinuxIoStats(FILE *out){ 682 FILE *in; 683 char z[200]; 684 sqlite3_snprintf(sizeof(z), z, "/proc/%d/io", getpid()); 685 in = fopen(z, "rb"); 686 if( in==0 ) return; 687 while( fgets(z, sizeof(z), in)!=0 ){ 688 static const struct { 689 const char *zPattern; 690 const char *zDesc; 691 } aTrans[] = { 692 { "rchar: ", "Bytes received by read():" }, 693 { "wchar: ", "Bytes sent to write():" }, 694 { "syscr: ", "Read() system calls:" }, 695 { "syscw: ", "Write() system calls:" }, 696 { "read_bytes: ", "Bytes read from storage:" }, 697 { "write_bytes: ", "Bytes written to storage:" }, 698 { "cancelled_write_bytes: ", "Cancelled write bytes:" }, 699 }; 700 int i; 701 for(i=0; i=3 ); 820 zDb = argv[2]; 821 eType = pathType(zDb); 822 if( eType==PATH_OTHER ) fatalError("unknown object type: \"%s\"", zDb); 823 if( eType==PATH_NEXIST ) fatalError("object does not exist: \"%s\"", zDb); 824 for(i=3; iiMax ) iKey = 1; 1054 }else if( eOrder==ORDER_DESC ){ 1055 iKey--; 1056 if( iKey<=0 ) iKey = iMax; 1057 }else{ 1058 iKey = (randInt()%iMax)+1; 1059 } 1060 nTotal += nData; 1061 if( nData==0 ){ nCount++; nExtra++; } 1062 } 1063 if( nAlloc ) sqlite3_free(pData); 1064 if( pStmt ) sqlite3_finalize(pStmt); 1065 if( pBlob ) sqlite3_blob_close(pBlob); 1066 if( bStats ){ 1067 display_stats(db, 0); 1068 } 1069 if( db ){ 1070 if( !doMultiTrans ) sqlite3_exec(db, "COMMIT", 0, 0, 0); 1071 if( !noCheckpoint ){ 1072 sqlite3_close(db); 1073 db = 0; 1074 } 1075 } 1076 tmElapsed = timeOfDay() - tmStart; 1077 if( db && noCheckpoint ){ 1078 sqlite3_close(db); 1079 db = 0; 1080 } 1081 if( nExtra ){ 1082 printf("%d cycles due to %d misses\n", nCount, nExtra); 1083 } 1084 if( eType==PATH_DB ){ 1085 printf("SQLite version: %s\n", sqlite3_libversion()); 1086 if( doIntegrityCk ){ 1087 sqlite3_open(zDb, &db); 1088 sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &pStmt, 0); 1089 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 1090 printf("integrity-check: %s\n", sqlite3_column_text(pStmt, 0)); 1091 } 1092 sqlite3_finalize(pStmt); 1093 sqlite3_close(db); 1094 db = 0; 1095 } 1096 } 1097 printf("--count %d --max-id %d", nCount-nExtra, iMax); 1098 switch( eOrder ){ 1099 case ORDER_RANDOM: printf(" --random\n"); break; 1100 case ORDER_DESC: printf(" --desc\n"); break; 1101 default: printf(" --asc\n"); break; 1102 } 1103 if( eType==PATH_DB ){ 1104 printf("--cache-size %d --jmode %s\n", iCache, zJMode); 1105 printf("--mmap %d%s\n", mmapSize, bBlobApi ? " --blob-api" : ""); 1106 if( noSync ) printf("--nosync\n"); 1107 } 1108 if( iPagesize ) printf("Database page size: %d\n", iPagesize); 1109 printf("Total elapsed time: %.3f\n", tmElapsed/1000.0); 1110 if( isUpdateTest ){ 1111 printf("Microseconds per BLOB write: %.3f\n", tmElapsed*1000.0/nCount); 1112 printf("Content write rate: %.1f MB/s\n", nTotal/(1000.0*tmElapsed)); 1113 }else{ 1114 printf("Microseconds per BLOB read: %.3f\n", tmElapsed*1000.0/nCount); 1115 printf("Content read rate: %.1f MB/s\n", nTotal/(1000.0*tmElapsed)); 1116 } 1117 return 0; 1118 } 1119 1120 1121 int main(int argc, char **argv){ 1122 if( argc<3 ) showHelp(); 1123 if( strcmp(argv[1],"init")==0 ){ 1124 return initMain(argc, argv); 1125 } 1126 if( strcmp(argv[1],"export")==0 ){ 1127 return exportMain(argc, argv); 1128 } 1129 if( strcmp(argv[1],"run")==0 ){ 1130 return runMain(argc, argv); 1131 } 1132 if( strcmp(argv[1],"stat")==0 ){ 1133 return statMain(argc, argv); 1134 } 1135 showHelp(); 1136 return 0; 1137 }