From d8757ff4f50603a487663267d026c977a867a858 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 28 Dec 2025 09:15:19 +0000 Subject: [PATCH 1/2] Initial plan From 38e945052098869ad55d20283fce6e94a81c2ef7 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 28 Dec 2025 09:23:47 +0000 Subject: [PATCH 2/2] Remove old benchmark tests and add new ported tests from Go Casbin - Removed all existing jCasbin benchmark tests - Created EnforcerBenchmarkTest.java (ported from model_b_test.go) - Created CachedEnforcerBenchmarkTest.java (ported from enforcer_cached_b_test.go) - Created ManagementApiBenchmarkTest.java (ported from management_api_b_test.go) - Created RoleManagerBenchmarkTest.java (ported from role_manager_b_test.go) - All tests follow JMH benchmark structure and compile successfully Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com> --- .../main/benchmark/BenchmarkABACModel.java | 53 ---- .../main/benchmark/BenchmarkAllModelApi.java | 189 -------------- .../main/benchmark/BenchmarkBasicModel.java | 51 ---- .../benchmark/BenchmarkKeyMatchModel.java | 51 ---- .../benchmark/BenchmarkPriorityModel.java | 51 ---- .../benchmark/BenchmarkRBACModelLarge.java | 64 ----- .../benchmark/BenchmarkRBACModelMedium.java | 64 ----- .../benchmark/BenchmarkRBACModelSingle.java | 51 ---- .../benchmark/BenchmarkRBACModelSmall.java | 64 ----- .../benchmark/BenchmarkRBACModelWithDeny.java | 51 ---- .../BenchmarkRBACModelWithDomains.java | 51 ---- .../BenchmarkRBACModelWithResourceRoles.java | 51 ---- .../main/benchmark/BenchmarkRoleManager.java | 155 ----------- .../CachedEnforcerBenchmarkTest.java | 238 +++++++++++++++++ .../main/benchmark/EnforcerBenchmarkTest.java | 245 ++++++++++++++++++ .../benchmark/ManagementApiBenchmarkTest.java | 190 ++++++++++++++ .../benchmark/RoleManagerBenchmarkTest.java | 211 +++++++++++++++ 17 files changed, 884 insertions(+), 946 deletions(-) delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkABACModel.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkAllModelApi.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkBasicModel.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkKeyMatchModel.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkPriorityModel.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelLarge.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelMedium.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelSingle.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelSmall.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithDeny.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithDomains.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithResourceRoles.java delete mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRoleManager.java create mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/CachedEnforcerBenchmarkTest.java create mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/EnforcerBenchmarkTest.java create mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/ManagementApiBenchmarkTest.java create mode 100644 src/test/java/org/casbin/jcasbin/main/benchmark/RoleManagerBenchmarkTest.java diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkABACModel.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkABACModel.java deleted file mode 100644 index ccc16629..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkABACModel.java +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.casbin.jcasbin.main.ModelUnitTest; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkABACModel { - private static Enforcer e = new Enforcer("examples/abac_model.conf", "",false); - private static ModelUnitTest.TestResource data1 = new ModelUnitTest.TestResource("data1", "alice"); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkABACModel.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(3) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkABACModel() { - for (int i = 0; i < 1000; i++) { - e.enforce("alice", data1, "read"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkAllModelApi.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkAllModelApi.java deleted file mode 100644 index 02479a22..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkAllModelApi.java +++ /dev/null @@ -1,189 +0,0 @@ -// Copyright 2022 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -/** - * A Simple and comprehensive Benchmark without info of GC. - * If you want see more info of result, please use other files in the same directory - * - * @author imp2002 - * @date 2022-07-12 8:45 - */ - -@BenchmarkMode(Mode.AverageTime) -@Warmup(iterations = 3) -@Measurement(iterations = 3) -@Threads(1) -@Fork(1) -@State(value = Scope.Benchmark) -@OutputTimeUnit(TimeUnit.NANOSECONDS) -public class BenchmarkAllModelApi { - public static Enforcer smallEnforcer; - public static Enforcer mediumEnforcer; - public static Enforcer largeEnforcer; - public static Enforcer enforcerForAdd; - - public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(org.casbin.jcasbin.main.benchmark.BenchmarkAllModelApi.class.getName()) - .exclude("Pref") - .exclude("random") - .build(); - new Runner(opt).run(); - } - - static { - smallEnforcer = new Enforcer("examples/rbac_model.conf", "", false); - mediumEnforcer = new Enforcer("examples/rbac_model.conf", "", false); - largeEnforcer = new Enforcer("examples/rbac_model.conf", "", false); - enforcerForAdd = new Enforcer("examples/rbac_model.conf", "", false); - - // 100 roles, 10 resources. - for (int i = 0; i < 100; i++) { - smallEnforcer.addPolicy("user" + i, "data" + i / 10, "read"); - } - // 1000 roles, 100 resources. - for (int i = 0; i < 1000; i++) { - mediumEnforcer.addPolicy("user" + i, "data" + i / 10, "read"); - } - // 10000 roles, 1000 resources. - for (int i = 0; i < 10000; i++) { - largeEnforcer.addPolicy("user" + i, "data" + i / 10, "read"); - } - } - - @Benchmark - public static void addPolicySmall() { - for (int i = 0; i < 100; i++) { - enforcerForAdd.addPolicy("user" + i, "data" + (int) (Math.random() * 50), "read"); - } - } - - @Benchmark - public static void addPolicyMedium() { - for (int i = 0; i < 1000; i++) { - enforcerForAdd.addPolicy("user" + i, "data" + (int) (Math.random() * 500), "read"); - } - } - - @Benchmark - public static void addPolicyLarge() { - for (int i = 0; i < 10000; i++) { - enforcerForAdd.addPolicy("user" + i, "data" + (int) (Math.random() * 500), "read"); - } - } - - - @Benchmark - public static void hasPolicySmall() { - for (int i = 0; i < 100; i++) { - smallEnforcer.hasPolicy("user" + (int) (Math.random() * 100), "data" + (int) (Math.random() * 10), "read"); - } - } - - @Benchmark - public static void hasPolicyMedium() { - for (int i = 0; i < 1000; i++) { - mediumEnforcer.hasPolicy("user" + (int) (Math.random() * 1000), "data" + (int) (Math.random() * 100), "read"); - } - } - - @Benchmark - public static void hasPolicyLarge() { - for (int i = 0; i < 10000; i++) { - largeEnforcer.hasPolicy("user" + (int) (Math.random() * 10000), "data" + (int) (Math.random() * 1000), "read"); - } - } - - @Benchmark - public static void updatePolicySmall() { - for (int i = 0; i < 100; i++) { - List oldRule = new ArrayList<>(); - List newRule = new ArrayList<>(); - oldRule.add("user" + (int) (Math.random() * 100)); - oldRule.add("data" + (int) (Math.random() * 10)); - oldRule.add("read"); - newRule.add("user" + (int) (Math.random() * 100)); - newRule.add("data" + (int) (Math.random() * 10)); - newRule.add("read"); - - smallEnforcer.updatePolicy(oldRule, newRule); - } - } - - @Benchmark - public static void updatePolicyMedium() { - for (int i = 0; i < 100; i++) { - List oldRule = new ArrayList<>(); - List newRule = new ArrayList<>(); - oldRule.add("user" + (int) (Math.random() * 1000)); - oldRule.add("data" + (int) (Math.random() * 100)); - oldRule.add("read"); - newRule.add("user" + (int) (Math.random() * 1000)); - newRule.add("data" + (int) (Math.random() * 100)); - newRule.add("read"); - - mediumEnforcer.updatePolicy(oldRule, newRule); - } - } - - @Benchmark - public static void updatePolicyLarge() { - for (int i = 0; i < 100; i++) { - List oldRule = new ArrayList<>(); - List newRule = new ArrayList<>(); - oldRule.add("user" + (int) (Math.random() * 10000)); - oldRule.add("data" + (int) (Math.random() * 1000)); - oldRule.add("read"); - newRule.add("user" + (int) (Math.random() * 10000)); - newRule.add("data" + (int) (Math.random() * 1000)); - newRule.add("read"); - - largeEnforcer.updatePolicy(oldRule, newRule); - } - } - - @Benchmark - public static void removePolicySmall() { - for (int i = 0; i < 100; i++) { - smallEnforcer.removePolicy("user" + (int) (Math.random() * 100), "data" + (int) (Math.random() * 10), "read"); - } - } - - @Benchmark - public static void removePolicyMedium() { - for (int i = 0; i < 1000; i++) { - mediumEnforcer.removePolicy("user" + (int) (Math.random() * 1000), "data" + (int) (Math.random() * 100), "read"); - } - } - - @Benchmark - public static void removePolicyLarge() { - for (int i = 0; i < 10000; i++) { - largeEnforcer.removePolicy("user" + (int) (Math.random() * 10000), "data" + (int) (Math.random() * 1000), "read"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkBasicModel.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkBasicModel.java deleted file mode 100644 index a50e1e2e..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkBasicModel.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkBasicModel { - private static Enforcer e = new Enforcer("examples/basic_model.conf", "examples/basic_policy.csv", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkBasicModel.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(3) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkBasicModel() { - for (int i = 0; i < 1000; i++) { - e.enforce("alice", "data1", "read"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkKeyMatchModel.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkKeyMatchModel.java deleted file mode 100644 index 0dfb16bd..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkKeyMatchModel.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkKeyMatchModel { - private static Enforcer e = new Enforcer("examples/keymatch_model.conf", "examples/keymatch_policy.csv", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkKeyMatchModel.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(3) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkKeyMatchModel() { - for (int i = 0; i < 1000; i++) { - e.enforce("alice", "/alice_data/resource1", "GET"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkPriorityModel.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkPriorityModel.java deleted file mode 100644 index 18129381..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkPriorityModel.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkPriorityModel { - private static Enforcer e = new Enforcer("examples/priority_model.conf", "examples/priority_policy.csv", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkPriorityModel.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(3) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkPriorityModel() { - for (int i = 0; i < 1000; i++) { - e.enforce("alice", "data1", "read"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelLarge.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelLarge.java deleted file mode 100644 index c7a9b151..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelLarge.java +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkRBACModelLarge { - private static Enforcer e = new Enforcer("examples/rbac_model.conf", "", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkRBACModelLarge.class.getName()) - .exclude("Pref") - .warmupIterations(1) - .measurementIterations(1) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkRBACModelLarge() { - for (int i = 0; i < 100000; i++) { - e.enforce("user50001", "data1500", "read"); - } - } - - static { - e.enableAutoBuildRoleLinks(false); - // 10000 roles, 1000 resources. - e.enableAutoBuildRoleLinks(false); - for (int i=0;i<10000;i++) { - e.addPolicy(String.format("group%d", i), String.format("data%d", i/10), "read"); - } - for (int i=0;i<100000;i++) { - e.addGroupingPolicy(String.format("user%d", i), String.format("group%d", i/10)); - } - e.buildRoleLinks(); - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelMedium.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelMedium.java deleted file mode 100644 index 0b24bd00..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelMedium.java +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkRBACModelMedium { - private static Enforcer e = new Enforcer("examples/rbac_model.conf", "", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkRBACModelMedium.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(1) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkRBACModelMedium() { - for (int i = 0; i < 10000; i++) { - e.enforce("user5001", "data150", "read"); - } - } - - static { - e.enableAutoBuildRoleLinks(false); - // 1000 roles, 100 resources. - e.enableAutoBuildRoleLinks(false); - for (int i=0;i<1000;i++) { - e.addPolicy(String.format("group%d", i), String.format("data%d", i/10), "read"); - } - for (int i=0;i<10000;i++) { - e.addGroupingPolicy(String.format("user%d", i), String.format("group%d", i/10)); - } - e.buildRoleLinks(); - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelSingle.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelSingle.java deleted file mode 100644 index 3d751941..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelSingle.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkRBACModelSingle { - private static Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkRBACModelSingle.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(3) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkRBACModel() { - for (int i = 0; i < 1000; i++) { - e.enforce("alice", "data2", "read"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelSmall.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelSmall.java deleted file mode 100644 index 18f60a06..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelSmall.java +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkRBACModelSmall { - private static Enforcer e = new Enforcer("examples/rbac_model.conf", "", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkRBACModelSmall.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(1) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkRBACModelSmall() { - for (int i = 0; i < 1000; i++) { - e.enforce("user501", "data9", "read"); - } - } - - static { - e.enableAutoBuildRoleLinks(false); - // 100 roles, 10 resources. - for (int i = 0; i < 100; i++) { - e.addPolicy(String.format("group%d", i), String.format("data%d", i/10), "read"); - } - // 1000 users. - for (int i = 0; i < 1000; i++) { - e.addGroupingPolicy(String.format("user%d", i), String.format("group%d", i/10)); - } - e.buildRoleLinks(); - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithDeny.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithDeny.java deleted file mode 100644 index 5802a1b5..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithDeny.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkRBACModelWithDeny { - private static Enforcer e = new Enforcer("examples/rbac_with_deny_model.conf", "examples/rbac_with_deny_policy.csv", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkRBACModelWithDeny.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(3) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkRBACModelWithDeny() { - for (int i = 0; i < 1000; i++) { - e.enforce("alice", "data1", "read"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithDomains.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithDomains.java deleted file mode 100644 index d6f581f6..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithDomains.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkRBACModelWithDomains { - private static Enforcer e = new Enforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkRBACModelWithDomains.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(3) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkRBACModelWithDomains() { - for (int i = 0; i < 1000; i++) { - e.enforce("alice", "domain1", "data1", "read"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithResourceRoles.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithResourceRoles.java deleted file mode 100644 index 61414979..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelWithResourceRoles.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.profile.GCProfiler; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -@OutputTimeUnit(TimeUnit.MILLISECONDS) -@BenchmarkMode(Mode.AverageTime) -public class BenchmarkRBACModelWithResourceRoles { - private static Enforcer e = new Enforcer("examples/rbac_with_resource_roles_model.conf", "examples/rbac_with_resource_roles_policy.csv", false); - - public static void main(String args[]) throws RunnerException { - Options opt = new OptionsBuilder() - .include(BenchmarkRBACModelWithResourceRoles.class.getName()) - .exclude("Pref") - .warmupIterations(3) - .measurementIterations(3) - .addProfiler(GCProfiler.class) - .forks(1) - .build(); - new Runner(opt).run(); - } - - @Threads(1) - @Benchmark - public static void benchmarkRBACModelWithResourceRoles() { - for (int i = 0; i < 1000; i++) { - e.enforce("alice", "data1", "read"); - } - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRoleManager.java b/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRoleManager.java deleted file mode 100644 index 11325926..00000000 --- a/src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRoleManager.java +++ /dev/null @@ -1,155 +0,0 @@ -// Copyright 2022 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. - -package org.casbin.jcasbin.main.benchmark; - -import org.casbin.jcasbin.main.Enforcer; -import org.casbin.jcasbin.rbac.RoleManager; -import org.casbin.jcasbin.util.BuiltInFunctions; -import org.openjdk.jmh.annotations.*; -import org.openjdk.jmh.runner.Runner; -import org.openjdk.jmh.runner.RunnerException; -import org.openjdk.jmh.runner.options.Options; -import org.openjdk.jmh.runner.options.OptionsBuilder; - -import java.util.concurrent.TimeUnit; - -/** - * @author Yixiang Zhao (@seriouszyx) - **/ -@BenchmarkMode(Mode.AverageTime) -@Warmup(iterations = 3) -@Measurement(iterations = 3) -@Threads(1) -@Fork(1) -@State(value = Scope.Benchmark) -@OutputTimeUnit(TimeUnit.NANOSECONDS) -public class BenchmarkRoleManager { - - public static Enforcer smallEnforcer; - public static Enforcer mediumEnforcer; - public static Enforcer largeEnforcer; - public static Enforcer buildRoleLinksWithPattern; - public static Enforcer buildRoleLinksWithDomainPattern; - public static Enforcer buildRoleLinksWithPatternAndDomainPattern; - public static Enforcer hasLinkWithPattern; - public static Enforcer hasLinkWithDomainPattern; - public static Enforcer hasLinkWithPatternAndDomainPattern; - - public static void main(String[] args) throws RunnerException { - Options opt = new OptionsBuilder() - .include(org.casbin.jcasbin.main.benchmark.BenchmarkRoleManager.class.getName()) - .exclude("Pref") - .exclude("random") - .build(); - new Runner(opt).run(); - } - - @Benchmark - public static void roleManagerSmall() { - RoleManager rm = smallEnforcer.getRoleManager(); - for (int i = 0; i < 100; i++) { - rm.hasLink("user501", "group" + i); - } - } - - @Benchmark - public static void roleManagerMedium() { - RoleManager rm = mediumEnforcer.getRoleManager(); - for (int i = 0; i < 1000; i++) { - rm.hasLink("user501", "group" + i); - } - } - - @Benchmark - public static void roleManagerLarge() { - RoleManager rm = largeEnforcer.getRoleManager(); - for (int i = 0; i < 10000; i++) { - rm.hasLink("user501", "group" + i); - } - } - - @Benchmark - public static void buildRoleLinksWithPatternLarge() { - buildRoleLinksWithPattern.buildRoleLinks(); - } - - @Benchmark - public static void buildRoleLinksWithDomainPatternLarge() { - buildRoleLinksWithDomainPattern.buildRoleLinks(); - } - - @Benchmark - public static void buildRoleLinksWithPatternAndDomainPatternLarge() { - buildRoleLinksWithPatternAndDomainPattern.buildRoleLinks(); - } - - @Benchmark - public static void hasLinkWithPatternLarge() { - RoleManager rm = hasLinkWithPattern.getRoleManager(); - rm.hasLink("staffUser1001", "staff001", "/orgs/1/sites/site001"); - } - - @Benchmark - public static void hasLinkWithDomainPatternLarge() { - RoleManager rm = hasLinkWithDomainPattern.getRoleManager(); - rm.hasLink("staffUser1001", "staff001", "/orgs/1/sites/site001"); - } - - @Benchmark - public static void hasLinkWithPatternAndDomainPatternLarge() { - RoleManager rm = hasLinkWithPatternAndDomainPattern.getRoleManager(); - rm.hasLink("staffUser1001", "staff001", "/orgs/1/sites/site001"); - } - - static { - smallEnforcer = initEnforcer(100, 1000); - mediumEnforcer = initEnforcer(1000, 10000); - largeEnforcer = initEnforcer(10000, 100000); - - buildRoleLinksWithPattern = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); - buildRoleLinksWithPattern.addNamedMatchingFunc("g", "", BuiltInFunctions::keyMatch4); - - buildRoleLinksWithDomainPattern = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); - buildRoleLinksWithDomainPattern.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); - - buildRoleLinksWithPatternAndDomainPattern = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); - buildRoleLinksWithPatternAndDomainPattern.addNamedMatchingFunc("g", "", BuiltInFunctions::keyMatch4); - buildRoleLinksWithPatternAndDomainPattern.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); - - hasLinkWithPattern = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); - hasLinkWithPattern.addNamedMatchingFunc("g", "", BuiltInFunctions::keyMatch4); - - hasLinkWithDomainPattern = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); - hasLinkWithDomainPattern.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); - - hasLinkWithPatternAndDomainPattern = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); - hasLinkWithPatternAndDomainPattern.addNamedMatchingFunc("g", "", BuiltInFunctions::keyMatch4); - hasLinkWithPatternAndDomainPattern.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); - } - - static Enforcer initEnforcer(int roleNum, int userNum) { - Enforcer enforcer = new Enforcer("examples/rbac_model.conf", "", false); - enforcer.enableAutoBuildRoleLinks(false); - // roleNum roles, roleNum/10 resources. - for (int i = 0; i < roleNum; i++) { - enforcer.addPolicy("group" + i, "data" + i / 10, "read"); - } - // userNum users. - for (int i = 0; i < userNum; i++) { - enforcer.addGroupingPolicy("user" + i, "group" + i / 10); - } - return enforcer; - } -} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/CachedEnforcerBenchmarkTest.java b/src/test/java/org/casbin/jcasbin/main/benchmark/CachedEnforcerBenchmarkTest.java new file mode 100644 index 00000000..7702c790 --- /dev/null +++ b/src/test/java/org/casbin/jcasbin/main/benchmark/CachedEnforcerBenchmarkTest.java @@ -0,0 +1,238 @@ +// Copyright 2017 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. + +package org.casbin.jcasbin.main.benchmark; + +import org.casbin.jcasbin.main.CachedEnforcer; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.Throughput) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 5, time = 1) +@Threads(1) +@Fork(1) +@State(value = Scope.Benchmark) +@OutputTimeUnit(TimeUnit.SECONDS) +public class CachedEnforcerBenchmarkTest { + + public static class TestResource { + String name; + String owner; + + public TestResource(String name, String owner) { + this.name = name; + this.owner = owner; + } + + public String getName() { + return name; + } + + public String getOwner() { + return owner; + } + } + + private static boolean rawEnforce(String sub, String obj, String act) { + String[][] policy = {{"alice", "data1", "read"}, {"bob", "data2", "write"}}; + for (String[] rule : policy) { + if (sub.equals(rule[0]) && obj.equals(rule[1]) && act.equals(rule[2])) { + return true; + } + } + return false; + } + + private static TestResource newTestResource(String name, String owner) { + return new TestResource(name, owner); + } + + @Benchmark + public void benchmarkCachedRaw() { + rawEnforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkCachedBasicModel() { + CachedEnforcer e = new CachedEnforcer("examples/basic_model.conf", "examples/basic_policy.csv"); + e.enforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkCachedRBACModel() { + CachedEnforcer e = new CachedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv"); + e.enforce("alice", "data2", "read"); + } + + @Benchmark + public void benchmarkCachedRBACModelSmall() { + CachedEnforcer e = new CachedEnforcer("examples/rbac_model.conf", ""); + + // 100 roles, 10 resources. + for (int i = 0; i < 100; i++) { + e.addPolicy(String.format("group%d", i), String.format("data%d", i / 10), "read"); + } + + // 1000 users. + for (int i = 0; i < 1000; i++) { + e.addGroupingPolicy(String.format("user%d", i), String.format("group%d", i / 10)); + } + + e.enforce("user501", "data9", "read"); + } + + @Benchmark + public void benchmarkCachedRBACModelMedium() { + CachedEnforcer e = new CachedEnforcer("examples/rbac_model.conf", ""); + + // 1000 roles, 100 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("group%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + // 10000 users. + List> gPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("group%d", i / 10)); + gPolicies.add(policy); + } + e.addGroupingPolicies(gPolicies); + + e.enforce("user5001", "data150", "read"); + } + + @Benchmark + public void benchmarkCachedRBACModelLarge() { + CachedEnforcer e = new CachedEnforcer("examples/rbac_model.conf", ""); + + // 10000 roles, 1000 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("group%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + // 100000 users. + List> gPolicies = new ArrayList<>(); + for (int i = 0; i < 100000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("group%d", i / 10)); + gPolicies.add(policy); + } + e.addGroupingPolicies(gPolicies); + + e.enforce("user50001", "data1500", "read"); + } + + @Benchmark + public void benchmarkCachedRBACModelWithResourceRoles() { + CachedEnforcer e = new CachedEnforcer("examples/rbac_with_resource_roles_model.conf", "examples/rbac_with_resource_roles_policy.csv"); + e.enforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkCachedRBACModelWithDomains() { + CachedEnforcer e = new CachedEnforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv"); + e.enforce("alice", "domain1", "data1", "read"); + } + + @Benchmark + public void benchmarkCachedABACModel() { + CachedEnforcer e = new CachedEnforcer("examples/abac_model.conf", ""); + TestResource data1 = newTestResource("data1", "alice"); + e.enforce("alice", data1, "read"); + } + + @Benchmark + public void benchmarkCachedKeyMatchModel() { + CachedEnforcer e = new CachedEnforcer("examples/keymatch_model.conf", "examples/keymatch_policy.csv"); + e.enforce("alice", "/alice_data/resource1", "GET"); + } + + @Benchmark + public void benchmarkCachedRBACModelWithDeny() { + CachedEnforcer e = new CachedEnforcer("examples/rbac_with_deny_model.conf", "examples/rbac_with_deny_policy.csv"); + e.enforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkCachedPriorityModel() { + CachedEnforcer e = new CachedEnforcer("examples/priority_model.conf", "examples/priority_policy.csv"); + e.enforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkCachedRBACModelMediumParallel(ThreadState state) { + state.e.enforce("user5001", "data150", "read"); + } + + @State(Scope.Thread) + public static class ThreadState { + CachedEnforcer e; + + @Setup(Level.Trial) + public void setup() { + e = new CachedEnforcer("examples/rbac_model.conf", ""); + + // 10000 roles, 1000 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("group%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + // 100000 users. + List> gPolicies = new ArrayList<>(); + for (int i = 0; i < 100000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("group%d", i / 10)); + gPolicies.add(policy); + } + e.addGroupingPolicies(gPolicies); + } + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(CachedEnforcerBenchmarkTest.class.getName()) + .build(); + new Runner(opt).run(); + } +} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/EnforcerBenchmarkTest.java b/src/test/java/org/casbin/jcasbin/main/benchmark/EnforcerBenchmarkTest.java new file mode 100644 index 00000000..73184c6b --- /dev/null +++ b/src/test/java/org/casbin/jcasbin/main/benchmark/EnforcerBenchmarkTest.java @@ -0,0 +1,245 @@ +// Copyright 2017 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. + +package org.casbin.jcasbin.main.benchmark; + +import org.casbin.jcasbin.main.Enforcer; +import org.casbin.jcasbin.util.BuiltInFunctions; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.Throughput) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 5, time = 1) +@Threads(1) +@Fork(1) +@State(value = Scope.Benchmark) +@OutputTimeUnit(TimeUnit.SECONDS) +public class EnforcerBenchmarkTest { + + public static class TestResource { + String name; + String owner; + + public TestResource(String name, String owner) { + this.name = name; + this.owner = owner; + } + + public String getName() { + return name; + } + + public String getOwner() { + return owner; + } + } + + public static class TestSubject { + private String name; + private int age; + + public TestSubject(String name, int age) { + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public int getAge() { + return age; + } + } + + private static boolean rawEnforce(String sub, String obj, String act) { + String[][] policy = {{"alice", "data1", "read"}, {"bob", "data2", "write"}}; + for (String[] rule : policy) { + if (sub.equals(rule[0]) && obj.equals(rule[1]) && act.equals(rule[2])) { + return true; + } + } + return false; + } + + private static TestResource newTestResource(String name, String owner) { + return new TestResource(name, owner); + } + + private static TestSubject newTestSubject(String name, int age) { + return new TestSubject(name, age); + } + + @Benchmark + public void benchmarkRaw() { + rawEnforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkBasicModel() { + Enforcer e = new Enforcer("examples/basic_model.conf", "examples/basic_policy.csv"); + e.enforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkRBACModel() { + Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv"); + e.enforce("alice", "data2", "read"); + } + + @Benchmark + public void benchmarkRBACModelSmall() { + Enforcer e = new Enforcer("examples/rbac_model.conf", ""); + + // 100 roles, 10 resources. + for (int i = 0; i < 100; i++) { + e.addPolicy(String.format("group%d", i), String.format("data%d", i / 10), "read"); + } + + // 1000 users. + for (int i = 0; i < 1000; i++) { + e.addGroupingPolicy(String.format("user%d", i), String.format("group%d", i / 10)); + } + + e.enforce("user501", "data9", "read"); + } + + @Benchmark + public void benchmarkRBACModelMedium() { + Enforcer e = new Enforcer("examples/rbac_model.conf", ""); + + // 1000 roles, 100 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("group%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + // 10000 users. + List> gPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("group%d", i / 10)); + gPolicies.add(policy); + } + e.addGroupingPolicies(gPolicies); + + e.enforce("user5001", "data99", "read"); + } + + @Benchmark + public void benchmarkRBACModelLarge() { + Enforcer e = new Enforcer("examples/rbac_model.conf", ""); + + // 10000 roles, 1000 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("group%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + // 100000 users. + List> gPolicies = new ArrayList<>(); + for (int i = 0; i < 100000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("group%d", i / 10)); + gPolicies.add(policy); + } + e.addGroupingPolicies(gPolicies); + + e.enforce("user50001", "data999", "read"); + } + + @Benchmark + public void benchmarkRBACModelWithResourceRoles() { + Enforcer e = new Enforcer("examples/rbac_with_resource_roles_model.conf", "examples/rbac_with_resource_roles_policy.csv"); + e.enforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkRBACModelWithDomains() { + Enforcer e = new Enforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv"); + e.enforce("alice", "domain1", "data1", "read"); + } + + @Benchmark + public void benchmarkABACModel() { + Enforcer e = new Enforcer("examples/abac_model.conf", ""); + TestResource data1 = newTestResource("data1", "alice"); + e.enforce("alice", data1, "read"); + } + + @Benchmark + public void benchmarkABACRuleModel() { + Enforcer e = new Enforcer("examples/abac_rule_model.conf", ""); + TestSubject sub = newTestSubject("alice", 18); + + for (int i = 0; i < 1000; i++) { + e.addPolicy("r.sub.Age > 20", String.format("data%d", i), "read"); + } + + e.enforce(sub, "data100", "read"); + } + + @Benchmark + public void benchmarkKeyMatchModel() { + Enforcer e = new Enforcer("examples/keymatch_model.conf", "examples/keymatch_policy.csv"); + e.enforce("alice", "/alice_data/resource1", "GET"); + } + + @Benchmark + public void benchmarkRBACModelWithDeny() { + Enforcer e = new Enforcer("examples/rbac_with_deny_model.conf", "examples/rbac_with_deny_policy.csv"); + e.enforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkPriorityModel() { + Enforcer e = new Enforcer("examples/priority_model.conf", "examples/priority_policy.csv"); + e.enforce("alice", "data1", "read"); + } + + @Benchmark + public void benchmarkRBACModelWithDomainPatternLarge() { + Enforcer e = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); + e.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + e.buildRoleLinks(); + e.enforce("staffUser1001", "/orgs/1/sites/site001", "App001.Module001.Action1001"); + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(EnforcerBenchmarkTest.class.getName()) + .build(); + new Runner(opt).run(); + } +} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/ManagementApiBenchmarkTest.java b/src/test/java/org/casbin/jcasbin/main/benchmark/ManagementApiBenchmarkTest.java new file mode 100644 index 00000000..3c291b3a --- /dev/null +++ b/src/test/java/org/casbin/jcasbin/main/benchmark/ManagementApiBenchmarkTest.java @@ -0,0 +1,190 @@ +// Copyright 2020 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. + +package org.casbin.jcasbin.main.benchmark; + +import org.casbin.jcasbin.main.Enforcer; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.ArrayList; +import java.util.List; +import java.util.Random; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.Throughput) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 5, time = 1) +@Threads(1) +@Fork(1) +@State(value = Scope.Benchmark) +@OutputTimeUnit(TimeUnit.SECONDS) +public class ManagementApiBenchmarkTest { + + private Random random = new Random(); + + @Benchmark + public void benchmarkHasPolicySmall() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 100 roles, 10 resources. + for (int i = 0; i < 100; i++) { + e.addPolicy(String.format("user%d", i), String.format("data%d", i / 10), "read"); + } + + e.hasPolicy(String.format("user%d", random.nextInt(100)), String.format("data%d", random.nextInt(100) / 10), "read"); + } + + @Benchmark + public void benchmarkHasPolicyMedium() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 1000 roles, 100 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + e.hasPolicy(String.format("user%d", random.nextInt(1000)), String.format("data%d", random.nextInt(1000) / 10), "read"); + } + + @Benchmark + public void benchmarkHasPolicyLarge() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 10000 roles, 1000 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + e.hasPolicy(String.format("user%d", random.nextInt(10000)), String.format("data%d", random.nextInt(10000) / 10), "read"); + } + + @Benchmark + public void benchmarkAddPolicySmall() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 100 roles, 10 resources. + for (int i = 0; i < 100; i++) { + e.addPolicy(String.format("user%d", i), String.format("data%d", i / 10), "read"); + } + + e.addPolicy(String.format("user%d", random.nextInt(100) + 100), String.format("data%d", (random.nextInt(100) + 100) / 10), "read"); + } + + @Benchmark + public void benchmarkAddPolicyMedium() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 1000 roles, 100 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + e.addPolicy(String.format("user%d", random.nextInt(1000) + 1000), String.format("data%d", (random.nextInt(1000) + 1000) / 10), "read"); + } + + @Benchmark + public void benchmarkAddPolicyLarge() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 10000 roles, 1000 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + e.addPolicy(String.format("user%d", random.nextInt(10000) + 10000), String.format("data%d", (random.nextInt(10000) + 10000) / 10), "read"); + } + + @Benchmark + public void benchmarkRemovePolicySmall() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 100 roles, 10 resources. + for (int i = 0; i < 100; i++) { + e.addPolicy(String.format("user%d", i), String.format("data%d", i / 10), "read"); + } + + e.removePolicy(String.format("user%d", random.nextInt(100)), String.format("data%d", random.nextInt(100) / 10), "read"); + } + + @Benchmark + public void benchmarkRemovePolicyMedium() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 1000 roles, 100 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + e.removePolicy(String.format("user%d", random.nextInt(1000)), String.format("data%d", random.nextInt(1000) / 10), "read"); + } + + @Benchmark + public void benchmarkRemovePolicyLarge() { + Enforcer e = new Enforcer("examples/basic_model.conf", ""); + + // 10000 roles, 1000 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + e.removePolicy(String.format("user%d", random.nextInt(10000)), String.format("data%d", random.nextInt(10000) / 10), "read"); + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(ManagementApiBenchmarkTest.class.getName()) + .build(); + new Runner(opt).run(); + } +} diff --git a/src/test/java/org/casbin/jcasbin/main/benchmark/RoleManagerBenchmarkTest.java b/src/test/java/org/casbin/jcasbin/main/benchmark/RoleManagerBenchmarkTest.java new file mode 100644 index 00000000..2542f36f --- /dev/null +++ b/src/test/java/org/casbin/jcasbin/main/benchmark/RoleManagerBenchmarkTest.java @@ -0,0 +1,211 @@ +// Copyright 2022 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. + +package org.casbin.jcasbin.main.benchmark; + +import org.casbin.jcasbin.main.Enforcer; +import org.casbin.jcasbin.rbac.RoleManager; +import org.casbin.jcasbin.util.BuiltInFunctions; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.Throughput) +@Warmup(iterations = 3, time = 1) +@Measurement(iterations = 5, time = 1) +@Threads(1) +@Fork(1) +@State(value = Scope.Benchmark) +@OutputTimeUnit(TimeUnit.SECONDS) +public class RoleManagerBenchmarkTest { + + @Benchmark + public void benchmarkRoleManagerSmall() { + Enforcer e = new Enforcer("examples/rbac_model.conf", ""); + // Do not rebuild the role inheritance relations for every AddGroupingPolicy() call. + e.enableAutoBuildRoleLinks(false); + + // 100 roles, 10 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 100; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("group%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + // 1000 users. + List> gPolicies = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("group%d", i / 10)); + gPolicies.add(policy); + } + e.addGroupingPolicies(gPolicies); + + RoleManager rm = e.getRoleManager(); + + for (int j = 0; j < 100; j++) { + rm.hasLink("user501", String.format("group%d", j)); + } + } + + @Benchmark + public void benchmarkRoleManagerMedium() { + Enforcer e = new Enforcer("examples/rbac_model.conf", ""); + // Do not rebuild the role inheritance relations for every AddGroupingPolicy() call. + e.enableAutoBuildRoleLinks(false); + + // 1000 roles, 100 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("group%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + // 10000 users. + List> gPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("group%d", i / 10)); + gPolicies.add(policy); + } + e.addGroupingPolicies(gPolicies); + + e.buildRoleLinks(); + + RoleManager rm = e.getRoleManager(); + + for (int j = 0; j < 1000; j++) { + rm.hasLink("user501", String.format("group%d", j)); + } + } + + @Benchmark + public void benchmarkRoleManagerLarge() { + Enforcer e = new Enforcer("examples/rbac_model.conf", ""); + + // 10000 roles, 1000 resources. + List> pPolicies = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("group%d", i)); + policy.add(String.format("data%d", i / 10)); + policy.add("read"); + pPolicies.add(policy); + } + e.addPolicies(pPolicies); + + // 100000 users. + List> gPolicies = new ArrayList<>(); + for (int i = 0; i < 100000; i++) { + List policy = new ArrayList<>(); + policy.add(String.format("user%d", i)); + policy.add(String.format("group%d", i / 10)); + gPolicies.add(policy); + } + e.addGroupingPolicies(gPolicies); + + RoleManager rm = e.getRoleManager(); + + for (int j = 0; j < 10000; j++) { + rm.hasLink("user501", String.format("group%d", j)); + } + } + + @Benchmark + public void benchmarkBuildRoleLinksWithPatternLarge() { + Enforcer e = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); + e.addNamedMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + e.buildRoleLinks(); + } + + @Benchmark + public void benchmarkBuildRoleLinksWithDomainPatternLarge() { + Enforcer e = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); + e.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + e.buildRoleLinks(); + } + + @Benchmark + public void benchmarkBuildRoleLinksWithPatternAndDomainPatternLarge() { + Enforcer e = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); + e.addNamedMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + e.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + e.buildRoleLinks(); + } + + @Benchmark + public void benchmarkHasLinkWithPatternLarge() { + Enforcer e = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); + e.addNamedMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + RoleManager rm = e.getRoleManager(); + rm.hasLink("staffUser1001", "staff001", "/orgs/1/sites/site001"); + } + + @Benchmark + public void benchmarkHasLinkWithDomainPatternLarge() { + Enforcer e = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); + e.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + RoleManager rm = e.getRoleManager(); + rm.hasLink("staffUser1001", "staff001", "/orgs/1/sites/site001"); + } + + @Benchmark + public void benchmarkHasLinkWithPatternAndDomainPatternLarge() { + Enforcer e = new Enforcer("examples/performance/rbac_with_pattern_large_scale_model.conf", "examples/performance/rbac_with_pattern_large_scale_policy.csv"); + e.addNamedMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + e.addNamedDomainMatchingFunc("g", "", BuiltInFunctions::keyMatch4); + RoleManager rm = e.getRoleManager(); + rm.hasLink("staffUser1001", "staff001", "/orgs/1/sites/site001"); + } + + @Benchmark + public void benchmarkConcurrentHasLinkWithMatching(ThreadState state) { + state.rm.hasLink("alice", "/book/123"); + } + + @State(Scope.Thread) + public static class ThreadState { + RoleManager rm; + + @Setup(Level.Trial) + public void setup() { + Enforcer e = new Enforcer("examples/rbac_with_pattern_model.conf", "examples/rbac_with_pattern_policy.csv"); + e.addNamedMatchingFunc("g2", "keyMatch2", BuiltInFunctions::keyMatch2); + rm = e.getRoleManager(); + } + } + + public static void main(String[] args) throws RunnerException { + Options opt = new OptionsBuilder() + .include(RoleManagerBenchmarkTest.class.getName()) + .build(); + new Runner(opt).run(); + } +}