Skip to content

Commit 26e7515

Browse files
authored
test: update the benchmark tests (#210)
1 parent 7df0d46 commit 26e7515

File tree

6 files changed

+3914
-18
lines changed

6 files changed

+3914
-18
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[request_definition]
2+
r = sub, obj, act
3+
4+
[policy_definition]
5+
p = sub, obj, act
6+
7+
[role_definition]
8+
g = _, _, _
9+
10+
[policy_effect]
11+
e = some(where (p.eft == allow))
12+
13+
[matchers]
14+
m = g(r.sub, p.sub, r.obj) && keyMatch4(r.obj, p.obj) && regexMatch(r.act, p.act)

examples/performance/rbac_with_pattern_large_scale_policy.csv

Lines changed: 3768 additions & 0 deletions
Large diffs are not rendered by default.

tests/benchmarks/config_path.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ static const std::string keymatch_policy_path = relative_path + "/examples/keyma
4040

4141
static const std::string priority_model_path = relative_path + "/examples/priority_model.conf";
4242
static const std::string priority_policy_path = relative_path + "/examples/priority_policy.csv";
43+
44+
static const std::string globmatch_model_path = relative_path + "/examples/globmatch_model.conf";
45+
static const std::string globmatch_policy_path = relative_path + "/examples/globmatch_policy.csv";
46+
47+
static const std::string rbac_with_pattern_large_scale_model_path = relative_path + "/examples/performance/rbac_with_pattern_large_scale_model.conf";
48+
static const std::string rbac_with_pattern_large_scale_policy_path = relative_path + "/examples/performance/rbac_with_pattern_large_scale_policy.csv";

tests/benchmarks/enforcer_cached_b.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,38 @@
2121

2222
#include "config_path.h"
2323

24+
static const std::vector<std::vector<std::string>> s_policy = {{"alice", "data1", "read"}, {"bob", "data2", "write"}};
25+
26+
static bool rawEnforce(const std::string& sub, const std::string& obj, const std::string& act) {
27+
for (const auto& rule : s_policy) {
28+
if (rule[0] == sub && rule[1] == obj && rule[2] == act)
29+
return true;
30+
}
31+
return false;
32+
}
33+
34+
static void BenchmarkCachedRaw(benchmark::State& state) {
35+
for (auto _ : state) rawEnforce("alice", "data1", "read");
36+
}
37+
38+
BENCHMARK(BenchmarkCachedRaw);
39+
2440
static void BenchmarkCachedBasicModel(benchmark::State& state) {
25-
casbin::CachedEnforcer e(basic_model_path, basic_policy_path);
41+
casbin::CachedEnforcer e(basic_model_path, basic_policy_path, false);
2642
casbin::DataList request = {"alice", "data1", "read"};
2743
for (auto _ : state) e.Enforce(request);
2844
}
2945

3046
BENCHMARK(BenchmarkCachedBasicModel);
3147

3248
static void BenchmarkCachedRBACModel(benchmark::State& state) {
33-
casbin::CachedEnforcer e(rbac_model_path, rbac_policy_path);
49+
casbin::CachedEnforcer e(rbac_model_path, rbac_policy_path, false);
3450
casbin::DataList request = {"alice", "data2", "read"};
3551
for (auto _ : state) e.Enforce(request);
3652
}
3753

3854
BENCHMARK(BenchmarkCachedRBACModel);
3955

40-
// ---- TODO ----
41-
// static void BenchmarkCachedRaw(benchmark::State& state) {
42-
// for (auto _ : state)
43-
// rawEnforce("alice", "data1", "read")
44-
// }
45-
46-
// BENCHMARK(BenchmarkCachedRaw);
47-
4856
static void BenchmarkCachedRBACModelSmall(benchmark::State& state) {
4957
casbin::CachedEnforcer e(rbac_model_path, "", false);
5058
// 100 roles, 10 resources.

tests/benchmarks/model_b.cpp

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static void BenchmarkRaw(benchmark::State& state) {
3838
BENCHMARK(BenchmarkRaw);
3939

4040
static void BenchmarkBasicModel(benchmark::State& state) {
41-
casbin::Enforcer e(basic_model_path, basic_policy_path);
41+
casbin::Enforcer e(basic_model_path, basic_policy_path, false);
4242

4343
casbin::DataList params = {"alice", "data1", "read"};
4444

@@ -48,7 +48,7 @@ static void BenchmarkBasicModel(benchmark::State& state) {
4848
BENCHMARK(BenchmarkBasicModel);
4949

5050
static void BenchmarkRBACModel(benchmark::State& state) {
51-
casbin::Enforcer e(rbac_model_path, rbac_policy_path);
51+
casbin::Enforcer e(rbac_model_path, rbac_policy_path, false);
5252

5353
casbin::DataList params = {"alice", "data2", "read"};
5454

@@ -57,6 +57,38 @@ static void BenchmarkRBACModel(benchmark::State& state) {
5757

5858
BENCHMARK(BenchmarkRBACModel);
5959

60+
static void BenchmarkRBACModelSizesSmall(benchmark::State& state) {
61+
// 100, 10, 1000
62+
int num_roles = 100, num_resources = 10, num_users = 1000;
63+
64+
casbin::Enforcer e(rbac_model_path, "", false);
65+
66+
for (int i = 0; i < num_roles; ++i) e.AddPolicy({"group-has-a-very-long-name-" + std::to_string(i), "data-has-a-very-long-name-" + std::to_string(i % num_resources), "read"});
67+
68+
for (int i = 0; i < num_users; ++i) {
69+
e.AddGroupingPolicy({"user-has-a-very-long-name-" + std::to_string(i), "group-has-a-very-long-name-" + std::to_string(i % num_roles)});
70+
}
71+
72+
int num_request = 17;
73+
std::vector<casbin::DataList> requests(num_request);
74+
75+
for (int i = 0; i < num_request; ++i) {
76+
int id_user = num_users / num_request * i,
77+
id_role = id_user / num_roles,
78+
id_resource = id_role % num_resources;
79+
if (i&2 == 0)
80+
id_resource = (id_resource + 1) % num_resources;
81+
82+
requests[i] = {"user-has-a-very-long-name-" + std::to_string(id_user), "data-has-a-very-long-name-" + std::to_string(id_resource), "read"};
83+
}
84+
85+
for (auto _ : state)
86+
for (auto& req: requests)
87+
e.Enforce(req);
88+
}
89+
90+
BENCHMARK(BenchmarkRBACModelSizesSmall);
91+
6092
static void BenchmarkRBACModelSmall(benchmark::State& state) {
6193
casbin::Enforcer e(rbac_model_path);
6294

@@ -73,7 +105,7 @@ static void BenchmarkRBACModelSmall(benchmark::State& state) {
73105
BENCHMARK(BenchmarkRBACModelSmall);
74106

75107
static void BenchmarkRBACModelWithResourceRoles(benchmark::State& state) {
76-
casbin::Enforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path);
108+
casbin::Enforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path, false);
77109

78110
casbin::DataList params = {"alice", "data1", "read"};
79111
for (auto _ : state) e.Enforce(params);
@@ -82,7 +114,7 @@ static void BenchmarkRBACModelWithResourceRoles(benchmark::State& state) {
82114
BENCHMARK(BenchmarkRBACModelWithResourceRoles);
83115

84116
static void BenchmarkRBACModelWithDomains(benchmark::State& state) {
85-
casbin::Enforcer e(rbac_with_domains_model_path, rbac_with_domains_policy_path);
117+
casbin::Enforcer e(rbac_with_domains_model_path, rbac_with_domains_policy_path, false);
86118
casbin::DataList params = {"alice", "domain1", "data1", "read"};
87119

88120
for (auto _ : state) e.Enforce(params);
@@ -100,8 +132,19 @@ BENCHMARK(BenchmarkRBACModelWithDomains);
100132
// }
101133
// }
102134

135+
// ------ TODO ------
136+
// static void BenchmarkABACRuleModel(benchmark::State& state) {
137+
// casbin::Enforcer e("examples/abac_model.conf")
138+
// data1 := newTestResource("data1", "alice")
139+
140+
// for(auto _ : state) {
141+
// _, _ = e.Enforce("alice", data1, "read")
142+
// }
143+
// }
144+
145+
103146
static void BenchmarkKeyMatchModel(benchmark::State& state) {
104-
casbin::Enforcer e(keymatch_model_path, keymatch_policy_path);
147+
casbin::Enforcer e(keymatch_model_path, keymatch_policy_path, false);
105148
casbin::DataList params = {"alice", "/alice_data/resource1", "GET"};
106149

107150
for (auto _ : state) e.Enforce(params);
@@ -110,7 +153,7 @@ static void BenchmarkKeyMatchModel(benchmark::State& state) {
110153
BENCHMARK(BenchmarkKeyMatchModel);
111154

112155
static void BenchmarkRBACModelWithDeny(benchmark::State& state) {
113-
casbin::Enforcer e(rbac_with_deny_model_path, rbac_with_deny_policy_path);
156+
casbin::Enforcer e(rbac_with_deny_model_path, rbac_with_deny_policy_path, false);
114157
casbin::DataList params = {"alice", "data1", "read"};
115158

116159
for (auto _ : state) e.Enforce(params);
@@ -119,7 +162,7 @@ static void BenchmarkRBACModelWithDeny(benchmark::State& state) {
119162
BENCHMARK(BenchmarkRBACModelWithDeny);
120163

121164
static void BenchmarkPriorityModel(benchmark::State& state) {
122-
casbin::Enforcer e(priority_model_path, priority_policy_path);
165+
casbin::Enforcer e(priority_model_path, priority_policy_path, false);
123166
casbin::DataList params = {"alice", "data1", "read"};
124167

125168
for (auto _ : state) e.Enforce(params);

tests/benchmarks/model_b_inten.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,48 @@
2121

2222
#include "config_path.h"
2323

24+
25+
static void _BenchmarkRBACModelSizes(benchmark::State& state, int num_roles, int num_resources, int num_users) {
26+
27+
casbin::Enforcer e(rbac_model_path, "", false);
28+
29+
for (int i = 0; i < num_roles; ++i) e.AddPolicy({"group-has-a-very-long-name-" + std::to_string(i), "data-has-a-very-long-name-" + std::to_string(i % num_resources), "read"});
30+
31+
for (int i = 0; i < num_users; ++i) {
32+
e.AddGroupingPolicy({"user-has-a-very-long-name-" + std::to_string(i), "group-has-a-very-long-name-" + std::to_string(i % num_roles)});
33+
}
34+
35+
int num_request = 17;
36+
std::vector<casbin::DataList> requests(num_request);
37+
38+
for (int i = 0; i < num_request; ++i) {
39+
int id_user = num_users / num_request * i,
40+
id_role = id_user / num_roles,
41+
id_resource = id_role % num_resources;
42+
if (i&2 == 0)
43+
id_resource = (id_resource + 1) % num_resources;
44+
45+
requests[i] = {"user-has-a-very-long-name-" + std::to_string(id_user), "data-has-a-very-long-name-" + std::to_string(id_resource), "read"};
46+
}
47+
48+
for (auto _ : state)
49+
for (auto& req: requests)
50+
e.Enforce(req);
51+
}
52+
53+
static void BenchmarkRBACModelSizesMedium(benchmark::State& state) {
54+
_BenchmarkRBACModelSizes(state, 1000, 100, 10000);
55+
}
56+
57+
BENCHMARK(BenchmarkRBACModelSizesMedium);
58+
59+
static void BenchmarkRBACModelSizesLarge(benchmark::State& state) {
60+
_BenchmarkRBACModelSizes(state, 10000, 1000, 100000);
61+
}
62+
63+
BENCHMARK(BenchmarkRBACModelSizesLarge);
64+
65+
2466
static const std::vector<std::vector<std::string>> s_policy = {{"alice", "data1", "read"}, {"bob", "data2", "write"}};
2567
static void BenchmarkRBACModelMedium(benchmark::State& state) {
2668
casbin::Enforcer e(rbac_model_path);
@@ -51,4 +93,19 @@ static void BenchmarkRBACModelLarge(benchmark::State& state) {
5193
for (auto _ : state) e.Enforce(params);
5294
}
5395

54-
BENCHMARK(BenchmarkRBACModelLarge);
96+
BENCHMARK(BenchmarkRBACModelLarge);
97+
98+
99+
static void BenchmarkRBACModelWithDomainPatternLarge(benchmark::State& state) {
100+
casbin::Enforcer e(rbac_with_pattern_large_scale_model_path, rbac_with_pattern_large_scale_policy_path);
101+
102+
e.AddNamedMatchingFunc("g", "", casbin::KeyMatch4);
103+
104+
casbin::DataList params = {"staffUser1001", "/orgs/1/sites/site001", "App001.Module001.Action1001"};
105+
106+
for (auto _ : state) e.Enforce(params);
107+
}
108+
109+
BENCHMARK(BenchmarkRBACModelWithDomainPatternLarge);
110+
111+

0 commit comments

Comments
 (0)