-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.lua
150 lines (144 loc) · 3.89 KB
/
benchmark.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
local v1 = require('resty.rax')
local v2 = require("./lib/resty/router")
local function p(s)
print(require("resty.inspect")(s))
end
local names = {
'actions',
'ad',
'admin',
'alioss_payload',
'badge_number',
'branch',
'branch_login',
'classview',
'env',
'feeplan',
'forum',
'friends',
'goddess',
'goddess_comment',
'goddess_comment_comment',
'h1',
'hello',
'hik',
'home_data',
'login_h5',
'logs',
'message',
'news',
'oauth',
'orders',
'parent',
'parent_student_relation',
'permission_view',
'poll',
'poll_log',
'post',
'post_comment',
'prizebook',
'school',
'session',
'settings',
'shyk',
'stage',
'stage_apply',
'stat',
'student',
'subscribe',
'system_message',
'teacher',
'test',
'thread',
'update_profile',
'usr',
'usr_login',
'volplan',
'volreg',
'wx',
'wx_login',
'wx_phone',
'wxqy_directives',
'wxqy_events',
'wxqy_validate_domain',
'youth_branch_login',
'youth_fee',
'youth_member',
'youth_member_delete_apply',
}
local routes1 = {}
local routes2 = {}
local args = {}
for index, name in ipairs(names) do
local url = string.format('/%s/records', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/json', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/create', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/detail/#id/age/:age', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/update/#id/time/:time', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/delete/#id/age/:age', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/download', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/merge', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/choices', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/filter', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
local url = string.format('/%s/get', name)
args[#args + 1] = { url, url, { 'get', 'post' } }
end
for index, value in ipairs(args) do
routes1[#routes1 + 1] = { path = value[1], handler = value[2], methods = value[3] }
end
local t1 = v1.new(routes1)
local t2 = v2:create(args)
local function timeit(func, n)
n = n or 1
func()
local start = os.clock() -- 获取初始时间
for i = 1, n do
func()
end
local finish = os.clock() -- 获取结束时间
local executionTime = finish - start -- 计算运行时间
print(string.format("Execution time: %.4f seconds", executionTime))
end
local function f(s)
end
local function v1t()
for index, name in ipairs(names) do
local u1 = string.format('/%s/detail/1/age/18', name)
local res, params = t1:match(u1, 'GET')
assert(params.age == '18' and params.id == '1')
local u2 = string.format('/%s/update/1/time/18', name)
local res, params = t1:match(u2, 'GET')
assert(params.time == '18' and params.id == '1')
local u3 = string.format('/%s/delete/1/age/118', name)
local res, params = t1:match(u3, 'GET')
assert(params.age == '118' and params.id == '1')
end
end
local function v2t()
for index, name in ipairs(names) do
local u1 = string.format('/%s/detail/1/age/18', name)
local res, params = t2:match(u1, 'GET')
assert(params)
assert(params.age == '18' and params.id == 1)
local u2 = string.format('/%s/update/1/time/18', name)
local res, params = t2:match(u2, 'GET')
assert(params)
assert(params.time == '18' and params.id == 1)
local u3 = string.format('/%s/delete/1/age/118', name)
local res, params = t2:match(u3, 'GET')
assert(params)
assert(params.age == '118' and params.id == 1)
end
end
timeit(v1t, 20000)
timeit(v2t, 20000)