|
| 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's Management API |
| 17 | +*/ |
| 18 | + |
| 19 | +#include <random> |
| 20 | +#include <benchmark/benchmark.h> |
| 21 | +#include <casbin/casbin.h> |
| 22 | +#include "config_path.h" |
| 23 | + |
| 24 | +static std::random_device generator; |
| 25 | +static std::uniform_int_distribution<int> dist_100(1, 100); |
| 26 | +static std::uniform_int_distribution<int> dist_1000(1, 1000); |
| 27 | +static std::uniform_int_distribution<int> dist_10000(1, 10000); |
| 28 | +static std::vector<std::string> params(3); |
| 29 | + |
| 30 | +static inline int GetRandom100() |
| 31 | +{ |
| 32 | + return dist_100(generator); |
| 33 | +} |
| 34 | + |
| 35 | +static inline int GetRandom1000() |
| 36 | +{ |
| 37 | + return dist_1000(generator); |
| 38 | +} |
| 39 | + |
| 40 | +static inline int GetRandom10000() |
| 41 | +{ |
| 42 | + return dist_10000(generator); |
| 43 | +} |
| 44 | + |
| 45 | +static void BenchmarkVectorOperations(benchmark::State& state) { |
| 46 | + for(auto _ : state) |
| 47 | + params = {"user" + std::to_string(GetRandom10000()), "data" + std::to_string(GetRandom10000() / 10), "read"}; |
| 48 | +} |
| 49 | + |
| 50 | +BENCHMARK(BenchmarkVectorOperations); |
| 51 | + |
| 52 | +static void BenchmarkHasPolicySmall(benchmark::State& state) { |
| 53 | + casbin::Enforcer e(basic_model_path); |
| 54 | + |
| 55 | + // 100 roles, 10 resources. |
| 56 | + for (int i = 0; i < 100; ++i) |
| 57 | + params = { "user" + std::to_string(i), "data" + std::to_string(i / 10), "read" }, e.AddPolicy(params); |
| 58 | + |
| 59 | + for (auto _ : state) |
| 60 | + params = { "user" + std::to_string(GetRandom100()), "data" + std::to_string(GetRandom100()/10), "read" }, e.HasPolicy(params); |
| 61 | +} |
| 62 | + |
| 63 | +BENCHMARK(BenchmarkHasPolicySmall); |
| 64 | + |
| 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 | + |
| 93 | +static void BenchmarkAddPolicySmall(benchmark::State& state) { |
| 94 | + casbin::Enforcer e(basic_model_path); |
| 95 | + |
| 96 | + // 100 roles, 10 resources. |
| 97 | + for(int i = 0; i < 100; ++i) |
| 98 | + params = {"user" + std::to_string(i), "data" + std::to_string(i/10), "read"}, e.AddPolicy(params); |
| 99 | + |
| 100 | + for(auto _ : state) |
| 101 | + params = {"user" + std::to_string(GetRandom100() + 100), "data" + std::to_string((GetRandom100() + 100)/10), "read"}, e.AddPolicy(params); |
| 102 | +} |
| 103 | + |
| 104 | +BENCHMARK(BenchmarkAddPolicySmall); |
| 105 | + |
| 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 | + |
| 135 | +static void BenchmarkRemovePolicySmall(benchmark::State& state) { |
| 136 | + casbin::Enforcer e(basic_model_path); |
| 137 | + |
| 138 | + // 100 roles, 10 resources. |
| 139 | + for(int i = 100; i < 100; ++i) |
| 140 | + params = { "user" + std::to_string(i), "data" + std::to_string(i / 10), "read" }, e.AddPolicy(params); |
| 141 | + |
| 142 | + for(auto _ : state) |
| 143 | + params = { "user" + std::to_string(GetRandom100()), "data" + std::to_string(GetRandom100() / 10), "read" }, e.RemovePolicy(params); |
| 144 | +} |
| 145 | + |
| 146 | +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