speedtest8.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 <time.h>
  29. #if defined(_MSC_VER)
  30. #include <windows.h>
  31. #else
  32. #include <unistd.h>
  33. #include <sys/times.h>
  34. #include <sched.h>
  35. #endif
  36. #include "sqlite3.h"
  37. /*
  38. ** hwtime.h contains inline assembler code for implementing
  39. ** high-performance timing routines.
  40. */
  41. #include "hwtime.h"
  42. /*
  43. ** Timers
  44. */
  45. static sqlite_uint64 prepTime = 0;
  46. static sqlite_uint64 runTime = 0;
  47. static sqlite_uint64 finalizeTime = 0;
  48. /*
  49. ** Prepare and run a single statement of SQL.
  50. */
  51. static void prepareAndRun(sqlite3 *db, const char *zSql, int bQuiet){
  52. sqlite3_stmt *pStmt;
  53. const char *stmtTail;
  54. sqlite_uint64 iStart, iElapse;
  55. int rc;
  56. if (!bQuiet){
  57. printf("***************************************************************\n");
  58. }
  59. if (!bQuiet) printf("SQL statement: [%s]\n", zSql);
  60. iStart = sqlite3Hwtime();
  61. rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &stmtTail);
  62. iElapse = sqlite3Hwtime() - iStart;
  63. prepTime += iElapse;
  64. if (!bQuiet){
  65. printf("sqlite3_prepare_v2() returns %d in %llu cycles\n", rc, iElapse);
  66. }
  67. if( rc==SQLITE_OK ){
  68. int nRow = 0;
  69. iStart = sqlite3Hwtime();
  70. while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; }
  71. iElapse = sqlite3Hwtime() - iStart;
  72. runTime += iElapse;
  73. if (!bQuiet){
  74. printf("sqlite3_step() returns %d after %d rows in %llu cycles\n",
  75. rc, nRow, iElapse);
  76. }
  77. iStart = sqlite3Hwtime();
  78. rc = sqlite3_finalize(pStmt);
  79. iElapse = sqlite3Hwtime() - iStart;
  80. finalizeTime += iElapse;
  81. if (!bQuiet){
  82. printf("sqlite3_finalize() returns %d in %llu cycles\n", rc, iElapse);
  83. }
  84. }
  85. }
  86. int main(int argc, char **argv){
  87. sqlite3 *db;
  88. int rc;
  89. int nSql;
  90. char *zSql;
  91. int i, j;
  92. FILE *in;
  93. sqlite_uint64 iStart, iElapse;
  94. sqlite_uint64 iSetup = 0;
  95. int nStmt = 0;
  96. int nByte = 0;
  97. const char *zArgv0 = argv[0];
  98. int bQuiet = 0;
  99. #if !defined(_MSC_VER)
  100. struct tms tmsStart, tmsEnd;
  101. clock_t clkStart, clkEnd;
  102. #endif
  103. #ifdef HAVE_OSINST
  104. extern sqlite3_vfs *sqlite3_instvfs_binarylog(char *, char *, char *);
  105. extern void sqlite3_instvfs_destroy(sqlite3_vfs *);
  106. sqlite3_vfs *pVfs = 0;
  107. #endif
  108. while (argc>3)
  109. {
  110. #ifdef HAVE_OSINST
  111. if( argc>4 && (strcmp(argv[1], "-log")==0) ){
  112. pVfs = sqlite3_instvfs_binarylog("oslog", 0, argv[2]);
  113. sqlite3_vfs_register(pVfs, 1);
  114. argv += 2;
  115. argc -= 2;
  116. continue;
  117. }
  118. #endif
  119. /*
  120. ** Increasing the priority slightly above normal can help with
  121. ** repeatability of testing. Note that with Cygwin, -5 equates
  122. ** to "High", +5 equates to "Low", and anything in between
  123. ** equates to "Normal".
  124. */
  125. if( argc>4 && (strcmp(argv[1], "-priority")==0) ){
  126. #if defined(_MSC_VER)
  127. int new_priority = atoi(argv[2]);
  128. if(!SetPriorityClass(GetCurrentProcess(),
  129. (new_priority<=-5) ? HIGH_PRIORITY_CLASS :
  130. (new_priority<=0) ? ABOVE_NORMAL_PRIORITY_CLASS :
  131. (new_priority==0) ? NORMAL_PRIORITY_CLASS :
  132. (new_priority<5) ? BELOW_NORMAL_PRIORITY_CLASS :
  133. IDLE_PRIORITY_CLASS)){
  134. printf ("error setting priority\n");
  135. exit(2);
  136. }
  137. #else
  138. struct sched_param myParam;
  139. sched_getparam(0, &myParam);
  140. printf ("Current process priority is %d.\n", (int)myParam.sched_priority);
  141. myParam.sched_priority = atoi(argv[2]);
  142. printf ("Setting process priority to %d.\n", (int)myParam.sched_priority);
  143. if (sched_setparam (0, &myParam) != 0){
  144. printf ("error setting priority\n");
  145. exit(2);
  146. }
  147. #endif
  148. argv += 2;
  149. argc -= 2;
  150. continue;
  151. }
  152. if( argc>3 && strcmp(argv[1], "-quiet")==0 ){
  153. bQuiet = -1;
  154. argv++;
  155. argc--;
  156. continue;
  157. }
  158. break;
  159. }
  160. if( argc!=3 ){
  161. fprintf(stderr, "Usage: %s [options] FILENAME SQL-SCRIPT\n"
  162. "Runs SQL-SCRIPT against a UTF8 database\n"
  163. "\toptions:\n"
  164. #ifdef HAVE_OSINST
  165. "\t-log <log>\n"
  166. #endif
  167. "\t-priority <value> : set priority of task\n"
  168. "\t-quiet : only display summary results\n",
  169. zArgv0);
  170. exit(1);
  171. }
  172. in = fopen(argv[2], "r");
  173. fseek(in, 0L, SEEK_END);
  174. nSql = ftell(in);
  175. zSql = malloc( nSql+1 );
  176. fseek(in, 0L, SEEK_SET);
  177. nSql = fread(zSql, 1, nSql, in);
  178. zSql[nSql] = 0;
  179. printf("SQLite version: %d\n", sqlite3_libversion_number());
  180. unlink(argv[1]);
  181. #if !defined(_MSC_VER)
  182. clkStart = times(&tmsStart);
  183. #endif
  184. iStart = sqlite3Hwtime();
  185. rc = sqlite3_open(argv[1], &db);
  186. iElapse = sqlite3Hwtime() - iStart;
  187. iSetup = iElapse;
  188. if (!bQuiet) printf("sqlite3_open() returns %d in %llu cycles\n", rc, iElapse);
  189. for(i=j=0; j<nSql; j++){
  190. if( zSql[j]==';' ){
  191. int isComplete;
  192. char c = zSql[j+1];
  193. zSql[j+1] = 0;
  194. isComplete = sqlite3_complete(&zSql[i]);
  195. zSql[j+1] = c;
  196. if( isComplete ){
  197. zSql[j] = 0;
  198. while( i<j && isspace(zSql[i]) ){ i++; }
  199. if( i<j ){
  200. int n = j - i;
  201. if( n>=6 && memcmp(&zSql[i], ".crash",6)==0 ) exit(1);
  202. nStmt++;
  203. nByte += n;
  204. prepareAndRun(db, &zSql[i], bQuiet);
  205. }
  206. zSql[j] = ';';
  207. i = j+1;
  208. }
  209. }
  210. }
  211. iStart = sqlite3Hwtime();
  212. sqlite3_close(db);
  213. iElapse = sqlite3Hwtime() - iStart;
  214. #if !defined(_MSC_VER)
  215. clkEnd = times(&tmsEnd);
  216. #endif
  217. iSetup += iElapse;
  218. if (!bQuiet) printf("sqlite3_close() returns in %llu cycles\n", iElapse);
  219. printf("\n");
  220. printf("Statements run: %15d stmts\n", nStmt);
  221. printf("Bytes of SQL text: %15d bytes\n", nByte);
  222. printf("Total prepare time: %15llu cycles\n", prepTime);
  223. printf("Total run time: %15llu cycles\n", runTime);
  224. printf("Total finalize time: %15llu cycles\n", finalizeTime);
  225. printf("Open/Close time: %15llu cycles\n", iSetup);
  226. printf("Total time: %15llu cycles\n",
  227. prepTime + runTime + finalizeTime + iSetup);
  228. #if !defined(_MSC_VER)
  229. printf("\n");
  230. printf("Total user CPU time: %15.3g secs\n", (tmsEnd.tms_utime - tmsStart.tms_utime)/(double)CLOCKS_PER_SEC );
  231. printf("Total system CPU time: %15.3g secs\n", (tmsEnd.tms_stime - tmsStart.tms_stime)/(double)CLOCKS_PER_SEC );
  232. printf("Total real time: %15.3g secs\n", (clkEnd -clkStart)/(double)CLOCKS_PER_SEC );
  233. #endif
  234. #ifdef HAVE_OSINST
  235. if( pVfs ){
  236. sqlite3_instvfs_destroy(pVfs);
  237. printf("vfs log written to %s\n", argv[0]);
  238. }
  239. #endif
  240. return 0;
  241. }