speedtest8inst1.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. ** Performance test for SQLite.
  3. **
  4. ** This program reads ASCII text from a file named on the command-line
  5. ** and submits that text to SQLite for evaluation. A new database
  6. ** is created at the beginning of the program. All statements are
  7. ** timed using the high-resolution timer built into Intel-class processors.
  8. **
  9. ** To compile this program, first compile the SQLite library separately
  10. ** will full optimizations. For example:
  11. **
  12. ** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c
  13. **
  14. ** Then link against this program. But to do optimize this program
  15. ** because that defeats the hi-res timer.
  16. **
  17. ** gcc speedtest8.c sqlite3.o -ldl -I../src
  18. **
  19. ** Then run this program with a single argument which is the name of
  20. ** a file containing SQL script that you want to test:
  21. **
  22. ** ./a.out test.db test.sql
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <ctype.h>
  28. #include <unistd.h>
  29. #include <stdarg.h>
  30. #include "sqlite3.h"
  31. #define ISSPACE(X) isspace((unsigned char)(X))
  32. #include "test_osinst.c"
  33. /*
  34. ** Prepare and run a single statement of SQL.
  35. */
  36. static void prepareAndRun(sqlite3_vfs *pInstVfs, sqlite3 *db, const char *zSql){
  37. sqlite3_stmt *pStmt;
  38. const char *stmtTail;
  39. int rc;
  40. char zMessage[1024];
  41. zMessage[1023] = '\0';
  42. sqlite3_uint64 iTime;
  43. sqlite3_snprintf(1023, zMessage, "sqlite3_prepare_v2: %s", zSql);
  44. sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage);
  45. iTime = sqlite3Hwtime();
  46. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &stmtTail);
  47. iTime = sqlite3Hwtime() - iTime;
  48. sqlite3_instvfs_binarylog_call(pInstVfs,BINARYLOG_PREPARE_V2,iTime,rc,zSql);
  49. if( rc==SQLITE_OK ){
  50. int nRow = 0;
  51. sqlite3_snprintf(1023, zMessage, "sqlite3_step loop: %s", zSql);
  52. sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage);
  53. iTime = sqlite3Hwtime();
  54. while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; }
  55. iTime = sqlite3Hwtime() - iTime;
  56. sqlite3_instvfs_binarylog_call(pInstVfs, BINARYLOG_STEP, iTime, rc, zSql);
  57. sqlite3_snprintf(1023, zMessage, "sqlite3_finalize: %s", zSql);
  58. sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage);
  59. iTime = sqlite3Hwtime();
  60. rc = sqlite3_finalize(pStmt);
  61. iTime = sqlite3Hwtime() - iTime;
  62. sqlite3_instvfs_binarylog_call(pInstVfs, BINARYLOG_FINALIZE, iTime, rc, zSql);
  63. }
  64. }
  65. static int stringcompare(const char *zLeft, const char *zRight){
  66. int ii;
  67. for(ii=0; zLeft[ii] && zRight[ii]; ii++){
  68. if( zLeft[ii]!=zRight[ii] ) return 0;
  69. }
  70. return( zLeft[ii]==zRight[ii] );
  71. }
  72. static char *readScriptFile(const char *zFile, int *pnScript){
  73. sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
  74. sqlite3_file *p;
  75. int rc;
  76. sqlite3_int64 nByte;
  77. char *zData = 0;
  78. int flags = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_DB;
  79. p = (sqlite3_file *)malloc(pVfs->szOsFile);
  80. rc = pVfs->xOpen(pVfs, zFile, p, flags, &flags);
  81. if( rc!=SQLITE_OK ){
  82. goto error_out;
  83. }
  84. rc = p->pMethods->xFileSize(p, &nByte);
  85. if( rc!=SQLITE_OK ){
  86. goto close_out;
  87. }
  88. zData = (char *)malloc(nByte+1);
  89. rc = p->pMethods->xRead(p, zData, nByte, 0);
  90. if( rc!=SQLITE_OK ){
  91. goto close_out;
  92. }
  93. zData[nByte] = '\0';
  94. p->pMethods->xClose(p);
  95. free(p);
  96. *pnScript = nByte;
  97. return zData;
  98. close_out:
  99. p->pMethods->xClose(p);
  100. error_out:
  101. free(p);
  102. free(zData);
  103. return 0;
  104. }
  105. int main(int argc, char **argv){
  106. const char zUsageMsg[] =
  107. "Usage: %s options...\n"
  108. " where available options are:\n"
  109. "\n"
  110. " -db DATABASE-FILE (database file to operate on)\n"
  111. " -script SCRIPT-FILE (script file to read sql from)\n"
  112. " -log LOG-FILE (log file to create)\n"
  113. " -logdata (log all data to log file)\n"
  114. "\n"
  115. " Options -db, -script and -log are compulsory\n"
  116. "\n"
  117. ;
  118. const char *zDb = 0;
  119. const char *zScript = 0;
  120. const char *zLog = 0;
  121. int logdata = 0;
  122. int ii;
  123. int i, j;
  124. int rc;
  125. sqlite3_vfs *pInstVfs; /* Instrumentation VFS */
  126. char *zSql = 0;
  127. int nSql;
  128. sqlite3 *db;
  129. for(ii=1; ii<argc; ii++){
  130. if( stringcompare("-db", argv[ii]) && (ii+1)<argc ){
  131. zDb = argv[++ii];
  132. }
  133. else if( stringcompare("-script", argv[ii]) && (ii+1)<argc ){
  134. zScript = argv[++ii];
  135. }
  136. else if( stringcompare("-log", argv[ii]) && (ii+1)<argc ){
  137. zLog = argv[++ii];
  138. }
  139. else if( stringcompare("-logdata", argv[ii]) ){
  140. logdata = 1;
  141. }
  142. else {
  143. goto usage;
  144. }
  145. }
  146. if( !zDb || !zScript || !zLog ) goto usage;
  147. zSql = readScriptFile(zScript, &nSql);
  148. if( !zSql ){
  149. fprintf(stderr, "Failed to read script file\n");
  150. return -1;
  151. }
  152. pInstVfs = sqlite3_instvfs_binarylog("logging", 0, zLog, logdata);
  153. rc = sqlite3_open_v2(
  154. zDb, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, "logging"
  155. );
  156. if( rc!=SQLITE_OK ){
  157. fprintf(stderr, "Failed to open db: %s\n", sqlite3_errmsg(db));
  158. return -2;
  159. }
  160. for(i=j=0; j<nSql; j++){
  161. if( zSql[j]==';' ){
  162. int isComplete;
  163. char c = zSql[j+1];
  164. zSql[j+1] = 0;
  165. isComplete = sqlite3_complete(&zSql[i]);
  166. zSql[j+1] = c;
  167. if( isComplete ){
  168. zSql[j] = 0;
  169. while( i<j && ISSPACE(zSql[i]) ){ i++; }
  170. if( i<j ){
  171. prepareAndRun(pInstVfs, db, &zSql[i]);
  172. }
  173. zSql[j] = ';';
  174. i = j+1;
  175. }
  176. }
  177. }
  178. sqlite3_instvfs_destroy(pInstVfs);
  179. return 0;
  180. usage:
  181. fprintf(stderr, zUsageMsg, argv[0]);
  182. return -3;
  183. }