Skip to content

Commit

Permalink
chore: Merge changes from casbin/naster
Browse files Browse the repository at this point in the history
Signed-off-by: Sagilio <Sagilio@outlook.com>
  • Loading branch information
Sagilio authored and Sagilio committed Mar 31, 2021
2 parents 17363d1 + ed292e6 commit 623d64b
Show file tree
Hide file tree
Showing 26 changed files with 823 additions and 57 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-release.yml
Expand Up @@ -176,7 +176,7 @@ jobs:
echo "Now branch name: $NowBranchName";
$PackageVersion = ($LastTag).TrimStart('v') + "-" + $env:BUILD_RUN_NUMBER + "." + $NowBranchName + "." + $env:SHA.SubString(0, 7);
echo "Publishing package version: ${PackageVersion}";
dotnet pack -c Release -o packages /p:PackageVersion=$PackageVersion /p:Version=$Version --no-build;
dotnet pack -c Release -o packages /p:PackageVersion=$PackageVersion /p:Version=$Version;
- name: Upload packages artefacts
uses: actions/upload-artifact@v1.0.0
Expand Down Expand Up @@ -232,7 +232,7 @@ jobs:
echo "Last tag is: $LastTag";
$Version = ($LastTag).TrimStart('v');
echo "Publishing version: $Version";
dotnet pack -c Release -o packages /p:PackageVersion=$Version /p:Version=$Version --no-build;
dotnet pack -c Release -o packages /p:PackageVersion=$Version /p:Version=$Version;
- name: Upload packages artefacts
uses: actions/upload-artifact@v1.0.0
Expand Down
8 changes: 6 additions & 2 deletions .releaserc.json
Expand Up @@ -4,8 +4,12 @@
"+([0-9])?(.{+([0-9]),x}).x",
"master",
{
"name": "develop",
"prerelease": true
"name": "alpha",
"prerelease": true
},
{
"name": "preview",
"prerelease": true
}
],
"plugins": [
Expand Down
2 changes: 2 additions & 0 deletions Casbin.Benchmark/EnforcerBenchmark.cs
Expand Up @@ -145,12 +145,14 @@ private void GlobalSetupFromFile(string modelFileName, string policyFileName = n
{
NowEnforcer = new Enforcer(
GetTestFilePath(modelFileName));
NowEnforcer.EnableCache(false);
return;
}

NowEnforcer = new Enforcer(
GetTestFilePath(modelFileName),
GetTestFilePath(policyFileName));
NowEnforcer.EnableCache(false);
}
#endregion

Expand Down
252 changes: 252 additions & 0 deletions Casbin.Benchmark/EnforcerWithCacheBenchmark.cs
@@ -0,0 +1,252 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Jobs;
using Casbin.Extensions;
using static Casbin.Benchmark.TestHelper;

namespace Casbin.Benchmark
{
[MemoryDiagnoser]
[BenchmarkCategory("EnforcerWithCache")]
[SimpleJob(RunStrategy.Throughput, targetCount: 10, runtimeMoniker: RuntimeMoniker.Net48)]
[SimpleJob(RunStrategy.Throughput, targetCount: 10, runtimeMoniker: RuntimeMoniker.NetCoreApp31, baseline: true)]
[SimpleJob(RunStrategy.Throughput, targetCount: 10, runtimeMoniker: RuntimeMoniker.NetCoreApp50)]
public class EnforcerWithCacheBenchmark
{
private Enforcer NowEnforcer { get; set; }
private string NowTestUserName { get; set; }
private string NowTestDataName { get; set; }
private TestResource NowTestResource { get; set; }
private int[][] RbacScale { get; } =
{
new[] {100, 1000}, // Small
new[] {1000, 10000}, // Medium
new[] {10000, 100000} // Large
};

#region GlobalSetup
[GlobalSetup(Targets = new[] {nameof(BasicModel)})]
public void GlobalSetupForBasicModel()
{
GlobalSetupFromFile("basic_model.conf", "basic_policy.csv");
Console.WriteLine("// Set the Basic enforcer");
}

[GlobalSetup(Targets = new[] {nameof(RbacModel)})]
public void GlobalSetupForRbacModel()
{
GlobalSetupFromFile("rbac_model.conf", "rbac_policy.csv");
Console.WriteLine("// Set the Rbac Model enforcer");
}

[GlobalSetup(Targets = new[] {nameof(RbacModelWithSmallScale)})]
public void GlobalSetupForRbacModelWithSmallScale()
{
int groupCount = RbacScale[0][0];
int userCount = RbacScale[0][1];
GlobalSetupForRbacModelWithScale(groupCount, userCount);
Console.WriteLine($"// Set the Rbac Model with small scale ({groupCount} groups and {userCount} users) enforcer.");
}

[GlobalSetup(Targets = new[] {nameof(RbacModelWithMediumScale)})]
public void GlobalSetupForRbacModelWithMediumScale()
{
int groupCount = RbacScale[1][0];
int userCount = RbacScale[1][1];
GlobalSetupForRbacModelWithScale(groupCount, userCount);
Console.WriteLine($"// Set the Rbac Model with medium scale ({groupCount} groups and {userCount} users) enforcer.");
}

[GlobalSetup(Targets = new[] { nameof(RbacModelWithLargeScale) })]
public void GlobalSetupForRbacModelWithLargeScale()
{
int groupCount = RbacScale[2][0];
int userCount = RbacScale[2][1];
GlobalSetupForRbacModelWithScale(groupCount, userCount);
Console.WriteLine($"// Set the RBAC with large scale ({groupCount} groups and {userCount} users) enforcer.");
}

[GlobalSetup(Targets = new[] {nameof(RbacModelWithResourceRoles)})]
public void GlobalSetupForRbacModelWithResourceRoles()
{
GlobalSetupFromFile("rbac_with_resource_roles_model.conf", "rbac_with_resource_roles_policy.csv");
Console.WriteLine("// Set the RbacModel With Resource Roles enforcer");
}

[GlobalSetup(Targets = new[] {nameof(RbacModelWithDomains)})]
public void GlobalSetupForRbacModelWithDomains()
{
GlobalSetupFromFile("rbac_with_domains_model.conf", "rbac_with_domains_policy.csv");
Console.WriteLine("// Set the Rbac Model With Domains enforcer");
}

[GlobalSetup(Targets = new[] {nameof(RbacModelWithDeny)})]
public void GlobalSetupForRbacModelWithDeny()
{
GlobalSetupFromFile("rbac_with_deny_model.conf", "rbac_with_deny_policy.csv");
Console.WriteLine("// Set the Rbac Model With Deny enforcer");
}

[GlobalSetup(Targets = new[] {nameof(AbacModel)})]
public void GlobalSetupForAbacModel()
{
GlobalSetupFromFile("abac_model.conf");
NowTestResource = new TestResource("data1", "alice");
Console.WriteLine("// Set the Abac Model enforcer");
}

[GlobalSetup(Targets = new[] {nameof(KeyMatchModel)})]
public void GlobalSetupForKeyMatchModel()
{
GlobalSetupFromFile("keymatch_model.conf", "keymatch_policy.csv");
Console.WriteLine("// Set the Key Match Model enforcer");
}

[GlobalSetup(Targets = new[] {nameof(PriorityModel)})]
public void GlobalSetupForPriorityModel()
{
GlobalSetupFromFile("priority_model.conf", "priority_policy.csv");
Console.WriteLine("// Set the Priority Model enforcer");
}
#endregion

#region private help method
private void GlobalSetupForRbacModelWithScale(int groupCount, int userCount)
{
GlobalSetupForRbacModel();
var policyList = new List<List<string>>();
for (int i = 0; i < groupCount; i++)
{
policyList.Add( new[] {$"group{i}", $"data{i / 10}", "read"}.ToList());
}
NowEnforcer.AddPolicies(policyList);

policyList.Clear();
for (int i = 0; i < userCount; i++)
{
policyList.Add( new[] {$"user{i}", $"group{i / 10}"}.ToList());
}
NowEnforcer.EnableAutoBuildRoleLinks(false);
NowEnforcer.AddGroupingPolicies(policyList);
NowEnforcer.BuildRoleLinks();

NowTestUserName = $"user{userCount / 2 + 1}"; // if 1000 => 501...
NowTestDataName = $"data{groupCount / 10 - 1}"; // if 100 => 9...
Console.WriteLine($"// Already set user name to {NowTestUserName}.");
Console.WriteLine($"// Already set data name to {NowTestDataName}.");
}

private void GlobalSetupFromFile(string modelFileName, string policyFileName = null)
{
if (policyFileName is null)
{
NowEnforcer = new Enforcer(GetTestFilePath(modelFileName));
NowEnforcer.EnableCache(true);
return;
}

NowEnforcer = new Enforcer(GetTestFilePath(modelFileName), GetTestFilePath(policyFileName));
NowEnforcer.EnableCache(true);
}
#endregion

[GlobalCleanup]
public void GlobalCleanup()
{
NowEnforcer = null;
Console.WriteLine("// Cleaned the enforcer");
}

[Benchmark]
//[Benchmark(Description = "ACL, 2 rules (2 users)")]
[BenchmarkCategory("BasicModel")]
public void BasicModel()
{
_ = NowEnforcer.Enforce("alice", "data1", "read");
}

[Benchmark]
//[Benchmark(Description = "RBAC, 5 rules (2 users, 1 role)")]
[BenchmarkCategory("RbacModel")]
public void RbacModel()
{
_ = NowEnforcer.Enforce("alice", "data2", "read");
}

[Benchmark]
//[Benchmark(Description = "RBAC (small), 1100 rules (1000 users, 100 roles)")]
[BenchmarkCategory("RbacModel")]
public void RbacModelWithSmallScale()
{
_ = NowEnforcer.Enforce(NowTestUserName, NowTestDataName, "read");
}

[Benchmark]
//[Benchmark(Description = "RBAC (medium), 11000 rules (10000 users, 1000 roles)")]
[BenchmarkCategory("RbacModel")]
public void RbacModelWithMediumScale()
{
_ = NowEnforcer.Enforce(NowTestUserName, NowTestDataName, "read");
}

[Benchmark]
//[Benchmark(Description = "RBAC (large), 110000 rules (100000 users, 10000 roles)")]
[BenchmarkCategory("RbacModel")]
public void RbacModelWithLargeScale()
{
_ = NowEnforcer.Enforce(NowTestUserName, NowTestDataName, "read");
}

[Benchmark]
//[Benchmark(Description = "RBAC with resource roles, 6 rules (2 users, 2 roles)")]
[BenchmarkCategory("RbacModel")]
public void RbacModelWithResourceRoles()
{
_ = NowEnforcer.Enforce("alice", "data1", "read");
}

[Benchmark]
//[Benchmark(Description = "RBAC with domains/tenants, 6 rules (2 users, 1 role, 2 domains)")]
[BenchmarkCategory("RbacModel")]
public void RbacModelWithDomains()
{
_ = NowEnforcer.Enforce("alice", "domain1", "data1", "read");
}

[Benchmark]
//[Benchmark(Description = "Deny-override, 6 rules (2 users, 1 role)")]
[BenchmarkCategory("RbacModel")]
public void RbacModelWithDeny()
{
_ = NowEnforcer.Enforce("alice", "data1", "read");
}

[Benchmark]
//[Benchmark(Description = "ABAC, 0 rule (0 user)")]
[BenchmarkCategory("AbacModel")]
public void AbacModel()
{
var data1 = NowTestResource;
_ = NowEnforcer.Enforce("alice", data1, "read");
}

[Benchmark]
//[Benchmark(Description = "RESTful, 5 rules (3 users)")]
[BenchmarkCategory("KeyMatchModel")]
public void KeyMatchModel()
{
_ = NowEnforcer.Enforce("alice", "/alice_data/resource1", "GET");
}

[Benchmark]
//[Benchmark(Description = "Priority, 9 rules (2 users, 2 roles)")]
[BenchmarkCategory("PriorityModel")]
public void PriorityModel()
{
_ = NowEnforcer.Enforce("alice", "data1", "read");
}
}
}
File renamed without changes.
1 change: 1 addition & 0 deletions Casbin.NET.sln
Expand Up @@ -12,6 +12,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
.releaserc.json = .releaserc.json
azure-pipelines.yml = azure-pipelines.yml
.github\workflows\build-and-release.yml = .github\workflows\build-and-release.yml
LICENSE = LICENSE
Expand Down
14 changes: 9 additions & 5 deletions NetCasbin.UnitTest/Casbin.UnitTests.csproj
@@ -1,25 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net5</TargetFrameworks>
<TargetFrameworks>net5;net461;net452</TargetFrameworks>
<DebugType>full</DebugType>
<IsPackable>false</IsPackable>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.0">
<PackageReference Include="coverlet.collector" Version="3.0.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' != 'net452'">
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetCasbin\Casbin.csproj" />
</ItemGroup>
Expand Down

0 comments on commit 623d64b

Please sign in to comment.