-
Notifications
You must be signed in to change notification settings - Fork 3
/
benchmark.luau
89 lines (77 loc) · 2.09 KB
/
benchmark.luau
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
-- Implementation of this benchmark: https://hono.dev/docs/concepts/benchmarks
local Router = require("@lynx/router")
local router = Router.new()
router:add("GET", "/user", function() end)
router:add("GET", "/user/comments", function() end)
router:add("GET", "/user/avatar", function() end)
router:add("GET", "/user/lookup/username/:username", function() end)
router:add("GET", "/user/lookup/email/:address", function() end)
router:add("GET", "/event/:id", function() end)
router:add("GET", "/event/:id/comments", function() end)
router:add("POST", "/event/:id/comment", function() end)
router:add("GET", "/map/:location/events", function() end)
router:add("GET", "/status", function() end)
router:add("GET", "/very/deeply/nested/route/hello/there", function() end)
router:add("GET", "/static/*", function() end)
local routes = {
{
name = "short static",
method = "GET",
path = "/user",
},
{
name = "static with same radix",
method = "GET",
path = "/user/comments",
},
{
name = "dynamic route",
method = "GET",
path = "/user/lookup/username/hey",
},
{
name = "mixed static dynamic",
method = "GET",
path = "/event/abcd1234/comments",
},
{
name = "post",
method = "POST",
path = "/event/abcd1234/comment",
},
{
name = "long static",
method = "GET",
path = "/very/deeply/nested/route/hello/there",
},
{
name = "wildcard",
method = "GET",
path = "/static/index.html",
},
}
local ITERATIONS = 1000000
for _, route in routes do
local total = 0
for _ = 1, ITERATIONS do
local start = os.clock()
Router.match(router, route.method, route.path)
local finish = os.clock()
total += finish - start
end
local average = total / ITERATIONS
local averageNs = average * 1e9
print(route.name, math.ceil(averageNs), "ns")
end
local totalTogether = 0
for _ = 1, ITERATIONS do
for _, route in routes do
local start = os.clock()
Router.match(router, route.method, route.path)
local finish = os.clock()
totalTogether += finish - start
end
end
local average = totalTogether / ITERATIONS
local averageNs = average * 1e9
print("all together", math.ceil(averageNs), "ns")