From 4067f673a05d4eee046fc5a09ee96b62fa27fc75 Mon Sep 17 00:00:00 2001 From: Franco Fung Date: Tue, 13 Aug 2024 09:08:45 -0700 Subject: [PATCH 1/4] Adding benchmark for new ValidateTokenAsync model vs old. --- .../ValidateTokenAsyncWithVPTests.cs | 86 +++++++++++++++++++ .../Properties/AssemblyInfo.cs | 1 + 2 files changed, 87 insertions(+) create mode 100644 benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs diff --git a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs new file mode 100644 index 0000000000..ca402f1305 --- /dev/null +++ b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs @@ -0,0 +1,86 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Collections.Generic; +using System.Linq; +using System.Security.Claims; +using System.Threading.Tasks; +using BenchmarkDotNet.Attributes; +using Microsoft.IdentityModel.JsonWebTokens; +using Microsoft.IdentityModel.Tokens; + +namespace Microsoft.IdentityModel.Benchmarks +{ + // dotnet run -c release -f net8.0 --filter Microsoft.IdentityModel.Benchmarks.ValidateTokenAsyncWithVPTests* + + public class ValidateTokenAsyncWithVPTests + { + private CallContext _callContext; + private JsonWebTokenHandler _jsonWebTokenHandler; + private SecurityTokenDescriptor _tokenDescriptor; + private SecurityTokenDescriptor _tokenDescriptorExtendedClaims; + private string _jws; + private string _jwsExtendedClaims; + private TokenValidationParameters _tokenValidationParameters; //TODO:Add additional test to compare with ValidationParameters also use clone for perf. + private ValidationParameters _validationParameters; + + [GlobalSetup] + public void Setup() + { + _tokenDescriptor = new SecurityTokenDescriptor + { + Claims = BenchmarkUtils.Claims, + SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256, + }; + + _tokenDescriptorExtendedClaims = new SecurityTokenDescriptor + { + Claims = BenchmarkUtils.ClaimsExtendedExample, + SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256, + }; + + _jsonWebTokenHandler = new JsonWebTokenHandler(); + _jws = _jsonWebTokenHandler.CreateToken(_tokenDescriptor); + _jwsExtendedClaims = _jsonWebTokenHandler.CreateToken(_tokenDescriptorExtendedClaims); + + _validationParameters = new ValidationParameters(); + _validationParameters.ValidAudiences.Add(BenchmarkUtils.Audience); + _validationParameters.ValidIssuers.Add(BenchmarkUtils.Issuer); + _validationParameters.IssuerSigningKeys.Add(BenchmarkUtils.SigningCredentialsRsaSha256.Key); + + _callContext = new CallContext(); + + _tokenValidationParameters = new TokenValidationParameters() + { + ValidAudience = BenchmarkUtils.Audience, + ValidateLifetime = true, + ValidIssuer = BenchmarkUtils.Issuer, + IssuerSigningKey = BenchmarkUtils.SigningCredentialsRsaSha256.Key, + }; + } + + [Benchmark] + public async Task> 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> JsonWebTokenHandler_ValidateTokenAsync_CreateClaims() + { + var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false); + var claimsIdentity = result.ClaimsIdentity; + var claims = claimsIdentity.Claims; + return claims.ToList(); + } + + [Benchmark] + public async Task JsonWebTokenHandler_ValidateTokenAsyncWithVP() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false); + + [Benchmark] + public async Task JsonWebTokenHandler_ValidateTokenAsync() => await _jsonWebTokenHandler.ValidateTokenAsync(_jws, _tokenValidationParameters).ConfigureAwait(false); + } +} diff --git a/src/Microsoft.IdentityModel.JsonWebTokens/Properties/AssemblyInfo.cs b/src/Microsoft.IdentityModel.JsonWebTokens/Properties/AssemblyInfo.cs index 9ec5a0543e..4b6c640a87 100644 --- a/src/Microsoft.IdentityModel.JsonWebTokens/Properties/AssemblyInfo.cs +++ b/src/Microsoft.IdentityModel.JsonWebTokens/Properties/AssemblyInfo.cs @@ -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")] From ea1c1be2492df83347d1a0afb82bde859417099a Mon Sep 17 00:00:00 2001 From: Franco Fung <38921563+FuPingFranco@users.noreply.github.com> Date: Tue, 13 Aug 2024 10:28:19 -0700 Subject: [PATCH 2/4] Update benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs Co-authored-by: Keegan Caruso --- .../ValidateTokenAsyncWithVPTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs index ca402f1305..129f83f665 100644 --- a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs +++ b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs @@ -81,6 +81,6 @@ public async Task> JsonWebTokenHandler_ValidateTokenAsync_CreateClai public async Task JsonWebTokenHandler_ValidateTokenAsyncWithVP() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false); [Benchmark] - public async Task JsonWebTokenHandler_ValidateTokenAsync() => await _jsonWebTokenHandler.ValidateTokenAsync(_jws, _tokenValidationParameters).ConfigureAwait(false); + public async Task JsonWebTokenHandler_ValidateTokenAsync() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false); } } From a7d26802576d2d11c0f3481199f6c583a4ea25aa Mon Sep 17 00:00:00 2001 From: Franco Fung Date: Wed, 14 Aug 2024 11:39:44 -0700 Subject: [PATCH 3/4] Updated class naming to be more explicit and added benchmark using clone --- ...okenAsyncWithValidationParametersTests.cs} | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) rename benchmark/Microsoft.IdentityModel.Benchmarks/{ValidateTokenAsyncWithVPTests.cs => ValidateTokenAsyncWithValidationParametersTests.cs} (73%) diff --git a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithValidationParametersTests.cs similarity index 73% rename from benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs rename to benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithValidationParametersTests.cs index 129f83f665..7bd5e999cf 100644 --- a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithVPTests.cs +++ b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithValidationParametersTests.cs @@ -11,9 +11,9 @@ namespace Microsoft.IdentityModel.Benchmarks { - // dotnet run -c release -f net8.0 --filter Microsoft.IdentityModel.Benchmarks.ValidateTokenAsyncWithVPTests* + // dotnet run -c release -f net8.0 --filter Microsoft.IdentityModel.Benchmarks.ValidateTokenAsyncWithValidationParametersTests* - public class ValidateTokenAsyncWithVPTests + public class ValidateTokenAsyncWithValidationParametersTests { private CallContext _callContext; private JsonWebTokenHandler _jsonWebTokenHandler; @@ -21,7 +21,7 @@ public class ValidateTokenAsyncWithVPTests private SecurityTokenDescriptor _tokenDescriptorExtendedClaims; private string _jws; private string _jwsExtendedClaims; - private TokenValidationParameters _tokenValidationParameters; //TODO:Add additional test to compare with ValidationParameters also use clone for perf. + private TokenValidationParameters _tokenValidationParameters; private ValidationParameters _validationParameters; [GlobalSetup] @@ -60,7 +60,7 @@ public void Setup() } [Benchmark] - public async Task> JsonWebTokenHandler_ValidateTokenAsyncWithVP_CreateClaims() + public async Task> JsonWebTokenHandler_ValidateTokenAsyncWithValidationParameters_CreateClaims() { var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false); var claimsIdentity = result.ClaimsIdentity; @@ -69,7 +69,7 @@ public async Task> JsonWebTokenHandler_ValidateTokenAsyncWithVP_Crea } [Benchmark] - public async Task> JsonWebTokenHandler_ValidateTokenAsync_CreateClaims() + public async Task> JsonWebTokenHandler_ValidateTokenAsyncWithTokenValidationParameters_CreateClaims() { var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false); var claimsIdentity = result.ClaimsIdentity; @@ -78,9 +78,19 @@ public async Task> JsonWebTokenHandler_ValidateTokenAsync_CreateClai } [Benchmark] - public async Task JsonWebTokenHandler_ValidateTokenAsyncWithVP() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false); + public async Task JsonWebTokenHandler_ValidateTokenAsyncWithValidationParameters() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false); [Benchmark] - public async Task JsonWebTokenHandler_ValidateTokenAsync() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false); + public async Task JsonWebTokenHandler_ValidateTokenAsyncWithTokenValidationParameters() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false); + + [Benchmark] + public async Task JsonWebTokenHandler_ValidateTokenAsyncWithTokenValidationParametersUsingClone() + { + var tokenValidationParameters = _tokenValidationParameters.Clone(); + tokenValidationParameters.ValidIssuer = "different-issuer"; + tokenValidationParameters.ValidAudience = "different-audience"; + tokenValidationParameters.ValidateLifetime = false; + return await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, tokenValidationParameters).ConfigureAwait(false); + } } } From ae495dd49fad3fffec18c91df502b693acc46c1c Mon Sep 17 00:00:00 2001 From: Franco Fung Date: Mon, 19 Aug 2024 00:56:25 -0700 Subject: [PATCH 4/4] Combine and categorize benchmarks into ValidateTokenAsyncTests --- .../Program.cs | 5 +- .../ValidateTokenAsyncTests.cs | 132 ++++++++++++++++-- ...TokenAsyncWithValidationParametersTests.cs | 96 ------------- 3 files changed, 124 insertions(+), 109 deletions(-) delete mode 100644 benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithValidationParametersTests.cs diff --git a/benchmark/Microsoft.IdentityModel.Benchmarks/Program.cs b/benchmark/Microsoft.IdentityModel.Benchmarks/Program.cs index 64344834e6..570a92b9af 100644 --- a/benchmark/Microsoft.IdentityModel.Benchmarks/Program.cs +++ b/benchmark/Microsoft.IdentityModel.Benchmarks/Program.cs @@ -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(); diff --git a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncTests.cs b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncTests.cs index 1bd6e9fedb..8724b49637 100644 --- a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncTests.cs +++ b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncTests.cs @@ -7,6 +7,7 @@ using System.Security.Claims; using System.Threading.Tasks; using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; using Microsoft.IdentityModel.JsonWebTokens; using Microsoft.IdentityModel.Tokens; @@ -14,15 +15,20 @@ 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() @@ -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 JwtSecurityTokenHandler_ValidateTokenAsync() => await _jwtSecurityTokenHandler.ValidateTokenAsync(_jws, _tokenValidationParameters).ConfigureAwait(false); + + [BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark(Baseline = true)] + public async Task JsonWebTokenHandler_ValidateTokenAsyncWithTVP() => await _jsonWebTokenHandler.ValidateTokenAsync(_jws, _tokenValidationParameters).ConfigureAwait(false); + + [BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark] + public async Task 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 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 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> JsonWebTokenHandler_ValidateTokenAsync_CreateClaims() + [BenchmarkCategory("ValidateTokenAsyncWithTokenValidationParameters"), Benchmark] + public async Task 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 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 JsonWebTokenHandler_ValidateTokenAsyncWithVP() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false); + + [BenchmarkCategory("ValidateTokenAsyncWithValidationParameters"), Benchmark] + public async Task 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 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> 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 JsonWebTokenHandler_ValidateTokenAsync() => await _jsonWebTokenHandler.ValidateTokenAsync(_jws, _validationParameters).ConfigureAwait(false); - - [Benchmark] - public async Task JwtSecurityTokenHandler_ValidateTokenAsync() => await _jwtSecurityTokenHandler.ValidateTokenAsync(_jws, _validationParameters).ConfigureAwait(false); - + [BenchmarkCategory("ValidateTokenAsyncClaimAccess"), Benchmark] + public async Task> JsonWebTokenHandler_ValidateTokenAsyncWithTVP_CreateClaims() + { + var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false); + var claimsIdentity = result.ClaimsIdentity; + var claims = claimsIdentity.Claims; + return claims.ToList(); + } } } diff --git a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithValidationParametersTests.cs b/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithValidationParametersTests.cs deleted file mode 100644 index 7bd5e999cf..0000000000 --- a/benchmark/Microsoft.IdentityModel.Benchmarks/ValidateTokenAsyncWithValidationParametersTests.cs +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -using System.Collections.Generic; -using System.Linq; -using System.Security.Claims; -using System.Threading.Tasks; -using BenchmarkDotNet.Attributes; -using Microsoft.IdentityModel.JsonWebTokens; -using Microsoft.IdentityModel.Tokens; - -namespace Microsoft.IdentityModel.Benchmarks -{ - // dotnet run -c release -f net8.0 --filter Microsoft.IdentityModel.Benchmarks.ValidateTokenAsyncWithValidationParametersTests* - - public class ValidateTokenAsyncWithValidationParametersTests - { - private CallContext _callContext; - private JsonWebTokenHandler _jsonWebTokenHandler; - private SecurityTokenDescriptor _tokenDescriptor; - private SecurityTokenDescriptor _tokenDescriptorExtendedClaims; - private string _jws; - private string _jwsExtendedClaims; - private TokenValidationParameters _tokenValidationParameters; - private ValidationParameters _validationParameters; - - [GlobalSetup] - public void Setup() - { - _tokenDescriptor = new SecurityTokenDescriptor - { - Claims = BenchmarkUtils.Claims, - SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256, - }; - - _tokenDescriptorExtendedClaims = new SecurityTokenDescriptor - { - Claims = BenchmarkUtils.ClaimsExtendedExample, - SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256, - }; - - _jsonWebTokenHandler = new JsonWebTokenHandler(); - _jws = _jsonWebTokenHandler.CreateToken(_tokenDescriptor); - _jwsExtendedClaims = _jsonWebTokenHandler.CreateToken(_tokenDescriptorExtendedClaims); - - _validationParameters = new ValidationParameters(); - _validationParameters.ValidAudiences.Add(BenchmarkUtils.Audience); - _validationParameters.ValidIssuers.Add(BenchmarkUtils.Issuer); - _validationParameters.IssuerSigningKeys.Add(BenchmarkUtils.SigningCredentialsRsaSha256.Key); - - _callContext = new CallContext(); - - _tokenValidationParameters = new TokenValidationParameters() - { - ValidAudience = BenchmarkUtils.Audience, - ValidateLifetime = true, - ValidIssuer = BenchmarkUtils.Issuer, - IssuerSigningKey = BenchmarkUtils.SigningCredentialsRsaSha256.Key, - }; - } - - [Benchmark] - public async Task> JsonWebTokenHandler_ValidateTokenAsyncWithValidationParameters_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> JsonWebTokenHandler_ValidateTokenAsyncWithTokenValidationParameters_CreateClaims() - { - var result = await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false); - var claimsIdentity = result.ClaimsIdentity; - var claims = claimsIdentity.Claims; - return claims.ToList(); - } - - [Benchmark] - public async Task JsonWebTokenHandler_ValidateTokenAsyncWithValidationParameters() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _validationParameters, _callContext, null).ConfigureAwait(false); - - [Benchmark] - public async Task JsonWebTokenHandler_ValidateTokenAsyncWithTokenValidationParameters() => await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, _tokenValidationParameters).ConfigureAwait(false); - - [Benchmark] - public async Task JsonWebTokenHandler_ValidateTokenAsyncWithTokenValidationParametersUsingClone() - { - var tokenValidationParameters = _tokenValidationParameters.Clone(); - tokenValidationParameters.ValidIssuer = "different-issuer"; - tokenValidationParameters.ValidAudience = "different-audience"; - tokenValidationParameters.ValidateLifetime = false; - return await _jsonWebTokenHandler.ValidateTokenAsync(_jwsExtendedClaims, tokenValidationParameters).ConfigureAwait(false); - } - } -}