1 /* Copyright Fedor Indutny. All rights reserved.
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
21 #include "http_parser.h"
27 static const char data[] =
28 "POST /joyent/http-parser HTTP/1.1\r\n"
29 "Host: github.com\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;
44 static int on_info(http_parser* p) {
49 static int on_data(http_parser* p, const char *at, size_t length) {
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,
64 int bench(int iter_count, int silent) {
65 struct http_parser parser;
73 err = gettimeofday(&start, NULL);
77 for (i = 0; i < iter_count; i++) {
79 http_parser_init(&parser, HTTP_REQUEST);
81 parsed = http_parser_execute(&parser, &settings, data, data_len);
82 assert(parsed == data_len);
86 err = gettimeofday(&end, NULL);
89 fprintf(stdout, "Benchmark result:\n");
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);
95 rps = (float) iter_count / rps;
96 fprintf(stdout, "%f req/sec\n", rps);
103 int main(int argc, char** argv) {
104 if (argc == 2 && strcmp(argv[1], "infinite") == 0) {
109 return bench(5000000, 0);