Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: FastEnforcer not fast #344

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion casbin/fast_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def enforce(self, *rvals):
"""decides whether a "subject" can access a "object" with the operation "action",
input parameters are usually: (sub, obj, act).
"""
if FastEnforcer._cache_key_order is None:
if self._cache_key_order is None:
result, _ = self.enforce_ex(*rvals)
else:
keys = [rvals[x] for x in self._cache_key_order]
Expand Down
19 changes: 19 additions & 0 deletions tests/test_fast_enforcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import os
import time
from typing import Sequence
from unittest import TestCase

Expand All @@ -35,6 +36,24 @@ def get_enforcer(self, model=None, adapter=None, cache_key_order: Sequence[int]


class TestFastEnforcer(TestCaseBase):
def test_performance(self) -> None:
e1 = self.get_enforcer(
get_examples("performance/rbac_with_pattern_large_scale_model.conf"),
get_examples("performance/rbac_with_pattern_large_scale_policy.csv"),
)
e2 = self.get_enforcer(
get_examples("performance/rbac_with_pattern_large_scale_model.conf"),
get_examples("performance/rbac_with_pattern_large_scale_policy.csv"),
[2, 1],
)
s_e1 = time.perf_counter()
e1.enforce("alice", "data1", "read")
t_e1 = time.perf_counter() - s_e1
s_e2 = time.perf_counter()
e2.enforce("alice", "data1", "read")
t_e2 = time.perf_counter() - s_e2
assert t_e1 > t_e2 * 5

def test_creates_proper_policy(self) -> None:
e = self.get_enforcer(
get_examples("basic_model.conf"),
Expand Down