diff --git a/CMakeLists.txt b/CMakeLists.txt index 0392f5dd..f3e35c24 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ endif() option(CASBIN_BUILD_TEST "State whether to build test" ON) option(CASBIN_BUILD_BENCHMARK "State whether to build benchmarks" ON) +option(INTENSIVE_BENCHMARK "State whether to build intensive benchmarks" OFF) option(CASBIN_BUILD_BINDINGS "State whether to build language bindings" ON) option(CASBIN_BUILD_PYTHON_BINDINGS "State whether to build python bindings" ON) option(CASBIN_INSTALL "State whether to install casbin targets on the current system" ON) diff --git a/tests/benchmarks/CMakeLists.txt b/tests/benchmarks/CMakeLists.txt index c6a34302..295c4e73 100644 --- a/tests/benchmarks/CMakeLists.txt +++ b/tests/benchmarks/CMakeLists.txt @@ -22,11 +22,22 @@ set(CASBIN_BENCHMARK_SOURCE role_manager_b.cpp ) +set(CASBIN_INTENSIVE_BENCHMARK_SOURCE + model_b_inten.cpp + enforcer_cached_b_inten.cpp + management_api_b_inten.cpp + role_manager_b_inten.cpp +) + set(CASBIN_BENCHMARK_HEADER config_path.h ) -add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_BENCHMARK_HEADER}) +if(INTENSIVE_BENCHMARK STREQUAL ON) + add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_INTENSIVE_BENCHMARK_SOURCE} ${CASBIN_BENCHMARK_HEADER}) +else() + add_executable(casbin_benchmark ${CASBIN_BENCHMARK_SOURCE} ${CASBIN_BENCHMARK_HEADER}) +endif() target_include_directories(casbin_benchmark PUBLIC ${CASBIN_INCLUDE_DIR}) diff --git a/tests/benchmarks/enforcer_cached_b.cpp b/tests/benchmarks/enforcer_cached_b.cpp index 70bd9bfe..1c7dca67 100644 --- a/tests/benchmarks/enforcer_cached_b.cpp +++ b/tests/benchmarks/enforcer_cached_b.cpp @@ -62,52 +62,6 @@ static void BenchmarkCachedRBACModelSmall(benchmark::State& state) { BENCHMARK(BenchmarkCachedRBACModelSmall); -static void BenchmarkCachedRBACModelMedium(benchmark::State& state) { - casbin::CachedEnforcer e(rbac_model_path, "", false); - std::vector> p_policies(1000); - // 1000 roles, 100 resources. - for (int i = 0; i < 1000; ++i) - p_policies[i] = { "group" + std::to_string(i), "data" + std::to_string(i / 10), "read" }; - - e.AddPolicies(p_policies); - - // 10000 users. - std::vector> g_policies(10000); - for (int i = 0; i < 10000; ++i) - g_policies[i] = { "user" + std::to_string(i), "group" + std::to_string(i/10) }; - - e.AddGroupingPolicies(g_policies); - casbin::DataList params = {"user5001", "data150", "read"}; - for (auto _ : state) - e.Enforce(params); -} - -// BENCHMARK(BenchmarkCachedRBACModelMedium); - -static void BenchmarkCachedRBACModelLarge(benchmark::State& state) { - casbin::CachedEnforcer e(rbac_model_path, "", false); - - // 10000 roles, 1000 resources. - std::vector> p_policies(10000); - for (int i = 0; i < 10000; ++i) - p_policies[i] = {"group", std::to_string(i), "data", std::to_string(i / 10), "read"}; - e.AddPolicies(p_policies); - - // 100000 users. - std::vector> g_policies(100000); - for (int i = 0; i < 100000; ++i) { - g_policies[i] = {"user" + std::to_string(i), "group", std::to_string(i / 10)}; - } - e.AddGroupingPolicies(g_policies); - casbin::DataList params = {"user50001", "data1500", "read"}; - for (auto _ : state) - { - e.Enforce(params); - } -} - -// BENCHMARK(BenchmarkCachedRBACModelLarge); - static void BenchmarkCachedRBACModelWithResourceRoles(benchmark::State& state) { casbin::CachedEnforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path, false); @@ -181,25 +135,3 @@ static void BenchmarkCachedPriorityModel(benchmark::State& state) { } BENCHMARK(BenchmarkCachedPriorityModel); - -static void BenchmarkCachedRBACModelMediumParallel(benchmark::State& state) { - - casbin::CachedEnforcer e(rbac_model_path, "", false); - casbin::DataList params = {"user5001", "data150", "read"}; - if (state.thread_index == 0) - { - std::vector> p_policies(10000); - for (int i = 0; i < 10000; ++i) - p_policies[i] = { "group" + std::to_string(i), "data" + std::to_string(i / 10), "read" }; - e.AddPolicies(p_policies); - - std::vector> g_policies(100000); - for (int i = 0; i < 100000; ++i) - e.AddGroupingPolicy({ "user" + std::to_string(i), "group" + std::to_string(i/10) }); - } - for (auto _ : state) { - e.Enforce(params); - } -} -// BENCHMARK(BenchmarkCachedRBACModelMediumParallel)->Threads(10); - diff --git a/tests/benchmarks/enforcer_cached_b_inten.cpp b/tests/benchmarks/enforcer_cached_b_inten.cpp new file mode 100644 index 00000000..7925e0f2 --- /dev/null +++ b/tests/benchmarks/enforcer_cached_b_inten.cpp @@ -0,0 +1,88 @@ +/* +* Copyright 2021 The casbin Authors. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* This is an intensive test file for benchmarking the performance of casbin::CachedEnforcer +*/ + +#include +#include +#include "config_path.h" + +static void BenchmarkCachedRBACModelMedium(benchmark::State& state) { + casbin::CachedEnforcer e(rbac_model_path, "", false); + std::vector> p_policies(1000); + // 1000 roles, 100 resources. + for (int i = 0; i < 1000; ++i) + p_policies[i] = { "group" + std::to_string(i), "data" + std::to_string(i / 10), "read" }; + + e.AddPolicies(p_policies); + + // 10000 users. + std::vector> g_policies(10000); + for (int i = 0; i < 10000; ++i) + g_policies[i] = { "user" + std::to_string(i), "group" + std::to_string(i/10) }; + + e.AddGroupingPolicies(g_policies); + casbin::DataList params = {"user5001", "data150", "read"}; + for (auto _ : state) + e.Enforce(params); +} + +BENCHMARK(BenchmarkCachedRBACModelMedium); + +static void BenchmarkCachedRBACModelLarge(benchmark::State& state) { + casbin::CachedEnforcer e(rbac_model_path, "", false); + + // 10000 roles, 1000 resources. + std::vector> p_policies(10000); + for (int i = 0; i < 10000; ++i) + p_policies[i] = {"group", std::to_string(i), "data", std::to_string(i / 10), "read"}; + e.AddPolicies(p_policies); + + // 100000 users. + std::vector> g_policies(100000); + for (int i = 0; i < 100000; ++i) { + g_policies[i] = {"user" + std::to_string(i), "group", std::to_string(i / 10)}; + } + e.AddGroupingPolicies(g_policies); + casbin::DataList params = {"user50001", "data1500", "read"}; + for (auto _ : state) + { + e.Enforce(params); + } +} + +BENCHMARK(BenchmarkCachedRBACModelLarge); + +static void BenchmarkCachedRBACModelMediumParallel(benchmark::State& state) { + + casbin::CachedEnforcer e(rbac_model_path, "", false); + casbin::DataList params = {"user5001", "data150", "read"}; + if (state.thread_index == 0) + { + std::vector> p_policies(10000); + for (int i = 0; i < 10000; ++i) + p_policies[i] = { "group" + std::to_string(i), "data" + std::to_string(i / 10), "read" }; + e.AddPolicies(p_policies); + + std::vector> g_policies(100000); + for (int i = 0; i < 100000; ++i) + e.AddGroupingPolicy({ "user" + std::to_string(i), "group" + std::to_string(i/10) }); + } + for (auto _ : state) { + e.Enforce(params); + } +} +// BENCHMARK(BenchmarkCachedRBACModelMediumParallel)->Threads(10); \ No newline at end of file diff --git a/tests/benchmarks/management_api_b.cpp b/tests/benchmarks/management_api_b.cpp index 58605fcf..7e479037 100644 --- a/tests/benchmarks/management_api_b.cpp +++ b/tests/benchmarks/management_api_b.cpp @@ -62,34 +62,6 @@ static void BenchmarkHasPolicySmall(benchmark::State& state) { BENCHMARK(BenchmarkHasPolicySmall); -static void BenchmarkHasPolicyMedium(benchmark::State& state) { - casbin::Enforcer e(basic_model_path); - - // 1000 roles, 100 resources. - // std::vector> p_policies(1000); - for (int i = 0; i < 1000; ++i) - params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); - // e.AddPolicies(p_policies); - for (auto _ : state) - params = { "user" + std::to_string(GetRandom1000()), "data" + std::to_string(GetRandom1000()/10), "read" }, e.HasPolicy(params); -} - -BENCHMARK(BenchmarkHasPolicyMedium); - -static void BenchmarkHasPolicyLarge(benchmark::State& state) { - casbin::Enforcer e(basic_model_path); - - // 10000 roles, 1000 resources. - for (int i = 0; i < 10000; i++) - params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); - - for(auto _ : state) { - params = {"user" + std::to_string(GetRandom10000()), "data" + std::to_string(GetRandom10000()/10), "read"}, e.HasPolicy(params); - } -} - -BENCHMARK(BenchmarkHasPolicyLarge); - static void BenchmarkAddPolicySmall(benchmark::State& state) { casbin::Enforcer e(basic_model_path); @@ -103,35 +75,6 @@ static void BenchmarkAddPolicySmall(benchmark::State& state) { BENCHMARK(BenchmarkAddPolicySmall); -static void BenchmarkAddPolicyMedium(benchmark::State& state) { - casbin::Enforcer e(basic_model_path); - - // 1000 roles, 100 resources. - for(int i = 0; i < 1000; ++i) - params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); - // _, err := e.AddPolicies(pPolicies) - - for(auto _ : state) { - params = {"user" + std::to_string(GetRandom1000() + 1000), "data" + std::to_string((GetRandom1000() + 1000) / 10), "read"}, e.AddPolicy(params); - } -} - -BENCHMARK(BenchmarkAddPolicyMedium); - -static void BenchmarkAddPolicyLarge(benchmark::State& state) { - casbin::Enforcer e(basic_model_path); - - // 10000 roles, 1000 resources. - for(int i = 0; i < 10000; ++i) - params = { "user" + std::to_string(i), "data" + std::to_string(i/10), "read" }, e.AddPolicy(params); - - for(auto _ : state) { - params = { "user" + std::to_string(GetRandom10000() + 10000), "data" + std::to_string((GetRandom10000() + 10000) / 10), "read" }, e.AddPolicy(params); - } -} - -BENCHMARK(BenchmarkAddPolicyLarge); - static void BenchmarkRemovePolicySmall(benchmark::State& state) { casbin::Enforcer e(basic_model_path); @@ -144,29 +87,3 @@ static void BenchmarkRemovePolicySmall(benchmark::State& state) { } BENCHMARK(BenchmarkRemovePolicySmall); - -static void BenchmarkRemovePolicyMedium(benchmark::State& state) { - casbin::Enforcer e(basic_model_path); - - // 1000 roles, 100 resources. - for(int i = 0; i < 1000; ++i) - params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); - - for(auto _ : state) - params = { "user" + std::to_string(GetRandom1000()), "data" + std::to_string(GetRandom1000() / 10), "read" }, e.RemovePolicy(params); -} - -BENCHMARK(BenchmarkRemovePolicyMedium); - -static void BenchmarkRemovePolicyLarge(benchmark::State& state) { - casbin::Enforcer e(basic_model_path); - - // 10000 roles, 1000 resources. - for(int i = 0; i < 10000; ++i) - params = { "user" + std::to_string(i), "data" + std::to_string(i / 10), "read" }, e.AddPolicy(params); - - for(auto _ : state) - params = { "user" + std::to_string(GetRandom10000()), "data" + std::to_string(GetRandom1000()), "read" }, e.RemovePolicy(params); -} - -BENCHMARK(BenchmarkRemovePolicyLarge); diff --git a/tests/benchmarks/management_api_b_inten.cpp b/tests/benchmarks/management_api_b_inten.cpp new file mode 100644 index 00000000..189b0ceb --- /dev/null +++ b/tests/benchmarks/management_api_b_inten.cpp @@ -0,0 +1,126 @@ +/* +* Copyright 2021 The casbin Authors. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* This is an intensive test file for benchmarking the performance of casbin's Management API +*/ + +#include +#include +#include +#include "config_path.h" + +static std::random_device generator; +static std::uniform_int_distribution dist_100(1, 100); +static std::uniform_int_distribution dist_1000(1, 1000); +static std::uniform_int_distribution dist_10000(1, 10000); +static std::vector params(3); + +static inline int GetRandom100() +{ + return dist_100(generator); +} + +static inline int GetRandom1000() +{ + return dist_1000(generator); +} + +static inline int GetRandom10000() +{ + return dist_10000(generator); +} + +static void BenchmarkHasPolicyMedium(benchmark::State& state) { + casbin::Enforcer e(basic_model_path); + + // 1000 roles, 100 resources. + // std::vector> p_policies(1000); + for (int i = 0; i < 1000; ++i) + params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); + // e.AddPolicies(p_policies); + for (auto _ : state) + params = { "user" + std::to_string(GetRandom1000()), "data" + std::to_string(GetRandom1000()/10), "read" }, e.HasPolicy(params); +} + +BENCHMARK(BenchmarkHasPolicyMedium); + +static void BenchmarkHasPolicyLarge(benchmark::State& state) { + casbin::Enforcer e(basic_model_path); + + // 10000 roles, 1000 resources. + for (int i = 0; i < 10000; i++) + params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); + + for(auto _ : state) { + params = {"user" + std::to_string(GetRandom10000()), "data" + std::to_string(GetRandom10000()/10), "read"}, e.HasPolicy(params); + } +} + +BENCHMARK(BenchmarkHasPolicyLarge); + +static void BenchmarkAddPolicyMedium(benchmark::State& state) { + casbin::Enforcer e(basic_model_path); + + // 1000 roles, 100 resources. + for(int i = 0; i < 1000; ++i) + params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); + // _, err := e.AddPolicies(pPolicies) + + for(auto _ : state) { + params = {"user" + std::to_string(GetRandom1000() + 1000), "data" + std::to_string((GetRandom1000() + 1000) / 10), "read"}, e.AddPolicy(params); + } +} + +BENCHMARK(BenchmarkAddPolicyMedium); + +static void BenchmarkAddPolicyLarge(benchmark::State& state) { + casbin::Enforcer e(basic_model_path); + + // 10000 roles, 1000 resources. + for(int i = 0; i < 10000; ++i) + params = { "user" + std::to_string(i), "data" + std::to_string(i/10), "read" }, e.AddPolicy(params); + + for(auto _ : state) { + params = { "user" + std::to_string(GetRandom10000() + 10000), "data" + std::to_string((GetRandom10000() + 10000) / 10), "read" }, e.AddPolicy(params); + } +} + +BENCHMARK(BenchmarkAddPolicyLarge); + +static void BenchmarkRemovePolicyMedium(benchmark::State& state) { + casbin::Enforcer e(basic_model_path); + + // 1000 roles, 100 resources. + for(int i = 0; i < 1000; ++i) + params = {"user" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); + + for(auto _ : state) + params = { "user" + std::to_string(GetRandom1000()), "data" + std::to_string(GetRandom1000() / 10), "read" }, e.RemovePolicy(params); +} + +BENCHMARK(BenchmarkRemovePolicyMedium); + +static void BenchmarkRemovePolicyLarge(benchmark::State& state) { + casbin::Enforcer e(basic_model_path); + + // 10000 roles, 1000 resources. + for(int i = 0; i < 10000; ++i) + params = { "user" + std::to_string(i), "data" + std::to_string(i / 10), "read" }, e.AddPolicy(params); + + for(auto _ : state) + params = { "user" + std::to_string(GetRandom10000()), "data" + std::to_string(GetRandom1000()), "read" }, e.RemovePolicy(params); +} + +BENCHMARK(BenchmarkRemovePolicyLarge); \ No newline at end of file diff --git a/tests/benchmarks/model_b.cpp b/tests/benchmarks/model_b.cpp index e603e8d9..43684321 100644 --- a/tests/benchmarks/model_b.cpp +++ b/tests/benchmarks/model_b.cpp @@ -76,43 +76,6 @@ static void BenchmarkRBACModelSmall(benchmark::State& state) { BENCHMARK(BenchmarkRBACModelSmall); -static void BenchmarkRBACModelMedium(benchmark::State& state) { - casbin::Enforcer e(rbac_model_path); - - // 1000 roles, 100 resources. - for (int i = 0; i < 1000; ++i) - e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}); - - // 10000 users. - for (int i = 0; i < 10000; ++i) - e.AddGroupingPolicy({"user" + std::to_string(i), "group" + std::to_string(i / 10)}); - - casbin::DataList params = {"user5001", "data99", "read"}; - for (auto _ : state) - e.Enforce(params); -} - -BENCHMARK(BenchmarkRBACModelMedium); - -static void BenchmarkRBACModelLarge(benchmark::State& state) { - casbin::Enforcer e(rbac_model_path); - - // 10000 roles, 1000 resources. - for(int i = 0; i < 10000; ++i) - e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}); - - // 100000 users. - for(int i = 0; i < 100000; i++) - e.AddGroupingPolicy({"user" + std::to_string(i), "group" + std::to_string(i / 10)}); - - casbin::DataList params = {"user50001", "data999", "read"}; - - for(auto _ : state) - e.Enforce(params); -} - -// BENCHMARK(BenchmarkRBACModelLarge); - static void BenchmarkRBACModelWithResourceRoles(benchmark::State& state) { casbin::Enforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path); casbin::DataList params = {"alice", "data1", "read"}; @@ -120,7 +83,7 @@ static void BenchmarkRBACModelWithResourceRoles(benchmark::State& state) { e.Enforce(params); } -// BENCHMARK(BenchmarkRBACModelWithResourceRoles); +BENCHMARK(BenchmarkRBACModelWithResourceRoles); static void BenchmarkRBACModelWithDomains(benchmark::State& state) { casbin::Enforcer e(rbac_with_domains_model_path, rbac_with_domains_policy_path); diff --git a/tests/benchmarks/model_b_inten.cpp b/tests/benchmarks/model_b_inten.cpp new file mode 100644 index 00000000..733b3075 --- /dev/null +++ b/tests/benchmarks/model_b_inten.cpp @@ -0,0 +1,59 @@ +/* +* Copyright 2021 The casbin Authors. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* This is an intensive test file for benchmarking the performance of casbin::Model +*/ + +#include +#include +#include "config_path.h" + +static const std::vector> s_policy = { {"alice", "data1", "read"}, {"bob", "data2", "write"} }; +static void BenchmarkRBACModelMedium(benchmark::State& state) { + casbin::Enforcer e(rbac_model_path); + + // 1000 roles, 100 resources. + for (int i = 0; i < 1000; ++i) + e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}); + + // 10000 users. + for (int i = 0; i < 10000; ++i) + e.AddGroupingPolicy({"user" + std::to_string(i), "group" + std::to_string(i / 10)}); + + casbin::DataList params = {"user5001", "data99", "read"}; + for (auto _ : state) + e.Enforce(params); +} + +BENCHMARK(BenchmarkRBACModelMedium); + +static void BenchmarkRBACModelLarge(benchmark::State& state) { + casbin::Enforcer e(rbac_model_path); + + // 10000 roles, 1000 resources. + for(int i = 0; i < 10000; ++i) + e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}); + + // 100000 users. + for(int i = 0; i < 100000; i++) + e.AddGroupingPolicy({"user" + std::to_string(i), "group" + std::to_string(i / 10)}); + + casbin::DataList params = {"user50001", "data999", "read"}; + + for(auto _ : state) + e.Enforce(params); +} + +BENCHMARK(BenchmarkRBACModelLarge); \ No newline at end of file diff --git a/tests/benchmarks/role_manager_b.cpp b/tests/benchmarks/role_manager_b.cpp index 519265f9..f6dc4d45 100644 --- a/tests/benchmarks/role_manager_b.cpp +++ b/tests/benchmarks/role_manager_b.cpp @@ -45,53 +45,3 @@ static void BenchmarkRoleManagerSmall(benchmark::State& state) { } BENCHMARK(BenchmarkRoleManagerSmall); - -static void BenchmarkRoleManagerMedium(benchmark::State& state) { - casbin::Enforcer e(rbac_model_path); - // Do not rebuild the role inheritance relations for every AddGroupingPolicy() call. - e.EnableAutoBuildRoleLinks(false); - - // 1000 roles, 100 resources. - - for (int i = 0; i < 1000; ++i) - params = {"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); - - // 10000 users. - - for (int i = 0; i < 10000; ++i) - g_params = {"user" + std::to_string(i), "group" + std::to_string(i / 10)}, e.AddGroupingPolicy(g_params); - - e.BuildRoleLinks(); - - auto rm = e.GetRoleManager(); - - for(auto _ : state) { - for(int j = 0; j < 1000; ++j) - rm->HasLink("user501", "group" + std::to_string(j)); - } -} - -BENCHMARK(BenchmarkRoleManagerMedium); - -static void BenchmarkRoleManagerLarge(benchmark::State& state) { - casbin::Enforcer e(rbac_model_path); - - // 10000 roles, 1000 resources. - - for (int i = 0; i < 10000; ++i) - params = {"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); - - // 100000 users. - - for (int i = 0; i < 100000; ++i) - g_params = {"user" + std::to_string(i), "group" + std::to_string(i / 10)}, e.AddGroupingPolicy(g_params); - - auto rm = e.GetRoleManager(); - - for(auto _ : state) { - for(int j = 0; j < 10000; ++j) - rm->HasLink("user501", "group" + std::to_string(j)); - } -} - -// BENCHMARK(BenchmarkRoleManagerLarge); diff --git a/tests/benchmarks/role_manager_b_inten.cpp b/tests/benchmarks/role_manager_b_inten.cpp new file mode 100644 index 00000000..6529e411 --- /dev/null +++ b/tests/benchmarks/role_manager_b_inten.cpp @@ -0,0 +1,74 @@ +/* +* Copyright 2021 The casbin Authors. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* +* This is an intensive test file for benchmarking the performance of casbin::RoleManager +*/ + +#include +#include +#include "config_path.h" + +static std::vector params(3); +static std::vector g_params(2); + +static void BenchmarkRoleManagerMedium(benchmark::State& state) { + casbin::Enforcer e(rbac_model_path); + // Do not rebuild the role inheritance relations for every AddGroupingPolicy() call. + e.EnableAutoBuildRoleLinks(false); + + // 1000 roles, 100 resources. + + for (int i = 0; i < 1000; ++i) + params = {"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); + + // 10000 users. + + for (int i = 0; i < 10000; ++i) + g_params = {"user" + std::to_string(i), "group" + std::to_string(i / 10)}, e.AddGroupingPolicy(g_params); + + e.BuildRoleLinks(); + + auto rm = e.GetRoleManager(); + + for(auto _ : state) { + for(int j = 0; j < 1000; ++j) + rm->HasLink("user501", "group" + std::to_string(j)); + } +} + +BENCHMARK(BenchmarkRoleManagerMedium); + +static void BenchmarkRoleManagerLarge(benchmark::State& state) { + casbin::Enforcer e(rbac_model_path); + + // 10000 roles, 1000 resources. + + for (int i = 0; i < 10000; ++i) + params = {"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}, e.AddPolicy(params); + + // 100000 users. + + for (int i = 0; i < 100000; ++i) + g_params = {"user" + std::to_string(i), "group" + std::to_string(i / 10)}, e.AddGroupingPolicy(g_params); + + auto rm = e.GetRoleManager(); + + for(auto _ : state) { + for(int j = 0; j < 10000; ++j) + rm->HasLink("user501", "group" + std::to_string(j)); + } +} + +BENCHMARK(BenchmarkRoleManagerLarge); \ No newline at end of file