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

feat: port the performance improvements of fastbin back to pycasbin #285

Closed
wants to merge 3 commits into from

Conversation

terry-xuan-gao
Copy link
Contributor

@terry-xuan-gao terry-xuan-gao commented Jan 26, 2023

Signed-off-by: terry-xuan-gao gaoxuanhit@qq.com

Description

  • port the performance improvements of fastbin back to pycasbin
  • the newly added code was tested (see the test below)

Related Issues

Fix: #71

New Behavior

The first thing I need to say is that the idea of all the work I do in this PR comes from fastbin, thanks to his work💡

  • a new class variable _cache_key_order has been added to the CoreEnforcer class, which defaults to be None
  • if the user enables this parameter, it enters the fast mode, and in the fast mode, the fastbin logic is used
  • in the fast mode, it is considered that the user understands the restrictions of the mode on the request and policy formats
  • in this way, we can achieve on-demand use of pycasbin performance by users
  • as for what is the fastbin logic and what is the restrictions of the mode on the request and policy formats, we can easily find it on fastbin's readme file

Test

  • test.py
from tests.test_enforcer import TestConfig
from tests.test_enforcer import TestFastEnforcer

if __name__ == "__main__":
    print("Hello World")

    test_config = TestConfig()
    test_config.test_enforcer_basic()
    test_config.test_enforce_ex_basic()
    test_config.test_model_set_load()
    test_config.test_enforcer_basic_without_spaces()
    test_config.test_enforce_basic_with_root()
    test_config.test_enforce_basic_without_resources()
    test_config.test_enforce_basic_without_users()
    test_config.test_enforce_ip_match()
    test_config.test_enforce_key_match()
    test_config.test_enforce_key_match2()
    test_config.test_enforce_key_match_custom_model()
    test_config.test_enforce_glob_match()
    test_config.test_enforce_priority()
    test_config.test_enforce_priority_explicit()
    test_config.test_enforce_priority_indeterminate()
    test_config.test_enforce_subpriority()
    test_config.test_enforce_subpriority_with_domain()
    test_config.test_multiple_policy_definitions()
    test_config.test_enforce_rbac()
    test_config.test_enforce_rbac_empty_policy()
    test_config.test_enforce_rbac_with_deny()
    test_config.test_enforce_rbac_with_domains()
    test_config.test_enforce_rbac_with_not_deny()
    test_config.test_enforce_rbac_with_resource_roles()
    test_config.test_enforce_rbac_with_pattern()
    test_config.test_rbac_with_multipy_matched_pattern()
    test_config.test_enforce_abac_log_enabled()
    test_config.test_abac_with_sub_rule()
    test_config.test_abac_with_multiple_sub_rules()
    test_config.test_matcher_using_in_operator_bracket()

    test_fast = TestFastEnforcer()
    test_fast.test_initializes_model()
    test_fast.test_creates_proper_policy()
    test_fast.test_able_to_clear_policy()
    test_fast.test_able_to_enforce_rule()
    test_fast.test_speed_of_fast_enforcer()
  • output:
gx@LAPTOP-UVCO35A4:pycasbin$ python3 ./downloads/test.py 
Hello World
Request: alice, data2, read ---> False
Request: bob, data1, write ---> False
Request: alice, data2, read ---> False
Request: bob, data1, write ---> False
Request: alice, data1, write ---> False
Request: alice, data2, read ---> False
Request: alice, data2, write ---> False
Request: bob, data1, read ---> False
Request: bob, data1, write ---> False
Request: bob, data2, read ---> False
Request: alice, write ---> False
Request: bob, read ---> False
Request: data1, write ---> False
Request: data2, read ---> False
Request: 192.168.3.1, data1, read ---> False
Request: alice, /bob_data/test, GET ---> False
Request: cathy, /cathy_data/12, POST ---> False
Request: alice, /alice_data2/myid, GET ---> False
Request: bob, /alice_data/0, GET ---> False
Request: bob, /bob_data/1, POST ---> False
Request: alice, data1, write ---> False
Request: alice, data2, read ---> False
Request: alice, data2, write ---> False
Request: bob, data1, read ---> False
Request: bob, data1, write ---> False
Request: bob, data2, write ---> False
Request: bob, data2, read ---> False
Request: data1_deny_group, data1, read ---> False
Request: data1_deny_group, data1, write ---> False
Request: bob, data2, read ---> False
Request: bob, data2, write ---> False
Request: data1_deny_group, data1, read ---> False
Request: data1_deny_group, data1, write ---> False
Request: alice, data1, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e212cb0>, /data1, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a0160>, /data1, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e212cb0>, /data2, read ---> False
Request: bob, data1, read ---> False
Request: bogus, data2, write ---> False
Request: alice, data1, read ---> False
Request: bob, data1, read ---> False
Request: bob, data2, write ---> False
Request: alice, data2, read ---> False
Request: alice, data2, write ---> False
Request: alice, data2, write ---> False
Request: alice, domain1, data2, read ---> False
Request: alice, domain1, data2, write ---> False
Request: bob, domain2, data1, read ---> False
Request: bob, domain2, data1, write ---> False
Request: alice, data2, write ---> False
Request: alice, data2, read ---> False
Request: bob, data1, read ---> False
Request: bob, data1, write ---> False
Request: bob, data2, read ---> False
Request: alice, /pen/2, GET ---> False
Request: bob, /book/1, GET ---> False
Request: bob, /book/2, GET ---> False
Request: alice, /pen2/2, GET ---> False
Request: bob, /book2/1, GET ---> False
Request: bob, /book2/2, GET ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e3021d0>, /data1, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e3021d0>, /data2, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e3021d0>, /data1, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a0fa0>, /data2, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a0fa0>, /data1, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a3220>, /data2, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a3220>, /data1, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a3220>, /data2, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e733f70>, /data1, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e733f70>, /data2, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e733f70>, /data1, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e733f70>, /data2, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a1d20>, /data2, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a1d20>, /data1, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a1d20>, /data2, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a11e0>, /data1, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a11e0>, /data2, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a11e0>, /data1, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a11e0>, /data2, write ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a3880>, /data1, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a3880>, /data2, read ---> False
Request: <tests.test_enforcer.TestSub object at 0x7fb09e2a3880>, /data1, write ---> False
Request: alice, data4, scribble ---> False
Request: alice2, data1, read ---> False
True 379.73499298095703

@casbin-bot
Copy link
Member

@Nekotoxin please review

@CLAassistant
Copy link

CLAassistant commented Jan 26, 2023

CLA assistant check
All committers have signed the CLA.

@terry-xuan-gao terry-xuan-gao changed the title feat:port the performance improvements of fastbin back to pycasbin feat: port the performance improvements of fastbin back to pycasbin Jan 26, 2023
@terry-xuan-gao terry-xuan-gao force-pushed the feat-easytask branch 2 times, most recently from 2f41a72 to 01e3200 Compare February 5, 2023 06:53
Signed-off-by: terry-xuan-gao <gaoxuanhit@qq.com>
casbin/model/model.py Outdated Show resolved Hide resolved
casbin/model/policy.py Outdated Show resolved Hide resolved
casbin/model/policy.py Outdated Show resolved Hide resolved
tests/model/test_policy.py Outdated Show resolved Hide resolved
tests/test_enforcer.py Outdated Show resolved Hide resolved
Signed-off-by: terry-xuan-gao <gaoxuanhit@qq.com>
@hsluoyz
Copy link
Member

hsluoyz commented Feb 5, 2023

@wakemaster39 @gerbyzation plz review

Signed-off-by: terry-xuan-gao <gaoxuanhit@qq.com>
@gerbyzation
Copy link

I'm not going to review this as I pivoted away from pycasbin years ago after I reported the issue and have not been looking at casbin code since. The only remark I'd like to leave is around documentation, this PR simply refers to fastbin but I think the constrains and performance considerations are worth documenting properly.

@leeqvip
Copy link
Member

leeqvip commented Oct 20, 2023

It has been implemented in #318, so close it now.

@leeqvip leeqvip closed this Oct 20, 2023
@terry-xuan-gao
Copy link
Contributor Author

terry-xuan-gao commented Apr 27, 2024

It has been implemented in #318, so close it now.

Hi friend, I have just reviewed the #318 , it is very good.
When I open this PR, I have put a thanks message in the Description:

The first thing I need to say is that the idea of all the work I do in this PR comes from fastbin, thanks to his work💡

I think this means respect, the respect of one coder for another coder's nice idea and hard work.
As for #318 , there are 3 commits, and the first commit is almost exactly the same as the work done here.
However, there is no thanks or respect message for the work of fastbin or mine.

It is not cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

advice on performance
6 participants