1 /*
2 * Copyright 2011 The Emscripten Authors. All rights reserved.
3 * Emscripten is available under two separate licenses, the MIT license and the
4 * University of Illinois/NCSA Open Source License. Both these licenses can be
5 * found in the LICENSE file.
6 */
7
8 #include <time.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sqlite3.h>
12
13 #include <emscripten.h>
14
15 int print = 1;
16
callback(void * NotUsed,int argc,char ** argv,char ** azColName)17 static int callback(void *NotUsed, int argc, char **argv, char **azColName){
18 int i;
19 if (!print) return 0;
20 for(i=0; i<argc; i++){
21 printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
22 }
23 printf("\n");
24 return 0;
25 }
26
test()27 int test(){
28 sqlite3 *db;
29 char *zErrMsg = 0;
30 int rc;
31 int i;
32 const char *commands[] = {
33 "CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));",
34 "INSERT INTO t2 VALUES(1,13153,'thirteen thousand one hundred fifty three');",
35 "INSERT INTO t2 VALUES(1,987,'some other number');",
36 "SELECT count(*) FROM t2;",
37 "SELECT datetime('2012-04-16 12:35:57', '+1 days');",
38 "SELECT a, b, c FROM t2;",
39 NULL
40 };
41
42 rc = sqlite3_open(":memory:", &db);
43 if( rc ){
44 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
45 sqlite3_close(db);
46 exit(1);
47 }
48 for (i = 0; commands[i]; i++) {
49 rc = sqlite3_exec(db, commands[i], callback, 0, &zErrMsg);
50 if( rc!=SQLITE_OK ){
51 fprintf(stderr, "SQL error on %d: %s\n", i, zErrMsg);
52 sqlite3_free(zErrMsg);
53 exit(1);
54 }
55 }
56 sqlite3_close(db);
57 return 0;
58 }
59
main(int argc,char ** argv)60 int main(int argc, char **argv){
61 sqlite3 *db;
62 char *zErrMsg = 0;
63 int rc, i;
64 double t;
65 int n, m;
66
67 n = argc > 1 ? atoi(argv[1]) : 5000;
68 m = argc > 2 ? atoi(argv[2]) : 1;
69
70 rc = sqlite3_open(":memory:", &db);
71 if( rc ){
72 fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
73 sqlite3_close(db);
74 exit(1);
75 }
76
77 #define RUN(cmd) \
78 { \
79 rc = sqlite3_exec(db, cmd, callback, 0, &zErrMsg); \
80 if( rc!=SQLITE_OK ){ \
81 fprintf(stderr, "SQL error on %d: %s\n", i, zErrMsg); \
82 sqlite3_free(zErrMsg); \
83 exit(1); \
84 } \
85 }
86
87 #define TIME(msg) \
88 { \
89 double now = emscripten_get_now(); \
90 printf(msg " : took %f ms\n", now - t); \
91 t = now; \
92 }
93
94 t = emscripten_get_now();
95 TIME("'startup' - IGNORE THIS VALUE, it is an artifact");
96
97 RUN("CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));");
98 TIME("create table");
99
100 RUN("BEGIN;");
101
102 // n*5 INSERTs in a transaction
103 for (i = 0; i < n; i++) {
104 RUN("INSERT INTO t1 VALUES(1,12345,'one 1 one 1 one 1');");
105 RUN("INSERT INTO t1 VALUES(2,23422,'two two two two');");
106 RUN("INSERT INTO t1 VALUES(3,31233,'three three 33333333333 three');");
107 RUN("INSERT INTO t1 VALUES(4,41414,'FOUR four 4 phor FOUR 44444');");
108 RUN("INSERT INTO t1 VALUES(5,52555,'five 5 FIVE Five phayve 55 5 5 5 5 55 5');");
109 }
110 TIME("25,000 inserts");
111
112 RUN("COMMIT;");
113 TIME("commit");
114
115 // Counts
116 for (i = 0; i < m; i++) {
117 print = i == 0;
118 RUN("SELECT count(*) FROM t1;");
119 RUN("SELECT count(*) FROM t1 WHERE a == 4");
120 RUN("SELECT count(*) FROM t1 WHERE b > 20000 AND b < 50000;");
121 RUN("SELECT count(*) FROM t1 WHERE c like '%three%';");
122 }
123 TIME("selects");
124
125 // Index
126 RUN("CREATE INDEX iiaa ON t1(a);");
127 RUN("CREATE INDEX iibb ON t1(b);");
128 TIME("create indexes");
129
130 for (i = 0; i < m; i++) {
131 print = i == 0;
132 RUN("SELECT count(*) FROM t1 WHERE a == 4");
133 RUN("SELECT count(*) FROM t1 WHERE b > 20000 AND b < 50000;");
134 }
135 TIME("selects with indexes");
136
137 sqlite3_close(db);
138
139 return test();
140 }
141
142