1 /*-
2  * Copyright (c) 2006. Ivan Voras <ivoras@gmail.com>
3  * Released under the Artistic License
4  */
5 
6 /*
7  * sqlcached benchmarking client
8  * $Id: client_bench.c,v 1.4 2006/05/12 23:32:28 ivoras Exp $
9  */
10 
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/socket.h>
14 #include <sys/time.h>
15 #include <sys/un.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <readline/readline.h>
22 #include <readline/history.h>
23 
24 #include "common.h"
25 #include "sqlcached_client.h"
26 
27 static char* unix_socket_name = DEFAULT_UNIX_SOCKET;
28 
29 int argc;
30 char **argv;
31 
32 struct sc_client_conn sc;
33 
time_float()34 static double time_float() {
35     struct timeval tv;
36     gettimeofday(&tv, NULL);
37     return tv.tv_sec + (double)tv.tv_usec / 1000000.0;
38 }
39 
main(int _argc,char ** _argv)40 int main(int _argc, char** _argv) {
41     char *msg;
42     int i, count, t;
43     double time1, time2;
44     struct sc_result_set rs;
45     char cmd[200];
46 
47     argc = _argc;
48     argv = _argv;
49 
50     if (sc_connect_unix(unix_socket_name, &sc, &msg) != SC_OK) {
51         fprintf(stderr, "Error connecting: %s\n", msg);
52         exit(1);
53     }
54 
55     count = 100000;
56 
57     if (sc_exec_query(&sc, "DELETE FROM cache", NULL, NULL) != SC_OK) {
58         fprintf(stderr, "Error deleting cache: %s\n", msg);
59         exit(1);
60     }
61 
62     printf("Timing %d INSERTs...\n", count);
63     t = time(NULL);
64     time1 = time_float();
65     for (i = 0; i < count; i++) {
66         sprintf(cmd, "INSERT INTO cache (key, value, time) VALUES ('key%d', 'value %d', %d)", i, i, t);
67         if (sc_exec_query(&sc, cmd, &rs, &msg) != SC_OK) {
68             fprintf(stderr, "error in INSERT: %s\n", msg);
69             exit(1);
70         }
71     }
72     time2 = time_float(NULL);
73 
74     printf("%0.1lf qps\n", (double)count / (time2 - time1));
75 
76 
77     printf("Timing %d SELECTs...\n", count);
78     t = time(NULL);
79     time1 = time_float();
80     for (i = 0; i < count; i++) {
81         sprintf(cmd, "SELECT value FROM cache WHERE key= 'key%d'", i);
82         if (sc_exec_query(&sc, cmd, &rs, &msg) != SC_OK) {
83             fprintf(stderr, "error in SELECT: %s\n", msg);
84             exit(1);
85         }
86     }
87     time2 = time_float(NULL);
88 
89     printf("%0.1lf qps\n", (double)count / (time2 - time1));
90 
91 
92     return 0;
93 }
94 
95