Merge pull request #85 from Callum-/dilatometer
[matches/MCTX3420.git] / testing / data_performance / sqlite.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <assert.h>
4 #include <sys/time.h>
5 #include <sqlite3.h>
6
7 typedef struct
8 {
9         float time;
10         float value;
11 } DataPoint;
12
13
14
15
16 int main(int argc, char ** argv)
17 {
18         assert(argc == 3);
19         
20         int bufsiz = atoi(argv[1]);
21         int numpoints = atoi(argv[2]);
22         assert(bufsiz > 0);
23         DataPoint * buffer = (DataPoint*)(calloc(bufsiz, sizeof(DataPoint)));
24
25
26
27         sqlite3 * db;
28         sqlite3_open("sqlite.db", &db);
29
30         
31         char query[BUFSIZ];
32         char * query_value = query+sprintf(query,"insert into sensor values(");
33         
34
35         struct timeval start_time;
36         gettimeofday(&start_time, NULL);
37
38         int i = 0;
39         while (i < numpoints)
40         {
41                 int j = 0;
42                 for (j = 0; j < bufsiz && i < numpoints; ++j)
43                 {
44                         buffer[j].time = i;
45                         buffer[j].value = i;
46                         
47                 }
48                 i += j;
49                 for (int k = 0; k < j; ++k)
50                 {
51                         sprintf(query_value,"%f,%f)", buffer[k].time, buffer[k].value);
52                         sqlite3_exec(db, query, NULL, NULL, NULL);
53                 }
54         }
55
56         struct timeval end_time;
57         gettimeofday(&end_time, NULL);
58
59
60         sqlite3_close(db);
61         free(buffer);
62         float time_elapsed = (float)(end_time.tv_sec - start_time.tv_sec) + 1e-6*(end_time.tv_usec - start_time.tv_usec);
63         printf("%f\n", time_elapsed);
64         return 0;
65 }

UCC git Repository :: git.ucc.asn.au