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