1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <time.h>
5 #include <sys/time.h>
6 
7 #include <sqlite3.h>
8 
time_float()9 static double time_float() {
10     struct timeval tv;
11     gettimeofday(&tv, NULL);
12     return tv.tv_sec + (double)tv.tv_usec / 1000000.0;
13 }
14 
15 
main(int argc,char ** argv)16 int main(int argc, char** argv) {
17     int i, count, t;
18     double time1, time2;
19     sqlite3 *db;
20     char *msg;
21     char sql[200];
22 
23     if (sqlite3_open(":memory:", &db) != SQLITE_OK) {
24         fprintf(stderr, "Error: %s\n", msg);
25         exit(1);
26     }
27 
28     sqlite3_exec(db, "CREATE TABLE cache (key varchar not null, value varchar not null, time integer not null, primary key(key))", NULL, NULL, NULL);
29     sqlite3_exec(db, "CREATE INDEX cache_time ON cache(time)", NULL, NULL, NULL);
30 
31     count = 100000;
32 
33     printf("Timing %d INSERTs...\n", count);
34     t = time(NULL);
35     time1 = time_float();
36     for (i = 0; i < count; i++) {
37         sprintf(sql, "INSERT INTO cache(key, value, time) VALUES ('key%d', 'value %d', %d)", i, i, t);
38         if (sqlite3_exec(db, sql, NULL, NULL, &msg) != SQLITE_OK) {
39             fprintf(stderr, "sqlite error %s\n", msg);
40             exit(1);
41         }
42     }
43     time2 = time_float();
44     printf("%0.1f qps\n", (float)(count / (time2-time1)));
45 
46 
47     printf("Timing %d SELECTs...\n", count);
48     time1 = time_float();
49     for (i = 0; i < count; i++) {
50         sprintf(sql, "SELECT value FROM cache WHERE key='key%d'", i);
51         if (sqlite3_exec(db, sql, NULL, NULL, &msg) != SQLITE_OK) {
52             fprintf(stderr, "sqlite error %s\n", msg);
53             exit(1);
54         }
55     }
56     time2 = time_float();
57     printf("%0.1f qps\n", (float)(count / (time2-time1)));
58 
59 
60     return 0;
61 }
62