Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add a benchmark tests option #162

Merged
merged 5 commits into from
Nov 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion tests/benchmarks/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down
68 changes: 0 additions & 68 deletions tests/benchmarks/enforcer_cached_b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<std::string>> 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<std::vector<std::string>> 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<std::vector<std::string>> 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<std::vector<std::string>> 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);

Expand Down Expand Up @@ -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<std::vector<std::string>> 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<std::vector<std::string>> 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);

88 changes: 88 additions & 0 deletions tests/benchmarks/enforcer_cached_b_inten.cpp
Original file line number Diff line number Diff line change
@@ -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 <benchmark/benchmark.h>
#include <casbin/casbin.h>
#include "config_path.h"

static void BenchmarkCachedRBACModelMedium(benchmark::State& state) {
casbin::CachedEnforcer e(rbac_model_path, "", false);
std::vector<std::vector<std::string>> 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<std::vector<std::string>> 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<std::vector<std::string>> 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<std::vector<std::string>> 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<std::vector<std::string>> 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<std::vector<std::string>> 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);
83 changes: 0 additions & 83 deletions tests/benchmarks/management_api_b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::vector<std::string>> 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);

Expand All @@ -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);

Expand All @@ -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);
Loading