-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbenchmark
executable file
·77 lines (63 loc) · 2.95 KB
/
benchmark
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
#!/usr/bin/env crystal run --release
require "benchmark"
require "radix"
require "./src/oak"
class Benchmarker
getter route_library
getter route_checks
getter oak_router
getter radix_router
def initialize
@shared_routes = {
"/get/" => :root,
"/get/users/:id" => :users,
"/get/users/:id/books" => :users_books,
"/get/books/:id" => :books,
"/get/books/:id/chapters" => :book_chapters,
"/get/books/:id/authors" => :book_authors,
"/get/books/:id/pictures" => :book_pictures,
"/get/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z" => :alphabet,
"/get/var/:b/:c/:d/:e/:f/:g/:h/:i/:j/:k/:l/:m/:n/:o/:p/:q/:r/:s/:t/:u/:v/:w/:x/:y/:z" => :variable_alphabet,
"/get/foobarbizfoobarbizfoobarbizfoobarbizfoobarbizbat/:id" => :foobar_bat,
"/get/foobarbizfoobarbizfoobarbizfoobarbizfoobarbizbom/:id" => :foobar_bom,
"/post/*" => :catchall,
}
@oak_router = Oak::Tree(Symbol).new
@radix_router = Radix::Tree(Symbol).new
@shared_routes.each do |k, v|
radix_router.add k, v
oak_router.add k, v
end
end
def run_check(router, check, expected_result)
result = router.find(check)
if expected_result.nil?
raise "returned a result when it shouldn't've" unless result.found? == false
return
end
actual_result = result.payload?
if actual_result != expected_result
raise "#{router.class} #{actual_result.inspect} did not match #{expected_result}"
end
end
def compare(name : String, route : String, result : Symbol?)
puts route
Benchmark.ips do |x|
x.report("oak: #{name}") { run_check(oak_router, route, result) }
x.report("radix: #{name}") { run_check(radix_router, route, result) }
end
puts
puts
end
def compare_to_radix
compare "root", "/get/", :root
compare "deep", "/get/books/23/chapters", :book_chapters
compare "wrong", "/get/books/23/pages", nil
compare "many segments", "/get/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z", :alphabet
compare "many variables", "/get/var/2/3/4/5/6/7/8/9/0/1/2/3/4/5/6/7/8/9/0/1/2/3/4/5/6", :variable_alphabet
compare "long_segments", "/get/foobarbizfoobarbizfoobarbizfoobarbizfoobarbizbat/3", :foobar_bat
compare "catchall route", "/post/products/23/reviews/", :catchall
end
end
benchmarker = Benchmarker.new
benchmarker.compare_to_radix