@@ -11,26 +11,48 @@ from tests.utils import (
1111 FakeServer,
1212)
1313
14- alias headers = bytes (''' GET /index.html HTTP/1.1\r\n Host: example.com\r\n User-Agent: Mozilla/5.0\r\n Content-Type: text/html\r\n Content-Length: 1234\r\n Connection: close\r\n Trailer: end-of-message\r\n\r\n ''' )
14+ alias headers = bytes (
15+ """ GET /index.html HTTP/1.1\r\n Host: example.com\r\n User-Agent: Mozilla/5.0\r\n Content-Type: text/html\r\n Content-Length: 1234\r\n Connection: close\r\n Trailer: end-of-message\r\n\r\n """
16+ )
1517alias body = bytes (String(" I am the body of an HTTP request" ) * 5 )
16- alias Request = bytes (''' GET /index.html HTTP/1.1\r\n Host: example.com\r\n User-Agent: Mozilla/5.0\r\n Content-Type: text/html\r\n Content-Length: 1234\r\n Connection: close\r\n Trailer: end-of-message\r\n\r\n ''' ) + body
17- alias Response = bytes (" HTTP/1.1 200 OK\r\n server: lightbug_http\r\n content-type: application/octet-stream\r\n connection: keep-alive\r\n content-length: 13\r\n date: 2024-06-02T13:41:50.766880+00:00\r\n\r\n " ) + body
18+ alias Request = bytes (
19+ """ GET /index.html HTTP/1.1\r\n Host: example.com\r\n User-Agent: Mozilla/5.0\r\n Content-Type: text/html\r\n Content-Length: 1234\r\n Connection: close\r\n Trailer: end-of-message\r\n\r\n """
20+ ) + body
21+ alias Response = bytes (
22+ " HTTP/1.1 200 OK\r\n server: lightbug_http\r\n content-type:"
23+ " application/octet-stream\r\n connection: keep-alive\r\n content-length:"
24+ " 13\r\n date: 2024-06-02T13:41:50.766880+00:00\r\n\r\n "
25+ ) + body
26+
1827
1928fn main ():
2029 run_benchmark()
2130
31+
2232fn run_benchmark ():
2333 try :
2434 var config = BenchConfig(warmup_iters = 100 )
2535 config.verbose_timing = True
2636 config.tabular_view = True
2737 var m = Bench(config)
28- m.bench_function[lightbug_benchmark_header_encode](BenchId(" HeaderEncode" ))
29- m.bench_function[lightbug_benchmark_header_parse](BenchId(" HeaderParse" ))
30- m.bench_function[lightbug_benchmark_request_encode](BenchId(" RequestEncode" ))
31- m.bench_function[lightbug_benchmark_request_parse](BenchId(" RequestParse" ))
32- m.bench_function[lightbug_benchmark_response_encode](BenchId(" ResponseEncode" ))
33- m.bench_function[lightbug_benchmark_response_parse](BenchId(" ResponseParse" ))
38+ m.bench_function[lightbug_benchmark_header_encode](
39+ BenchId(" HeaderEncode" )
40+ )
41+ m.bench_function[lightbug_benchmark_header_parse](
42+ BenchId(" HeaderParse" )
43+ )
44+ m.bench_function[lightbug_benchmark_request_encode](
45+ BenchId(" RequestEncode" )
46+ )
47+ m.bench_function[lightbug_benchmark_request_parse](
48+ BenchId(" RequestParse" )
49+ )
50+ m.bench_function[lightbug_benchmark_response_encode](
51+ BenchId(" ResponseEncode" )
52+ )
53+ m.bench_function[lightbug_benchmark_response_parse](
54+ BenchId(" ResponseParse" )
55+ )
3456 m.dump_report()
3557 except :
3658 print (" failed to start benchmark" )
@@ -41,18 +63,21 @@ var headers_struct = Headers(
4163 Header(" Content-Length" , " 1234" ),
4264 Header(" Connection" , " close" ),
4365 Header(" Date" , " some-datetime" ),
44- Header(" SomeHeader" , " SomeValue" )
66+ Header(" SomeHeader" , " SomeValue" ),
4567)
4668
69+
4770@parameter
4871fn lightbug_benchmark_response_encode (inout b : Bencher):
4972 @always_inline
5073 @parameter
5174 fn response_encode ():
52- var res = HTTPResponse(body, headers = headers_struct)
75+ var res = HTTPResponse(body, headers = headers_struct)
5376 _ = encode(res^ )
77+
5478 b.iter[response_encode]()
5579
80+
5681@parameter
5782fn lightbug_benchmark_response_parse (inout b : Bencher):
5883 @always_inline
@@ -63,8 +88,10 @@ fn lightbug_benchmark_response_parse(inout b: Bencher):
6388 _ = HTTPResponse.from_bytes(res^ )
6489 except :
6590 pass
91+
6692 b.iter[response_parse]()
6793
94+
6895@parameter
6996fn lightbug_benchmark_request_parse (inout b : Bencher):
7097 @always_inline
@@ -75,21 +102,25 @@ fn lightbug_benchmark_request_parse(inout b: Bencher):
75102 _ = HTTPRequest.from_bytes(" 127.0.0.1/path" , 4096 , r^ )
76103 except :
77104 pass
105+
78106 b.iter[request_parse]()
79107
108+
80109@parameter
81110fn lightbug_benchmark_request_encode (inout b : Bencher):
82111 @always_inline
83112 @parameter
84113 fn request_encode ():
85114 var req = HTTPRequest(
86115 URI .parse(" http://127.0.0.1:8080/some-path" )[URI ],
87- headers = headers_struct,
88- body = body
116+ headers = headers_struct,
117+ body = body,
89118 )
90119 _ = encode(req^ )
120+
91121 b.iter[request_encode]()
92-
122+
123+
93124@parameter
94125fn lightbug_benchmark_header_encode (inout b : Bencher):
95126 @always_inline
@@ -98,8 +129,10 @@ fn lightbug_benchmark_header_encode(inout b: Bencher):
98129 var b = ByteWriter()
99130 var h = headers_struct
100131 h.encode_to(b)
132+
101133 b.iter[header_encode]()
102134
135+
103136@parameter
104137fn lightbug_benchmark_header_parse (inout b : Bencher):
105138 @always_inline
@@ -115,16 +148,21 @@ fn lightbug_benchmark_header_parse(inout b: Bencher):
115148
116149 b.iter[header_parse]()
117150
151+
118152fn lightbug_benchmark_server ():
119153 var server_report = benchmark.run[run_fake_server](max_iters = 1 )
120154 print (" Server: " )
121155 server_report.print(benchmark.Unit.ms)
122156
123157
124158fn lightbug_benchmark_misc () -> None :
125- var direct_set_report = benchmark.run[init_test_and_set_a_direct](max_iters = 1 )
159+ var direct_set_report = benchmark.run[init_test_and_set_a_direct](
160+ max_iters = 1
161+ )
126162
127- var recreating_set_report = benchmark.run[init_test_and_set_a_copy](max_iters = 1 )
163+ var recreating_set_report = benchmark.run[init_test_and_set_a_copy](
164+ max_iters = 1
165+ )
128166
129167 print (" Direct set: " )
130168 direct_set_report.print(benchmark.Unit.ms)
@@ -134,6 +172,7 @@ fn lightbug_benchmark_misc() -> None:
134172
135173var GetRequest = HTTPRequest(URI .parse(" http://127.0.0.1/path" )[URI ])
136174
175+
137176fn run_fake_server ():
138177 var handler = FakeResponder()
139178 var listener = new_fake_listener(2 , encode(GetRequest))
0 commit comments