Skip to content

Commit 3f1a05a

Browse files
committed
benchmark: initial
1 parent 94a55d1 commit 3f1a05a

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ tags
55
test
66
test_g
77
test_fast
8+
bench
89
url_parser
910
parsertrace
1011
parsertrace_g

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ CPPFLAGS_DEBUG = $(CPPFLAGS) -DHTTP_PARSER_STRICT=1
2929
CPPFLAGS_DEBUG += $(CPPFLAGS_DEBUG_EXTRA)
3030
CPPFLAGS_FAST = $(CPPFLAGS) -DHTTP_PARSER_STRICT=0
3131
CPPFLAGS_FAST += $(CPPFLAGS_FAST_EXTRA)
32+
CPPFLAGS_BENCH = $(CPPFLAGS_FAST)
3233

3334
CFLAGS += -Wall -Wextra -Werror
3435
CFLAGS_DEBUG = $(CFLAGS) -O0 -g $(CFLAGS_DEBUG_EXTRA)
3536
CFLAGS_FAST = $(CFLAGS) -O3 $(CFLAGS_FAST_EXTRA)
37+
CFLAGS_BENCH = $(CFLAGS_FAST) -Wno-unused-parameter
3638
CFLAGS_LIB = $(CFLAGS_FAST) -fPIC
3739

3840
LDFLAGS_LIB = $(LDFLAGS) -shared
@@ -61,6 +63,12 @@ test_fast: http_parser.o test.o http_parser.h
6163
test.o: test.c http_parser.h Makefile
6264
$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c test.c -o $@
6365

66+
bench: http_parser.o bench.o
67+
$(CC) $(CFLAGS_BENCH) $(LDFLAGS) http_parser.o bench.o -o $@
68+
69+
bench.o: bench.c http_parser.h Makefile
70+
$(CC) $(CPPFLAGS_BENCH) $(CFLAGS_BENCH) -c bench.c -o $@
71+
6472
http_parser.o: http_parser.c http_parser.h Makefile
6573
$(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c http_parser.c
6674

bench.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
"GET /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+
"Cache-Control: max-age=0\r\n\r\n";
41+
static const size_t data_len = sizeof(data) - 1;
42+
43+
static int on_info(http_parser* p) {
44+
return 0;
45+
}
46+
47+
48+
static int on_data(http_parser* p, const char *at, size_t length) {
49+
return 0;
50+
}
51+
52+
static http_parser_settings settings = {
53+
.on_message_begin = on_info,
54+
.on_headers_complete = on_info,
55+
.on_message_complete = on_info,
56+
.on_header_field = on_data,
57+
.on_header_value = on_data,
58+
.on_url = on_data,
59+
.on_status = on_data,
60+
.on_body = on_data
61+
};
62+
63+
int bench(int iter_count, int silent) {
64+
struct http_parser parser;
65+
int i;
66+
int err;
67+
struct timeval start;
68+
struct timeval end;
69+
float rps;
70+
71+
if (!silent) {
72+
err = gettimeofday(&start, NULL);
73+
assert(err == 0);
74+
}
75+
76+
for (i = 0; i < iter_count; i++) {
77+
size_t parsed;
78+
http_parser_init(&parser, HTTP_REQUEST);
79+
80+
parsed = http_parser_execute(&parser, &settings, data, data_len);
81+
assert(parsed == data_len);
82+
}
83+
84+
if (!silent) {
85+
err = gettimeofday(&end, NULL);
86+
assert(err == 0);
87+
88+
fprintf(stdout, "Benchmark result:\n");
89+
90+
rps = (float) (end.tv_sec - start.tv_sec) +
91+
(end.tv_usec - start.tv_usec) * 1e-6f;
92+
fprintf(stdout, "Took %f seconds to run\n", rps);
93+
94+
rps = (float) iter_count / rps;
95+
fprintf(stdout, "%f req/sec\n", rps);
96+
fflush(stdout);
97+
}
98+
99+
return 0;
100+
}
101+
102+
int main(int argc, char** argv) {
103+
if (argc == 2 && strcmp(argv[1], "infinite") == 0) {
104+
for (;;)
105+
bench(5000000, 1);
106+
return 0;
107+
} else {
108+
return bench(5000000, 0);
109+
}
110+
}

0 commit comments

Comments
 (0)