1818
1919#include < benchmark/benchmark.h>
2020#include < casbin/casbin.h>
21+ #include " config_path.h"
2122
2223static 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
3233static 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
3940BENCHMARK (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