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

chore: Pycasbin benchmark #199

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions pycasbin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ def isAuthorized(req):

Rest of the method's name is on par with `casbin-cpp`.

### Benchmark

Pycasbin use `pytest` for benchmark.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Pycasbin use `pytest` for benchmark.
Pycasbin use `pytest` for benchmark.
`pip3 install -U pytest`


Install `pytest` and `pycasbin` in your local machine, then run the benchmark by `python3 -m pytest --benchmark-verbose --benchmark-columns=mean,stddev,iqr,ops,rounds casbin-cpp/pycasbin/benchmarks/benchmark_model.py`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do this instead:

python3 -m pytest --benchmark-verbose --benchmark-columns=mean,stddev,iqr,ops,rounds casbin-cpp/pycasbin/benchmarks/benchmark_model.py


#### Summary

This sums up the basic usage of `pycasbin` module:
Expand Down
167 changes: 167 additions & 0 deletions pycasbin/benchmarks/benchmark_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Copyright 2021 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.

import os
import casbin

def raw_enforce(sub, obj, act):
policy = [["alice", "data1", "read"], ["bob", "data2", "write"]]
for rule in policy:
if sub == rule[0] and obj == rule[1] and act == rule[2]:
return True

return False


def get_examples(path):
examples_path = os.path.split(os.path.realpath(__file__))[0] + "/../../examples/"
return os.path.abspath(examples_path + path)


def get_enforcer(model=None, adapter=None):
return casbin.Enforcer(
model,
adapter,
)


def test_benchmark_raw(benchmark):
@benchmark
def benchmark_raw():
raw_enforce("alice", "data1", "read")


def test_benchmark_basic_model(benchmark):
e = get_enforcer(get_examples("basic_model.conf"), get_examples("basic_policy.csv"))

@benchmark
def benchmark_basic_model():
e.enforce("alice", "data1", "read")


def test_benchmark_rbac_model(benchmark):
e = get_enforcer(get_examples("rbac_model.conf"), get_examples("rbac_policy.csv"))

@benchmark
def benchmark_rbac_model():
e.enforce("alice", "data1", "read")


def test_benchmark_rbac_model_small(benchmark):
e = get_enforcer(get_examples("rbac_model.conf"))

e.add_policies(
{("group" + str(i), "data" + str(int(i / 10)), "read") for i in range(100)}
)
e.add_grouping_policies(
{("user" + str(i), "group" + str(int(i / 10))) for i in range(1000)}
)

@benchmark
def benchmark_rbac_model():
e.enforce("user501", "data9", "read")


def test_benchmark_rbac_model_medium(benchmark):
e = get_enforcer(get_examples("rbac_model.conf"))

e.add_policies(
{("group" + str(i), "data" + str(int(i / 10)), "read") for i in range(1000)}
)
e.add_grouping_policies(
{("user" + str(i), "group" + str(int(i / 10))) for i in range(10000)}
)

@benchmark
def benchmark_rbac_model():
e.enforce("user501", "data9", "read")


# TODO: pycasbin cost too much time in large model
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it crash often?

How about we set up a flag through an OS env variable for intensive testing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It cost too much time in my machine, don't crash.

# def test_benchmark_rbac_model_large(benchmark):
# e = get_enforcer(get_examples("rbac_model.conf"))

# e.add_policies(
# {("group" + str(i), "data" + str(int(i / 10)), "read") for i in range(10000)}
# )
# e.add_grouping_policies(
# {("user" + str(i), "group" + str(int(i / 10))) for i in range(100000)}
# )

# @benchmark
# def benchmark_rbac_model():
# e.enforce("user501", "data9", "read")


def test_benchmark_rbac_model_with_resource_roles(benchmark):
e = get_enforcer(
get_examples("rbac_with_resource_roles_model.conf"),
get_examples("rbac_with_resource_roles_policy.csv"),
)

@benchmark
def benchmark_rbac_model_with_resource_roles():
e.enforce("alice", "data1", "read")


def test_benchmark_rbac_model_with_domains(benchmark):
e = get_enforcer(
get_examples("rbac_with_domains_model.conf"),
get_examples("rbac_with_domains_policy.csv"),
)

@benchmark
def benchmark_rbac_model_with_domains():
e.enforce("alice", "domain1", "data1", "read")


def test_benchmark_abac_model(benchmark):
e = get_enforcer(get_examples("abac_model.conf"))
sub = "alice"
obj = {"Owner": "alice", "id": "data1"}

@benchmark
def benchmark_abac_model():
e.enforce(sub, obj, "read")


def test_benchmark_rbac_with_deny(benchmark):
e = get_enforcer(
get_examples("rbac_with_deny_model.conf"),
get_examples("rbac_with_deny_policy.csv"),
)

@benchmark
def benchmark_rbac_with_deny():
e.enforce("alice", "data1", "read")


def test_benchmark_prioriry(benchmark):
e = get_enforcer(
get_examples("priority_model.conf"), get_examples("priority_policy.csv")
)

@benchmark
def benchmark_rbac_with_deny():
e.enforce("alice", "data1", "read")


def test_benchmark_keymatch(benchmark):
e = get_enforcer(
get_examples("keymatch_model.conf"), get_examples("keymatch_policy.csv")
)

@benchmark
def benchmark_keymatch():
e.enforce("alice", "/alice_data/resource1", "GET")