speedtest16.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /*
  32. ** hwtime.h contains inline assembler code for implementing
  33. ** high-performance timing routines.
  34. */
  35. #include "hwtime.h"
  36. /*
  37. ** Convert a zero-terminated ASCII string into a zero-terminated
  38. ** UTF-16le string. Memory to hold the returned string comes
  39. ** from malloc() and should be freed by the caller.
  40. */
  41. static void *asciiToUtf16le(const char *z){
  42. int n = strlen(z);
  43. char *z16;
  44. int i, j;
  45. z16 = malloc( n*2 + 2 );
  46. for(i=j=0; i<=n; i++){
  47. z16[j++] = z[i];
  48. z16[j++] = 0;
  49. }
  50. return (void*)z16;
  51. }
  52. /*
  53. ** Timers
  54. */
  55. static sqlite_uint64 prepTime = 0;
  56. static sqlite_uint64 runTime = 0;
  57. static sqlite_uint64 finalizeTime = 0;
  58. /*
  59. ** Prepare and run a single statement of SQL.
  60. */
  61. static void prepareAndRun(sqlite3 *db, const char *zSql){
  62. void *utf16;
  63. sqlite3_stmt *pStmt;
  64. const void *stmtTail;
  65. sqlite_uint64 iStart, iElapse;
  66. int rc;
  67. printf("****************************************************************\n");
  68. printf("SQL statement: [%s]\n", zSql);
  69. utf16 = asciiToUtf16le(zSql);
  70. iStart = sqlite3Hwtime();
  71. rc = sqlite3_prepare16_v2(db, utf16, -1, &pStmt, &stmtTail);
  72. iElapse = sqlite3Hwtime() - iStart;
  73. prepTime += iElapse;
  74. printf("sqlite3_prepare16_v2() returns %d in %llu cycles\n", rc, iElapse);
  75. if( rc==SQLITE_OK ){
  76. int nRow = 0;
  77. iStart = sqlite3Hwtime();
  78. while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; }
  79. iElapse = sqlite3Hwtime() - iStart;
  80. runTime += iElapse;
  81. printf("sqlite3_step() returns %d after %d rows in %llu cycles\n",
  82. rc, nRow, iElapse);
  83. iStart = sqlite3Hwtime();
  84. rc = sqlite3_finalize(pStmt);
  85. iElapse = sqlite3Hwtime() - iStart;
  86. finalizeTime += iElapse;
  87. printf("sqlite3_finalize() returns %d in %llu cycles\n", rc, iElapse);
  88. }
  89. free(utf16);
  90. }
  91. int main(int argc, char **argv){
  92. void *utf16;
  93. sqlite3 *db;
  94. int rc;
  95. int nSql;
  96. char *zSql;
  97. int i, j;
  98. FILE *in;
  99. sqlite_uint64 iStart, iElapse;
  100. sqlite_uint64 iSetup = 0;
  101. int nStmt = 0;
  102. int nByte = 0;
  103. if( argc!=3 ){
  104. fprintf(stderr, "Usage: %s FILENAME SQL-SCRIPT\n"
  105. "Runs SQL-SCRIPT as UTF16 against a UTF16 database\n",
  106. argv[0]);
  107. exit(1);
  108. }
  109. in = fopen(argv[2], "r");
  110. fseek(in, 0L, SEEK_END);
  111. nSql = ftell(in);
  112. zSql = malloc( nSql+1 );
  113. fseek(in, 0L, SEEK_SET);
  114. nSql = fread(zSql, 1, nSql, in);
  115. zSql[nSql] = 0;
  116. printf("SQLite version: %d\n", sqlite3_libversion_number());
  117. unlink(argv[1]);
  118. utf16 = asciiToUtf16le(argv[1]);
  119. iStart = sqlite3Hwtime();
  120. rc = sqlite3_open16(utf16, &db);
  121. iElapse = sqlite3Hwtime() - iStart;
  122. iSetup = iElapse;
  123. printf("sqlite3_open16() returns %d in %llu cycles\n", rc, iElapse);
  124. free(utf16);
  125. for(i=j=0; j<nSql; j++){
  126. if( zSql[j]==';' ){
  127. int isComplete;
  128. char c = zSql[j+1];
  129. zSql[j+1] = 0;
  130. isComplete = sqlite3_complete(&zSql[i]);
  131. zSql[j+1] = c;
  132. if( isComplete ){
  133. zSql[j] = 0;
  134. while( i<j && isspace(zSql[i]) ){ i++; }
  135. if( i<j ){
  136. nStmt++;
  137. nByte += j-i;
  138. prepareAndRun(db, &zSql[i]);
  139. }
  140. zSql[j] = ';';
  141. i = j+1;
  142. }
  143. }
  144. }
  145. iStart = sqlite3Hwtime();
  146. sqlite3_close(db);
  147. iElapse = sqlite3Hwtime() - iStart;
  148. iSetup += iElapse;
  149. printf("sqlite3_close() returns in %llu cycles\n", iElapse);
  150. printf("\n");
  151. printf("Statements run: %15d\n", nStmt);
  152. printf("Bytes of SQL text: %15d\n", nByte);
  153. printf("Total prepare time: %15llu cycles\n", prepTime);
  154. printf("Total run time: %15llu cycles\n", runTime);
  155. printf("Total finalize time: %15llu cycles\n", finalizeTime);
  156. printf("Open/Close time: %15llu cycles\n", iSetup);
  157. printf("Total Time: %15llu cycles\n",
  158. prepTime + runTime + finalizeTime + iSetup);
  159. return 0;
  160. }