speedtest8inst1.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "test_osinst.c"
  32. /*
  33. ** Prepare and run a single statement of SQL.
  34. */
  35. static void prepareAndRun(sqlite3_vfs *pInstVfs, sqlite3 *db, const char *zSql){
  36. sqlite3_stmt *pStmt;
  37. const char *stmtTail;
  38. int rc;
  39. char zMessage[1024];
  40. zMessage[1023] = '\0';
  41. sqlite3_uint64 iTime;
  42. sqlite3_snprintf(1023, zMessage, "sqlite3_prepare_v2: %s", zSql);
  43. sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage);
  44. iTime = sqlite3Hwtime();
  45. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &stmtTail);
  46. iTime = sqlite3Hwtime() - iTime;
  47. sqlite3_instvfs_binarylog_call(pInstVfs,BINARYLOG_PREPARE_V2,iTime,rc,zSql);
  48. if( rc==SQLITE_OK ){
  49. int nRow = 0;
  50. sqlite3_snprintf(1023, zMessage, "sqlite3_step loop: %s", zSql);
  51. sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage);
  52. iTime = sqlite3Hwtime();
  53. while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; }
  54. iTime = sqlite3Hwtime() - iTime;
  55. sqlite3_instvfs_binarylog_call(pInstVfs, BINARYLOG_STEP, iTime, rc, zSql);
  56. sqlite3_snprintf(1023, zMessage, "sqlite3_finalize: %s", zSql);
  57. sqlite3_instvfs_binarylog_marker(pInstVfs, zMessage);
  58. iTime = sqlite3Hwtime();
  59. rc = sqlite3_finalize(pStmt);
  60. iTime = sqlite3Hwtime() - iTime;
  61. sqlite3_instvfs_binarylog_call(pInstVfs, BINARYLOG_FINALIZE, iTime, rc, zSql);
  62. }
  63. }
  64. static int stringcompare(const char *zLeft, const char *zRight){
  65. int ii;
  66. for(ii=0; zLeft[ii] && zRight[ii]; ii++){
  67. if( zLeft[ii]!=zRight[ii] ) return 0;
  68. }
  69. return( zLeft[ii]==zRight[ii] );
  70. }
  71. static char *readScriptFile(const char *zFile, int *pnScript){
  72. sqlite3_vfs *pVfs = sqlite3_vfs_find(0);
  73. sqlite3_file *p;
  74. int rc;
  75. sqlite3_int64 nByte;
  76. char *zData = 0;
  77. int flags = SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_DB;
  78. p = (sqlite3_file *)malloc(pVfs->szOsFile);
  79. rc = pVfs->xOpen(pVfs, zFile, p, flags, &flags);
  80. if( rc!=SQLITE_OK ){
  81. goto error_out;
  82. }
  83. rc = p->pMethods->xFileSize(p, &nByte);
  84. if( rc!=SQLITE_OK ){
  85. goto close_out;
  86. }
  87. zData = (char *)malloc(nByte+1);
  88. rc = p->pMethods->xRead(p, zData, nByte, 0);
  89. if( rc!=SQLITE_OK ){
  90. goto close_out;
  91. }
  92. zData[nByte] = '\0';
  93. p->pMethods->xClose(p);
  94. free(p);
  95. *pnScript = nByte;
  96. return zData;
  97. close_out:
  98. p->pMethods->xClose(p);
  99. error_out:
  100. free(p);
  101. free(zData);
  102. return 0;
  103. }
  104. int main(int argc, char **argv){
  105. const char zUsageMsg[] =
  106. "Usage: %s options...\n"
  107. " where available options are:\n"
  108. "\n"
  109. " -db DATABASE-FILE (database file to operate on)\n"
  110. " -script SCRIPT-FILE (script file to read sql from)\n"
  111. " -log LOG-FILE (log file to create)\n"
  112. " -logdata (log all data to log file)\n"
  113. "\n"
  114. " Options -db, -script and -log are compulsory\n"
  115. "\n"
  116. ;
  117. const char *zDb = 0;
  118. const char *zScript = 0;
  119. const char *zLog = 0;
  120. int logdata = 0;
  121. int ii;
  122. int i, j;
  123. int rc;
  124. sqlite3_vfs *pInstVfs; /* Instrumentation VFS */
  125. char *zSql = 0;
  126. int nSql;
  127. sqlite3 *db;
  128. for(ii=1; ii<argc; ii++){
  129. if( stringcompare("-db", argv[ii]) && (ii+1)<argc ){
  130. zDb = argv[++ii];
  131. }
  132. else if( stringcompare("-script", argv[ii]) && (ii+1)<argc ){
  133. zScript = argv[++ii];
  134. }
  135. else if( stringcompare("-log", argv[ii]) && (ii+1)<argc ){
  136. zLog = argv[++ii];
  137. }
  138. else if( stringcompare("-logdata", argv[ii]) ){
  139. logdata = 1;
  140. }
  141. else {
  142. goto usage;
  143. }
  144. }
  145. if( !zDb || !zScript || !zLog ) goto usage;
  146. zSql = readScriptFile(zScript, &nSql);
  147. if( !zSql ){
  148. fprintf(stderr, "Failed to read script file\n");
  149. return -1;
  150. }
  151. pInstVfs = sqlite3_instvfs_binarylog("logging", 0, zLog, logdata);
  152. rc = sqlite3_open_v2(
  153. zDb, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, "logging"
  154. );
  155. if( rc!=SQLITE_OK ){
  156. fprintf(stderr, "Failed to open db: %s\n", sqlite3_errmsg(db));
  157. return -2;
  158. }
  159. for(i=j=0; j<nSql; j++){
  160. if( zSql[j]==';' ){
  161. int isComplete;
  162. char c = zSql[j+1];
  163. zSql[j+1] = 0;
  164. isComplete = sqlite3_complete(&zSql[i]);
  165. zSql[j+1] = c;
  166. if( isComplete ){
  167. zSql[j] = 0;
  168. while( i<j && isspace(zSql[i]) ){ i++; }
  169. if( i<j ){
  170. prepareAndRun(pInstVfs, db, &zSql[i]);
  171. }
  172. zSql[j] = ';';
  173. i = j+1;
  174. }
  175. }
  176. }
  177. sqlite3_instvfs_destroy(pInstVfs);
  178. return 0;
  179. usage:
  180. fprintf(stderr, zUsageMsg, argv[0]);
  181. return -3;
  182. }