forked from brimworks/lua-http-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.lua
executable file
·309 lines (266 loc) · 8.25 KB
/
bench.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env lua
local socket = require"socket"
local time = socket.gettime
local clock = os.clock
local quiet = false
local disable_gc = true
if arg[1] == '-gc' then
disable_gc = false
table.remove(arg,1)
else
print"GC is disabled so we can track memory usage better"
print""
end
local function printf(fmt, ...)
local res
if not quiet then
fmt = fmt or ''
res = print(string.format(fmt, ...))
io.stdout:flush()
end
return res
end
local function full_gc()
-- make sure all free-able memory is freed
collectgarbage"collect"
collectgarbage"collect"
collectgarbage"collect"
end
local function bench(name, N, func, ...)
local start1,start2
printf('run bench: %s', name)
start1 = clock()
start2 = time()
func(N, ...)
local diff1 = (clock() - start1)
local diff2 = (time() - start2)
printf("total time: %10.6f (%10.6f) seconds", diff1, diff2)
end
local lhp = require 'http.parser'
local function parse_path_query_fragment(uri)
local path, query, fragment, off
-- parse path
path, off = uri:match('([^?]*)()')
-- parse query
if uri:sub(off, off) == '?' then
query, off = uri:match('([^#]*)()', off + 1)
end
-- parse fragment
if uri:sub(off, off) == '#' then
fragment = uri:sub(off + 1)
off = #uri
end
return path or '/', query, fragment
end
local expects = {}
local requests = {}
-- NOTE: All requests must be version HTTP/1.1 since we re-use the same HTTP parser for all requests.
requests.ab = {
"GET /foo/t.html?qstring#frag HTTP/1.1\r\nHost: localhost:8000\r\nUser-Agent: ApacheBench/2.3\r\nContent-Length: 5\r\nAccept: */*\r\n\r\nbody\n",
}
expects.ab = {
url = "/foo/t.html?qstring#frag",
path = "/foo/t.html",
query_string = "qstring",
fragment = "frag",
headers = {
Host = "localhost:8000",
["User-Agent"] = "ApacheBench/2.3",
Accept = "*/*",
},
body = "body\n",
}
requests.no_buff_body = {
"GET / HTTP/1.1\r\n",
"Host: foo:80\r\n",
"Content-Length: 12\r\n",
"\r\n",
"chunk1", "chunk2",
}
expects.no_buff_body = {
body = "chunk1chunk2"
}
requests.httperf = {
"GET / HTTP/1.1\r\nHost: localhost\r\nUser-Agent: httperf/0.9.0\r\n\r\n"
}
expects.httperf = {
url = "/",
path = "/",
headers = {
Host = "localhost",
["User-Agent"] = "httperf/0.9.0",
},
}
requests.firefox = {
"GET / HTTP/1.1\r\nHost: two.local:8000\r\nUser-Agent: Mozilla/5.0 (X11; U;Linux i686; en-US; rv:1.9.0.15)Gecko/2009102815 Ubuntu/9.04 (jaunty)Firefox/3.0.15\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language:en-gb,en;q=0.5\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection:keep-alive\r\n\r\n"
}
expects.firefox = {
url = "/",
path = "/",
headers = {
["User-Agent"] = "Mozilla/5.0 (X11; U;Linux i686; en-US; rv:1.9.0.15)Gecko/2009102815 Ubuntu/9.04 (jaunty)Firefox/3.0.15",
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
["Accept-Language"] = "en-gb,en;q=0.5",
["Accept-Encoding"] = "gzip,deflate",
["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
["Keep-Alive"] = "300",
Connection = "keep-alive",
}
}
local function init_parser()
local reqs = {}
local cur = nil
local cb = {}
function cb.on_message_begin()
assert(cur == nil)
cur = { headers = {} }
end
function cb.on_url(value)
assert(cur.url == nil, "[url]=["..tostring(cur.url).."] .. [" .. tostring(value) .. "]")
cur.url = value
cur.path, cur.query_string, cur.fragment = parse_path_query_fragment(value)
end
function cb.on_body(value)
if ( nil == cur.body ) then
cur.body = ''
end
if nil ~= value then
cur.body = cur.body .. value
end
end
function cb.on_header(field, value)
assert(cur.headers[field] == nil)
cur.headers[field] = value
end
function cb.on_message_complete()
assert(nil ~= cur)
table.insert(reqs, cur)
cur = nil
end
return lhp.request(cb), reqs
end
local function null_cb()
end
local null_cbs = {
on_message_begin = null_cb,
"message_begin",
on_url = null_cb,
on_header = null_cb,
on_headers_complete = null_cb,
on_body = null_cb,
on_message_complete = null_cb,
}
local function init_null_parser()
return lhp.request(null_cbs)
end
local function assert_deeply(got, expect, context)
assert(type(expect) == "table", "Expected [" .. context .. "] to be a table")
for k, v in pairs(expect) do
local ctx = context .. "." .. k
if type(expect[k]) == "table" then
assert_deeply(got[k], expect[k], ctx)
else
assert(got[k] == expect[k], "Expected [" .. ctx .. "] to be '" .. tostring(expect[k]) .. "', but got '" .. tostring(got[k]) .. "'")
end
end
end
local function good_client(parser, data)
for _, line in ipairs(data) do
local bytes_read = parser:execute(line)
assert(bytes_read == #line, "only ["..tostring(bytes_read).."] bytes read, expected ["..tostring(#line).."]")
end
end
local function bad_client(parser, data)
for _, line in ipairs(data) do
local total = 0
for i=1,#line do
local bytes_read = parser:execute(line:sub(i,i))
assert(1 == bytes_read, "only ["..tostring(bytes_read).."] bytes read, expected ["..tostring(#line).."]")
total = total + 1
end
assert(total == #line, "only ["..tostring(bytes_read).."] bytes read, expected ["..tostring(#line).."]")
end
end
local function apply_client(N, client, parser, requests)
for i=1,N do
for name, data in pairs(requests) do
client(parser, data)
assert(parser:method() == "GET", "Method is GET")
end
end
end
local function apply_client_memtest(name, client)
local start_mem, end_mem
local parser, reqs = init_parser()
full_gc()
start_mem = (collectgarbage"count" * 1024)
--print(name, 'start memory size: ', start_mem)
if disable_gc then collectgarbage"stop" end
apply_client(1, client, parser, requests)
end_mem = (collectgarbage"count" * 1024)
--print(name, 'end memory size: ', end_mem)
print(name, 'total memory used: ', (end_mem - start_mem))
print()
-- validate parsed request data.
local idx = 0
for name, data in pairs(requests) do
idx = idx + 1
local got = reqs[idx]
local expect = expects[name]
assert_deeply(got, expect, name)
end
reqs = nil
parser = nil
collectgarbage"restart"
full_gc()
end
local function apply_client_speedtest(name, client)
local start_mem, end_mem
local parser = init_null_parser()
full_gc()
start_mem = (collectgarbage"count" * 1024)
--print(name, 'start memory size: ', start_mem)
if disable_gc then collectgarbage"stop" end
bench(name, 10000, apply_client, client, parser, requests)
end_mem = (collectgarbage"count" * 1024)
--print(name, 'end memory size: ', end_mem)
print(name, 'total memory used: ', (end_mem - start_mem))
print()
parser = nil
collectgarbage"restart"
full_gc()
end
local function per_parser_overhead(N)
local start_mem, end_mem
local parsers = {}
-- pre-grow table
for i=1,N do
parsers[i] = true -- add place-holder values.
end
full_gc()
start_mem = (collectgarbage"count" * 1024)
--print('overhead: start memory size: ', start_mem)
for i=1,N do
parsers[i] = init_null_parser()
end
full_gc()
end_mem = (collectgarbage"count" * 1024)
--print('overhead: end memory size: ', end_mem)
print('overhead: total memory used: ', (end_mem - start_mem) / N, ' bytes per parser')
parsers = nil
full_gc()
end
local clients = {
good = good_client,
bad = bad_client,
}
print('memory test')
for name,client in pairs(clients) do
apply_client_memtest(name, client)
end
print('speed test')
for name,client in pairs(clients) do
apply_client_speedtest(name, client)
end
print('overhead test')
per_parser_overhead(1000)