Skip to content

Commit 42dd53e

Browse files
authored
Merge pull request #120 from EmperorYP7/benchmark-model
test: Benchmarks for `Model` and `RoleManager`
2 parents 0fdacfa + 0da8958 commit 42dd53e

File tree

5 files changed

+277
-4
lines changed

5 files changed

+277
-4
lines changed

tests/benchmarks/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
add_executable(casbin_benchmark
1616
config_path.h
1717
main.cpp
18+
model_b.cpp
1819
enforcer_cached_b.cpp
1920
management_api_b.cpp
21+
role_manager_b.cpp
2022
)
2123

2224
target_include_directories(casbin_benchmark PUBLIC ${CMAKE_SOURCE_DIR})

tests/benchmarks/enforcer_cached_b.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ static void BenchmarkCachedRBACModelLarge(benchmark::State& state) {
9393
p_policies[i] = {"group", std::to_string(i), "data", std::to_string(i / 10), "read"};
9494
e.AddPolicies(p_policies);
9595

96-
// 100000 users.
97-
std::vector<std::vector<std::string>> g_policies(100000);
98-
for (int i = 0; i < 100000; ++i) {
96+
// 100000 users.
97+
std::vector<std::vector<std::string>> g_policies(100000);
98+
for (int i = 0; i < 100000; ++i) {
9999
g_policies[i] = {"user" + std::to_string(i), "group", std::to_string(i / 10)};
100100
}
101101
e.AddGroupingPolicies(g_policies);

tests/benchmarks/management_api_b.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static inline int GetRandom1000()
3939

4040
static inline int GetRandom10000()
4141
{
42-
return dist_10000(generator);
42+
return dist_10000(generator);
4343
}
4444

4545
static void BenchmarkVectorOperations(benchmark::State& state) {

tests/benchmarks/model_b.cpp

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* This is a test file for benchmarking the performance of casbin::Model
17+
*/
18+
19+
#include <benchmark/benchmark.h>
20+
#include <casbin/casbin.h>
21+
#include "config_path.h"
22+
23+
static const std::vector<std::vector<std::string>> s_policy = { {"alice", "data1", "read"}, {"bob", "data2", "write"} };
24+
25+
static bool rawEnforce(const std::string& sub, const std::string& obj, const std::string& act) {
26+
for(const auto& rule : s_policy) {
27+
if(rule[0] == sub && rule[1] == obj && rule[2] == act)
28+
return true;
29+
}
30+
return false;
31+
}
32+
33+
static void BenchmarkRaw(benchmark::State& state) {
34+
for(auto _ : state)
35+
rawEnforce("alice", "data1", "read");
36+
}
37+
38+
BENCHMARK(BenchmarkRaw);
39+
40+
static void BenchmarkBasicModel(benchmark::State& state) {
41+
casbin::Enforcer e(basic_model_path, basic_policy_path);
42+
casbin::DataList params = {"alice", "data1", "read"};
43+
44+
for(auto _ : state)
45+
e.Enforce(params);
46+
}
47+
48+
BENCHMARK(BenchmarkBasicModel);
49+
50+
static void BenchmarkRBACModel(benchmark::State& state) {
51+
casbin::Enforcer e(rbac_model_path, rbac_policy_path);
52+
53+
casbin::DataList params = {"alice", "data2", "read"};
54+
55+
for (auto _ : state)
56+
e.Enforce(params);
57+
}
58+
59+
BENCHMARK(BenchmarkRBACModel);
60+
61+
static void BenchmarkRBACModelSmall(benchmark::State& state) {
62+
casbin::Enforcer e(rbac_model_path);
63+
64+
// 100 roles, 10 resources.
65+
for(int i = 0; i < 100; ++i)
66+
e.AddPolicy({ "group" + std::to_string(i), "data" + std::to_string(i/10), "read" });
67+
68+
// 1000 users.
69+
for(int i = 0; i < 1000; ++i)
70+
e.AddGroupingPolicy({ "user" + std::to_string(i), "group" + std::to_string(i / 10) });
71+
72+
casbin::DataList params = {"user501", "data9", "read"};
73+
for (auto _ : state)
74+
e.Enforce(params);
75+
}
76+
77+
BENCHMARK(BenchmarkRBACModelSmall);
78+
79+
static void BenchmarkRBACModelMedium(benchmark::State& state) {
80+
casbin::Enforcer e(rbac_model_path);
81+
82+
// 1000 roles, 100 resources.
83+
for (int i = 0; i < 1000; ++i)
84+
e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"});
85+
86+
// 10000 users.
87+
for (int i = 0; i < 10000; ++i)
88+
e.AddGroupingPolicy({"user" + std::to_string(i), "group" + std::to_string(i / 10)});
89+
90+
casbin::DataList params = {"user5001", "data99", "read"};
91+
for (auto _ : state)
92+
e.Enforce(params);
93+
}
94+
95+
BENCHMARK(BenchmarkRBACModelMedium);
96+
97+
static void BenchmarkRBACModelLarge(benchmark::State& state) {
98+
casbin::Enforcer e(rbac_model_path);
99+
100+
// 10000 roles, 1000 resources.
101+
for(int i = 0; i < 10000; ++i)
102+
e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"});
103+
104+
// 100000 users.
105+
for(int i = 0; i < 100000; i++)
106+
e.AddGroupingPolicy({"user" + std::to_string(i), "group" + std::to_string(i / 10)});
107+
108+
casbin::DataList params = {"user50001", "data999", "read"};
109+
110+
for(auto _ : state)
111+
e.Enforce(params);
112+
}
113+
114+
// BENCHMARK(BenchmarkRBACModelLarge);
115+
116+
static void BenchmarkRBACModelWithResourceRoles(benchmark::State& state) {
117+
casbin::Enforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path);
118+
casbin::DataList params = {"alice", "data1", "read"};
119+
for (auto _ : state)
120+
e.Enforce(params);
121+
}
122+
123+
// BENCHMARK(BenchmarkRBACModelWithResourceRoles);
124+
125+
static void BenchmarkRBACModelWithDomains(benchmark::State& state) {
126+
casbin::Enforcer e(rbac_with_domains_model_path, rbac_with_domains_policy_path);
127+
casbin::DataList params = {"alice", "domain1", "data1", "read"};
128+
129+
for(auto _ : state)
130+
e.Enforce(params);
131+
}
132+
133+
BENCHMARK(BenchmarkRBACModelWithDomains);
134+
135+
// ------ TODO ------
136+
// static void BenchmarkABACModel(benchmark::State& state) {
137+
// casbin::Enforcer e("examples/abac_model.conf")
138+
// data1 := newTestResource("data1", "alice")
139+
140+
141+
// for(auto _ : state) {
142+
// _, _ = e.Enforce("alice", data1, "read")
143+
// }
144+
// }
145+
146+
static void BenchmarkKeyMatchModel(benchmark::State& state) {
147+
casbin::Enforcer e(keymatch_model_path, keymatch_policy_path);
148+
casbin::DataList params = {"alice", "/alice_data/resource1", "GET"};
149+
150+
for (auto _ : state)
151+
e.Enforce(params);
152+
}
153+
154+
BENCHMARK(BenchmarkKeyMatchModel);
155+
156+
static void BenchmarkRBACModelWithDeny(benchmark::State& state) {
157+
casbin::Enforcer e(rbac_with_deny_model_path, rbac_with_deny_policy_path);
158+
casbin::DataList params = {"alice", "data1", "read"};
159+
160+
for(auto _ : state)
161+
e.Enforce(params);
162+
}
163+
164+
BENCHMARK(BenchmarkRBACModelWithDeny);
165+
166+
static void BenchmarkPriorityModel(benchmark::State& state) {
167+
casbin::Enforcer e(priority_model_path, priority_policy_path);
168+
casbin::DataList params = {"alice", "data1", "read"};
169+
170+
for(auto _ : state)
171+
e.Enforce(params);
172+
}
173+
174+
BENCHMARK(BenchmarkPriorityModel);
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* This is a test file for benchmarking the performance of casbin::RoleManager
17+
*/
18+
19+
#include <benchmark/benchmark.h>
20+
#include <casbin/casbin.h>
21+
#include "config_path.h"
22+
23+
static std::vector<std::string> params(3);
24+
static std::vector<std::string> g_params(2);
25+
26+
static void BenchmarkRoleManagerSmall(benchmark::State& state) {
27+
casbin::Enforcer e(rbac_model_path);
28+
// Do not rebuild the role inheritance relations for every AddGroupingPolicy() call.
29+
e.EnableAutoBuildRoleLinks(false);
30+
31+
// 100 roles, 10 resources.
32+
for (int i = 0; i < 100; ++i)
33+
params = {"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params);
34+
35+
// 1000 users.
36+
for (int i = 0; i < 1000; ++i)
37+
g_params = {"user" + std::to_string(i), "group" + std::to_string(i / 10)}, e.AddGroupingPolicy(g_params);
38+
39+
auto rm = e.GetRoleManager();
40+
41+
for(auto _ : state) {
42+
for(int j = 0; j < 100; ++j)
43+
rm->HasLink("user501", "group" + std::to_string(j));
44+
}
45+
}
46+
47+
BENCHMARK(BenchmarkRoleManagerSmall);
48+
49+
static void BenchmarkRoleManagerMedium(benchmark::State& state) {
50+
casbin::Enforcer e(rbac_model_path);
51+
// Do not rebuild the role inheritance relations for every AddGroupingPolicy() call.
52+
e.EnableAutoBuildRoleLinks(false);
53+
54+
// 1000 roles, 100 resources.
55+
56+
for (int i = 0; i < 1000; ++i)
57+
params = {"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params);
58+
59+
// 10000 users.
60+
61+
for (int i = 0; i < 10000; ++i)
62+
g_params = {"user" + std::to_string(i), "group" + std::to_string(i / 10)}, e.AddGroupingPolicy(g_params);
63+
64+
e.BuildRoleLinks();
65+
66+
auto rm = e.GetRoleManager();
67+
68+
for(auto _ : state) {
69+
for(int j = 0; j < 1000; ++j)
70+
rm->HasLink("user501", "group" + std::to_string(j));
71+
}
72+
}
73+
74+
BENCHMARK(BenchmarkRoleManagerMedium);
75+
76+
static void BenchmarkRoleManagerLarge(benchmark::State& state) {
77+
casbin::Enforcer e(rbac_model_path);
78+
79+
// 10000 roles, 1000 resources.
80+
81+
for (int i = 0; i < 10000; ++i)
82+
params = {"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params);
83+
84+
// 100000 users.
85+
86+
for (int i = 0; i < 100000; ++i)
87+
g_params = {"user" + std::to_string(i), "group" + std::to_string(i / 10)}, e.AddGroupingPolicy(g_params);
88+
89+
auto rm = e.GetRoleManager();
90+
91+
for(auto _ : state) {
92+
for(int j = 0; j < 10000; ++j)
93+
rm->HasLink("user501", "group" + std::to_string(j));
94+
}
95+
}
96+
97+
// BENCHMARK(BenchmarkRoleManagerLarge);

0 commit comments

Comments
 (0)