blobslap_client.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Gearmand client and server library.
  4. *
  5. * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
  6. * Copyright (C) 2008 Brian Aker, Eric Day
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are
  11. * met:
  12. *
  13. * * Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following disclaimer
  18. * in the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * * The names of its contributors may not be used to endorse or
  22. * promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. */
  38. /**
  39. * @file
  40. * @brief Blob slap client utility
  41. */
  42. #include "gear_config.h"
  43. #include <benchmark/benchmark.h>
  44. #include <iostream>
  45. #include <cstdlib>
  46. #include <cstring>
  47. #include <cstdio>
  48. #include <unistd.h>
  49. #define BLOBSLAP_DEFAULT_NUM_TASKS 10
  50. #define BLOBSLAP_DEFAULT_BLOB_MIN_SIZE 0
  51. #define BLOBSLAP_DEFAULT_BLOB_MAX_SIZE 1024
  52. #define BLOBSLAP_BUFFER_SIZE 8192
  53. static gearman_return_t _created(gearman_task_st *task);
  54. static gearman_return_t _data(gearman_task_st *task);
  55. static gearman_return_t _status(gearman_task_st *task);
  56. static gearman_return_t _complete(gearman_task_st *task);
  57. static gearman_return_t _fail(gearman_task_st *task);
  58. namespace {
  59. void client_logging_fn(const char *message, gearman_verbose_t verbose, void*)
  60. {
  61. fprintf(stderr, "%s (%s)\n", message, gearman_verbose_name(verbose));
  62. }
  63. }
  64. static void _usage(char *name);
  65. int main(int argc, char *argv[])
  66. {
  67. gearman_benchmark_st benchmark;
  68. int c;
  69. char *host= NULL;
  70. in_port_t port= 0;
  71. const char *function= GEARMAN_BENCHMARK_DEFAULT_FUNCTION;
  72. uint32_t num_tasks= BLOBSLAP_DEFAULT_NUM_TASKS;
  73. size_t min_size= BLOBSLAP_DEFAULT_BLOB_MIN_SIZE;
  74. size_t max_size= BLOBSLAP_DEFAULT_BLOB_MAX_SIZE;
  75. unsigned long int count= 1;
  76. gearman_client_st master_client;
  77. bool shutdown_worker= false;
  78. if (gearman_client_create(&master_client) == NULL)
  79. {
  80. std::cerr << "Failed to allocate memory for client" << std::endl;
  81. return EXIT_FAILURE;
  82. }
  83. gearman_client_add_options(&master_client, GEARMAN_CLIENT_UNBUFFERED_RESULT);
  84. while ((c= getopt(argc, argv, "bc:f:h:m:M:n:p:s:ve?")) != -1)
  85. {
  86. switch(c)
  87. {
  88. case 'b':
  89. benchmark.background= true;
  90. break;
  91. case 'c':
  92. count= strtoul(optarg, NULL, 10);
  93. break;
  94. case 'f':
  95. function= optarg;
  96. break;
  97. case 'h':
  98. {
  99. if (gearman_failed(gearman_client_add_server(&master_client, host, port)))
  100. {
  101. std::cerr << "Failed while adding server " << host << ":" << port << " :" << gearman_client_error(&master_client) << std::endl;
  102. exit(EXIT_FAILURE);
  103. }
  104. }
  105. break;
  106. case 'm':
  107. min_size= static_cast<size_t>(strtoul(optarg, NULL, 10));
  108. break;
  109. case 'M':
  110. max_size= static_cast<size_t>(strtoul(optarg, NULL, 10));
  111. break;
  112. case 'n':
  113. num_tasks= uint32_t(strtoul(optarg, NULL, 10));
  114. break;
  115. case 'p':
  116. port= in_port_t(atoi(optarg));
  117. break;
  118. case 's':
  119. srand(uint32_t(atoi(optarg)));
  120. break;
  121. case 'e':
  122. shutdown_worker= true;
  123. break;
  124. case 'v':
  125. benchmark.verbose++;
  126. break;
  127. case '?':
  128. gearman_client_free(&master_client);
  129. _usage(argv[0]);
  130. exit(EXIT_SUCCESS);
  131. default:
  132. gearman_client_free(&master_client);
  133. _usage(argv[0]);
  134. exit(EXIT_FAILURE);
  135. }
  136. }
  137. if (benchmark.verbose > 2)
  138. {
  139. gearman_client_set_log_fn(&master_client, client_logging_fn, NULL, GEARMAN_VERBOSE_DEBUG);
  140. }
  141. if (host == NULL)
  142. {
  143. if (getenv("GEARMAN_SERVERS") == NULL)
  144. {
  145. if (gearman_failed(gearman_client_add_server(&master_client, NULL, port)))
  146. {
  147. std::cerr << "Failing to add localhost:" << port << " :" << gearman_client_error(&master_client) << std::endl;
  148. exit(EXIT_FAILURE);
  149. }
  150. }
  151. }
  152. if (min_size > max_size)
  153. {
  154. std::cerr << "Min data size must be smaller than max data size" << std::endl;
  155. exit(EXIT_FAILURE);
  156. }
  157. if (num_tasks == 0)
  158. {
  159. std::cerr << "Number of tasks must be larger than zero\n" << std::endl;
  160. exit(EXIT_FAILURE);
  161. }
  162. gearman_task_st *tasks= new gearman_task_st[num_tasks];
  163. if (not tasks)
  164. {
  165. std::cerr << "Failed to allocate " << num_tasks << " tasks" << std::endl;
  166. exit(EXIT_FAILURE);
  167. }
  168. char *blob= new char[max_size];
  169. if (not blob)
  170. {
  171. std::cerr << "Failed to allocate blob with length of " << max_size << std::endl;
  172. exit(EXIT_FAILURE);
  173. }
  174. memset(blob, 'x', max_size);
  175. bool error= false;
  176. do
  177. {
  178. gearman_client_st client;
  179. if (gearman_client_clone(&client, &master_client) == NULL)
  180. {
  181. std::cerr << "Failed to allocate client clone" << std::endl;
  182. exit(EXIT_FAILURE);
  183. }
  184. for (uint32_t x= 0; x < num_tasks; x++)
  185. {
  186. size_t blob_size;
  187. if (min_size == max_size)
  188. {
  189. blob_size= max_size;
  190. }
  191. else
  192. {
  193. blob_size= size_t(rand());
  194. if (max_size > RAND_MAX)
  195. blob_size*= size_t(rand()) + 1;
  196. blob_size= (blob_size % (max_size - min_size)) + min_size;
  197. }
  198. const char *blob_ptr= blob_size ? blob : NULL;
  199. gearman_return_t ret;
  200. if (benchmark.background)
  201. {
  202. (void)gearman_client_add_task_background(&client, &(tasks[x]),
  203. &benchmark, function, NULL,
  204. blob_ptr, blob_size, &ret);
  205. }
  206. else
  207. {
  208. (void)gearman_client_add_task(&client, &(tasks[x]), &benchmark,
  209. function, NULL, blob_ptr, blob_size,
  210. &ret);
  211. }
  212. if (gearman_failed(ret))
  213. {
  214. if (ret == GEARMAN_LOST_CONNECTION)
  215. {
  216. if (benchmark.verbose > 1)
  217. {
  218. std::cerr << "Error occurred while trying to add task: " << gearman_client_error(&client);
  219. }
  220. continue;
  221. }
  222. if (benchmark.background)
  223. {
  224. std::cerr << "Task #" << x << " failed during gearman_client_add_task_background(" << gearman_strerror(ret) << " -> " << gearman_client_error(&client) << std::endl ;
  225. }
  226. else
  227. {
  228. std::cerr << "Task #" << x << " failed during gearman_client_add_task(" << gearman_strerror(ret) << " -> " << gearman_client_error(&client) << std::endl ;
  229. }
  230. error= true;
  231. goto exit_immediately;
  232. }
  233. }
  234. gearman_client_set_created_fn(&client, _created);
  235. gearman_client_set_data_fn(&client, _data);
  236. gearman_client_set_status_fn(&client, _status);
  237. gearman_client_set_complete_fn(&client, _complete);
  238. gearman_client_set_fail_fn(&client, _fail);
  239. gearman_client_set_timeout(&client, 1000);
  240. gearman_return_t ret;
  241. do {
  242. ret= gearman_client_run_tasks(&client);
  243. } while (gearman_continue(ret));
  244. if (ret == GEARMAN_TIMEOUT)
  245. {
  246. error= true;
  247. }
  248. else if (gearman_failed(ret) and ret != GEARMAN_LOST_CONNECTION)
  249. {
  250. std::cerr << "gearman_client_run_tasks(" << gearman_strerror(ret) << ") -> " << gearman_client_error(&client);
  251. for (uint32_t x= 0; x < num_tasks; x++)
  252. {
  253. if (gearman_task_error(&tasks[x]))
  254. {
  255. std::cerr << "\t Task #" << x << " failed with " << gearman_task_error(&tasks[x]) << std::endl;
  256. }
  257. }
  258. error= true;
  259. }
  260. for (uint32_t x= 0; x < num_tasks; x++)
  261. {
  262. gearman_task_free(&(tasks[x]));
  263. }
  264. count--;
  265. gearman_client_free(&client);
  266. } while (count or error);
  267. exit_immediately:
  268. if (shutdown_worker)
  269. {
  270. gearman_client_do(&master_client, "shutdown", 0, 0, 0, 0, 0);
  271. }
  272. delete [] blob;
  273. delete [] tasks;
  274. gearman_client_free(&master_client);
  275. if (benchmark.verbose)
  276. {
  277. std::cout << "Successfully completed all tasks" << std::endl;
  278. }
  279. return error ? EXIT_FAILURE : 0;
  280. }
  281. static gearman_return_t _created(gearman_task_st *task)
  282. {
  283. gearman_benchmark_st *benchmark= static_cast<gearman_benchmark_st *>(gearman_task_context(task));
  284. if (benchmark->background && benchmark->verbose > 0)
  285. {
  286. benchmark_check_time(benchmark);
  287. }
  288. if (benchmark->verbose > 2)
  289. {
  290. std::cout << "Created: " << gearman_task_job_handle(task) << std::endl;
  291. }
  292. return GEARMAN_SUCCESS;
  293. }
  294. static gearman_return_t _status(gearman_task_st *task)
  295. {
  296. gearman_benchmark_st *benchmark= static_cast<gearman_benchmark_st *>(gearman_task_context(task));
  297. if (benchmark->verbose > 2)
  298. {
  299. std::cout << "Status " << gearman_task_job_handle(task) << " " << gearman_task_numerator(task) << " " << gearman_task_denominator(task) << std::endl;
  300. }
  301. return GEARMAN_SUCCESS;
  302. }
  303. static gearman_return_t _data(gearman_task_st *task)
  304. {
  305. char buffer[BLOBSLAP_BUFFER_SIZE];
  306. gearman_return_t ret;
  307. gearman_benchmark_st *benchmark= static_cast<gearman_benchmark_st *>(gearman_task_context(task));
  308. while (1)
  309. {
  310. size_t size= gearman_task_recv_data(task, buffer, BLOBSLAP_BUFFER_SIZE, &ret);
  311. if (gearman_failed(GEARMAN_SUCCESS))
  312. {
  313. return ret;
  314. }
  315. if (size == 0)
  316. {
  317. break;
  318. }
  319. }
  320. if (benchmark->verbose > 2)
  321. {
  322. std::cerr << "Data: " << gearman_task_job_handle(task) << " " << gearman_task_data_size(task) << std::endl;
  323. }
  324. return GEARMAN_SUCCESS;
  325. }
  326. static gearman_return_t _complete(gearman_task_st *task)
  327. {
  328. char buffer[BLOBSLAP_BUFFER_SIZE];
  329. gearman_benchmark_st *benchmark= static_cast<gearman_benchmark_st *>(gearman_task_context(task));
  330. while (1)
  331. {
  332. gearman_return_t ret;
  333. size_t size= gearman_task_recv_data(task, buffer, BLOBSLAP_BUFFER_SIZE, &ret);
  334. if (gearman_failed(ret))
  335. {
  336. return ret;
  337. }
  338. if (size == 0)
  339. {
  340. break;
  341. }
  342. }
  343. if (benchmark->verbose > 0)
  344. {
  345. benchmark_check_time(benchmark);
  346. }
  347. if (benchmark->verbose > 1)
  348. {
  349. std::cout << "Completed: " << gearman_task_job_handle(task) << " " << gearman_task_data_size(task) << std::endl;
  350. }
  351. return GEARMAN_SUCCESS;
  352. }
  353. static gearman_return_t _fail(gearman_task_st *task)
  354. {
  355. gearman_benchmark_st *benchmark= static_cast<gearman_benchmark_st *>(gearman_task_context(task));
  356. if (benchmark->verbose > 0)
  357. benchmark_check_time(benchmark);
  358. if (benchmark->verbose > 1)
  359. {
  360. std::cerr << "Failed " << gearman_task_job_handle(task) << " " << gearman_task_error(task) << std::endl;
  361. }
  362. return GEARMAN_SUCCESS;
  363. }
  364. static void _usage(char *name)
  365. {
  366. printf("\nusage: %s\n"
  367. "\t[-c count] [-f <function>] [-h <host>] [-m <min_size>]\n"
  368. "\t[-M <max_size>] [-n <num_tasks>] [-p <port>] [-s] [-v]\n\n", name);
  369. printf("\t-c <count> - number of times to run all tasks\n");
  370. printf("\t-f <function> - function name for tasks (default %s)\n", GEARMAN_BENCHMARK_DEFAULT_FUNCTION);
  371. printf("\t-h <host> - job server host, can specify many\n");
  372. printf("\t-m <min_size> - minimum blob size (default %d)\n", BLOBSLAP_DEFAULT_BLOB_MIN_SIZE);
  373. printf("\t-M <max_size> - maximum blob size (default %d)\n", BLOBSLAP_DEFAULT_BLOB_MAX_SIZE);
  374. printf("\t-n <num_tasks> - number of tasks to run at once (default %d)\n", BLOBSLAP_DEFAULT_NUM_TASKS);
  375. printf("\t-p <port> - job server port\n");
  376. printf("\t-s <seed> - seed random number for blobsize with <seed>\n");
  377. printf("\t-e - tell worker to shutdown when done\n");
  378. printf("\t-b - background\n");
  379. printf("\t-v - increase verbose level\n");
  380. }