bench_app.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. This is a short implementation fo the TechEmpower Framework Benchmarks. See:
  3. http://frameworkbenchmarks.readthedocs.io/en/latest/
  4. At the moment it's incomplete and only answers the plaintext and json tests
  5. using the full HTTP framework stack (without any DB support).
  6. */
  7. #include "http.h"
  8. #include "fio_cli.h"
  9. /* *****************************************************************************
  10. Internal Helpers
  11. ***************************************************************************** */
  12. /* initialize CLI helper and manage it's default options */
  13. static void cli_init(int argc, char const *argv[]);
  14. /* cleanup any leftovers */
  15. static void cleanup(void);
  16. /* reusable objects */
  17. static FIOBJ HTTP_HEADER_SERVER;
  18. static FIOBJ HTTP_VALUE_SERVER;
  19. static FIOBJ JSON_KEY;
  20. static FIOBJ JSON_VALUE;
  21. /* *****************************************************************************
  22. Routing
  23. ***************************************************************************** */
  24. /* adds a route to our simple router */
  25. static void route_add(char *path, void (*handler)(http_s *));
  26. /* routes a request to the correct handler */
  27. static void route_perform(http_s *);
  28. /* cleanup for our router */
  29. static void route_clear(void);
  30. /* *****************************************************************************
  31. Request handlers
  32. ***************************************************************************** */
  33. /* handles JSON requests */
  34. static void on_request_json(http_s *h);
  35. /* handles plain text requests (Hello World) */
  36. static void on_request_plain_text(http_s *h);
  37. /* *****************************************************************************
  38. The main function
  39. ***************************************************************************** */
  40. int main(int argc, char const *argv[]) {
  41. /* initialize the CLI helper and options */
  42. cli_init(argc, argv);
  43. /* sertup routes */
  44. route_add("/json", on_request_json);
  45. route_add("/plaintext", on_request_plain_text);
  46. /* Server name and header */
  47. HTTP_HEADER_SERVER = fiobj_str_new("server", 6);
  48. HTTP_VALUE_SERVER = fiobj_str_new("facil.io " FIO_VERSION_STRING,
  49. strlen("facil.io " FIO_VERSION_STRING));
  50. /* JSON values to be serialized */
  51. JSON_KEY = fiobj_str_new("message", 7);
  52. JSON_VALUE = fiobj_str_new("Hello, World!", 13);
  53. /* Test for static file service */
  54. const char *public_folder = fio_cli_get("-www");
  55. if (public_folder) {
  56. fprintf(stderr, "* serving static files from:%s\n", public_folder);
  57. }
  58. /* listen to HTTP connections */
  59. http_listen(fio_cli_get("-port"), fio_cli_get("-address"),
  60. .on_request = route_perform, .public_folder = public_folder,
  61. .log = fio_cli_get_bool("-log"));
  62. /* Start the facil.io reactor */
  63. fio_start(.threads = fio_cli_get_i("-t"), .workers = fio_cli_get_i("-w"));
  64. /* perform cleanup */
  65. cleanup();
  66. return 0;
  67. }
  68. /* *****************************************************************************
  69. Request handlers
  70. ***************************************************************************** */
  71. /* handles JSON requests */
  72. static void on_request_json(http_s *h) {
  73. http_set_header(h, HTTP_HEADER_CONTENT_TYPE, http_mimetype_find("json", 4));
  74. FIOBJ json;
  75. /* create a new Hash to be serialized for every request */
  76. FIOBJ hash = fiobj_hash_new2(1);
  77. fiobj_hash_set(hash, JSON_KEY, fiobj_dup(JSON_VALUE));
  78. json = fiobj_obj2json(hash, 0);
  79. fiobj_free(hash);
  80. fio_str_info_s tmp = fiobj_obj2cstr(json);
  81. http_send_body(h, tmp.data, tmp.len);
  82. fiobj_free(json);
  83. }
  84. /* handles plain text requests (Hello World) */
  85. static void on_request_plain_text(http_s *h) {
  86. http_set_header(h, HTTP_HEADER_CONTENT_TYPE, http_mimetype_find("txt", 3));
  87. http_send_body(h, "Hello, World!", 13);
  88. }
  89. /* *****************************************************************************
  90. CLI
  91. ***************************************************************************** */
  92. /* initialize CLI helper and manage it's default options */
  93. static void cli_init(int argc, char const *argv[]) {
  94. fio_cli_start(argc, argv, 0, 0,
  95. "This is a facil.io framework benchmark application.\n"
  96. "\nFor details about the benchmarks visit:\n"
  97. "http://frameworkbenchmarks.readthedocs.io/en/latest/\n"
  98. "\nThe following arguments are supported:",
  99. FIO_CLI_PRINT_HEADER("Concurrency:"),
  100. FIO_CLI_INT("-threads -t The number of threads to use. "
  101. "System dependent default."),
  102. FIO_CLI_INT("-workers -w The number of processes to use. "
  103. "System dependent default."),
  104. FIO_CLI_PRINT_HEADER("Address Binding:"),
  105. FIO_CLI_INT("-port -p The port number to listen to "
  106. "(set to 0 for Unix Sockets."),
  107. FIO_CLI_STRING("-address -b The address to bind to."),
  108. FIO_CLI_PRINT_HEADER("HTTP Settings:"),
  109. FIO_CLI_STRING("-public -www A public folder for serve an HTTP "
  110. "static file service."),
  111. FIO_CLI_BOOL("-log -v Turns logging on (logs to terminal)."),
  112. FIO_CLI_PRINT_HEADER("Misc:"),
  113. FIO_CLI_STRING("-database -db The database adrress (URL)."));
  114. /* setup default port */
  115. if (!fio_cli_get("-p")) {
  116. fio_cli_set("-p", "8080");
  117. fio_cli_set("-port", "8080");
  118. }
  119. /* setup database address */
  120. if (!fio_cli_get("-db")) {
  121. char *database = getenv("DBHOST");
  122. if (!database)
  123. database = "localhost";
  124. fio_cli_set("-db", database);
  125. fio_cli_set("-database", database);
  126. }
  127. }
  128. /* *****************************************************************************
  129. Routing
  130. ***************************************************************************** */
  131. typedef void (*fio_router_handler_fn)(http_s *);
  132. #define FIO_SET_NAME fio_router
  133. #define FIO_SET_OBJ_TYPE fio_router_handler_fn
  134. #define FIO_SET_KEY_TYPE fio_str_s
  135. #define FIO_SET_KEY_COPY(dest, obj) fio_str_concat(&(dest), &(obj))
  136. #define FIO_SET_KEY_DESTROY(obj) fio_str_free(&(obj))
  137. #define FIO_SET_KEY_COMPARE(k1, k2) fio_str_iseq(&(k1), &k2)
  138. #define FIO_INCLUDE_STR
  139. #define FIO_STR_NO_REF
  140. #include <fio.h>
  141. /* the router is a simple hash map */
  142. static fio_router_s routes;
  143. /* adds a route to our simple router */
  144. static void route_add(char *path, void (*handler)(http_s *)) {
  145. /* add handler to the hash map */
  146. fio_str_s tmp = FIO_STR_INIT_STATIC(path);
  147. /* fio hash maps support up to 96 full collisions, we can use len as hash */
  148. fio_router_insert(&routes, fio_str_len(&tmp), tmp, handler, NULL);
  149. }
  150. /* routes a request to the correct handler */
  151. static void route_perform(http_s *h) {
  152. /* add required Serevr header */
  153. http_set_header(h, HTTP_HEADER_SERVER, fiobj_dup(HTTP_VALUE_SERVER));
  154. /* collect path from hash map */
  155. fio_str_info_s tmp_i = fiobj_obj2cstr(h->path);
  156. fio_str_s tmp = FIO_STR_INIT_EXISTING(tmp_i.data, tmp_i.len, 0);
  157. fio_router_handler_fn handler = fio_router_find(&routes, tmp_i.len, tmp);
  158. /* forward request or send error */
  159. if (handler) {
  160. handler(h);
  161. return;
  162. }
  163. http_send_error(h, 404);
  164. }
  165. /* cleanup for our router */
  166. static void route_clear(void) { fio_router_free(&routes); }
  167. /* *****************************************************************************
  168. Cleanup
  169. ***************************************************************************** */
  170. /* cleanup any leftovers */
  171. static void cleanup(void) {
  172. fio_cli_end();
  173. fiobj_free(HTTP_HEADER_SERVER);
  174. fiobj_free(HTTP_VALUE_SERVER);
  175. fiobj_free(JSON_KEY);
  176. fiobj_free(JSON_VALUE);
  177. route_clear();
  178. }