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