2 Sample test application.
17 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
18 #define TEST_SERVERS 5
20 long int timedif(struct timeval a
, struct timeval b
)
24 us
= a
.tv_usec
- b
.tv_usec
;
26 s
= a
.tv_sec
- b
.tv_sec
;
31 char *server_startup()
34 char server_string_buffer
[8096];
37 end_ptr
= server_string_buffer
;
39 for (x
= 0; x
< TEST_SERVERS
; x
++)
41 char buffer
[1024]; /* Nothing special for number */
44 sprintf(buffer
, "memcached -d -P /tmp/%umemc.pid -p %u", x
, x
+ TEST_PORT_BASE
);
46 count
= sprintf(end_ptr
, "localhost:%u,", x
+ TEST_PORT_BASE
);
51 return strdup(server_string_buffer
);
54 void server_shutdown(char *server_string
)
60 for (x
= 0; x
< TEST_SERVERS
; x
++)
62 char buffer
[1024]; /* Nothing special for number */
63 sprintf(buffer
, "cat /tmp/%umemc.pid | xargs kill", x
);
71 int main(int argc
, char *argv
[])
75 char *collection_to_run
= NULL
;
77 memcached_server_st
*servers
;
78 collection_st
*collection
;
82 collection
= gets_collections();
86 collection_to_run
= argv
[1];
91 if ((server_list
= getenv("MEMCACHED_SERVERS")))
93 printf("servers %s\n", server_list
);
94 servers
= memcached_servers_parse(server_list
);
99 server_list
= server_startup();
100 printf("servers %s\n", server_list
);
101 servers
= memcached_servers_parse(server_list
);
108 for (x
= 0; x
< memcached_server_list_count(servers
); x
++)
110 printf("\t%s : %u\n", servers
[x
].hostname
, servers
[x
].port
);
111 assert(servers
[x
].fd
== -1);
112 assert(servers
[x
].cursor_active
== 0);
117 for (next
= collection
; next
->name
; next
++)
122 if (collection_to_run
&& fnmatch(collection_to_run
, next
->name
, 0))
125 fprintf(stderr
, "\n%s\n\n", next
->name
);
127 for (x
= 0; run
->name
; run
++)
132 struct timeval start_time
, end_time
;
135 if (wildcard
&& fnmatch(wildcard
, run
->name
, 0))
138 fprintf(stderr
, "Testing %s", run
->name
);
140 memc
= memcached_create(NULL
);
143 rc
= memcached_server_push(memc
, servers
);
144 assert(rc
== MEMCACHED_SUCCESS
);
146 if (run
->requires_flush
)
148 memcached_flush(memc
, 0);
149 memcached_quit(memc
);
152 for (loop
= 0; loop
< memcached_server_list_count(servers
); loop
++)
154 assert(memc
->hosts
[loop
].fd
== -1);
155 assert(memc
->hosts
[loop
].cursor_active
== 0);
163 if (rc
!= MEMCACHED_SUCCESS
)
165 fprintf(stderr
, "\t\t\t\t\t [ skipping ]\n");
170 gettimeofday(&start_time
, NULL
);
171 failed
= run
->function(memc
);
172 gettimeofday(&end_time
, NULL
);
173 load_time
= timedif(end_time
, start_time
);
175 fprintf(stderr
, "\t\t\t\t\t %ld.%03ld [ failed ]\n", load_time
/ 1000,
178 fprintf(stderr
, "\t\t\t\t\t %ld.%03ld [ ok ]\n", load_time
/ 1000,
182 (void)next
->post(memc
);
186 memcached_free(memc
);
190 fprintf(stderr
, "All tests completed successfully\n\n");
192 memcached_server_list_free(servers
);
194 server_shutdown(server_list
);