-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
bench.lua
271 lines (226 loc) · 7.54 KB
/
bench.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
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
local socket = require("socket")
local Enforcer = require("src.main.Enforcer")
local path = os.getenv("PWD") or io.popen("cd"):read()
local function rawEnforce(sub, obj, act)
local policy = {{"alice", "data1", "read"}, {"bob", "data2", "write"}}
for _, rule in pairs(policy) do
if rule[1] == sub and rule[2] == obj and rule[3] == act then
return true
end
end
return false
end
local function benchmarkRaw()
local x = socket.gettime()*1000
for i = 1, 10000 do
rawEnforce("alice", "data1", "read")
end
x = socket.gettime()*1000 - x
return x, 10000
end
local function benchmarkBasicModel()
local e = Enforcer:new(path .. "/examples/basic_model.conf", path .. "/examples/basic_policy.csv")
e:enableLog(false)
local x = socket.gettime()*1000
for i = 1, 10000 do
_ = e:enforce("alice", "data1", "read")
end
x = socket.gettime()*1000 - x
return x, 10000
end
local function benchmarkRBACModel()
local e = Enforcer:new(path .. "/examples/rbac_model.conf", path .. "/examples/rbac_policy.csv")
e:enableLog(false)
local x = socket.gettime()*1000
for i = 1, 10000 do
_ = e:enforce("alice", "data2", "read")
end
x = socket.gettime()*1000 - x
return x, 10000
end
local function benchmarkRBACModelSmall()
local e = Enforcer:new(path .. "/examples/rbac_model.conf", path .. "/examples/empty_policy.csv")
e:enableLog(false)
local pPolicies = {}
-- 100 roles, 10 resources.
for i = 1, 100 do
table.insert(pPolicies, {"group"..tostring(i), "data"..tostring(i%10), "read"})
end
e:AddPolicies(pPolicies)
-- 1000 users.
local gPolicies = {}
for i = 1, 1000 do
table.insert(gPolicies, {"user"..tostring(i), "group"..tostring(i%10)})
end
e:AddGroupingPolicies(gPolicies)
local x = socket.gettime()*1000
for i = 1, 1000 do
_ = e:enforce("user501", "data9", "read")
end
x = socket.gettime()*1000 - x
return x, 1000
end
local function benchmarkRBACModelMedium()
local e = Enforcer:new(path .. "/examples/rbac_model.conf", path .. "/examples/empty_policy.csv")
e:enableLog(false)
local pPolicies = {}
-- 1000 roles, 100 resources.
for i = 1, 1000 do
table.insert(pPolicies, {"group"..tostring(i), "data"..tostring(i%100), "read"})
end
e:AddPolicies(pPolicies)
local gPolicies = {}
-- 10000 users.
for i = 1, 10000 do
table.insert(gPolicies, {"user"..tostring(i), "group"..tostring(i%10)})
end
e:AddGroupingPolicies(gPolicies)
local x = socket.gettime()*1000
for i = 1, 100 do
_ = e:enforce("user5001", "data99", "read")
end
x = socket.gettime()*1000 - x
return x, 100
end
local function benchmarkRBACModelLarge()
local e = Enforcer:new(path .. "/examples/rbac_model.conf", path .. "/examples/empty_policy.csv")
e:enableLog(false)
local pPolicies = {}
-- 10000 roles, 1000 resources.
for i = 1, 10000 do
table.insert(pPolicies, {"group"..tostring(i), "data"..tostring(i%1000), "read"})
end
e:AddPolicies(pPolicies)
local gPolicies = {}
-- 100000 users.
for i = 1, 100000 do
table.insert(gPolicies, {"user"..tostring(i), "group"..tostring(i%10)})
end
e:AddGroupingPolicies(gPolicies)
local x = socket.gettime()*1000
for i = 1, 10 do
_ = e:enforce("user50001", "data999", "read")
end
x = socket.gettime()*1000 - x
return x, 10
end
local function benchmarkRBACModelWithResourceRoles()
local e = Enforcer:new(path .. "/examples/rbac_with_resource_roles_model.conf", path .. "/examples/rbac_with_resource_roles_policy.csv")
e:enableLog(false)
local x = socket.gettime()*1000
for i = 1, 1000 do
_ = e:enforce("alice", "data1", "read")
end
x = socket.gettime()*1000 - x
return x, 1000
end
local function benchmarkRBACModelWithDomains()
local e = Enforcer:new(path .. "/examples/rbac_with_domains_model.conf", path .. "/examples/rbac_with_domains_policy.csv")
e:enableLog(false)
local x = socket.gettime()*1000
for i = 1, 1000 do
_ = e:enforce("alice", "domain1", "data1", "read")
end
x = socket.gettime()*1000 - x
return x, 1000
end
local function benchmarkABACModel()
local e = Enforcer:new(path .. "/examples/abac_model.conf", path .. "/examples/empty_policy.csv")
e:enableLog(false)
local data1 = {["Name"] = "data1", ["Owner"] = "alice"}
local x = socket.gettime()*1000
for i = 1, 1000 do
_ = e:enforce("alice", data1, "read")
end
x = socket.gettime()*1000 - x
return x, 1000
end
local function benchmarkKeyMatchModel()
local e = Enforcer:new(path .. "/examples/keymatch_model.conf", path .. "/examples/keymatch_policy.csv")
e:enableLog(false)
local x = socket.gettime()*1000
for i = 1, 1000 do
_ = e:enforce("alice", "/alice_data/resource1", "GET")
end
x = socket.gettime()*1000 - x
return x, 1000
end
local function benchmarkRBACModelWithDeny()
local e = Enforcer:new(path .. "/examples/rbac_with_deny_model.conf", path .. "/examples/rbac_with_deny_policy.csv")
e:enableLog(false)
local x = socket.gettime()*1000
for i = 1, 1000 do
_ = e:enforce("alice", "data1", "read")
end
x = socket.gettime()*1000 - x
return x, 1000
end
local function benchmarkPriorityModel()
local e = Enforcer:new(path .. "/examples/priority_model.conf", path .. "/examples/priority_policy.csv")
e:enableLog(false)
local x = socket.gettime()*1000
for i = 1, 1000 do
_ = e:enforce("alice", "data1", "read")
end
x = socket.gettime()*1000 - x
return x, 1000
end
local function runBenchmark()
local t, o
local time = {}
local ops = {}
local benchName = {}
t, o = benchmarkRaw()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "Raw")
t, o = benchmarkBasicModel()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "Basic Model")
t, o = benchmarkRBACModel()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "RBAC Model")
t, o = benchmarkRBACModelSmall()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "RBAC Model Small")
t, o = benchmarkRBACModelMedium()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "RBAC Model Medium")
t, o = benchmarkRBACModelLarge()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "RBAC Model Large")
t, o = benchmarkRBACModelWithResourceRoles()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "RBAC Model with Resources")
t, o = benchmarkRBACModelWithDomains()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "RBAC Model with Domains")
t, o = benchmarkABACModel()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "ABAC Model")
t, o = benchmarkKeyMatchModel()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "KeyMatch Model")
t, o = benchmarkRBACModelWithDeny()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "RBAC Model with Deny")
t, o = benchmarkPriorityModel()
table.insert(time, t)
table.insert(ops, o)
table.insert(benchName, "Priority Model")
print("Benchmark:")
for k, v in pairs(benchName) do
print(v .. ":", "\tTotal ops = " .. tostring(ops[k]), "\tTime per op = " .. string.format("%.4f ms", time[k]/ops[k]))
end
end
runBenchmark()