Skip to content

Commit f00c5d5

Browse files
authored
Merge pull request #116 from EmperorYP7/benchmark
test: Benchmarks for `CachedEnforcer`
2 parents 617ba26 + 17cc825 commit f00c5d5

File tree

3 files changed

+204
-2
lines changed

3 files changed

+204
-2
lines changed

tests/benchmarks/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
add_executable(casbin_benchmark
16+
config_path.h
1617
main.cpp
1718
enforcer_cached_b.cpp
1819
)

tests/benchmarks/config_path.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 file contains the path to various config resources within casbin
17+
*/
18+
19+
#include <string>
20+
21+
static const std::string relative_path = "../../../";
22+
static const std::string basic_model_path = relative_path + "examples/basic_model.conf";
23+
static const std::string basic_policy_path = relative_path + "examples/basic_policy.csv";
24+
static const std::string rbac_model_path = relative_path + "examples/rbac_model.conf";
25+
static const std::string rbac_policy_path = relative_path + "examples/rbac_policy.csv";
26+
static const std::string rbac_with_resource_roles_model_path = relative_path + "examples/rbac_with_resource_roles_model.conf";
27+
static const std::string rbac_with_resource_roles_policy_path = relative_path + "examples/rbac_with_resource_roles_policy.csv";
28+
static const std::string rbac_with_domains_model_path = relative_path + "examples/rbac_with_domains_model.conf";
29+
static const std::string rbac_with_domains_policy_path = relative_path + "examples/rbac_with_domains_policy.csv";
30+
static const std::string keymatch_model_path = relative_path + "examples/keymatch_model.conf";
31+
static const std::string keymatch_policy_path = relative_path + "examples/keymatch_policy.csv";
32+
static const std::string rbac_with_deny_model_path = relative_path + "examples/rbac_with_deny_model.conf";
33+
static const std::string rbac_with_deny_policy_path = relative_path + "examples/rbac_with_deny_policy.csv";
34+
static const std::string priority_model_path = relative_path + "examples/priority_model.conf";
35+
static const std::string priority_policy_path = relative_path + "examples/priority_policy.csv";

tests/benchmarks/enforcer_cached_b.cpp

Lines changed: 168 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818

1919
#include <benchmark/benchmark.h>
2020
#include <casbin/casbin.h>
21+
#include "config_path.h"
2122

2223
static void BenchmarkCachedBasicModel(benchmark::State& state) {
23-
casbin::CachedEnforcer e("../../../examples/basic_model.conf", "../../../examples/basic_policy.csv");
24+
casbin::CachedEnforcer e(basic_model_path, basic_policy_path);
2425
std::vector<std::string> request = {"alice", "data1", "read"};
2526
for (auto _ : state)
2627
e.Enforce(request);
@@ -30,10 +31,175 @@ BENCHMARK(BenchmarkCachedBasicModel);
3031

3132

3233
static void BenchmarkCachedRBACModel(benchmark::State& state) {
33-
casbin::CachedEnforcer e("../../../examples/rbac_model.conf", "../../../examples/rbac_policy.csv");
34+
casbin::CachedEnforcer e(rbac_model_path, rbac_policy_path);
3435
std::vector<std::string> request = {"alice", "data2", "read"};
3536
for (auto _ : state)
3637
e.Enforce(request);
3738
}
3839

3940
BENCHMARK(BenchmarkCachedRBACModel);
41+
42+
// ---- TODO ----
43+
// static void BenchmarkCachedRaw(benchmark::State& state) {
44+
// for (auto _ : state)
45+
// rawEnforce("alice", "data1", "read")
46+
// }
47+
48+
// BENCHMARK(BenchmarkCachedRaw);
49+
50+
static void BenchmarkCachedRBACModelSmall(benchmark::State& state) {
51+
casbin::CachedEnforcer e(rbac_model_path, "", false);
52+
// 100 roles, 10 resources.
53+
for (int i = 0; i < 100; ++i)
54+
e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"});
55+
// 1000 users.
56+
for (int i = 0; i < 1000; ++i)
57+
e.AddGroupingPolicy({ "user" + std::to_string(i), "group", std::to_string(i / 10) });
58+
std::vector<std::string> params = {"user501", "data9", "read"};
59+
for (auto _ : state)
60+
e.Enforce(params);
61+
}
62+
63+
BENCHMARK(BenchmarkCachedRBACModelSmall);
64+
65+
static void BenchmarkCachedRBACModelMedium(benchmark::State& state) {
66+
casbin::CachedEnforcer e(rbac_model_path, "", false);
67+
std::vector<std::vector<std::string>> p_policies(1000);
68+
// 1000 roles, 100 resources.
69+
for (int i = 0; i < 1000; ++i)
70+
p_policies[i] = { "group" + std::to_string(i), "data" + std::to_string(i / 10), "read" };
71+
72+
e.AddPolicies(p_policies);
73+
74+
// 10000 users.
75+
std::vector<std::vector<std::string>> g_policies(10000);
76+
for (int i = 0; i < 10000; ++i)
77+
g_policies[i] = { "user" + std::to_string(i), "group" + std::to_string(i/10) };
78+
79+
e.AddGroupingPolicies(g_policies);
80+
std::vector<std::string> params = {"user5001", "data150", "read"};
81+
for (auto _ : state)
82+
e.Enforce(params);
83+
}
84+
85+
// BENCHMARK(BenchmarkCachedRBACModelMedium);
86+
87+
static void BenchmarkCachedRBACModelLarge(benchmark::State& state) {
88+
casbin::CachedEnforcer e(rbac_model_path, "", false);
89+
90+
// 10000 roles, 1000 resources.
91+
std::vector<std::vector<std::string>> p_policies(10000);
92+
for (int i = 0; i < 10000; ++i)
93+
p_policies[i] = {"group", std::to_string(i), "data", std::to_string(i / 10), "read"};
94+
e.AddPolicies(p_policies);
95+
96+
// 100000 users.
97+
std::vector<std::vector<std::string>> g_policies(100000);
98+
for (int i = 0; i < 100000; ++i) {
99+
g_policies[i] = {"user" + std::to_string(i), "group", std::to_string(i / 10)};
100+
}
101+
e.AddGroupingPolicies(g_policies);
102+
std::vector<std::string> params = {"user50001", "data1500", "read"};
103+
for (auto _ : state)
104+
{
105+
e.Enforce(params);
106+
}
107+
}
108+
109+
// BENCHMARK(BenchmarkCachedRBACModelLarge);
110+
111+
static void BenchmarkCachedRBACModelWithResourceRoles(benchmark::State& state) {
112+
casbin::CachedEnforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path, false);
113+
114+
std::vector<std::string> params = {"alice", "data1", "read"};
115+
for (auto _ : state)
116+
{
117+
e.Enforce(params);
118+
}
119+
}
120+
121+
BENCHMARK(BenchmarkCachedRBACModelWithResourceRoles);
122+
123+
static void BenchmarkCachedRBACModelWithDomains(benchmark::State& state) {
124+
casbin::CachedEnforcer e(rbac_with_domains_model_path, rbac_with_domains_policy_path, false);
125+
126+
std::vector<std::string> params = {"alice", "domain1", "data1", "read"};
127+
128+
for(auto _ : state)
129+
{
130+
e.Enforce(params);
131+
}
132+
}
133+
134+
BENCHMARK(BenchmarkCachedRBACModelWithDomains);
135+
136+
// ---- TODO ----
137+
// static void BenchmarkCachedABACModel(benchmark::State& state) {
138+
// casbin::CachedEnforcer e("examples/abac_model.conf", false);
139+
// auto data1 = casbin::GetData({
140+
// {"Name", "data1"},
141+
// {"Owner", "alice"}
142+
// });
143+
144+
// casbin::DataList params = {"alice", data1, "read"};
145+
// for (auto _ : state)
146+
// {
147+
// e.Enforce(params);
148+
// }
149+
// }
150+
151+
static void BenchmarkCachedKeyMatchModel(benchmark::State& state) {
152+
casbin::CachedEnforcer e(keymatch_model_path, keymatch_policy_path, false);
153+
std::vector<std::string> params = {"alice", "/alice_data/resource1", "GET"};
154+
for (auto _ : state)
155+
{
156+
e.Enforce(params);
157+
}
158+
}
159+
160+
BENCHMARK(BenchmarkCachedKeyMatchModel);
161+
162+
static void BenchmarkCachedRBACModelWithDeny(benchmark::State& state) {
163+
casbin::CachedEnforcer e(rbac_with_deny_model_path, rbac_with_deny_policy_path, false);
164+
165+
std::vector<std::string> params = {"alice", "data1", "read"};
166+
for (auto _ : state)
167+
{
168+
e.Enforce(params);
169+
}
170+
}
171+
172+
BENCHMARK(BenchmarkCachedRBACModelWithDeny);
173+
174+
static void BenchmarkCachedPriorityModel(benchmark::State& state) {
175+
casbin::CachedEnforcer e(priority_model_path, priority_policy_path, false);
176+
177+
std::vector<std::string> params = {"alice", "data1", "read"};
178+
179+
for(auto _ : state)
180+
e.Enforce(params);
181+
}
182+
183+
BENCHMARK(BenchmarkCachedPriorityModel);
184+
185+
static void BenchmarkCachedRBACModelMediumParallel(benchmark::State& state) {
186+
187+
casbin::CachedEnforcer e(rbac_model_path, "", false);
188+
std::vector<std::string> params = {"user5001", "data150", "read"};
189+
if (state.thread_index == 0)
190+
{
191+
std::vector<std::vector<std::string>> p_policies(10000);
192+
for (int i = 0; i < 10000; ++i)
193+
p_policies[i] = { "group" + std::to_string(i), "data" + std::to_string(i / 10), "read" };
194+
e.AddPolicies(p_policies);
195+
196+
std::vector<std::vector<std::string>> g_policies(100000);
197+
for (int i = 0; i < 100000; ++i)
198+
e.AddGroupingPolicy({ "user" + std::to_string(i), "group" + std::to_string(i/10) });
199+
}
200+
for (auto _ : state) {
201+
e.Enforce(params);
202+
}
203+
}
204+
// BENCHMARK(BenchmarkCachedRBACModelMediumParallel)->Threads(10);
205+

0 commit comments

Comments
 (0)