Skip to content

Commit 7790669

Browse files
authored
fix: add a benchmark tests option (#162)
* fix: expedite benchmark tests Signed-off-by: noob20000405 <[email protected]> * fix: add a benchmark tests option Signed-off-by: noob20000405 <[email protected]> * fix: add a benchmark tests option Signed-off-by: noob20000405 <[email protected]> * fix: modification of intensive benchmark option Signed-off-by: noob20000405 <[email protected]> * fix: modification of intensive benchmark option Signed-off-by: noob20000405 <[email protected]>
1 parent abaa217 commit 7790669

File tree

10 files changed

+361
-240
lines changed

10 files changed

+361
-240
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ endif()
5353

5454
option(CASBIN_BUILD_TEST "State whether to build test" ON)
5555
option(CASBIN_BUILD_BENCHMARK "State whether to build benchmarks" ON)
56+
option(INTENSIVE_BENCHMARK "State whether to build intensive benchmarks" OFF)
5657
option(CASBIN_BUILD_BINDINGS "State whether to build language bindings" ON)
5758
option(CASBIN_BUILD_PYTHON_BINDINGS "State whether to build python bindings" ON)
5859
option(CASBIN_INSTALL "State whether to install casbin targets on the current system" ON)

tests/benchmarks/CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,22 @@ set(CASBIN_BENCHMARK_SOURCE
2222
role_manager_b.cpp
2323
)
2424

25+
set(CASBIN_INTENSIVE_BENCHMARK_SOURCE
26+
model_b_inten.cpp
27+
enforcer_cached_b_inten.cpp
28+
management_api_b_inten.cpp
29+
role_manager_b_inten.cpp
30+
)
31+
2532
set(CASBIN_BENCHMARK_HEADER
2633
config_path.h
2734
)
2835

29-
add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_BENCHMARK_HEADER})
36+
if(INTENSIVE_BENCHMARK STREQUAL ON)
37+
add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_INTENSIVE_BENCHMARK_SOURCE} ${CASBIN_BENCHMARK_HEADER})
38+
else()
39+
add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_BENCHMARK_HEADER})
40+
endif()
3041

3142
target_include_directories(casbin_benchmark PUBLIC ${CASBIN_INCLUDE_DIR})
3243

tests/benchmarks/enforcer_cached_b.cpp

Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -62,52 +62,6 @@ static void BenchmarkCachedRBACModelSmall(benchmark::State& state) {
6262

6363
BENCHMARK(BenchmarkCachedRBACModelSmall);
6464

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-
casbin::DataList 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-
casbin::DataList params = {"user50001", "data1500", "read"};
103-
for (auto _ : state)
104-
{
105-
e.Enforce(params);
106-
}
107-
}
108-
109-
// BENCHMARK(BenchmarkCachedRBACModelLarge);
110-
11165
static void BenchmarkCachedRBACModelWithResourceRoles(benchmark::State& state) {
11266
casbin::CachedEnforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path, false);
11367

@@ -181,25 +135,3 @@ static void BenchmarkCachedPriorityModel(benchmark::State& state) {
181135
}
182136

183137
BENCHMARK(BenchmarkCachedPriorityModel);
184-
185-
static void BenchmarkCachedRBACModelMediumParallel(benchmark::State& state) {
186-
187-
casbin::CachedEnforcer e(rbac_model_path, "", false);
188-
casbin::DataList 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-
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 an intensive test file for benchmarking the performance of casbin::CachedEnforcer
17+
*/
18+
19+
#include <benchmark/benchmark.h>
20+
#include <casbin/casbin.h>
21+
#include "config_path.h"
22+
23+
static void BenchmarkCachedRBACModelMedium(benchmark::State& state) {
24+
casbin::CachedEnforcer e(rbac_model_path, "", false);
25+
std::vector<std::vector<std::string>> p_policies(1000);
26+
// 1000 roles, 100 resources.
27+
for (int i = 0; i < 1000; ++i)
28+
p_policies[i] = { "group" + std::to_string(i), "data" + std::to_string(i / 10), "read" };
29+
30+
e.AddPolicies(p_policies);
31+
32+
// 10000 users.
33+
std::vector<std::vector<std::string>> g_policies(10000);
34+
for (int i = 0; i < 10000; ++i)
35+
g_policies[i] = { "user" + std::to_string(i), "group" + std::to_string(i/10) };
36+
37+
e.AddGroupingPolicies(g_policies);
38+
casbin::DataList params = {"user5001", "data150", "read"};
39+
for (auto _ : state)
40+
e.Enforce(params);
41+
}
42+
43+
BENCHMARK(BenchmarkCachedRBACModelMedium);
44+
45+
static void BenchmarkCachedRBACModelLarge(benchmark::State& state) {
46+
casbin::CachedEnforcer e(rbac_model_path, "", false);
47+
48+
// 10000 roles, 1000 resources.
49+
std::vector<std::vector<std::string>> p_policies(10000);
50+
for (int i = 0; i < 10000; ++i)
51+
p_policies[i] = {"group", std::to_string(i), "data", std::to_string(i / 10), "read"};
52+
e.AddPolicies(p_policies);
53+
54+
// 100000 users.
55+
std::vector<std::vector<std::string>> g_policies(100000);
56+
for (int i = 0; i < 100000; ++i) {
57+
g_policies[i] = {"user" + std::to_string(i), "group", std::to_string(i / 10)};
58+
}
59+
e.AddGroupingPolicies(g_policies);
60+
casbin::DataList params = {"user50001", "data1500", "read"};
61+
for (auto _ : state)
62+
{
63+
e.Enforce(params);
64+
}
65+
}
66+
67+
BENCHMARK(BenchmarkCachedRBACModelLarge);
68+
69+
static void BenchmarkCachedRBACModelMediumParallel(benchmark::State& state) {
70+
71+
casbin::CachedEnforcer e(rbac_model_path, "", false);
72+
casbin::DataList params = {"user5001", "data150", "read"};
73+
if (state.thread_index == 0)
74+
{
75+
std::vector<std::vector<std::string>> p_policies(10000);
76+
for (int i = 0; i < 10000; ++i)
77+
p_policies[i] = { "group" + std::to_string(i), "data" + std::to_string(i / 10), "read" };
78+
e.AddPolicies(p_policies);
79+
80+
std::vector<std::vector<std::string>> g_policies(100000);
81+
for (int i = 0; i < 100000; ++i)
82+
e.AddGroupingPolicy({ "user" + std::to_string(i), "group" + std::to_string(i/10) });
83+
}
84+
for (auto _ : state) {
85+
e.Enforce(params);
86+
}
87+
}
88+
// BENCHMARK(BenchmarkCachedRBACModelMediumParallel)->Threads(10);

tests/benchmarks/management_api_b.cpp

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,6 @@ static void BenchmarkHasPolicySmall(benchmark::State& state) {
6262

6363
BENCHMARK(BenchmarkHasPolicySmall);
6464

65-
static void BenchmarkHasPolicyMedium(benchmark::State& state) {
66-
casbin::Enforcer e(basic_model_path);
67-
68-
// 1000 roles, 100 resources.
69-
// std::vector<std::vector<std::string>> p_policies(1000);
70-
for (int i = 0; i < 1000; ++i)
71-
params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params);
72-
// e.AddPolicies(p_policies);
73-
for (auto _ : state)
74-
params = { "user" + std::to_string(GetRandom1000()), "data" + std::to_string(GetRandom1000()/10), "read" }, e.HasPolicy(params);
75-
}
76-
77-
BENCHMARK(BenchmarkHasPolicyMedium);
78-
79-
static void BenchmarkHasPolicyLarge(benchmark::State& state) {
80-
casbin::Enforcer e(basic_model_path);
81-
82-
// 10000 roles, 1000 resources.
83-
for (int i = 0; i < 10000; i++)
84-
params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params);
85-
86-
for(auto _ : state) {
87-
params = {"user" + std::to_string(GetRandom10000()), "data" + std::to_string(GetRandom10000()/10), "read"}, e.HasPolicy(params);
88-
}
89-
}
90-
91-
BENCHMARK(BenchmarkHasPolicyLarge);
92-
9365
static void BenchmarkAddPolicySmall(benchmark::State& state) {
9466
casbin::Enforcer e(basic_model_path);
9567

@@ -103,35 +75,6 @@ static void BenchmarkAddPolicySmall(benchmark::State& state) {
10375

10476
BENCHMARK(BenchmarkAddPolicySmall);
10577

106-
static void BenchmarkAddPolicyMedium(benchmark::State& state) {
107-
casbin::Enforcer e(basic_model_path);
108-
109-
// 1000 roles, 100 resources.
110-
for(int i = 0; i < 1000; ++i)
111-
params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params);
112-
// _, err := e.AddPolicies(pPolicies)
113-
114-
for(auto _ : state) {
115-
params = {"user" + std::to_string(GetRandom1000() + 1000), "data" + std::to_string((GetRandom1000() + 1000) / 10), "read"}, e.AddPolicy(params);
116-
}
117-
}
118-
119-
BENCHMARK(BenchmarkAddPolicyMedium);
120-
121-
static void BenchmarkAddPolicyLarge(benchmark::State& state) {
122-
casbin::Enforcer e(basic_model_path);
123-
124-
// 10000 roles, 1000 resources.
125-
for(int i = 0; i < 10000; ++i)
126-
params = { "user" + std::to_string(i), "data" + std::to_string(i/10), "read" }, e.AddPolicy(params);
127-
128-
for(auto _ : state) {
129-
params = { "user" + std::to_string(GetRandom10000() + 10000), "data" + std::to_string((GetRandom10000() + 10000) / 10), "read" }, e.AddPolicy(params);
130-
}
131-
}
132-
133-
BENCHMARK(BenchmarkAddPolicyLarge);
134-
13578
static void BenchmarkRemovePolicySmall(benchmark::State& state) {
13679
casbin::Enforcer e(basic_model_path);
13780

@@ -144,29 +87,3 @@ static void BenchmarkRemovePolicySmall(benchmark::State& state) {
14487
}
14588

14689
BENCHMARK(BenchmarkRemovePolicySmall);
147-
148-
static void BenchmarkRemovePolicyMedium(benchmark::State& state) {
149-
casbin::Enforcer e(basic_model_path);
150-
151-
// 1000 roles, 100 resources.
152-
for(int i = 0; i < 1000; ++i)
153-
params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params);
154-
155-
for(auto _ : state)
156-
params = { "user" + std::to_string(GetRandom1000()), "data" + std::to_string(GetRandom1000() / 10), "read" }, e.RemovePolicy(params);
157-
}
158-
159-
BENCHMARK(BenchmarkRemovePolicyMedium);
160-
161-
static void BenchmarkRemovePolicyLarge(benchmark::State& state) {
162-
casbin::Enforcer e(basic_model_path);
163-
164-
// 10000 roles, 1000 resources.
165-
for(int i = 0; i < 10000; ++i)
166-
params = { "user" + std::to_string(i), "data" + std::to_string(i / 10), "read" }, e.AddPolicy(params);
167-
168-
for(auto _ : state)
169-
params = { "user" + std::to_string(GetRandom10000()), "data" + std::to_string(GetRandom1000()), "read" }, e.RemovePolicy(params);
170-
}
171-
172-
BENCHMARK(BenchmarkRemovePolicyLarge);

0 commit comments

Comments
 (0)