-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbenchmark.cr
45 lines (37 loc) · 1.25 KB
/
benchmark.cr
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
require "./crouter"
require "benchmark"
{% for i in 5..8 %}
{% route_count = 2 ** i %}
class MyRouter{{route_count}} < Crouter::Router
\{% for i in 0..{{route_count}} %}
get "/route_{{i}}(/:param1(/:param2))" do
param1, param2 = { params["param1"]?, params["param2"]? }.map { |param| param || "nothing" }
context.response << "hi from route_{{i}}, you passed #{param1} and #{param2}"
end
\{% end %}
end
{% end %}
raw_handler = ->(context : HTTP::Server::Context) { context.response << "raw throughput" }
def route_gen(max_index)
"/route_#{rand(0..max_index)}#{"/foo#{"/bar" if rand < 5}" if rand < 5}"
end
def send_request(router, route)
request = HTTP::Request.new("GET", route)
response = HTTP::Server::Response.new(IO::Memory.new)
context = HTTP::Server::Context.new request, response
router.call(context)
response
end
Benchmark.ips do |bm|
puts "requests per second"
bm.report("without router (raw server throughput)") do
send_request raw_handler, route_gen(10_000)
end
{% for i in 5..8 %}
{% route_count = 2 ** i %}
%router = MyRouter{{route_count.id}}.new
bm.report("through router with {{route_count}} routes") do
send_request %router, route_gen({{route_count}})
end
{% end %}
end