Skip to content

Commit

Permalink
Split code into features (#137)
Browse files Browse the repository at this point in the history
* upgrade async-std to 1.6 beta

* add ip feature

* add glob feature

* add cacehd feature

* fix runtime-tokio clippy warnings

* add watcher feature

* don't clone if watcher feature has been disabled

* use async-rs/async-std#768 for fixing tests

* activate all features for bench test over previous version

* benchmark pull_request and decrease the threshold

* switch to github actions

* fix typo

* Fix: cargo test features doesn't like space

* better management of feature:logging and feature:watcher

* use async-std/master for testing

* add basic wasm32 support

* use runtime-async-std for wasm32 test

* fix clippy warnings

* fix typo

* make other tests avalaible only on no-wasm32 target

* finish split code into features && bump version
  • Loading branch information
0x8f701 authored May 10, 2020
1 parent 1824313 commit 23af2bf
Show file tree
Hide file tree
Showing 23 changed files with 892 additions and 280 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name: Benchmark
on:
pull_request:
push:
branches:
- master
Expand All @@ -23,7 +24,8 @@ jobs:
github-token: ${{ secrets.PERSONAL_GITHUB_TOKEN }}
auto-push: true
# Show alert with commit comment on detecting possible performance regression
alert-threshold: '200%'
alert-threshold: '150%'
comment-on-alert: true
comment-always: true
fail-on-alert: true
alert-comment-cc-users: '@GopherJ'
70 changes: 70 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: CI

on:
pull_request:
push:
branches:
- master

jobs:
build:
name: Auto Build CI
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@master

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt, clippy
override: true

- name: Install Dependencies
run: sudo apt-get install libssl-dev

- name: Cargo Clean
uses: actions-rs/cargo@v1
with:
command: clean

- name: Cargo Build
uses: actions-rs/cargo@v1
with:
command: build

# Todo: https://github.com/rust-lang/cargo/issues/2980
- name: Cargo Test For All Features Using async-std
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features runtime-async-std,cached,glob,ip,watcher,logging

- name: Cargo Test For All Features Using tokio
uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features runtime-tokio,cached,glob,ip,watcher,logging

- name: Cargo Check Wasm
uses: actions-rs/cargo@v1
with:
command: check
target: wasm32-unknown-unknown
override: true
args: --no-default-features --features runtime-async-std,cached,glob,ip,watcher,logging

- name: Cargo Clippy
uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings

- name: Cargo Fmt Check
uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check
33 changes: 33 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Coverage

on:
pull_request:
push:
branches:
- master

jobs:
cover:
name: Auto Codecov Coverage
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@master

- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true

- name: Run cargo-tarpaulin
uses: actions-rs/[email protected]
with:
args: --out Xml

- name: Upload to codecov.io
uses: codecov/codecov-action@v1
with:
token: ${{secrets.CODECOV_TOKEN}}
49 changes: 0 additions & 49 deletions .travis.yml

This file was deleted.

19 changes: 13 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casbin"
version = "0.7.1"
version = "0.7.2"
authors = ["Joey <[email protected]>", "Cheng JIANG <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -15,27 +15,34 @@ keywords = ["auth", "authorization", "rbac", "acl", "abac"]
[dependencies]
regex = "1.3.1"
rhai = { version = "0.13.0", default-features = false, features = ["sync", "only_i32", "no_function", "no_float"] }
ip_network = "0.3.4"
ttl_cache = "0.5.1"
ip_network = { version = "0.3.4", optional = true }
ttl_cache = { version = "0.5.1", optional = true }
lazy_static = "1.4.0"
indexmap = "1.3.1"
async-std = { version = "1.5.0", optional = true }
async-trait = "0.1.24"
log = { version = "0.4.8", optional = true }
tokio = { version = "0.2.11", optional = true, default-features = false }
globset = "0.4.5"
globset = { version = "0.4.5", optional = true }
thiserror = "1.0.14"

[features]
default = ["runtime-async-std"]

runtime-tokio = ["tokio/fs", "tokio/io-util", "tokio/stream", "tokio/rt-threaded", "tokio/blocking"]
runtime-tokio = ["tokio/fs", "tokio/io-util"]
runtime-async-std = ["async-std"]
logging = ["log"]
ip = ["ip_network"]
glob = ["globset"]
cached = ["ttl_cache"]
watcher = []

[profile.release.build-override]
opt-level = 0

[dev-dependencies]
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
async-std = { version = "1.5.0", features = [ "attributes" ] }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "0.2.11", features = [ "full" ] }
async-std = { version = "1.5.0", features = [ "attributes" ] }
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
[![Crates.io](https://img.shields.io/crates/v/casbin.svg)](https://crates.io/crates/casbin)
[![crates.io](https://img.shields.io/crates/d/casbin)](https://crates.io/crates/casbin)
[![Docs](https://docs.rs/casbin/badge.svg)](https://docs.rs/casbin)
[![Build Status](https://travis-ci.org/casbin/casbin-rs.svg?branch=master)](https://travis-ci.org/casbin/casbin-rs)
[![codecov](https://codecov.io/gh/casbin/casbin-rs/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin/casbin-rs)
[![CI](https://github.com/casbin/casbin-rs/workflows/CI/badge.svg)](https://github.com/casbin/casbin-rs/actions)
[![Codecov](https://codecov.io/gh/casbin/casbin-rs/branch/master/graph/badge.svg)](https://codecov.io/gh/casbin/casbin-rs)

**Casbin-RS** is a powerful and efficient open-source access control library for Rust projects. It provides support for enforcing authorization based on various [access control models](https://en.wikipedia.org/wiki/Computer_security_model).

Expand All @@ -27,7 +27,7 @@ Add this package to `Cargo.toml` of your project. (Check https://crates.io/crate

```toml
[dependencies]
casbin = { version = "0.7.1", default-features = false, features = ["runtime-async-std", "logging"] }
casbin = { version = "0.7.2", default-features = false, features = ["runtime-async-std", "logging"] }
async-std = { version = "1.5.0", features = ["attributes"] }
env_logger = "0.7.1"
```
Expand Down
11 changes: 11 additions & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn b_benchmark_basic_model(b: &mut Bencher) {
});
}

#[cfg(feature = "cached")]
#[bench]
fn b_benmark_cached_basic_model(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new(
Expand All @@ -89,6 +90,7 @@ fn b_benchmark_rbac_model(b: &mut Bencher) {
});
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_rbac_model(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new(
Expand Down Expand Up @@ -139,6 +141,7 @@ fn b_benchmark_rbac_model_small(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["user501", "data9", "read"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_rbac_model_small(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new("examples/rbac_model.conf", ())).unwrap();
Expand Down Expand Up @@ -213,6 +216,7 @@ fn b_benchmark_rbac_model_medium(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["user5001", "data15", "read"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_rbac_model_medium(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new("examples/rbac_model.conf", ())).unwrap();
Expand Down Expand Up @@ -287,6 +291,7 @@ fn b_benchmark_rbac_model_large(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["user50001", "data1500", "read"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_rbac_model_large(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new("examples/rbac_model.conf", ())).unwrap();
Expand Down Expand Up @@ -335,6 +340,7 @@ fn b_benchmark_rbac_with_resource_roles(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["alice", "data1", "read"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_rbac_with_resource_roles(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new(
Expand All @@ -357,6 +363,7 @@ fn b_benchmark_rbac_model_with_domains(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["alice", "domain1", "data1", "read"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_rbac_model_with_domains(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new(
Expand All @@ -375,6 +382,7 @@ fn b_benchmark_abac_model(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["alice", r#"{"Owner": "alice"}"#, "read"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_abac_model(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new("examples/abac_model.conf", ())).unwrap();
Expand All @@ -393,6 +401,7 @@ fn b_benchmark_key_match(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["alice", "/alice_data/resource1", "GET"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_key_match(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new(
Expand All @@ -415,6 +424,7 @@ fn b_benchmark_rbac_with_deny(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["alice", "data1", "read"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_rbac_with_deny(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new(
Expand All @@ -437,6 +447,7 @@ fn b_benchmark_priority_model(b: &mut Bencher) {
b.iter(|| await_future(e.enforce(&["alice", "data1", "read"])).unwrap());
}

#[cfg(feature = "cached")]
#[bench]
fn b_benchmark_cached_priority_model(b: &mut Bencher) {
let mut e = await_future(CachedEnforcer::new(
Expand Down
2 changes: 2 additions & 0 deletions src/adapter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use async_trait::async_trait;

#[cfg(not(target_arch = "wasm32"))]
pub mod file_adapter;
pub mod memory_adapter;
pub mod null_adapter;

#[cfg(not(target_arch = "wasm32"))]
pub use file_adapter::FileAdapter;
pub use memory_adapter::MemoryAdapter;
pub use null_adapter::NullAdapter;
Expand Down
Loading

1 comment on commit 23af2bf

@0x8f701
Copy link
Member Author

Choose a reason for hiding this comment

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

Rust Benchmark

Benchmark suite Current: 23af2bf Previous: 1824313 Ratio
b_benchmark_abac_model 6770 ns/iter (± 919) 6978 ns/iter (± 1313) 0.97
b_benchmark_basic_model 7086 ns/iter (± 999) 7023 ns/iter (± 1190) 1.01
b_benchmark_key_match 23709 ns/iter (± 3191) 25305 ns/iter (± 3271) 0.94
b_benchmark_priority_model 8425 ns/iter (± 1024) 8981 ns/iter (± 1303) 0.94
b_benchmark_raw 8 ns/iter (± 1) 8 ns/iter (± 1) 1
b_benchmark_rbac_model 20621 ns/iter (± 2827) 22176 ns/iter (± 2143) 0.93
b_benchmark_rbac_model_large 61145960 ns/iter (± 6755344) 66588398 ns/iter (± 4405553) 0.92
b_benchmark_rbac_model_medium 5889772 ns/iter (± 1155067) 6128903 ns/iter (± 865542) 0.96
b_benchmark_rbac_model_small 579908 ns/iter (± 86440) 638322 ns/iter (± 60206) 0.91
b_benchmark_rbac_model_with_domains 12167 ns/iter (± 2039) 12799 ns/iter (± 1465) 0.95
b_benchmark_rbac_with_deny 34374 ns/iter (± 5191) 37412 ns/iter (± 8135) 0.92
b_benchmark_rbac_with_resource_roles 9377 ns/iter (± 1428) 9790 ns/iter (± 879) 0.96

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.