Skip to content

Commit f572ce5

Browse files
Copilothsluoyz
andcommitted
Update all benchmark classes with comprehensive Javadoc and align JMH parameters
Co-authored-by: hsluoyz <3787410+hsluoyz@users.noreply.github.com>
1 parent 3080011 commit f572ce5

11 files changed

+411
-63
lines changed

src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkABACModel.java

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,56 @@
2525

2626
import java.util.concurrent.TimeUnit;
2727

28+
/**
29+
* Benchmark for ABAC (Attribute-Based Access Control) model.
30+
*
31+
* <p>This benchmark tests ABAC authorization performance using attribute-based expressions.
32+
* ABAC allows access decisions based on attributes of the subject, resource, and environment
33+
* without requiring explicit policies for each permission combination.
34+
* The scenario uses deterministic policy generation to ensure reproducible results across runs.
35+
*
36+
* <p><b>Data Scale:</b>
37+
* <ul>
38+
* <li>Total rules: 0</li>
39+
* <li>Total users: 0</li>
40+
* </ul>
41+
*
42+
* <p><b>Authorization Logic:</b>
43+
* The model uses attribute matching defined in the ABAC model configuration.
44+
* Access is granted when the resource owner matches the requesting user.
45+
*
46+
* <p><b>Test Case:</b> Enforce "alice", data1 (owned by "alice"), "read"
47+
*
48+
* <p><b>Recommended JMH Options:</b>
49+
* <pre>
50+
* -f 2 -wi 3 -i 5 -t 1
51+
* (2 forks, 3 warmup iterations, 5 measurement iterations, 1 thread)
52+
* </pre>
53+
*
54+
* @see <a href="https://casbin.org/docs/en/abac">Casbin ABAC Model</a>
55+
*/
2856
@OutputTimeUnit(TimeUnit.MILLISECONDS)
2957
@BenchmarkMode(Mode.AverageTime)
3058
public class BenchmarkABACModel {
31-
private static Enforcer e = new Enforcer("examples/abac_model.conf", "",false);
59+
private static Enforcer e = new Enforcer("examples/abac_model.conf", "", false);
3260
private static ModelUnitTest.TestResource data1 = new ModelUnitTest.TestResource("data1", "alice");
3361

3462
public static void main(String args[]) throws RunnerException {
3563
Options opt = new OptionsBuilder()
3664
.include(BenchmarkABACModel.class.getName())
3765
.exclude("Pref")
3866
.warmupIterations(3)
39-
.measurementIterations(3)
67+
.measurementIterations(5)
4068
.addProfiler(GCProfiler.class)
41-
.forks(1)
69+
.forks(2)
70+
.threads(1)
4271
.build();
4372
new Runner(opt).run();
4473
}
4574

4675
@Threads(1)
4776
@Benchmark
4877
public static void benchmarkABACModel() {
49-
for (int i = 0; i < 1000; i++) {
50-
e.enforce("alice", data1, "read");
51-
}
78+
e.enforce("alice", data1, "read");
5279
}
5380
}

src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkBasicModel.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,34 @@
2424

2525
import java.util.concurrent.TimeUnit;
2626

27+
/**
28+
* Benchmark for ACL (Access Control List) model.
29+
*
30+
* <p>This benchmark tests basic ACL authorization performance with a simple subject-object-action policy.
31+
* The scenario uses deterministic policy generation to ensure reproducible results across runs.
32+
*
33+
* <p><b>Data Scale:</b>
34+
* <ul>
35+
* <li>Total rules: 2</li>
36+
* <li>Total users: 2</li>
37+
* </ul>
38+
*
39+
* <p><b>Policy Structure:</b>
40+
* <pre>
41+
* p, alice, data1, read
42+
* p, bob, data2, write
43+
* </pre>
44+
*
45+
* <p><b>Test Case:</b> Enforce "alice", "data1", "read"
46+
*
47+
* <p><b>Recommended JMH Options:</b>
48+
* <pre>
49+
* -f 2 -wi 3 -i 5 -t 1
50+
* (2 forks, 3 warmup iterations, 5 measurement iterations, 1 thread)
51+
* </pre>
52+
*
53+
* @see <a href="https://casbin.org/docs/en/supported-models#acl">Casbin ACL Model</a>
54+
*/
2755
@OutputTimeUnit(TimeUnit.MILLISECONDS)
2856
@BenchmarkMode(Mode.AverageTime)
2957
public class BenchmarkBasicModel {
@@ -34,18 +62,17 @@ public static void main(String args[]) throws RunnerException {
3462
.include(BenchmarkBasicModel.class.getName())
3563
.exclude("Pref")
3664
.warmupIterations(3)
37-
.measurementIterations(3)
65+
.measurementIterations(5)
3866
.addProfiler(GCProfiler.class)
39-
.forks(1)
67+
.forks(2)
68+
.threads(1)
4069
.build();
4170
new Runner(opt).run();
4271
}
4372

4473
@Threads(1)
4574
@Benchmark
4675
public static void benchmarkBasicModel() {
47-
for (int i = 0; i < 1000; i++) {
48-
e.enforce("alice", "data1", "read");
49-
}
76+
e.enforce("alice", "data1", "read");
5077
}
5178
}

src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkKeyMatchModel.java

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,39 @@
2424

2525
import java.util.concurrent.TimeUnit;
2626

27+
/**
28+
* Benchmark for RESTful/KeyMatch model.
29+
*
30+
* <p>This benchmark tests RESTful authorization performance with pattern matching.
31+
* The KeyMatch model allows flexible URL pattern matching for RESTful APIs,
32+
* supporting wildcards and path parameters.
33+
* The scenario uses deterministic policy generation to ensure reproducible results across runs.
34+
*
35+
* <p><b>Data Scale:</b>
36+
* <ul>
37+
* <li>Total rules: 5</li>
38+
* <li>Total users: 3 (alice, bob, cathy)</li>
39+
* </ul>
40+
*
41+
* <p><b>Policy Structure:</b>
42+
* <pre>
43+
* p, alice, /alice_data/*, GET
44+
* p, alice, /alice_data/resource1, POST
45+
* p, bob, /alice_data/resource2, GET
46+
* p, bob, /bob_data/*, POST
47+
* p, cathy, /cathy_data, (GET)|(POST)
48+
* </pre>
49+
*
50+
* <p><b>Test Case:</b> Enforce "alice", "/alice_data/resource1", "GET"
51+
*
52+
* <p><b>Recommended JMH Options:</b>
53+
* <pre>
54+
* -f 2 -wi 3 -i 5 -t 1
55+
* (2 forks, 3 warmup iterations, 5 measurement iterations, 1 thread)
56+
* </pre>
57+
*
58+
* @see <a href="https://casbin.org/docs/en/function#keymatch">Casbin KeyMatch Functions</a>
59+
*/
2760
@OutputTimeUnit(TimeUnit.MILLISECONDS)
2861
@BenchmarkMode(Mode.AverageTime)
2962
public class BenchmarkKeyMatchModel {
@@ -34,18 +67,17 @@ public static void main(String args[]) throws RunnerException {
3467
.include(BenchmarkKeyMatchModel.class.getName())
3568
.exclude("Pref")
3669
.warmupIterations(3)
37-
.measurementIterations(3)
70+
.measurementIterations(5)
3871
.addProfiler(GCProfiler.class)
39-
.forks(1)
72+
.forks(2)
73+
.threads(1)
4074
.build();
4175
new Runner(opt).run();
4276
}
4377

4478
@Threads(1)
4579
@Benchmark
4680
public static void benchmarkKeyMatchModel() {
47-
for (int i = 0; i < 1000; i++) {
48-
e.enforce("alice", "/alice_data/resource1", "GET");
49-
}
81+
e.enforce("alice", "/alice_data/resource1", "GET");
5082
}
5183
}

src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkPriorityModel.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,44 @@
2424

2525
import java.util.concurrent.TimeUnit;
2626

27+
/**
28+
* Benchmark for priority-based model.
29+
*
30+
* <p>This benchmark tests priority-based authorization performance.
31+
* The priority model allows policy evaluation based on explicit priority ordering,
32+
* where higher priority policies override lower priority ones.
33+
* The scenario uses deterministic policy generation to ensure reproducible results across runs.
34+
*
35+
* <p><b>Data Scale:</b>
36+
* <ul>
37+
* <li>Total rules: 9 (7 policies + 2 role assignments)</li>
38+
* <li>Total users: 2 (alice, bob)</li>
39+
* <li>Total roles: 2 (data1_deny_group, data2_allow_group)</li>
40+
* </ul>
41+
*
42+
* <p><b>Policy Structure:</b>
43+
* <pre>
44+
* p, alice, data1, read, allow
45+
* p, data1_deny_group, data1, read, deny
46+
* p, data1_deny_group, data1, write, deny
47+
* p, alice, data1, write, allow
48+
* g, alice, data1_deny_group
49+
* p, data2_allow_group, data2, read, allow
50+
* p, bob, data2, read, deny
51+
* p, bob, data2, write, deny
52+
* g, bob, data2_allow_group
53+
* </pre>
54+
*
55+
* <p><b>Test Case:</b> Enforce "alice", "data1", "read"
56+
*
57+
* <p><b>Recommended JMH Options:</b>
58+
* <pre>
59+
* -f 2 -wi 3 -i 5 -t 1
60+
* (2 forks, 3 warmup iterations, 5 measurement iterations, 1 thread)
61+
* </pre>
62+
*
63+
* @see <a href="https://casbin.org/docs/en/syntax-for-models#policy-effect">Casbin Priority Model</a>
64+
*/
2765
@OutputTimeUnit(TimeUnit.MILLISECONDS)
2866
@BenchmarkMode(Mode.AverageTime)
2967
public class BenchmarkPriorityModel {
@@ -34,18 +72,17 @@ public static void main(String args[]) throws RunnerException {
3472
.include(BenchmarkPriorityModel.class.getName())
3573
.exclude("Pref")
3674
.warmupIterations(3)
37-
.measurementIterations(3)
75+
.measurementIterations(5)
3876
.addProfiler(GCProfiler.class)
39-
.forks(1)
77+
.forks(2)
78+
.threads(1)
4079
.build();
4180
new Runner(opt).run();
4281
}
4382

4483
@Threads(1)
4584
@Benchmark
4685
public static void benchmarkPriorityModel() {
47-
for (int i = 0; i < 1000; i++) {
48-
e.enforce("alice", "data1", "read");
49-
}
86+
e.enforce("alice", "data1", "read");
5087
}
5188
}

src/test/java/org/casbin/jcasbin/main/benchmark/BenchmarkRBACModelLarge.java

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,38 @@
2424

2525
import java.util.concurrent.TimeUnit;
2626

27+
/**
28+
* Benchmark for RBAC model with large-scale data.
29+
*
30+
* <p>This benchmark tests RBAC authorization performance with a large dataset.
31+
* The scenario uses deterministic policy generation to ensure reproducible results across runs.
32+
*
33+
* <p><b>Data Scale:</b>
34+
* <ul>
35+
* <li>Total rules: 110000 (10000 role policies + 100000 user-role assignments)</li>
36+
* <li>Total users: 100000</li>
37+
* <li>Total roles: 10000</li>
38+
* <li>Total resources: 1000</li>
39+
* </ul>
40+
*
41+
* <p><b>Generation Logic:</b>
42+
* <ul>
43+
* <li>For each role i (0-9999): p, group{i}, data{i/10}, read</li>
44+
* <li>For each user i (0-99999): g, user{i}, group{i/10}</li>
45+
* <li>Each 10 roles are bound to 1 resource</li>
46+
* <li>Each 10 users are bound to 1 role</li>
47+
* </ul>
48+
*
49+
* <p><b>Test Case:</b> Enforce "user50001", "data1500", "read"
50+
*
51+
* <p><b>Recommended JMH Options:</b>
52+
* <pre>
53+
* -f 2 -wi 3 -i 5 -t 1
54+
* (2 forks, 3 warmup iterations, 5 measurement iterations, 1 thread)
55+
* </pre>
56+
*
57+
* @see <a href="https://casbin.org/docs/en/supported-models#rbac">Casbin RBAC Model</a>
58+
*/
2759
@OutputTimeUnit(TimeUnit.MILLISECONDS)
2860
@BenchmarkMode(Mode.AverageTime)
2961
public class BenchmarkRBACModelLarge {
@@ -33,30 +65,29 @@ public static void main(String args[]) throws RunnerException {
3365
Options opt = new OptionsBuilder()
3466
.include(BenchmarkRBACModelLarge.class.getName())
3567
.exclude("Pref")
36-
.warmupIterations(1)
37-
.measurementIterations(1)
68+
.warmupIterations(3)
69+
.measurementIterations(5)
3870
.addProfiler(GCProfiler.class)
39-
.forks(1)
71+
.forks(2)
72+
.threads(1)
4073
.build();
4174
new Runner(opt).run();
4275
}
4376

4477
@Threads(1)
4578
@Benchmark
4679
public static void benchmarkRBACModelLarge() {
47-
for (int i = 0; i < 100000; i++) {
48-
e.enforce("user50001", "data1500", "read");
49-
}
80+
e.enforce("user50001", "data1500", "read");
5081
}
5182

5283
static {
5384
e.enableAutoBuildRoleLinks(false);
5485
// 10000 roles, 1000 resources.
55-
e.enableAutoBuildRoleLinks(false);
56-
for (int i=0;i<10000;i++) {
86+
for (int i = 0; i < 10000; i++) {
5787
e.addPolicy(String.format("group%d", i), String.format("data%d", i/10), "read");
5888
}
59-
for (int i=0;i<100000;i++) {
89+
// 100000 users.
90+
for (int i = 0; i < 100000; i++) {
6091
e.addGroupingPolicy(String.format("user%d", i), String.format("group%d", i/10));
6192
}
6293
e.buildRoleLinks();

0 commit comments

Comments
 (0)