Skip to content

Commit

Permalink
Adding benchmark for new ValidateTokenAsync model vs old (#2779)
Browse files Browse the repository at this point in the history
* Adding benchmark for new ValidateTokenAsync model vs old.

* Update benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs

Co-authored-by: Keegan Caruso <Keegan.Caruso@microsoft.com>

* Updated class naming to be more explicit and added benchmark using clone

* Combine and categorize benchmarks into ValidateTokenAsyncTests

---------

Co-authored-by: Franco Fung <francofung@microsoft.com>
Co-authored-by: Keegan Caruso <Keegan.Caruso@microsoft.com>
  • Loading branch information
3 people authored Aug 19, 2024
1 parent 34a421f commit a120bde
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 13 deletions.
5 changes: 3 additions & 2 deletions benchmark/Microsoft.IdentityModel.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ private static void DebugThroughTests()

ValidateTokenAsyncTests validateTokenAsyncTests = new ValidateTokenAsyncTests();
validateTokenAsyncTests.Setup();
TokenValidationResult tokenValidationResult = validateTokenAsyncTests.JsonWebTokenHandler_ValidateTokenAsync().Result;
var claims = validateTokenAsyncTests.JsonWebTokenHandler_ValidateTokenAsync_CreateClaims();
TokenValidationResult tokenValidationResult = validateTokenAsyncTests.JsonWebTokenHandler_ValidateTokenAsyncWithTVP().Result;
TokenValidationResult validationResult = validateTokenAsyncTests.JsonWebTokenHandler_ValidateTokenAsyncWithVP().Result;
var claims = validateTokenAsyncTests.JsonWebTokenHandler_ValidateTokenAsyncWithTVP_CreateClaims();

ValidateSignedHttpRequestAsyncTests validateSignedHttpRequestAsyncTests = new ValidateSignedHttpRequestAsyncTests();
validateSignedHttpRequestAsyncTests.Setup();
Expand Down
132 changes: 121 additions & 11 deletions benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@
using System.Security.Claims;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.Tokens;

namespace Microsoft.IdentityModel.Benchmarks
{
// dotnet run -c release -f net8.0 --filter Microsoft.IdentityModel.Benchmarks.ValidateTokenAsyncTests*

[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory)]
public class ValidateTokenAsyncTests
{
private CallContext _callContext;
private JsonWebTokenHandler _jsonWebTokenHandler;
private JwtSecurityTokenHandler _jwtSecurityTokenHandler;
private SecurityTokenDescriptor _tokenDescriptor;
private SecurityTokenDescriptor _tokenDescriptorExtendedClaims;
private string _jws;
private string _jwsExtendedClaims;
private TokenValidationParameters _validationParameters;
private TokenValidationParameters _tokenValidationParameters;
private TokenValidationParameters _invalidTokenValidationParameters;
private ValidationParameters _validationParameters;
private ValidationParameters _invalidValidationParameters;

[GlobalSetup]
public void Setup()
Expand All @@ -46,29 +52,133 @@ public void Setup()
_jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
_jwtSecurityTokenHandler.SetDefaultTimesOnTokenCreation = false;

_validationParameters = new TokenValidationParameters()
_tokenValidationParameters = new TokenValidationParameters()
{
ValidAudience = BenchmarkUtils.Audience,
ValidateLifetime = true,
ValidIssuer = BenchmarkUtils.Issuer,
IssuerSigningKey = BenchmarkUtils.SigningCredentialsRsaSha256.Key,
};

_validationParameters = new ValidationParameters();
_validationParameters.ValidAudiences.Add(BenchmarkUtils.Audience);
_validationParameters.ValidIssuers.Add(BenchmarkUtils.Issuer);
_validationParameters.IssuerSigningKeys.Add(BenchmarkUtils.SigningCredentialsRsaSha256.Key);

_invalidTokenValidationParameters = new TokenValidationParameters()
{
ValidAudience = BenchmarkUtils.Audience,
ValidateLifetime = true,
ValidIssuer = BenchmarkUtils.Issuer,
ValidateIssuerSigningKey = true,
ValidateTokenReplay = true,
ValidateSignatureLast = true
};

_invalidValidationParameters = new ValidationParameters();
_invalidValidationParameters.ValidAudiences.Add(BenchmarkUtils.Audience);
_invalidValidationParameters.ValidIssuers.Add(BenchmarkUtils.Issuer);

_callContext = new CallContext();
}

[BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark]
public async Task<TokenValidationResult> JwtSecurityTokenHandler_ValidateTokenAsync() => await _jwtSecurityTokenHandler.ValidateTokenAsync(_jws, _tokenValidationParameters).ConfigureAwait(false);

[BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark(Baseline = true)]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithTVP() => await _jsonWebTokenHandler.ValidateTokenAsync(_jws, _tokenValidationParameters).ConfigureAwait(false);

[BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithTVPUsingModifiedClone()
{
var tokenValidationParameters = _tokenValidationParameters.Clone();
tokenValidationParameters.ValidIssuer = "different-issuer";
tokenValidationParameters.ValidAudience = "different-audience";
tokenValidationParameters.ValidateLifetime = false;
return await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, tokenValidationParameters).ConfigureAwait(false);
}

[BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithTVP_SucceedOnThirdAttempt()
{
TokenValidationResult result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false);

return result;
}

[BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithTVPUsingClone_SucceedOnThirdAttempt()
{
TokenValidationResult result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters.Clone()).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters.Clone()).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters.Clone()).ConfigureAwait(false);

return result;
}

[Benchmark]
public async Task<List<Claim>> JsonWebTokenHandler_ValidateTokenAsync_CreateClaims()
[BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithTVP_SucceedOnFifthAttempt()
{
var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters).ConfigureAwait(false);
TokenValidationResult result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false);

return result;
}

[BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithTVPUsingClone_SucceedOnFifthAttempt()
{
TokenValidationResult result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters.Clone()).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters.Clone()).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters.Clone()).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidTokenValidationParameters.Clone()).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters.Clone()).ConfigureAwait(false);

return result;
}

[BenchmarkCategory("ValidateTokenAsyncWithValidationParameters"), Benchmark(Baseline = true)]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithVP() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false);

[BenchmarkCategory("ValidateTokenAsyncWithValidationParameters"), Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithVP_SucceedOnThirdAttempt()
{
var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, null).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, null).ConfigureAwait(false);
return await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false);
}

[BenchmarkCategory("ValidateTokenAsyncWithValidationParameters"), Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsyncWithVP_SucceedOnFifthAttempt()
{
var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, null).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, null).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, null).ConfigureAwait(false);
result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _invalidValidationParameters, _callContext, null).ConfigureAwait(false);
return await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false);
}

[BenchmarkCategory("ValidateTokenAsyncClaimAccess"), Benchmark]
public async Task<List<Claim>> JsonWebTokenHandler_ValidateTokenAsyncWithVP_CreateClaims()
{
var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false);
var claimsIdentity = result.ClaimsIdentity;
var claims = claimsIdentity.Claims;
return claims.ToList();
}

[Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateTokenAsync() => await _jsonWebTokenHandler.ValidateTokenAsync(_jws, _validationParameters).ConfigureAwait(false);

[Benchmark]
public async Task<TokenValidationResult> JwtSecurityTokenHandler_ValidateTokenAsync() => await _jwtSecurityTokenHandler.ValidateTokenAsync(_jws, _validationParameters).ConfigureAwait(false);

[BenchmarkCategory("ValidateTokenAsyncClaimAccess"), Benchmark]
public async Task<List<Claim>> JsonWebTokenHandler_ValidateTokenAsyncWithTVP_CreateClaims()
{
var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false);
var claimsIdentity = result.ClaimsIdentity;
var claims = claimsIdentity.Claims;
return claims.ToList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
[assembly: InternalsVisibleTo("Microsoft.IdentityModel.S2S.Tokens, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: InternalsVisibleTo("Microsoft.IdentityModel.S2S.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: InternalsVisibleTo("Microsoft.IdentityModel.S2S.Tokens.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]
[assembly: InternalsVisibleTo("Microsoft.IdentityModel.Benchmarks, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b5fc90e7027f67871e773a8fde8938c81dd402ba65b9201d60593e96c492651e889cc13f1415ebb53fac1131ae0bd333c5ee6021672d9718ea31a8aebd0da0072f25d87dba6fc90ffd598ed4da35e44c398c454307e8e33b8426143daec9f596836f97c8f74750e5975c64e2189f45def46b2a2b1247adc3652bf5c308055da9")]

0 comments on commit a120bde

Please sign in to comment.