/
bench.rb
453 lines (398 loc) · 11.1 KB
/
bench.rb
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# -*- coding: utf-8 -*-
# frozen_string_literal: true
##
## Usage:
## $ gem install bundler
## $ bundler install
## $ ruby bench.rb -n 1000000
## $ ruby bench.rb -n 1000000 --rack=off --sinatra=off --multiplexer=off # --jet=off --keight=off --hanami=off --httprouter=off
##
$LOAD_PATH << File.absolute_path("../../lib", __FILE__)
require 'benchmarker'
Benchmarker.parse_cmdopts()
class GetFlag
def initialize(defaults={})
@flag_all = to_bool($opt_all)
@defaults = defaults
@output = String.new
end
attr_reader :output
def call(key, lib, &b)
default = @defaults[key]
default != nil or
raise KeyError.new(key.inspect)
opt_val = eval "$opt_#{key}"
arr = [to_bool(opt_val), @flag_all, default]
flag = arr.find {|x| x != nil }
#
begin
require lib
rescue LoadError
flag = nil
end
#
version = flag == false ? "(skipped)" \
: flag == nil ? "(not installed)" \
: yield
gem = [lib].flatten()[0].gsub('/', '-')
@output << "** %-20s : %s\n" % [gem, version]
#
return flag
end
def to_bool(val, default=nil)
case val
when 'on' , 'true' , 'yes', '1' ; return true
when 'off', 'false', 'no' , '0' ; return false
else ; return default
end
end
end
get_flag = GetFlag.new(
:rack => true,
:rocket => false,
:jet => true,
:keight => true,
:hanami => true,
:httprouter => true,
:multiplexer => true,
:sinatra => true,
)
flag_rack = get_flag.(:rack , "rack" ) { Rack.release }
flag_rocket = get_flag.(:rocket , "rocketrouter" ) { RocketRouter::VERSION }
flag_jet = get_flag.(:jet , "rack/jet_router" ) { Rack::JetRouter::RELEASE }
flag_keight = get_flag.(:keight , "keight" ) { K8::RELEASE }
flag_hanami = get_flag.(:hanami , "hanami/router" ) { Hanami::Router::VERSION }
flag_httprouter = get_flag.(:httprouter , "http_router" ) { require "http_router/version"; HttpRouter::VERSION }
flag_multiplexer = get_flag.(:multiplexer, "rack/multiplexer") { Rack::Multiplexer::VERSION }
flag_sinatra = get_flag.(:sinatra , "sinatra/base" ) { Sinatra::VERSION }
ENTRIES = ('a'..'z').map.with_index {|x, i| "%s%02d" % [x*3, i+1] }
target_urlpaths = [
"/api/aaa01",
"/api/aaa01/123",
# "/api/aaa01/123/comments/7",
"/api/zzz26",
"/api/zzz26/456",
# "/api/zzz26/456/comments/7",
]
def generate_apps(env_key, key_class)
if key_class == String ; id, c_id = 'id', 'comment_id'
elsif key_class == Symbol ; id, c_id = :id, :comment_id
else ; raise "** internal error: key_class=#{key_class.inspect}"
end
index_app = proc {|env|
[200, {"Content-Type"=>"text/html"}, ["<h1>hello</h1>"]]
}
show_app = proc {|env|
d = env[env_key]
[200, {"Content-Type"=>"text/html"}, ["<h1>id=#{d[id]}</h1>"]]
}
comment_app = proc {|env|
d = env[env_key]
[200, {"Content-Type"=>"text/html"}, ["<h1>id=#{d[id]}, comment_id=#{d[c_id]}</h1>"]]
}
return index_app, show_app, comment_app
end
def let(*args)
return (yield *args)
end
rack_app = flag_rack && let() {
#proc do |env|
# [200, {"Content-Type"=>"text/html"}, ["<h1>hello</h1>"]]
#end
proc do |env; req, resp|
req = Rack::Request.new(env)
resp = Rack::Response.new
#[resp.status, resp.headers, ["<h1>hello</h1>"]]
#[200, {"Content-Type"=>"text/html"}, ["<h1>hello</h1>"]]
resp.status = 200
resp.headers["Content-Type"] = "text/html"
resp.write("<h1>hello</h1>")
resp.finish()
end
}
rocket_app = flag_rocket && let() {
index_app, show_app, comment_app = generate_apps('rack.urlpath_params', String)
mapping = {
"/api" => ENTRIES.each_with_object({}) {|x, map|
map.update({
"/#{x}" => {
"" => index_app,
"/{id}" => show_app,
"/{id}/comments" => {
"/{comment_id}" => comment_app,
},
},
})
},
}
opts = {
}
RocketRouter.new(mapping, **opts)
}
jet_app = flag_jet && let() {
index_app, show_app, comment_app = generate_apps('rack.urlpath_params', String)
mapping = {
"/api" => ENTRIES.each_with_object({}) {|x, map|
map.update({
"/#{x}" => {
"" => index_app,
"/:id" => show_app,
"/:id/comments" => {
"/:comment_id" => comment_app,
},
},
})
},
}
opts = {
cache_size: 0,
_enable_range: true,
#prefix_minlength_target: /\A\/api\/\w/,
}
Rack::JetRouter.new(mapping, **opts)
}
keight_app = flag_keight && let() {
class K8HelloAction < K8::Action
mapping '', :GET=>:do_index
mapping '/{id}', :GET=>:do_show
def do_index()
"<h1>hello</h1>"
end
def do_show(id)
"<h1>id=#{id.inspect}</h1>"
end
end
class K8CommentAction < K8::Action
mapping '/{comment_id}', :GET=>:do_show
def do_show(id, comment_id)
"<h1>id=#{id}, comment_id=#{comment_id}</h1>"
end
end
mapping = [
["/api", ENTRIES.each_with_object([]) {|x, arr|
arr << ["/#{x}" , K8HelloAction]
arr << ["/#{x}/{id}/comments", K8CommentAction]
}
],
]
opts = {
#urlpath_cache_size: 0,
}
K8::RackApplication.new(mapping, **opts)
}
hanami_app = flag_hanami && let() {
## ref: https://github.com/hanami/router
index_app, show_app, comment_app = generate_apps('router.params', Symbol)
Hanami::Router.new do
scope "api" do
ENTRIES.each do |x|
get "/#{x}" , to: index_app
get "/#{x}/:id", to: show_app
get "/#{x}/:id/comments/:comment_id", to: comment_app
end
end
end
}
httprouter_app = flag_httprouter && let() {
require 'uri'
require 'cgi'
class <<URI
#alias unescape decode_www_form
def unescape(x)
CGI.unescape(x)
end
end
#
index_app, show_app, comment_app = generate_apps('router.params', Symbol)
HttpRouter.new.tap do |r|
ENTRIES.each do |x|
r.add("/api/#{x}" ).to(index_app)
r.add("/api/#{x}/:id").to(show_app)
r.add("/api/#{x}/:id/comments/:comment_id").to(comment_app)
end
end
}
multiplexer_app = flag_multiplexer && let() {
index_app, show_app, comment_app = generate_apps('rack.request.query_hash', String)
Rack::Multiplexer.new().tap do |app|
ENTRIES.each do |x|
app.get "/api/#{x}" , index_app
app.get "/api/#{x}/:id" , show_app
app.get "/api/#{x}/:id/comments/:comment_id" , comment_app
end
end
}
sinatra_app = flag_sinatra && let() {
class SinaApp < Sinatra::Base
## run benchmarks without middlewares
set :sessions , false
set :logging , false
set :protection , false
set :x_cascade , false
#
ENTRIES.each do |x|
get "/api/#{x}" do
"<h1>hello</h1>"
end
get "/api/#{x}/:id" do
"<h1>id=#{params['id']}</h1>"
end
get "/api/#{x}/:id/comments/:comment_id" do
"<h1>id=#{params['id']}, comment_id=#{params['comment_id']}</h1>"
end
end
end
SinaApp.new
}
begin
Rack::MockRequest
rescue
require 'rack'
end
$environ = Rack::MockRequest.env_for("http://localhost/", method: 'GET')
$environ.freeze
def newenv(path)
env = $environ.dup
env['PATH_INFO'] = path
env
end
N = Benchmarker::OPTIONS.delete(:loop) || 1000_000
title = "Router library benchmark"
width = target_urlpaths.collect(&:length).max()
Benchmarker.scope(title, width: width + 17, loop: 1, iter: 1, extra: 0, sleep: 0) do
puts get_flag.output()
puts "** N=#{N}"
puts ""
### empty task
task nil do
i = 0; n = N
while (i += 1) <= n
#newenv("/api")
end
end
### Rack
#flag_rack and target_urlpaths.each do |path|
# rack_app.call(newenv(path)) # warm up
# task "(Rack plain app) #{path}" do # no routing
# env = newenv(path)
# i = 0; n = N
# while (i += 1) <= n
# result = rack_app.call(env)
# end
# result
# end
#end
flag_rack and target_urlpaths.each do |path|
rack_app.call(newenv(path)) # warm up
task "(Rack::Req+Res) #{path}" do # no routing
env = newenv(path)
i = 0; n = N
while (i += 1) <= n
result = rack_app.call(env)
end
result
end
end
### RocketRouter
flag_rocket and target_urlpaths.each do |path|
rocket_app.call(newenv(path)) # warm up
task "(RocketRouter) #{path}" do
env = newenv(path)
i = 0; n = N
while (i += 1) <= n
result = rocket_app.call(env)
#result = rocket_app.find(path)
end
result
end
end
### Rack::JetRouter
flag_jet and target_urlpaths.each do |path|
jet_app.call(newenv(path)) # warm up
task "(JetRouter) #{path}" do
env = newenv(path)
i = 0; n = N
while (i += 1) <= n
result = jet_app.call(env)
#result = jet_app.find(path)
end
result
end
end
### Keight
flag_keight and target_urlpaths.each do |path|
keight_app.call(newenv(path)) # warm up
task "(Keight) #{path}" do
env = newenv(path)
i = 0; n = N
while (i += 1) <= n
result = keight_app.call(env)
end
result
end
end
### Hanami::Router
flag_hanami and target_urlpaths.each do |path|
hanami_app.call(newenv(path)) # warm up
task "(Hanami::Router) #{path}" do
env = newenv(path)
i = 0; n = N
while (i += 1) <= n
result = hanami_app.call(env)
#result = hanami_app.route(path)
end
result
end
end
### HttpRouter
flag_httprouter and target_urlpaths.each do |path|
httprouter_app.call(newenv(path)) # warm up
task "(HttpRouter) #{path}" do
env = newenv(path)
i = 0; n = N
while (i += 1) <= n
result = httprouter_app.call(env)
#result = httprouter_app.route(path)
end
result
end
end
### Rack::Multiplexer
flag_multiplexer and target_urlpaths.each do |path|
multiplexer_app.call(newenv(path)) # warm up
task "(Multiplexer) #{path}" do
env = newenv(path)
i = 0; n = N
while (i += 1) <= n
result = multiplexer_app.call(env)
end
result
end
end
### Sinatra
flag_sinatra and target_urlpaths.each do |path|
sinatra_app.call(newenv(path)) # warm up
task "(Sinatra) #{path}" do
env = newenv(path)
i = 0; n = N
while (i += 1) <= n
result = sinatra_app.call(env)
end
result
end
end
## validation
validate do |result| # or: validate do |result, task_name, tag|
tuple = result
assert tuple[0] == 200, "Expected 200 but got #{tuple[0]}"
body = tuple[2].each {|x| break x }
expected_bodies = [
"<h1>hello</h1>",
"<h1>id=123</h1>",
"<h1>id=456</h1>",
"<h1>id=123, comment_id=7</h1>",
"<h1>id=456, comment_id=7</h1>",
]
assert expected_bodies.include?(body), "#{body.inspect}: Unpexpected body."
end
end