Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added benchmarks to look at the performance of Audiences Vs Audience …
Browse files Browse the repository at this point in the history
…Members
JoshLozensky committed May 14, 2024
1 parent bd23bd3 commit d52ac1a
Showing 2 changed files with 83 additions and 0 deletions.
45 changes: 45 additions & 0 deletions benchmark/Microsoft.IdentityModel.Benchmarks/BenchmarkUtils.cs
Original file line number Diff line number Diff line change
@@ -16,6 +16,14 @@ public class BenchmarkUtils

public const string Audience = "http://www.contoso.com/protected";

public readonly static IEnumerable<string> Audiences = [

Check failure on line 19 in benchmark/Microsoft.IdentityModel.Benchmarks/BenchmarkUtils.cs

GitHub Actions / Wilson GitHub Action Test

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.

Check failure on line 19 in benchmark/Microsoft.IdentityModel.Benchmarks/BenchmarkUtils.cs

GitHub Actions / Wilson GitHub Action Test

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.
"http://www.contoso.com/protected",
"http://www.contoso.com/protected1",
"http://www.contoso.com/protected2",
"http://www.contoso.com/protected3",
"http://www.contoso.com/protected4"
];

private static RSA _rsa;
private static SymmetricSecurityKey _symmetricKey;

@@ -60,6 +68,43 @@ public static Dictionary<string, object> Claims
}
}

public static Dictionary<string, object> ClaimsNoAudience
{
get
{
DateTime now = DateTime.UtcNow;
return new Dictionary<string, object>()
{
{ "role", new List<string>() { "role1", "Developer", "Sales"} },
{ JwtRegisteredClaimNames.Email, "Bob@contoso.com" },
{ JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(now + TimeSpan.FromDays(1)) },
{ JwtRegisteredClaimNames.Nbf, EpochTime.GetIntDate(now) },
{ JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(now) },
{ JwtRegisteredClaimNames.GivenName, "Bob" },
{ JwtRegisteredClaimNames.Iss, Issuer },
};
}
}

public static Dictionary<string, object> ClaimsMultipleAudiences
{
get
{
DateTime now = DateTime.UtcNow;
return new Dictionary<string, object>()
{
{ "role", new List<string>() { "role1", "Developer", "Sales"} },
{ JwtRegisteredClaimNames.Email, "Bob@contoso.com" },
{ JwtRegisteredClaimNames.Exp, EpochTime.GetIntDate(now + TimeSpan.FromDays(1)) },
{ JwtRegisteredClaimNames.Nbf, EpochTime.GetIntDate(now) },
{ JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(now) },
{ JwtRegisteredClaimNames.GivenName, "Bob" },
{ JwtRegisteredClaimNames.Iss, Issuer },
{ JwtRegisteredClaimNames.Aud, Audiences }
};
}
}

public static SigningCredentials SigningCredentialsRsaSha256 => new(RsaSecurityKey, SecurityAlgorithms.RsaSha256, SecurityAlgorithms.Sha256);

public static EncryptingCredentials EncryptingCredentialsAes256Sha512 => new(SymmetricEncryptionKey512, "dir", SecurityAlgorithms.Aes256CbcHmacSha512);
38 changes: 38 additions & 0 deletions benchmark/Microsoft.IdentityModel.Benchmarks/CreateTokenTests.cs
Original file line number Diff line number Diff line change
@@ -14,6 +14,9 @@ public class CreateTokenTests
{
private JsonWebTokenHandler _jsonWebTokenHandler;
private SecurityTokenDescriptor _tokenDescriptor;
private SecurityTokenDescriptor _tokenDescriptorMultipleAudiencesMemberAndClaims;
private SecurityTokenDescriptor _tokenDescriptorMultipleAudiencesMemberOnly;
private SecurityTokenDescriptor _tokenDescriptorSingleAudienceUsingAudiencesMember;

[GlobalSetup]
public void Setup()
@@ -25,9 +28,44 @@ public void Setup()
Claims = BenchmarkUtils.Claims,
SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256,
};

_tokenDescriptorMultipleAudiencesMemberOnly = new SecurityTokenDescriptor
{
Claims = BenchmarkUtils.ClaimsNoAudience,
SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256,
Audiences = BenchmarkUtils.Audiences,
};

_tokenDescriptorMultipleAudiencesMemberAndClaims = new SecurityTokenDescriptor
{
Claims = BenchmarkUtils.ClaimsMultipleAudiences,
SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256,
Audiences = BenchmarkUtils.Audiences,
};

_tokenDescriptorSingleAudienceUsingAudiencesMember = new SecurityTokenDescriptor
{
Claims = BenchmarkUtils.ClaimsNoAudience,
SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256,
Audiences = [BenchmarkUtils.Audience],

Check failure on line 50 in benchmark/Microsoft.IdentityModel.Benchmarks/CreateTokenTests.cs

GitHub Actions / Wilson GitHub Action Test

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.

Check failure on line 50 in benchmark/Microsoft.IdentityModel.Benchmarks/CreateTokenTests.cs

GitHub Actions / Wilson GitHub Action Test

Feature 'collection expressions' is not available in C# 10.0. Please use language version 12.0 or greater.
};
}

[Benchmark]
public string JsonWebTokenHandler_CreateToken() => _jsonWebTokenHandler.CreateToken(_tokenDescriptor);

[Benchmark]
public string JsonWebTokenHandler_CreateToken_SingleAudienceUsingAudiencesMemberOnly() =>
_jsonWebTokenHandler.CreateToken(_tokenDescriptorSingleAudienceUsingAudiencesMember);

[Benchmark]
public string JsonWebTokenHandler_CreateToken_MultipleAudiencesMemberOnly() =>
_jsonWebTokenHandler.CreateToken(_tokenDescriptorMultipleAudiencesMemberOnly);

[Benchmark]
public string JsonWebTokenHandler_CreateToken_MultipleAudiencesMemberAndClaims() =>
_jsonWebTokenHandler.CreateToken(_tokenDescriptorMultipleAudiencesMemberAndClaims);


}
}

0 comments on commit d52ac1a

Please sign in to comment.