-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathModelBenchmark.cs
More file actions
64 lines (54 loc) · 2.04 KB
/
ModelBenchmark.cs
File metadata and controls
64 lines (54 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Jobs;
namespace Casbin.Benchmark
{
[MemoryDiagnoser]
[BenchmarkCategory("Model")]
[SimpleJob(RunStrategy.Throughput, RuntimeMoniker.Net60, baseline: true)]
[SimpleJob(RunStrategy.Throughput, RuntimeMoniker.Net70)]
[SimpleJob(RunStrategy.Throughput, RuntimeMoniker.Net80)]
public class ModelBenchmark
{
private readonly Enforcer _enforcer;
public ModelBenchmark()
{
_enforcer = new Enforcer(TestHelper.GetTestFilePath("rbac_model.conf"));
}
private string NowTestUserName { get; set; }
private string NowTestDataName { get; set; }
[Params(10, 100, 1000, 10000)] public int NowPolicyCount { get; set; }
[GlobalSetup(Targets = new[] { nameof(AddPolicy), nameof(HasPolicy), nameof(RemovePolicy) })]
public void GlobalSetup()
{
for (int i = 0; i < NowPolicyCount; i++)
{
_enforcer.AddPolicy($"group{i}", $"obj{i / 10}", "read");
}
Console.WriteLine($"// Already set {NowPolicyCount} policies.");
NowTestUserName = $"name{NowPolicyCount / 2 + 1}";
NowTestDataName = $"data{NowPolicyCount / 2 + 1}";
Console.WriteLine($"// Already set user name to {NowTestUserName}.");
Console.WriteLine($"// Already set data name to {NowTestDataName}.");
}
[Benchmark]
[BenchmarkCategory("ModelManagement")]
public void HasPolicy()
{
_enforcer.HasPolicy(NowTestUserName, NowTestDataName, "read");
}
[Benchmark]
[BenchmarkCategory("ModelManagement")]
public void AddPolicy()
{
_enforcer.AddPolicy(NowTestUserName, NowTestDataName, "read");
}
[Benchmark]
[BenchmarkCategory("ModelManagement")]
public void RemovePolicy()
{
_enforcer.RemovePolicy(NowTestUserName, NowTestDataName, "read");
}
}
}