2
0

example_httpsrv_benchmark.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* _
  2. * ___ __ _ __ _ _ _(_)
  3. * / __|/ _` |/ _` | | | | |
  4. * \__ \ (_| | (_| | |_| | |
  5. * |___/\__,_|\__, |\__,_|_|
  6. * |___/
  7. *
  8. * Cross-platform library which helps to develop web servers or frameworks.
  9. *
  10. * Copyright (C) 2016-2020 Silvio Clecio <[email protected]>
  11. *
  12. * Sagui library is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * Sagui library is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with Sagui library; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include <stdlib.h>
  27. #include <stdint.h>
  28. #ifdef _WIN32
  29. #include <windows.h>
  30. #else /* _WIN32 */
  31. #include <unistd.h>
  32. #endif /* _WIN32 */
  33. #include <sagui.h>
  34. /* NOTE: Error checking has been omitted to make it clear. */
  35. #define CONNECTION_LIMIT 1000 /* Change to 10000 for C10K problem. */
  36. static unsigned int get_cpu_count(void) {
  37. #ifdef _WIN32
  38. #ifndef _SC_NPROCESSORS_ONLN
  39. SYSTEM_INFO info;
  40. GetSystemInfo(&info);
  41. #define sysconf(void) info.dwNumberOfProcessors
  42. #define _SC_NPROCESSORS_ONLN
  43. #endif /* _SC_NPROCESSORS_ONLN */
  44. #endif /* _WIN32 */
  45. #ifdef _SC_NPROCESSORS_ONLN
  46. return (unsigned int) sysconf(_SC_NPROCESSORS_ONLN);
  47. #else /* _SC_NPROCESSORS_ONLN */
  48. return 0;
  49. #endif /* _SC_NPROCESSORS_ONLN */
  50. }
  51. static void req_cb(__SG_UNUSED void *cls, __SG_UNUSED struct sg_httpreq *req,
  52. struct sg_httpres *res) {
  53. sg_httpres_send(res, "Hello world", "text/plain", 200);
  54. }
  55. int main(int argc, const char *argv[]) {
  56. struct sg_httpsrv *srv;
  57. unsigned int cpu_count;
  58. uint16_t port;
  59. if (argc != 2) {
  60. printf("%s <PORT>\n", argv[0]);
  61. return EXIT_FAILURE;
  62. }
  63. port = strtol(argv[1], NULL, 10);
  64. cpu_count = get_cpu_count();
  65. srv = sg_httpsrv_new(req_cb, NULL);
  66. sg_httpsrv_set_thr_pool_size(srv, cpu_count);
  67. sg_httpsrv_set_con_limit(srv, CONNECTION_LIMIT);
  68. if (!sg_httpsrv_listen(srv, port, false)) {
  69. sg_httpsrv_free(srv);
  70. return EXIT_FAILURE;
  71. }
  72. fprintf(stdout, "Number of processors: %d\n", cpu_count);
  73. fprintf(stdout, "Connections limit: %d\n", CONNECTION_LIMIT);
  74. fprintf(stdout, "Server running at http://localhost:%d\n",
  75. sg_httpsrv_port(srv));
  76. fflush(stdout);
  77. getchar();
  78. sg_httpsrv_free(srv);
  79. return EXIT_SUCCESS;
  80. }