blob: e81f1a6dba0e067fbd84677ad6ba7fa5a5c4ec66 [file] [log] [blame]
drh7720d642008-04-03 16:01:27 +00001/*
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**
shane9bcbdad2008-05-29 20:22:37 +000018** gcc speedtest16.c sqlite3.o -ldl -I../src
drh7720d642008-04-03 16:01:27 +000019**
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**
drh17afdd22008-04-03 17:57:24 +000023** ./a.out database.db test.sql
drh7720d642008-04-03 16:01:27 +000024*/
25#include <stdio.h>
26#include <string.h>
27#include <stdlib.h>
drhd543a422008-04-03 19:40:59 +000028#include <ctype.h>
29#include <unistd.h>
drh7720d642008-04-03 16:01:27 +000030#include "sqlite3.h"
31
shane9bcbdad2008-05-29 20:22:37 +000032/*
33** hwtime.h contains inline assembler code for implementing
34** high-performance timing routines.
drh7720d642008-04-03 16:01:27 +000035*/
shane9bcbdad2008-05-29 20:22:37 +000036#include "hwtime.h"
drh7720d642008-04-03 16:01:27 +000037
38/*
39** Convert a zero-terminated ASCII string into a zero-terminated
40** UTF-16le string. Memory to hold the returned string comes
41** from malloc() and should be freed by the caller.
42*/
43static void *asciiToUtf16le(const char *z){
44 int n = strlen(z);
45 char *z16;
46 int i, j;
47
48 z16 = malloc( n*2 + 2 );
49 for(i=j=0; i<=n; i++){
50 z16[j++] = z[i];
51 z16[j++] = 0;
52 }
53 return (void*)z16;
54}
55
56/*
57** Timers
58*/
shane9bcbdad2008-05-29 20:22:37 +000059static sqlite_uint64 prepTime = 0;
60static sqlite_uint64 runTime = 0;
61static sqlite_uint64 finalizeTime = 0;
drh7720d642008-04-03 16:01:27 +000062
63/*
64** Prepare and run a single statement of SQL.
65*/
66static void prepareAndRun(sqlite3 *db, const char *zSql){
67 void *utf16;
68 sqlite3_stmt *pStmt;
69 const void *stmtTail;
shane9bcbdad2008-05-29 20:22:37 +000070 sqlite_uint64 iStart, iElapse;
drh7720d642008-04-03 16:01:27 +000071 int rc;
72
73 printf("****************************************************************\n");
74 printf("SQL statement: [%s]\n", zSql);
75 utf16 = asciiToUtf16le(zSql);
shane9bcbdad2008-05-29 20:22:37 +000076 iStart = sqlite3Hwtime();
drh7720d642008-04-03 16:01:27 +000077 rc = sqlite3_prepare16_v2(db, utf16, -1, &pStmt, &stmtTail);
shane9bcbdad2008-05-29 20:22:37 +000078 iElapse = sqlite3Hwtime() - iStart;
drh7720d642008-04-03 16:01:27 +000079 prepTime += iElapse;
80 printf("sqlite3_prepare16_v2() returns %d in %llu cycles\n", rc, iElapse);
81 if( rc==SQLITE_OK ){
82 int nRow = 0;
shane9bcbdad2008-05-29 20:22:37 +000083 iStart = sqlite3Hwtime();
drh7720d642008-04-03 16:01:27 +000084 while( (rc=sqlite3_step(pStmt))==SQLITE_ROW ){ nRow++; }
shane9bcbdad2008-05-29 20:22:37 +000085 iElapse = sqlite3Hwtime() - iStart;
drh7720d642008-04-03 16:01:27 +000086 runTime += iElapse;
87 printf("sqlite3_step() returns %d after %d rows in %llu cycles\n",
88 rc, nRow, iElapse);
shane9bcbdad2008-05-29 20:22:37 +000089 iStart = sqlite3Hwtime();
drh7720d642008-04-03 16:01:27 +000090 rc = sqlite3_finalize(pStmt);
shane9bcbdad2008-05-29 20:22:37 +000091 iElapse = sqlite3Hwtime() - iStart;
drh7720d642008-04-03 16:01:27 +000092 finalizeTime += iElapse;
93 printf("sqlite3_finalize() returns %d in %llu cycles\n", rc, iElapse);
94 }
95 free(utf16);
96}
97
98int main(int argc, char **argv){
99 void *utf16;
100 sqlite3 *db;
101 int rc;
102 int nSql;
103 char *zSql;
104 int i, j;
105 FILE *in;
shane9bcbdad2008-05-29 20:22:37 +0000106 sqlite_uint64 iStart, iElapse;
107 sqlite_uint64 iSetup = 0;
drh17afdd22008-04-03 17:57:24 +0000108 int nStmt = 0;
109 int nByte = 0;
drh7720d642008-04-03 16:01:27 +0000110
drh17afdd22008-04-03 17:57:24 +0000111 if( argc!=3 ){
112 fprintf(stderr, "Usage: %s FILENAME SQL-SCRIPT\n"
drh7720d642008-04-03 16:01:27 +0000113 "Runs SQL-SCRIPT as UTF16 against a UTF16 database\n",
114 argv[0]);
115 exit(1);
116 }
drh17afdd22008-04-03 17:57:24 +0000117 in = fopen(argv[2], "r");
drh7720d642008-04-03 16:01:27 +0000118 fseek(in, 0L, SEEK_END);
119 nSql = ftell(in);
120 zSql = malloc( nSql+1 );
121 fseek(in, 0L, SEEK_SET);
122 nSql = fread(zSql, 1, nSql, in);
123 zSql[nSql] = 0;
124
125 printf("SQLite version: %d\n", sqlite3_libversion_number());
drh17afdd22008-04-03 17:57:24 +0000126 unlink(argv[1]);
127 utf16 = asciiToUtf16le(argv[1]);
shane9bcbdad2008-05-29 20:22:37 +0000128 iStart = sqlite3Hwtime();
drh7720d642008-04-03 16:01:27 +0000129 rc = sqlite3_open16(utf16, &db);
shane9bcbdad2008-05-29 20:22:37 +0000130 iElapse = sqlite3Hwtime() - iStart;
drh7720d642008-04-03 16:01:27 +0000131 iSetup = iElapse;
132 printf("sqlite3_open16() returns %d in %llu cycles\n", rc, iElapse);
133 free(utf16);
134 for(i=j=0; j<nSql; j++){
135 if( zSql[j]==';' ){
136 int isComplete;
137 char c = zSql[j+1];
138 zSql[j+1] = 0;
139 isComplete = sqlite3_complete(&zSql[i]);
140 zSql[j+1] = c;
141 if( isComplete ){
142 zSql[j] = 0;
143 while( i<j && isspace(zSql[i]) ){ i++; }
144 if( i<j ){
drh17afdd22008-04-03 17:57:24 +0000145 nStmt++;
146 nByte += j-i;
drh7720d642008-04-03 16:01:27 +0000147 prepareAndRun(db, &zSql[i]);
148 }
149 zSql[j] = ';';
150 i = j+1;
151 }
152 }
153 }
shane9bcbdad2008-05-29 20:22:37 +0000154 iStart = sqlite3Hwtime();
drh7720d642008-04-03 16:01:27 +0000155 sqlite3_close(db);
shane9bcbdad2008-05-29 20:22:37 +0000156 iElapse = sqlite3Hwtime() - iStart;
drh7720d642008-04-03 16:01:27 +0000157 iSetup += iElapse;
158 printf("sqlite3_close() returns in %llu cycles\n", iElapse);
159 printf("\n");
drh17afdd22008-04-03 17:57:24 +0000160 printf("Statements run: %15d\n", nStmt);
161 printf("Bytes of SQL text: %15d\n", nByte);
drh7720d642008-04-03 16:01:27 +0000162 printf("Total prepare time: %15llu cycles\n", prepTime);
163 printf("Total run time: %15llu cycles\n", runTime);
164 printf("Total finalize time: %15llu cycles\n", finalizeTime);
165 printf("Open/Close time: %15llu cycles\n", iSetup);
166 printf("Total Time: %15llu cycles\n",
167 prepTime + runTime + finalizeTime + iSetup);
168 return 0;
169}