speedtest16.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. ** Performance test for SQLite.
  3. **
  4. ** This program reads ASCII text from a file named on the command-line.
  5. ** It converts each SQL statement into UTF16 and submits it to SQLite
  6. ** for evaluation. A new UTF16 database is created at the beginning of
  7. ** the program. All statements are timed using the high-resolution timer
  8. ** built into Intel-class processors.
  9. **
  10. ** To compile this program, first compile the SQLite library separately
  11. ** will full optimizations. For example:
  12. **
  13. ** gcc -c -O6 -DSQLITE_THREADSAFE=0 sqlite3.c
  14. **
  15. ** Then link against this program. But to do optimize this program
  16. ** because that defeats the hi-res timer.
  17. **
  18. ** gcc speedtest16.c sqlite3.o -ldl -I../src
  19. **
  20. ** Then run this program with a single argument which is the name of
  21. ** a file containing SQL script that you want to test:
  22. **
  23. ** ./a.out database.db test.sql
  24. */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <ctype.h>
  29. #include <unistd.h>
  30. #include "sqlite3.h"
  31. #define ISSPACE(X) isspace((unsigned char)(X))
  32. /*
  33. ** hwtime.h contains inline assembler code for implementing
  34. ** high-performance timing routines.
  35. */
  36. #include "hwtime.h"
  37. /*
  38. ** Convert a zero-terminated ASCII string into a zero-terminated
  39. ** UTF-16le string. Memory to hold the returned string comes
  40. ** from malloc() and should be freed by the caller.
  41. */
  42. static void *asciiToUtf16le(const char *z){
  43. int n = strlen(z);
  44. char *z16;
  45. int i, j;
  46. z16 = malloc( n*2 + 2 );
  47. for(i=j=0; i<=n; i++){
  48. z16[j++] = z[i];
  49. z16[j++] = 0;
  50. }
  51. return (void*)z16;
  52. }
  53. /*
  54. ** Timers
  55. */
  56. static sqlite_uint64 prepTime = 0;
  57. static sqlite_uint64 runTime = 0;
  58. static sqlite_uint64 finalizeTime = 0;
  59. /*
  60. ** Prepare and run a single statement of SQL.
  61. */
  62. static void prepareAndRun(sqlite3 *db, const char *zSql){
  63. void *utf16;
  64. sqlite3_stmt *pStmt;
  65. const void *stmtTail;
  66. sqlite_uint64 iStart, iElapse;
  67. int rc;
  68. printf("****************************************************************\n");
  69. printf("SQL statement: [%s]\n", zSql);
  70. utf16 = asciiToUtf16le(zSql);
  71. iStart = sqlite3Hwtime();
  72. rc = sqlite3_prepare16_v2(db, utf16, -1, &pStmt, &stmtTail);
  73. iElapse = sqlite3Hwtime() - iStart;
  74. prepTime += iElapse;
  75. printf("sqlite3_prepare16_v2() returns %d in %llu cycles\n", rc, iElapse);
  76. if( rc==SQLITE_OK ){
  77. int nRow = 0;
  78. iStart = sqlite3Hwtime();
  79. while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; }
  80. iElapse = sqlite3Hwtime() - iStart;
  81. runTime += iElapse;
  82. printf("sqlite3_step() returns %d after %d rows in %llu cycles\n",
  83. rc, nRow, iElapse);
  84. iStart = sqlite3Hwtime();
  85. rc = sqlite3_finalize(pStmt);
  86. iElapse = sqlite3Hwtime() - iStart;
  87. finalizeTime += iElapse;
  88. printf("sqlite3_finalize() returns %d in %llu cycles\n", rc, iElapse);
  89. }
  90. free(utf16);
  91. }
  92. int main(int argc, char **argv){
  93. void *utf16;
  94. sqlite3 *db;
  95. int rc;
  96. int nSql;
  97. char *zSql;
  98. int i, j;
  99. FILE *in;
  100. sqlite_uint64 iStart, iElapse;
  101. sqlite_uint64 iSetup = 0;
  102. int nStmt = 0;
  103. int nByte = 0;
  104. if( argc!=3 ){
  105. fprintf(stderr, "Usage: %s FILENAME SQL-SCRIPT\n"
  106. "Runs SQL-SCRIPT as UTF16 against a UTF16 database\n",
  107. argv[0]);
  108. exit(1);
  109. }
  110. in = fopen(argv[2], "r");
  111. fseek(in, 0L, SEEK_END);
  112. nSql = ftell(in);
  113. zSql = malloc( nSql+1 );
  114. fseek(in, 0L, SEEK_SET);
  115. nSql = fread(zSql, 1, nSql, in);
  116. zSql[nSql] = 0;
  117. printf("SQLite version: %d\n", sqlite3_libversion_number());
  118. unlink(argv[1]);
  119. utf16 = asciiToUtf16le(argv[1]);
  120. iStart = sqlite3Hwtime();
  121. rc = sqlite3_open16(utf16, &db);
  122. iElapse = sqlite3Hwtime() - iStart;
  123. iSetup = iElapse;
  124. printf("sqlite3_open16() returns %d in %llu cycles\n", rc, iElapse);
  125. free(utf16);
  126. for(i=j=0; j<nSql; j++){
  127. if( zSql[j]==';' ){
  128. int isComplete;
  129. char c = zSql[j+1];
  130. zSql[j+1] = 0;
  131. isComplete = sqlite3_complete(&zSql[i]);
  132. zSql[j+1] = c;
  133. if( isComplete ){
  134. zSql[j] = 0;
  135. while( i<j && ISSPACE(zSql[i]) ){ i++; }
  136. if( i<j ){
  137. nStmt++;
  138. nByte += j-i;
  139. prepareAndRun(db, &zSql[i]);
  140. }
  141. zSql[j] = ';';
  142. i = j+1;
  143. }
  144. }
  145. }
  146. iStart = sqlite3Hwtime();
  147. sqlite3_close(db);
  148. iElapse = sqlite3Hwtime() - iStart;
  149. iSetup += iElapse;
  150. printf("sqlite3_close() returns in %llu cycles\n", iElapse);
  151. printf("\n");
  152. printf("Statements run: %15d\n", nStmt);
  153. printf("Bytes of SQL text: %15d\n", nByte);
  154. printf("Total prepare time: %15llu cycles\n", prepTime);
  155. printf("Total run time: %15llu cycles\n", runTime);
  156. printf("Total finalize time: %15llu cycles\n", finalizeTime);
  157. printf("Open/Close time: %15llu cycles\n", iSetup);
  158. printf("Total Time: %15llu cycles\n",
  159. prepTime + runTime + finalizeTime + iSetup);
  160. return 0;
  161. }