Skip to content

Commit

Permalink
add os matrix for CI (#167)
Browse files Browse the repository at this point in the history
* add os matrix for CI

* try to fix coverage

* try to fix ci workflow yml format

* try to fix clippy warnings

* fix typo

* remove unecessary command args

* simplify code

* add role manager bench

* try to fix clippy warnings
  • Loading branch information
GopherJ authored May 25, 2020
1 parent 5bfb12f commit 58c2063
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 59 deletions.
44 changes: 33 additions & 11 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,54 @@ on:
jobs:
build:
name: Auto Build CI
runs-on: ubuntu-latest
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
rust: [nightly, beta, stable]

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

- name: Install Rust toolchain
- name: Install Rust toolchain ${{ matrix.rust }}
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
toolchain: ${{ matrix.rust }}
components: rustfmt, clippy
override: true

- name: Install Dependencies
run: sudo apt-get install libssl-dev
- name: Cache cargo registry
uses: actions/cache@v1
with:
path: ~/.cargo/registry
key: ${{ matrix.os }}-${{ matrix.rust }}-cargo-registry-${{ hashFiles('**/Cargo.toml') }}

- name: Cache cargo index
uses: actions/cache@v1
with:
path: ~/.cargo/git
key: ${{ matrix.os }}-${{ matrix.rust }}-cargo-index-${{ hashFiles('**/Cargo.toml') }}

- name: Cargo Clean
- name: Cache cargo build
uses: actions/cache@v1
with:
path: target
key: ${{ matrix.os }}-${{ matrix.rust }}-cargo-build-target-${{ hashFiles('**/Cargo.toml') }}

- name: Release build async-std
uses: actions-rs/cargo@v1
with:
command: clean
command: build
args: --release --no-default-features --features runtime-async-std,cached,glob,ip,watcher,logging,incremental,explain

- name: Cargo Build
- name: Release build tokio
uses: actions-rs/cargo@v1
with:
command: build
args: --release --no-default-features --features runtime-tokio,cached,glob,ip,watcher,logging,incremental,explain

# Todo: https://github.com/rust-lang/cargo/issues/2980
- name: Cargo Test For All Features Using async-std
uses: actions-rs/cargo@v1
with:
Expand All @@ -53,9 +73,11 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: check
args: --target wasm32-unknown-unknown --no-default-features --features runtime-async-std,cached,glob,ip,watcher,logging,incremental
target: wasm32-unknown-unknown
override: true
args: --no-default-features --features runtime-async-std,cached,glob,ip,watcher,logging,incremental

- name: Cargo Clippy
- name: Clippy warnings
uses: actions-rs/cargo@v1
with:
command: clippy
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Run cargo-tarpaulin
uses: actions-rs/tarpaulin@v0.1
with:
args: --out Xml
args: --no-default-features --features runtime-async-std,cached,glob,ip,watcher,logging,incremental,explain

- name: Upload to codecov.io
uses: codecov/codecov-action@v1
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "casbin"
version = "0.9.2"
version = "0.9.3"
authors = ["Joey <joey.xf@gmail.com>", "Cheng JIANG <jiang.cheng@vip.163.com>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
137 changes: 136 additions & 1 deletion benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,51 @@ fn b_benchmark_cached_rbac_model(b: &mut Bencher) {
});
}

#[bench]
fn b_benchmark_role_manager_small(b: &mut Bencher) {
let mut e = await_future(Enforcer::new("examples/rbac_model.conf", ())).unwrap();

e.enable_auto_build_role_links(false);

// 100 roles, 10 resources.
await_future(
e.add_policies(
(0..100_u64)
.map(|i| {
vec![
format!("group{}", i),
format!("data{}", i / 10),
"read".to_owned(),
]
})
.collect::<Vec<Vec<String>>>(),
),
)
.unwrap();

// 1000 users.
await_future(
e.add_grouping_policies(
(0..1000)
.map(|i| vec![format!("user{}", i), format!("group{}", i / 10)])
.collect::<Vec<Vec<String>>>(),
),
)
.unwrap();

e.build_role_links().unwrap();

let rm = e.get_role_manager();

b.iter(|| {
(0..100_u64).for_each(|i| {
rm.write()
.unwrap()
.has_link("user501", &format!("group{}", i), None);
})
});
}

#[bench]
fn b_benchmark_rbac_model_small(b: &mut Bencher) {
let mut e = await_future(Enforcer::new("examples/rbac_model.conf", ())).unwrap();
Expand Down Expand Up @@ -179,6 +224,51 @@ fn b_benchmark_cached_rbac_model_small(b: &mut Bencher) {
b.iter(|| e.enforce(_mut(&["user501", "data9", "read"])).unwrap());
}

#[bench]
fn b_benchmark_role_manager_medium(b: &mut Bencher) {
let mut e = await_future(Enforcer::new("examples/rbac_model.conf", ())).unwrap();

e.enable_auto_build_role_links(false);

// 1000 roles, 100 resources.
await_future(
e.add_policies(
(0..1000_u64)
.map(|i| {
vec![
format!("group{}", i),
format!("data{}", i / 10),
"read".to_owned(),
]
})
.collect::<Vec<Vec<String>>>(),
),
)
.unwrap();

// 10000 users.
await_future(
e.add_grouping_policies(
(0..10000)
.map(|i| vec![format!("user{}", i), format!("group{}", i / 10)])
.collect::<Vec<Vec<String>>>(),
),
)
.unwrap();

e.build_role_links().unwrap();

let rm = e.get_role_manager();

b.iter(|| {
(0..1000_u64).for_each(|i| {
rm.write()
.unwrap()
.has_link("user5001", &format!("group{}", i), None);
})
});
}

#[bench]
fn b_benchmark_rbac_model_medium(b: &mut Bencher) {
let mut e = await_future(Enforcer::new("examples/rbac_model.conf", ())).unwrap();
Expand Down Expand Up @@ -251,7 +341,52 @@ fn b_benchmark_cached_rbac_model_medium(b: &mut Bencher) {

e.build_role_links().unwrap();

b.iter(|| e.enforce(_mut(&["user5001", "data150", "read"])).unwrap());
b.iter(|| e.enforce(_mut(&["user5001", "data15", "read"])).unwrap());
}

#[bench]
fn b_benchmark_role_manager_large(b: &mut Bencher) {
let mut e = await_future(Enforcer::new("examples/rbac_model.conf", ())).unwrap();

e.enable_auto_build_role_links(false);

// 10000 roles, 1000 resources.
await_future(
e.add_policies(
(0..10000_u64)
.map(|i| {
vec![
format!("group{}", i),
format!("data{}", i / 10),
"read".to_owned(),
]
})
.collect::<Vec<Vec<String>>>(),
),
)
.unwrap();

// 100000 users.
await_future(
e.add_grouping_policies(
(0..100000)
.map(|i| vec![format!("user{}", i), format!("group{}", i / 10)])
.collect::<Vec<Vec<String>>>(),
),
)
.unwrap();

e.build_role_links().unwrap();

let rm = e.get_role_manager();

b.iter(|| {
(0..10000_u64).for_each(|i| {
rm.write()
.unwrap()
.has_link("user50001", &format!("group{}", i), None);
})
});
}

#[bench]
Expand Down
62 changes: 18 additions & 44 deletions src/enforcer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use async_trait::async_trait;
use rhai::{
def_package,
packages::{ArithmeticPackage, BasicArrayPackage, BasicMapPackage, LogicPackage, Package},
Engine, EvalAltResult, RegisterFn, Scope,
Engine, RegisterFn, Scope,
};

def_package!(rhai:CasbinPackage:"Package for Casbin", lib, {
Expand Down Expand Up @@ -127,8 +127,7 @@ impl Enforcer {

let m_ast_compiled = self
.engine
.compile_expression(&escape_eval(m_ast.value.as_str()))
.map_err(|err| Box::<EvalAltResult>::from(err))?;
.compile_expression(&escape_eval(m_ast.value.as_str()))?;

if policy_len != 0 {
for pvals in policies.iter() {
Expand Down Expand Up @@ -217,48 +216,23 @@ impl CoreApi for Enforcer {
engine.register_fn(key, *func);
}

let mut e = {
let mut e = Self {
model,
adapter,
fm,
eft,
rm,
enabled: true,
auto_save: true,
auto_build_role_links: true,
#[cfg(feature = "watcher")]
auto_notify_watcher: true,
#[cfg(feature = "watcher")]
watcher: None,
events: HashMap::new(),
engine,
#[cfg(feature = "logging")]
{
let logger = Box::new(DefaultLogger::default());
Self {
model,
adapter,
fm,
eft,
rm,
enabled: true,
auto_save: true,
auto_build_role_links: true,
#[cfg(feature = "watcher")]
auto_notify_watcher: true,
#[cfg(feature = "watcher")]
watcher: None,
events: HashMap::new(),
engine,
logger,
}
}

#[cfg(not(feature = "logging"))]
{
Self {
model,
adapter,
fm,
eft,
rm,
enabled: true,
auto_save: true,
auto_build_role_links: true,
#[cfg(feature = "watcher")]
auto_notify_watcher: true,
#[cfg(feature = "watcher")]
watcher: None,
events: HashMap::new(),
engine,
}
}
logger: Box::new(DefaultLogger::default()),
};

#[cfg(any(feature = "logging", feature = "watcher"))]
Expand Down
4 changes: 3 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rhai::EvalAltResult;
use rhai::{EvalAltResult, ParseError};
use thiserror::Error;

use std::{error::Error as StdError, io::Error as IoError};
Expand Down Expand Up @@ -60,6 +60,8 @@ pub enum Error {

#[error("Casbin Evaluation Error: `{0:?}`")]
RhaiError(#[from] Box<EvalAltResult>),
#[error("Casbin Parse Error: `{0:?}`")]
RhaiParseError(#[from] Box<ParseError>),

#[error("Casbin Request Error: `{0:?}`")]
RequestError(#[from] RequestError),
Expand Down

0 comments on commit 58c2063

Please sign in to comment.