-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
175 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using BenchmarkDotNet.Attributes; | ||
|
||
using Bogus; | ||
|
||
namespace Privileged.Tests; | ||
|
||
[MemoryDiagnoser] | ||
public class AuthorizationContextBenchmarks | ||
{ | ||
private List<AuthorizationRule> _rules; | ||
private AuthorizationContext _authorziationContext; | ||
|
||
public AuthorizationContextBenchmarks() | ||
{ | ||
var generator = new Faker<AuthorizationRule>("en_US") | ||
.UseSeed(2024) | ||
.CustomInstantiator((f) => | ||
new AuthorizationRule | ||
( | ||
Action: f.Random.WeightedRandom(["read", "update", "delete", "all"], [.40f, .30f, .20f, 10f]), | ||
Subject: f.Name.FirstName(), | ||
Fields: f.Random.WeightedRandom<List<string>?>([null, ["id"]], [.90f, .10f]), | ||
Denied: f.Random.WeightedRandom([false, true], [.90f, .10f]) | ||
) | ||
); | ||
|
||
_rules = generator.Generate(1000); | ||
_authorziationContext = new AuthorizationContext(_rules); | ||
} | ||
|
||
[Benchmark] | ||
public bool AuthorizedReadBenchmark() | ||
{ | ||
return _authorziationContext.Authorized("read", "Bob"); | ||
} | ||
|
||
[Benchmark] | ||
public bool AuthorizedAllBenchmark() | ||
{ | ||
return _authorziationContext.Authorized("delete", "all"); | ||
} | ||
|
||
[Benchmark] | ||
public bool AuthorizedFieldBenchmark() | ||
{ | ||
return _authorziationContext.Authorized("update", "Allen", "id"); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
test/Privileged.Tests/AuthorizationContextPerformanceTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using BenchmarkDotNet.Configs; | ||
using BenchmarkDotNet.Exporters; | ||
using BenchmarkDotNet.Jobs; | ||
using BenchmarkDotNet.Loggers; | ||
using BenchmarkDotNet.Running; | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace Privileged.Tests; | ||
|
||
public class AuthorizationContextPerformanceTests | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public AuthorizationContextPerformanceTests(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public void BenchmarkRunnerTest() | ||
{ | ||
var config = ManualConfig | ||
.CreateMinimumViable() | ||
.AddJob(Job.ShortRun) | ||
.WithOptions(ConfigOptions.DisableOptimizationsValidator) | ||
.WithOption(ConfigOptions.JoinSummary, true); | ||
|
||
var summary = BenchmarkRunner.Run<AuthorizationContextBenchmarks>(config); | ||
|
||
// write benchmark summary | ||
var logger = new AccumulationLogger(); | ||
|
||
MarkdownExporter.Console.ExportToLog(summary, logger); | ||
|
||
_output.WriteLine(logger.GetLog()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
global using Xunit; | ||
global using FluentAssertions; | ||
global using Xunit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
using Xunit.Abstractions; | ||
|
||
namespace Privileged.Tests; | ||
|
||
public class JsonSerializationTests | ||
{ | ||
private readonly ITestOutputHelper _output; | ||
|
||
public JsonSerializationTests(ITestOutputHelper output) | ||
{ | ||
_output = output; | ||
} | ||
|
||
[Fact] | ||
public void SerializationRules() | ||
{ | ||
var context = new AuthorizationBuilder() | ||
.Allow("test", AuthorizationSubjects.All) | ||
.Allow(AuthorizationActions.All, "Post") | ||
.Allow("read", "User", ["title", "id"]) | ||
.Forbid("publish", "Post") | ||
.Build(); | ||
|
||
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web) | ||
{ | ||
WriteIndented = true, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
TypeInfoResolver = AuthorizationJsonContext.Default | ||
}; | ||
|
||
var json = JsonSerializer.Serialize(context.Rules, options); | ||
json.Should().NotBeNullOrEmpty(); | ||
|
||
_output.WriteLine(json); | ||
|
||
var rules = JsonSerializer.Deserialize<List<AuthorizationRule>>(json, options); | ||
rules.Should().NotBeNullOrEmpty(); | ||
rules.Count.Should().Be(4); | ||
Check warning on line 41 in test/Privileged.Tests/JsonSerializationTests.cs GitHub Actions / build
|
||
|
||
var jsonContext = new AuthorizationContext(rules); | ||
jsonContext.Should().NotBeNull(); | ||
|
||
var contextJson = JsonSerializer.Serialize(jsonContext, options); | ||
contextJson.Should().NotBeNullOrEmpty(); | ||
} | ||
} | ||
|
||
[JsonSerializable(typeof(AuthorizationContext))] | ||
[JsonSerializable(typeof(AuthorizationRule))] | ||
[JsonSerializable(typeof(List<AuthorizationRule>))] | ||
[JsonSerializable(typeof(IReadOnlyCollection<AuthorizationRule>))] | ||
public partial class AuthorizationJsonContext : JsonSerializerContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters