bench.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Copyright Fedor Indutny. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include "http_parser.h"
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <sys/time.h>
  26. static const char data[] =
  27. "POST /joyent/http-parser HTTP/1.1\r\n"
  28. "Host: github.com\r\n"
  29. "DNT: 1\r\n"
  30. "Accept-Encoding: gzip, deflate, sdch\r\n"
  31. "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n"
  32. "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) "
  33. "AppleWebKit/537.36 (KHTML, like Gecko) "
  34. "Chrome/39.0.2171.65 Safari/537.36\r\n"
  35. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,"
  36. "image/webp,*/*;q=0.8\r\n"
  37. "Referer: https://github.com/joyent/http-parser\r\n"
  38. "Connection: keep-alive\r\n"
  39. "Transfer-Encoding: chunked\r\n"
  40. "Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n";
  41. static const size_t data_len = sizeof(data) - 1;
  42. static int on_info(http_parser* p) {
  43. return 0;
  44. }
  45. static int on_data(http_parser* p, const char *at, size_t length) {
  46. return 0;
  47. }
  48. static http_parser_settings settings = {
  49. .on_message_begin = on_info,
  50. .on_headers_complete = on_info,
  51. .on_message_complete = on_info,
  52. .on_header_field = on_data,
  53. .on_header_value = on_data,
  54. .on_url = on_data,
  55. .on_status = on_data,
  56. .on_body = on_data
  57. };
  58. int bench(int iter_count, int silent) {
  59. struct http_parser parser;
  60. int i;
  61. int err;
  62. struct timeval start;
  63. struct timeval end;
  64. float rps;
  65. if (!silent) {
  66. err = gettimeofday(&start, NULL);
  67. assert(err == 0);
  68. }
  69. for (i = 0; i < iter_count; i++) {
  70. size_t parsed;
  71. http_parser_init(&parser, HTTP_REQUEST);
  72. parsed = http_parser_execute(&parser, &settings, data, data_len);
  73. assert(parsed == data_len);
  74. }
  75. if (!silent) {
  76. err = gettimeofday(&end, NULL);
  77. assert(err == 0);
  78. fprintf(stdout, "Benchmark result:\n");
  79. rps = (float) (end.tv_sec - start.tv_sec) +
  80. (end.tv_usec - start.tv_usec) * 1e-6f;
  81. fprintf(stdout, "Took %f seconds to run\n", rps);
  82. rps = (float) iter_count / rps;
  83. fprintf(stdout, "%f req/sec\n", rps);
  84. fflush(stdout);
  85. }
  86. return 0;
  87. }
  88. int main(int argc, char** argv) {
  89. if (argc == 2 && strcmp(argv[1], "infinite") == 0) {
  90. for (;;)
  91. bench(5000000, 1);
  92. return 0;
  93. } else {
  94. return bench(5000000, 0);
  95. }
  96. }