|
| 1 | +#include <time.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include "sqlite3.h" |
| 4 | + |
| 5 | +static int callback(void *NotUsed, int argc, char **argv, char **azColName){ |
| 6 | + int i; |
| 7 | + for(i=0; i<argc; i++){ |
| 8 | + printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); |
| 9 | + } |
| 10 | + printf("\n"); |
| 11 | + return 0; |
| 12 | +} |
| 13 | + |
| 14 | +int test(){ |
| 15 | + sqlite3 *db; |
| 16 | + char *zErrMsg = 0; |
| 17 | + int rc; |
| 18 | + int i; |
| 19 | + const char *commands[] = { |
| 20 | + "CREATE TABLE t2(a INTEGER, b INTEGER, c VARCHAR(100));", |
| 21 | + "INSERT INTO t2 VALUES(1,13153,'thirteen thousand one hundred fifty three');", |
| 22 | + "INSERT INTO t2 VALUES(1,987,'some other number');", |
| 23 | + "SELECT count(*) FROM t2;", |
| 24 | + "SELECT a, b, c FROM t2;", |
| 25 | + NULL |
| 26 | + }; |
| 27 | + |
| 28 | + rc = sqlite3_open(":memory:", &db); |
| 29 | + if( rc ){ |
| 30 | + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); |
| 31 | + sqlite3_close(db); |
| 32 | + exit(1); |
| 33 | + } |
| 34 | + for (i = 0; commands[i]; i++) { |
| 35 | + rc = sqlite3_exec(db, commands[i], callback, 0, &zErrMsg); |
| 36 | + if( rc!=SQLITE_OK ){ |
| 37 | + fprintf(stderr, "SQL error on %d: %s\n", i, zErrMsg); |
| 38 | + sqlite3_free(zErrMsg); |
| 39 | + exit(1); |
| 40 | + } |
| 41 | + } |
| 42 | + sqlite3_close(db); |
| 43 | + return 0; |
| 44 | +} |
| 45 | + |
| 46 | +int main(){ |
| 47 | + sqlite3 *db; |
| 48 | + char *zErrMsg = 0; |
| 49 | + int rc, i; |
| 50 | + clock_t t; |
| 51 | + |
| 52 | + rc = sqlite3_open(":memory:", &db); |
| 53 | + if( rc ){ |
| 54 | + fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); |
| 55 | + sqlite3_close(db); |
| 56 | + exit(1); |
| 57 | + } |
| 58 | + |
| 59 | + #define RUN(cmd) \ |
| 60 | + { \ |
| 61 | + rc = sqlite3_exec(db, cmd, callback, 0, &zErrMsg); \ |
| 62 | + if( rc!=SQLITE_OK ){ \ |
| 63 | + fprintf(stderr, "SQL error on %d: %s\n", i, zErrMsg); \ |
| 64 | + sqlite3_free(zErrMsg); \ |
| 65 | + exit(1); \ |
| 66 | + } \ |
| 67 | + } |
| 68 | + |
| 69 | + #define TIME(msg) \ |
| 70 | + { \ |
| 71 | + printf(msg " : took %d ms\n", (1000*(clock()-t))/CLOCKS_PER_SEC); \ |
| 72 | + t = clock(); \ |
| 73 | + } |
| 74 | + |
| 75 | + t = clock(); |
| 76 | + TIME("'startup'"); |
| 77 | + |
| 78 | + RUN("CREATE TABLE t1(a INTEGER, b INTEGER, c VARCHAR(100));"); |
| 79 | + TIME("create table"); |
| 80 | + |
| 81 | + RUN("BEGIN;"); |
| 82 | + |
| 83 | + // 25000 INSERTs in a transaction |
| 84 | + for (i = 0; i < 5000; i++) { |
| 85 | + RUN("INSERT INTO t1 VALUES(1,12345,'one 1 one 1 one 1');"); |
| 86 | + RUN("INSERT INTO t1 VALUES(2,23422,'two two two two');"); |
| 87 | + RUN("INSERT INTO t1 VALUES(3,31233,'three three 33333333333 three');"); |
| 88 | + RUN("INSERT INTO t1 VALUES(4,41414,'FOUR four 4 phor FOUR 44444');"); |
| 89 | + RUN("INSERT INTO t1 VALUES(5,52555,'five 5 FIVE Five phayve 55 5 5 5 5 55 5');"); |
| 90 | + } |
| 91 | + TIME("25,000 inserts"); |
| 92 | + |
| 93 | + RUN("COMMIT;"); |
| 94 | + TIME("commit"); |
| 95 | + |
| 96 | + // Counts |
| 97 | + RUN("SELECT count(*) FROM t1;"); |
| 98 | + RUN("SELECT count(*) FROM t1 WHERE a == 4"); |
| 99 | + RUN("SELECT count(*) FROM t1 WHERE b > 20000 AND b < 50000;"); |
| 100 | + RUN("SELECT count(*) FROM t1 WHERE c like '%three%';"); |
| 101 | + TIME("selects"); |
| 102 | + |
| 103 | + // Index |
| 104 | + RUN("CREATE INDEX iiaa ON t1(a);"); |
| 105 | + RUN("CREATE INDEX iibb ON t1(b);"); |
| 106 | + TIME("create indexes"); |
| 107 | + |
| 108 | + RUN("SELECT count(*) FROM t1 WHERE a == 4"); |
| 109 | + RUN("SELECT count(*) FROM t1 WHERE b > 20000 AND b < 50000;"); |
| 110 | + TIME("selects with indexes"); |
| 111 | + |
| 112 | + sqlite3_close(db); |
| 113 | + |
| 114 | + return test(); |
| 115 | +} |
| 116 | + |
0 commit comments