From 192215a1b1fa036568f0e6400062d1bfd36f7a64 Mon Sep 17 00:00:00 2001 From: noob20000405 Date: Mon, 1 Nov 2021 13:39:32 +0100 Subject: [PATCH 1/2] fix: expedite benchmark tests Signed-off-by: noob20000405 --- tests/benchmarks/enforcer_cached_b.cpp | 50 +------------- tests/benchmarks/management_api_b.cpp | 94 ++------------------------ tests/benchmarks/model_b.cpp | 43 +----------- tests/benchmarks/role_manager_b.cpp | 54 +-------------- 4 files changed, 13 insertions(+), 228 deletions(-) diff --git a/tests/benchmarks/enforcer_cached_b.cpp b/tests/benchmarks/enforcer_cached_b.cpp index 70bd9bfe..3135eadf 100644 --- a/tests/benchmarks/enforcer_cached_b.cpp +++ b/tests/benchmarks/enforcer_cached_b.cpp @@ -47,7 +47,7 @@ BENCHMARK(BenchmarkCachedRBACModel); // BENCHMARK(BenchmarkCachedRaw); -static void BenchmarkCachedRBACModelSmall(benchmark::State& state) { +static void BenchmarkCachedRBACModelLoop(benchmark::State& state) { casbin::CachedEnforcer e(rbac_model_path, "", false); // 100 roles, 10 resources. for (int i = 0; i < 100; ++i) @@ -60,53 +60,7 @@ static void BenchmarkCachedRBACModelSmall(benchmark::State& state) { e.Enforce(params); } -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); +BENCHMARK(BenchmarkCachedRBACModelLoop); static void BenchmarkCachedRBACModelWithResourceRoles(benchmark::State& state) { casbin::CachedEnforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path, false); diff --git a/tests/benchmarks/management_api_b.cpp b/tests/benchmarks/management_api_b.cpp index 58605fcf..29c35cd1 100644 --- a/tests/benchmarks/management_api_b.cpp +++ b/tests/benchmarks/management_api_b.cpp @@ -49,7 +49,7 @@ static void BenchmarkVectorOperations(benchmark::State& state) { BENCHMARK(BenchmarkVectorOperations); -static void BenchmarkHasPolicySmall(benchmark::State& state) { +static void BenchmarkHasPolicyLoop(benchmark::State& state) { casbin::Enforcer e(basic_model_path); // 100 roles, 10 resources. @@ -60,37 +60,9 @@ static void BenchmarkHasPolicySmall(benchmark::State& state) { params = { "user" + std::to_string(GetRandom100()), "data" + std::to_string(GetRandom100()/10), "read" }, e.HasPolicy(params); } -BENCHMARK(BenchmarkHasPolicySmall); +BENCHMARK(BenchmarkHasPolicyLoop); -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) { +static void BenchmarkAddPolicyLoop(benchmark::State& state) { casbin::Enforcer e(basic_model_path); // 100 roles, 10 resources. @@ -101,38 +73,9 @@ static void BenchmarkAddPolicySmall(benchmark::State& state) { params = {"user" + std::to_string(GetRandom100() + 100), "data" + std::to_string((GetRandom100() + 100)/10), "read"}, e.AddPolicy(params); } -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); +BENCHMARK(BenchmarkAddPolicyLoop); -static void BenchmarkRemovePolicySmall(benchmark::State& state) { +static void BenchmarkRemovePolicyLoop(benchmark::State& state) { casbin::Enforcer e(basic_model_path); // 100 roles, 10 resources. @@ -143,30 +86,5 @@ static void BenchmarkRemovePolicySmall(benchmark::State& state) { params = { "user" + std::to_string(GetRandom100()), "data" + std::to_string(GetRandom100() / 10), "read" }, e.RemovePolicy(params); } -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(BenchmarkRemovePolicyLoop); -BENCHMARK(BenchmarkRemovePolicyLarge); diff --git a/tests/benchmarks/model_b.cpp b/tests/benchmarks/model_b.cpp index e603e8d9..99d50ad7 100644 --- a/tests/benchmarks/model_b.cpp +++ b/tests/benchmarks/model_b.cpp @@ -58,7 +58,7 @@ static void BenchmarkRBACModel(benchmark::State& state) { BENCHMARK(BenchmarkRBACModel); -static void BenchmarkRBACModelSmall(benchmark::State& state) { +static void BenchmarkRBACModelLoop(benchmark::State& state) { casbin::Enforcer e(rbac_model_path); // 100 roles, 10 resources. @@ -74,44 +74,7 @@ static void BenchmarkRBACModelSmall(benchmark::State& state) { e.Enforce(params); } -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); +BENCHMARK(BenchmarkRBACModelLoop); static void BenchmarkRBACModelWithResourceRoles(benchmark::State& state) { casbin::Enforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path); @@ -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/role_manager_b.cpp b/tests/benchmarks/role_manager_b.cpp index 519265f9..0df13528 100644 --- a/tests/benchmarks/role_manager_b.cpp +++ b/tests/benchmarks/role_manager_b.cpp @@ -23,7 +23,7 @@ static std::vector params(3); static std::vector g_params(2); -static void BenchmarkRoleManagerSmall(benchmark::State& state) { +static void BenchmarkRoleManagerLoop(benchmark::State& state) { casbin::Enforcer e(rbac_model_path); // Do not rebuild the role inheritance relations for every AddGroupingPolicy() call. e.EnableAutoBuildRoleLinks(false); @@ -44,54 +44,4 @@ 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); +BENCHMARK(BenchmarkRoleManagerLoop); \ No newline at end of file From 9105df56a6b28bf7ed55cca2ccca27e9917573d0 Mon Sep 17 00:00:00 2001 From: noob20000405 Date: Mon, 1 Nov 2021 20:36:51 +0100 Subject: [PATCH 2/2] fix: expedite benchmark tests --- casbin/rbac/default_role_manager.cpp | 36 +++++----- casbin/rbac/default_role_manager.h | 15 ++-- tests/benchmarks/enforcer_cached_b.cpp | 50 +++++++++++++- tests/benchmarks/management_api_b.cpp | 95 ++++++++++++++++++++++++-- tests/benchmarks/model_b.cpp | 41 ++++++++++- tests/benchmarks/role_manager_b.cpp | 54 ++++++++++++++- 6 files changed, 254 insertions(+), 37 deletions(-) diff --git a/casbin/rbac/default_role_manager.cpp b/casbin/rbac/default_role_manager.cpp index f902e70e..11d70467 100644 --- a/casbin/rbac/default_role_manager.cpp +++ b/casbin/rbac/default_role_manager.cpp @@ -25,13 +25,13 @@ namespace casbin { -Role* Role :: NewRole(std::string name) { - Role* role = new Role; +std::shared_ptr Role :: NewRole(std::string name) { + auto role = std::make_shared(); role->name = name; return role; } -void Role :: AddRole(Role* role) { +void Role :: AddRole(std::shared_ptr role) { for (int i = 0 ; i < this->roles.size() ; i++) { if (this->roles[i]->name == role->name) return; @@ -40,7 +40,7 @@ void Role :: AddRole(Role* role) { this->roles.push_back(role); } -void Role :: DeleteRole(Role* role) { +void Role :: DeleteRole(std::shared_ptr role) { for (int i = 0; i < roles.size();i++) { if (roles[i]->name == role->name) roles.erase(roles.begin()+i); @@ -80,7 +80,7 @@ std::string Role :: ToString() { names += "("; for (int i = 0; i < roles.size(); i ++) { - Role* role = roles[i]; + auto role = roles[i]; if (i == 0) names += role->name; else @@ -104,7 +104,7 @@ std::vector Role :: GetRoles() { bool DefaultRoleManager :: HasRole(std::string name) { bool ok = false; if (this->has_pattern){ - for (std::unordered_map :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){ + for (auto it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){ if (this->matching_func(name, it->first)) ok = true; } @@ -115,8 +115,8 @@ bool DefaultRoleManager :: HasRole(std::string name) { return ok; } -Role* DefaultRoleManager :: CreateRole(std::string name) { - Role* role; +std::shared_ptr DefaultRoleManager :: CreateRole(std::string name) { + std::shared_ptr role; bool ok = this->all_roles.find(name) != this->all_roles.end(); if (!ok) { all_roles[name] = Role :: NewRole(name); @@ -125,9 +125,9 @@ Role* DefaultRoleManager :: CreateRole(std::string name) { role = all_roles[name]; if (this->has_pattern) { - for (std::unordered_map :: iterator it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){ + for (auto it = this->all_roles.begin() ; it != this->all_roles.end() ; it++){ if (this->matching_func(name, it->first) && name!=it->first) { - Role* role1; + std::shared_ptr role1; bool ok1 = this->all_roles.find(it->first) != this->all_roles.end(); if (!ok1) { all_roles[it->first] = Role :: NewRole(it->first); @@ -178,8 +178,8 @@ void DefaultRoleManager :: AddLink(std::string name1, std::string name2, std::ve } else if (domain.size() > 1) throw CasbinRBACException("error: domain should be 1 parameter"); - Role* role1 = this->CreateRole(name1); - Role* role2 = this->CreateRole(name2); + auto role1 = this->CreateRole(name1); + auto role2 = this->CreateRole(name2); role1->AddRole(role2); } @@ -199,8 +199,8 @@ void DefaultRoleManager :: DeleteLink(std::string name1, std::string name2, std: if (!HasRole(name1) || !HasRole(name2)) throw CasbinRBACException("error: name1 or name2 does not exist"); - Role* role1 = this->CreateRole(name1); - Role* role2 = this->CreateRole(name2); + auto role1 = this->CreateRole(name1); + auto role2 = this->CreateRole(name2); role1->DeleteRole(role2); } @@ -221,7 +221,7 @@ bool DefaultRoleManager :: HasLink(std::string name1, std::string name2, std::ve if (!HasRole(name1) || !HasRole(name2)) return false; - Role* role1 = this->CreateRole(name1); + auto role1 = this->CreateRole(name1); return role1->HasRole(name2, max_hierarchy_level); } @@ -260,8 +260,8 @@ std::vector DefaultRoleManager :: GetUsers(std::string name, std::v throw CasbinRBACException("error: name does not exist"); std::vector names; - for (std::unordered_map::iterator it = this->all_roles.begin(); it != this->all_roles.end(); it++) { - Role* role = it->second; + for (auto it = this->all_roles.begin(); it != this->all_roles.end(); it++) { + auto role = it->second; if (role->HasDirectRole(name)) names.push_back(role->name); } @@ -285,7 +285,7 @@ void DefaultRoleManager :: PrintRoles() { // LogUtil::SetLogger(*logger); std::string text = this->all_roles.begin()->second->ToString(); - std::unordered_map :: iterator it = this->all_roles.begin(); + auto it = this->all_roles.begin(); it++; for ( ; it != this->all_roles.end() ; it++) text += ", " + it->second->ToString(); diff --git a/casbin/rbac/default_role_manager.h b/casbin/rbac/default_role_manager.h index 39b14b13..e8293323 100644 --- a/casbin/rbac/default_role_manager.h +++ b/casbin/rbac/default_role_manager.h @@ -1,3 +1,4 @@ + /* * Copyright 2020 The casbin Authors. All Rights Reserved. * @@ -31,16 +32,16 @@ typedef bool (*MatchingFunc)(const std::string&, const std::string&); class Role { private: - std::vector roles; + std::vector> roles; public: std::string name; - static Role* NewRole(std::string name); + static std::shared_ptr NewRole(std::string name); - void AddRole(Role* role); + void AddRole(std::shared_ptr role); - void DeleteRole(Role* role); + void DeleteRole(std::shared_ptr role); bool HasRole(std::string name, int hierarchy_level); @@ -53,14 +54,14 @@ class Role { class DefaultRoleManager : public RoleManager { private: - std::unordered_map all_roles; + std::unordered_map> all_roles; bool has_pattern; int max_hierarchy_level; MatchingFunc matching_func; bool HasRole(std::string name); - Role* CreateRole(std::string name); + std::shared_ptr CreateRole(std::string name); public: @@ -116,4 +117,4 @@ class DefaultRoleManager : public RoleManager { }; // namespace casbin -#endif \ No newline at end of file +#endif diff --git a/tests/benchmarks/enforcer_cached_b.cpp b/tests/benchmarks/enforcer_cached_b.cpp index 3135eadf..70bd9bfe 100644 --- a/tests/benchmarks/enforcer_cached_b.cpp +++ b/tests/benchmarks/enforcer_cached_b.cpp @@ -47,7 +47,7 @@ BENCHMARK(BenchmarkCachedRBACModel); // BENCHMARK(BenchmarkCachedRaw); -static void BenchmarkCachedRBACModelLoop(benchmark::State& state) { +static void BenchmarkCachedRBACModelSmall(benchmark::State& state) { casbin::CachedEnforcer e(rbac_model_path, "", false); // 100 roles, 10 resources. for (int i = 0; i < 100; ++i) @@ -60,7 +60,53 @@ static void BenchmarkCachedRBACModelLoop(benchmark::State& state) { e.Enforce(params); } -BENCHMARK(BenchmarkCachedRBACModelLoop); +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); diff --git a/tests/benchmarks/management_api_b.cpp b/tests/benchmarks/management_api_b.cpp index 29c35cd1..e508464c 100644 --- a/tests/benchmarks/management_api_b.cpp +++ b/tests/benchmarks/management_api_b.cpp @@ -49,7 +49,7 @@ static void BenchmarkVectorOperations(benchmark::State& state) { BENCHMARK(BenchmarkVectorOperations); -static void BenchmarkHasPolicyLoop(benchmark::State& state) { +static void BenchmarkHasPolicySmall(benchmark::State& state) { casbin::Enforcer e(basic_model_path); // 100 roles, 10 resources. @@ -60,9 +60,37 @@ static void BenchmarkHasPolicyLoop(benchmark::State& state) { params = { "user" + std::to_string(GetRandom100()), "data" + std::to_string(GetRandom100()/10), "read" }, e.HasPolicy(params); } -BENCHMARK(BenchmarkHasPolicyLoop); +BENCHMARK(BenchmarkHasPolicySmall); -static void BenchmarkAddPolicyLoop(benchmark::State& state) { +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); // 100 roles, 10 resources. @@ -73,9 +101,38 @@ static void BenchmarkAddPolicyLoop(benchmark::State& state) { params = {"user" + std::to_string(GetRandom100() + 100), "data" + std::to_string((GetRandom100() + 100)/10), "read"}, e.AddPolicy(params); } -BENCHMARK(BenchmarkAddPolicyLoop); +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 BenchmarkRemovePolicyLoop(benchmark::State& state) { +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); // 100 roles, 10 resources. @@ -86,5 +143,31 @@ static void BenchmarkRemovePolicyLoop(benchmark::State& state) { params = { "user" + std::to_string(GetRandom100()), "data" + std::to_string(GetRandom100() / 10), "read" }, e.RemovePolicy(params); } -BENCHMARK(BenchmarkRemovePolicyLoop); +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/model_b.cpp b/tests/benchmarks/model_b.cpp index 99d50ad7..1798bb8a 100644 --- a/tests/benchmarks/model_b.cpp +++ b/tests/benchmarks/model_b.cpp @@ -58,7 +58,7 @@ static void BenchmarkRBACModel(benchmark::State& state) { BENCHMARK(BenchmarkRBACModel); -static void BenchmarkRBACModelLoop(benchmark::State& state) { +static void BenchmarkRBACModelSmall(benchmark::State& state) { casbin::Enforcer e(rbac_model_path); // 100 roles, 10 resources. @@ -74,7 +74,44 @@ static void BenchmarkRBACModelLoop(benchmark::State& state) { e.Enforce(params); } -BENCHMARK(BenchmarkRBACModelLoop); +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); diff --git a/tests/benchmarks/role_manager_b.cpp b/tests/benchmarks/role_manager_b.cpp index 0df13528..03333ec7 100644 --- a/tests/benchmarks/role_manager_b.cpp +++ b/tests/benchmarks/role_manager_b.cpp @@ -23,7 +23,7 @@ static std::vector params(3); static std::vector g_params(2); -static void BenchmarkRoleManagerLoop(benchmark::State& state) { +static void BenchmarkRoleManagerSmall(benchmark::State& state) { casbin::Enforcer e(rbac_model_path); // Do not rebuild the role inheritance relations for every AddGroupingPolicy() call. e.EnableAutoBuildRoleLinks(false); @@ -44,4 +44,54 @@ static void BenchmarkRoleManagerLoop(benchmark::State& state) { } } -BENCHMARK(BenchmarkRoleManagerLoop); \ No newline at end of file +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);