|
| 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 a test file for benchmarking the performance of casbin::Model |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <benchmark/benchmark.h> |
| 20 | +#include <casbin/casbin.h> |
| 21 | +#include "config_path.h" |
| 22 | + |
| 23 | +static const std::vector<std::vector<std::string>> s_policy = { {"alice", "data1", "read"}, {"bob", "data2", "write"} }; |
| 24 | + |
| 25 | +static bool rawEnforce(const std::string& sub, const std::string& obj, const std::string& act) { |
| 26 | + for(const auto& rule : s_policy) { |
| 27 | + if(rule[0] == sub && rule[1] == obj && rule[2] == act) |
| 28 | + return true; |
| 29 | + } |
| 30 | + return false; |
| 31 | +} |
| 32 | + |
| 33 | +static void BenchmarkRaw(benchmark::State& state) { |
| 34 | + for(auto _ : state) |
| 35 | + rawEnforce("alice", "data1", "read"); |
| 36 | +} |
| 37 | + |
| 38 | +BENCHMARK(BenchmarkRaw); |
| 39 | + |
| 40 | +static void BenchmarkBasicModel(benchmark::State& state) { |
| 41 | + casbin::Enforcer e(basic_model_path, basic_policy_path); |
| 42 | + casbin::DataList params = {"alice", "data1", "read"}; |
| 43 | + |
| 44 | + for(auto _ : state) |
| 45 | + e.Enforce(params); |
| 46 | +} |
| 47 | + |
| 48 | +BENCHMARK(BenchmarkBasicModel); |
| 49 | + |
| 50 | +static void BenchmarkRBACModel(benchmark::State& state) { |
| 51 | + casbin::Enforcer e(rbac_model_path, rbac_policy_path); |
| 52 | + |
| 53 | + casbin::DataList params = {"alice", "data2", "read"}; |
| 54 | + |
| 55 | + for (auto _ : state) |
| 56 | + e.Enforce(params); |
| 57 | +} |
| 58 | + |
| 59 | +BENCHMARK(BenchmarkRBACModel); |
| 60 | + |
| 61 | +static void BenchmarkRBACModelSmall(benchmark::State& state) { |
| 62 | + casbin::Enforcer e(rbac_model_path); |
| 63 | + |
| 64 | + // 100 roles, 10 resources. |
| 65 | + for(int i = 0; i < 100; ++i) |
| 66 | + e.AddPolicy({ "group" + std::to_string(i), "data" + std::to_string(i/10), "read" }); |
| 67 | + |
| 68 | + // 1000 users. |
| 69 | + for(int i = 0; i < 1000; ++i) |
| 70 | + e.AddGroupingPolicy({ "user" + std::to_string(i), "group" + std::to_string(i / 10) }); |
| 71 | + |
| 72 | + casbin::DataList params = {"user501", "data9", "read"}; |
| 73 | + for (auto _ : state) |
| 74 | + e.Enforce(params); |
| 75 | +} |
| 76 | + |
| 77 | +BENCHMARK(BenchmarkRBACModelSmall); |
| 78 | + |
| 79 | +static void BenchmarkRBACModelMedium(benchmark::State& state) { |
| 80 | + casbin::Enforcer e(rbac_model_path); |
| 81 | + |
| 82 | + // 1000 roles, 100 resources. |
| 83 | + for (int i = 0; i < 1000; ++i) |
| 84 | + e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}); |
| 85 | + |
| 86 | + // 10000 users. |
| 87 | + for (int i = 0; i < 10000; ++i) |
| 88 | + e.AddGroupingPolicy({"user" + std::to_string(i), "group" + std::to_string(i / 10)}); |
| 89 | + |
| 90 | + casbin::DataList params = {"user5001", "data99", "read"}; |
| 91 | + for (auto _ : state) |
| 92 | + e.Enforce(params); |
| 93 | +} |
| 94 | + |
| 95 | +BENCHMARK(BenchmarkRBACModelMedium); |
| 96 | + |
| 97 | +static void BenchmarkRBACModelLarge(benchmark::State& state) { |
| 98 | + casbin::Enforcer e(rbac_model_path); |
| 99 | + |
| 100 | + // 10000 roles, 1000 resources. |
| 101 | + for(int i = 0; i < 10000; ++i) |
| 102 | + e.AddPolicy({"group" + std::to_string(i), "data" + std::to_string(i / 10), "read"}); |
| 103 | + |
| 104 | + // 100000 users. |
| 105 | + for(int i = 0; i < 100000; i++) |
| 106 | + e.AddGroupingPolicy({"user" + std::to_string(i), "group" + std::to_string(i / 10)}); |
| 107 | + |
| 108 | + casbin::DataList params = {"user50001", "data999", "read"}; |
| 109 | + |
| 110 | + for(auto _ : state) |
| 111 | + e.Enforce(params); |
| 112 | +} |
| 113 | + |
| 114 | +// BENCHMARK(BenchmarkRBACModelLarge); |
| 115 | + |
| 116 | +static void BenchmarkRBACModelWithResourceRoles(benchmark::State& state) { |
| 117 | + casbin::Enforcer e(rbac_with_resource_roles_model_path, rbac_with_resource_roles_policy_path); |
| 118 | + casbin::DataList params = {"alice", "data1", "read"}; |
| 119 | + for (auto _ : state) |
| 120 | + e.Enforce(params); |
| 121 | +} |
| 122 | + |
| 123 | +// BENCHMARK(BenchmarkRBACModelWithResourceRoles); |
| 124 | + |
| 125 | +static void BenchmarkRBACModelWithDomains(benchmark::State& state) { |
| 126 | + casbin::Enforcer e(rbac_with_domains_model_path, rbac_with_domains_policy_path); |
| 127 | + casbin::DataList params = {"alice", "domain1", "data1", "read"}; |
| 128 | + |
| 129 | + for(auto _ : state) |
| 130 | + e.Enforce(params); |
| 131 | +} |
| 132 | + |
| 133 | +BENCHMARK(BenchmarkRBACModelWithDomains); |
| 134 | + |
| 135 | +// ------ TODO ------ |
| 136 | +// static void BenchmarkABACModel(benchmark::State& state) { |
| 137 | +// casbin::Enforcer e("examples/abac_model.conf") |
| 138 | +// data1 := newTestResource("data1", "alice") |
| 139 | + |
| 140 | + |
| 141 | +// for(auto _ : state) { |
| 142 | +// _, _ = e.Enforce("alice", data1, "read") |
| 143 | +// } |
| 144 | +// } |
| 145 | + |
| 146 | +static void BenchmarkKeyMatchModel(benchmark::State& state) { |
| 147 | + casbin::Enforcer e(keymatch_model_path, keymatch_policy_path); |
| 148 | + casbin::DataList params = {"alice", "/alice_data/resource1", "GET"}; |
| 149 | + |
| 150 | + for (auto _ : state) |
| 151 | + e.Enforce(params); |
| 152 | +} |
| 153 | + |
| 154 | +BENCHMARK(BenchmarkKeyMatchModel); |
| 155 | + |
| 156 | +static void BenchmarkRBACModelWithDeny(benchmark::State& state) { |
| 157 | + casbin::Enforcer e(rbac_with_deny_model_path, rbac_with_deny_policy_path); |
| 158 | + casbin::DataList params = {"alice", "data1", "read"}; |
| 159 | + |
| 160 | + for(auto _ : state) |
| 161 | + e.Enforce(params); |
| 162 | +} |
| 163 | + |
| 164 | +BENCHMARK(BenchmarkRBACModelWithDeny); |
| 165 | + |
| 166 | +static void BenchmarkPriorityModel(benchmark::State& state) { |
| 167 | + casbin::Enforcer e(priority_model_path, priority_policy_path); |
| 168 | + casbin::DataList params = {"alice", "data1", "read"}; |
| 169 | + |
| 170 | + for(auto _ : state) |
| 171 | + e.Enforce(params); |
| 172 | +} |
| 173 | + |
| 174 | +BENCHMARK(BenchmarkPriorityModel); |
0 commit comments