1
0

bench.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <stdint.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <sys/time.h>
  27. /* 8 gb */
  28. static const int64_t kBytes = 8LL << 30;
  29. static const char data[] =
  30. "POST /joyent/http-parser HTTP/1.1\r\n"
  31. "Host: github.com\r\n"
  32. "DNT: 1\r\n"
  33. "Accept-Encoding: gzip, deflate, sdch\r\n"
  34. "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4\r\n"
  35. "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) "
  36. "AppleWebKit/537.36 (KHTML, like Gecko) "
  37. "Chrome/39.0.2171.65 Safari/537.36\r\n"
  38. "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,"
  39. "image/webp,*/*;q=0.8\r\n"
  40. "Referer: https://github.com/joyent/http-parser\r\n"
  41. "Connection: keep-alive\r\n"
  42. "Transfer-Encoding: chunked\r\n"
  43. "Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n";
  44. static const size_t data_len = sizeof(data) - 1;
  45. static int on_info(http_parser* p) {
  46. return 0;
  47. }
  48. static int on_data(http_parser* p, const char *at, size_t length) {
  49. return 0;
  50. }
  51. static http_parser_settings settings = {
  52. .on_message_begin = on_info,
  53. .on_headers_complete = on_info,
  54. .on_message_complete = on_info,
  55. .on_header_field = on_data,
  56. .on_header_value = on_data,
  57. .on_url = on_data,
  58. .on_status = on_data,
  59. .on_body = on_data
  60. };
  61. int bench(int iter_count, int silent) {
  62. struct http_parser parser;
  63. int i;
  64. int err;
  65. struct timeval start;
  66. struct timeval end;
  67. if (!silent) {
  68. err = gettimeofday(&start, NULL);
  69. assert(err == 0);
  70. }
  71. fprintf(stderr, "req_len=%d\n", (int) data_len);
  72. for (i = 0; i < iter_count; i++) {
  73. size_t parsed;
  74. http_parser_init(&parser, HTTP_REQUEST);
  75. parsed = http_parser_execute(&parser, &settings, data, data_len);
  76. assert(parsed == data_len);
  77. }
  78. if (!silent) {
  79. double elapsed;
  80. double bw;
  81. double total;
  82. err = gettimeofday(&end, NULL);
  83. assert(err == 0);
  84. fprintf(stdout, "Benchmark result:\n");
  85. elapsed = (double) (end.tv_sec - start.tv_sec) +
  86. (end.tv_usec - start.tv_usec) * 1e-6f;
  87. total = (double) iter_count * data_len;
  88. bw = (double) total / elapsed;
  89. fprintf(stdout, "%.2f mb | %.2f mb/s | %.2f req/sec | %.2f s\n",
  90. (double) total / (1024 * 1024),
  91. bw / (1024 * 1024),
  92. (double) iter_count / elapsed,
  93. elapsed);
  94. fflush(stdout);
  95. }
  96. return 0;
  97. }
  98. int main(int argc, char** argv) {
  99. int64_t iterations;
  100. iterations = kBytes / (int64_t) data_len;
  101. if (argc == 2 && strcmp(argv[1], "infinite") == 0) {
  102. for (;;)
  103. bench(iterations, 1);
  104. return 0;
  105. } else {
  106. return bench(iterations, 0);
  107. }
  108. }