20
20
*/
21
21
#include "http_parser.h"
22
22
#include <assert.h>
23
+ #include <stdint.h>
23
24
#include <stdio.h>
24
25
#include <string.h>
25
26
#include <sys/time.h>
26
27
28
+ /* 8 gb */
29
+ static const int64_t kBytes = 8LL << 30 ;
30
+
27
31
static const char data [] =
28
32
"POST /joyent/http-parser HTTP/1.1\r\n"
29
33
"Host: github.com\r\n"
@@ -38,7 +42,7 @@ static const char data[] =
38
42
"Referer: https://github.com/joyent/http-parser\r\n"
39
43
"Connection: keep-alive\r\n"
40
44
"Transfer-Encoding: chunked\r\n"
41
- "Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n\r\n " ;
45
+ "Cache-Control: max-age=0\r\n\r\nb\r\nhello world\r\n0\r\n" ;
42
46
static const size_t data_len = sizeof (data ) - 1 ;
43
47
44
48
static int on_info (http_parser * p ) {
@@ -67,13 +71,13 @@ int bench(int iter_count, int silent) {
67
71
int err ;
68
72
struct timeval start ;
69
73
struct timeval end ;
70
- float rps ;
71
74
72
75
if (!silent ) {
73
76
err = gettimeofday (& start , NULL );
74
77
assert (err == 0 );
75
78
}
76
79
80
+ fprintf (stderr , "req_len=%d\n" , (int ) data_len );
77
81
for (i = 0 ; i < iter_count ; i ++ ) {
78
82
size_t parsed ;
79
83
http_parser_init (& parser , HTTP_REQUEST );
@@ -83,29 +87,42 @@ int bench(int iter_count, int silent) {
83
87
}
84
88
85
89
if (!silent ) {
90
+ double elapsed ;
91
+ double bw ;
92
+ double total ;
93
+
86
94
err = gettimeofday (& end , NULL );
87
95
assert (err == 0 );
88
96
89
97
fprintf (stdout , "Benchmark result:\n" );
90
98
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 );
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 );
94
110
95
- rps = (float ) iter_count / rps ;
96
- fprintf (stdout , "%f req/sec\n" , rps );
97
111
fflush (stdout );
98
112
}
99
113
100
114
return 0 ;
101
115
}
102
116
103
117
int main (int argc , char * * argv ) {
118
+ int64_t iterations ;
119
+
120
+ iterations = kBytes / (int64_t ) data_len ;
104
121
if (argc == 2 && strcmp (argv [1 ], "infinite" ) == 0 ) {
105
122
for (;;)
106
- bench (5000000 , 1 );
123
+ bench (iterations , 1 );
107
124
return 0 ;
108
125
} else {
109
- return bench (5000000 , 0 );
126
+ return bench (iterations , 0 );
110
127
}
111
128
}
0 commit comments