2020 */
2121#include "http_parser.h"
2222#include <assert.h>
23+ #include <stdint.h>
2324#include <stdio.h>
2425#include <string.h>
2526#include <sys/time.h>
2627
28+ /* 8 gb */
29+ static const int64_t kBytes = 8LL << 30 ;
30+
2731static const char data [] =
2832 "POST /joyent/http-parser HTTP/1.1\r\n"
2933 "Host: github.com\r\n"
@@ -38,7 +42,7 @@ static const char data[] =
3842 "Referer: https://github.com/joyent/http-parser\r\n"
3943 "Connection: keep-alive\r\n"
4044 "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" ;
4246static const size_t data_len = sizeof (data ) - 1 ;
4347
4448static int on_info (http_parser * p ) {
@@ -67,13 +71,13 @@ int bench(int iter_count, int silent) {
6771 int err ;
6872 struct timeval start ;
6973 struct timeval end ;
70- float rps ;
7174
7275 if (!silent ) {
7376 err = gettimeofday (& start , NULL );
7477 assert (err == 0 );
7578 }
7679
80+ fprintf (stderr , "req_len=%d\n" , (int ) data_len );
7781 for (i = 0 ; i < iter_count ; i ++ ) {
7882 size_t parsed ;
7983 http_parser_init (& parser , HTTP_REQUEST );
@@ -83,29 +87,42 @@ int bench(int iter_count, int silent) {
8387 }
8488
8589 if (!silent ) {
90+ double elapsed ;
91+ double bw ;
92+ double total ;
93+
8694 err = gettimeofday (& end , NULL );
8795 assert (err == 0 );
8896
8997 fprintf (stdout , "Benchmark result:\n" );
9098
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 );
94110
95- rps = (float ) iter_count / rps ;
96- fprintf (stdout , "%f req/sec\n" , rps );
97111 fflush (stdout );
98112 }
99113
100114 return 0 ;
101115}
102116
103117int main (int argc , char * * argv ) {
118+ int64_t iterations ;
119+
120+ iterations = kBytes / (int64_t ) data_len ;
104121 if (argc == 2 && strcmp (argv [1 ], "infinite" ) == 0 ) {
105122 for (;;)
106- bench (5000000 , 1 );
123+ bench (iterations , 1 );
107124 return 0 ;
108125 } else {
109- return bench (5000000 , 0 );
126+ return bench (iterations , 0 );
110127 }
111128}
0 commit comments