Skip to content

Commit

Permalink
Add new benchmarks (#2316)
Browse files Browse the repository at this point in the history
* Add new benchmarks, ignore local outputs, fix crank config

* Fix private members, combine solutions
  • Loading branch information
westin-m authored Sep 14, 2023
1 parent e541808 commit d7f2e2d
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 24 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,5 @@ backup/*
/.vs/
test/ApiChangeTest/Resource/devAssemblies
test/ApiChangeTest/Resource/reports
/benchmark/Microsoft.IdentityModel.Benchmarks/BenchmarkDotNet.Artifacts/results
/benchmark/Microsoft.IdentityModel.Benchmarks/.vs
9 changes: 9 additions & 0 deletions Wilson.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.33711.374
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmark", "benchmark", "{2F79F3C4-F4E3-46DD-8B34-8EF403A6F7F5}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{BD2706C5-6C57-484D-89C8-A0CF5F8E3D19}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{8905D2E3-4499-4A86-BF3E-F098F228DD59}"
Expand Down Expand Up @@ -101,6 +103,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.IdentityModel.Aot
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.IdentityModel.AotCompatibility.Tests", "test\Microsoft.IdentityModel.AotCompatibility.Tests\Microsoft.IdentityModel.AotCompatibility.Tests.csproj", "{CD0EEF56-7221-4420-8181-48EE82E91306}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.IdentityModel.Benchmarks", "benchmark\Microsoft.IdentityModel.Benchmarks\Microsoft.IdentityModel.Benchmarks.csproj", "{F1BB31E4-8865-4425-8BD4-94F1815C16E0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -243,6 +247,10 @@ Global
{CD0EEF56-7221-4420-8181-48EE82E91306}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD0EEF56-7221-4420-8181-48EE82E91306}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD0EEF56-7221-4420-8181-48EE82E91306}.Release|Any CPU.Build.0 = Release|Any CPU
{F1BB31E4-8865-4425-8BD4-94F1815C16E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1BB31E4-8865-4425-8BD4-94F1815C16E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1BB31E4-8865-4425-8BD4-94F1815C16E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1BB31E4-8865-4425-8BD4-94F1815C16E0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -284,6 +292,7 @@ Global
{EF9A4431-6D2C-4DD1-BF6B-6F2CC619DEE1} = {8905D2E3-4499-4A86-BF3E-F098F228DD59}
{8105289F-3D54-4054-9738-5985F3B6CF2C} = {8905D2E3-4499-4A86-BF3E-F098F228DD59}
{CD0EEF56-7221-4420-8181-48EE82E91306} = {8905D2E3-4499-4A86-BF3E-F098F228DD59}
{F1BB31E4-8865-4425-8BD4-94F1815C16E0} = {2F79F3C4-F4E3-46DD-8B34-8EF403A6F7F5}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2F681326-7ED4-45F6-BD1D-1119EA388F42}
Expand Down
42 changes: 42 additions & 0 deletions benchmark/Microsoft.IdentityModel.Benchmarks/CreateJWETests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using BenchmarkDotNet.Attributes;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;

namespace Microsoft.IdentityModel.Benchmarks
{
[Config(typeof(AntiVirusFriendlyConfig))]
[HideColumns("Type", "Job", "WarmupCount", "LaunchCount")]
[MemoryDiagnoser]
public class CreateJWETests
{
private JsonWebTokenHandler _jsonWebTokenHandler;
private JwtSecurityTokenHandler _jwtSecurityTokenHandler;
private SecurityTokenDescriptor _tokenDescriptor;

[GlobalSetup]
public void Setup()
{
_jsonWebTokenHandler = new JsonWebTokenHandler();
_jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
_tokenDescriptor = new SecurityTokenDescriptor
{
SigningCredentials = KeyingMaterial.JsonWebKeyRsa256SigningCredentials,
EncryptingCredentials = KeyingMaterial.DefaultSymmetricEncryptingCreds_Aes256_Sha512_512,
Subject = new ClaimsIdentity(Default.PayloadClaims),
TokenType = "TokenType"
};
}

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

[Benchmark]
public string JwtSecurityTokenHandler_CreateJWE() => _jwtSecurityTokenHandler.CreateEncodedJwt(_tokenDescriptor);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using BenchmarkDotNet.Attributes;
using Microsoft.IdentityModel.Protocols.SignedHttpRequest;
using Microsoft.IdentityModel.Protocols.SignedHttpRequest.Tests;
using Microsoft.IdentityModel.Protocols;

namespace Microsoft.IdentityModel.Benchmarks
{
[HideColumns("Type", "Job", "WarmupCount", "LaunchCount")]
[MemoryDiagnoser]
public class CreateSignedHttpRequestTests
{
private SignedHttpRequestHandler _signedHttpRequestHandler;
private SignedHttpRequestDescriptor _signedHttpRequestDescriptor;

[GlobalSetup]
public void Setup()
{
_signedHttpRequestHandler = new SignedHttpRequestHandler();
_signedHttpRequestDescriptor = new SignedHttpRequestDescriptor(
SignedHttpRequestTestUtils.DefaultEncodedAccessToken,
new HttpRequestData(),
SignedHttpRequestTestUtils.DefaultSigningCredentials,
new SignedHttpRequestCreationParameters()
{
CreateM = false,
CreateP = false,
CreateU = false
});
}

[Benchmark]
public string SHRHandler_CreateSignedHttpRequest() => _signedHttpRequestHandler.CreateSignedHttpRequest(_signedHttpRequestDescriptor);

}
}
11 changes: 6 additions & 5 deletions benchmark/Microsoft.IdentityModel.Benchmarks/CreateTokenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,25 @@
namespace Microsoft.IdentityModel.Benchmarks
{
[HideColumns("Type", "Job", "WarmupCount", "LaunchCount")]
[MemoryDiagnoser]
public class CreateTokenTests
{
JsonWebTokenHandler jsonWebTokenHandler;
SecurityTokenDescriptor tokenDescriptor;
private JsonWebTokenHandler _jsonWebTokenHandler;
private SecurityTokenDescriptor _tokenDescriptor;

[GlobalSetup]
public void Setup()
{
jsonWebTokenHandler = new JsonWebTokenHandler();
tokenDescriptor = new SecurityTokenDescriptor
_jsonWebTokenHandler = new JsonWebTokenHandler();
_tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(Default.PayloadClaims),
SigningCredentials = KeyingMaterial.JsonWebKeyRsa256SigningCredentials,
};
}

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

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>Microsoft.IdentityModel.Benchmarks</AssemblyName>
<PackageId>Microsoft.IdentityModel.Benchmarks</PackageId>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<SignAssembly>True</SignAssembly>
<DelaySign>True</DelaySign>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)..\..\build\35MSSharedLib1024.snk</AssemblyOriginatorKeyFile>
<ErrorOnDuplicatePublishOutputFiles>False</ErrorOnDuplicatePublishOutputFiles>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,6 +24,8 @@
<ProjectReference Include="..\..\src\Microsoft.IdentityModel.JsonWebTokens\Microsoft.IdentityModel.JsonWebTokens.csproj" />
<ProjectReference Include="..\..\src\Microsoft.IdentityModel.Tokens\Microsoft.IdentityModel.Tokens.csproj" />
<ProjectReference Include="..\..\src\System.IdentityModel.Tokens.Jwt\System.IdentityModel.Tokens.Jwt.csproj" />
<ProjectReference Include="..\..\src\Microsoft.IdentityModel.Protocols.SignedHttpRequest\Microsoft.IdentityModel.Protocols.SignedHttpRequest.csproj" />
<ProjectReference Include="..\..\test\Microsoft.IdentityModel.Protocols.SignedHttpRequest.Tests\Microsoft.IdentityModel.Protocols.SignedHttpRequest.Tests.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using BenchmarkDotNet.Attributes;
using Microsoft.IdentityModel.JsonWebTokens;
using Microsoft.IdentityModel.TestUtils;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Threading.Tasks;

namespace Microsoft.IdentityModel.Benchmarks
{
[Config(typeof(AntiVirusFriendlyConfig))]
[HideColumns("Type", "Job", "WarmupCount", "LaunchCount")]
[MemoryDiagnoser]
public class ValidateJWEAsyncTests
{
private JsonWebTokenHandler _jsonWebTokenHandler;
private JwtSecurityTokenHandler _jwtSecurityTokenHandler;
private SecurityTokenDescriptor _tokenDescriptor;
private string _jweFromJsonHandler;
private string _jweFromJwtHandler;
private TokenValidationParameters _validationParameters;

[GlobalSetup]
public void Setup()
{
_jsonWebTokenHandler = new JsonWebTokenHandler();
_jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
_tokenDescriptor = new SecurityTokenDescriptor
{
SigningCredentials = KeyingMaterial.JsonWebKeyRsa256SigningCredentials,
EncryptingCredentials = KeyingMaterial.DefaultSymmetricEncryptingCreds_Aes256_Sha512_512,
Subject = new ClaimsIdentity(Default.PayloadClaims),
TokenType = "TokenType"
};
_jweFromJsonHandler = _jsonWebTokenHandler.CreateToken(_tokenDescriptor);
_jweFromJwtHandler = _jwtSecurityTokenHandler.CreateEncodedJwt(_tokenDescriptor);
_validationParameters = new TokenValidationParameters
{
IssuerSigningKey = KeyingMaterial.JsonWebKeyRsa256SigningCredentials.Key,
TokenDecryptionKey = KeyingMaterial.DefaultSymmetricSecurityKey_512,
ValidAudience = Default.Audience,
ValidIssuer = Default.Issuer
};
}

[Benchmark]
public async Task<TokenValidationResult> JsonWebTokenHandler_ValidateJWEAsync() => await _jsonWebTokenHandler.ValidateTokenAsync(_jweFromJsonHandler, _validationParameters);

[Benchmark]
public ClaimsPrincipal JwtSecurityTokenHandler_ValidateJWEAsync() => _jwtSecurityTokenHandler.ValidateToken(_jweFromJwtHandler, _validationParameters, out _);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using BenchmarkDotNet.Attributes;
using Microsoft.IdentityModel.Protocols.SignedHttpRequest.Tests;
using Microsoft.IdentityModel.Protocols.SignedHttpRequest;
using Microsoft.IdentityModel.Protocols;
using System.Threading;
using System.Threading.Tasks;

namespace Microsoft.IdentityModel.Benchmarks
{
[HideColumns("Type", "Job", "WarmupCount", "LaunchCount")]
[MemoryDiagnoser]
public class ValidateSignedHttpRequestAsyncTests
{
private SignedHttpRequestHandler _signedHttpRequestHandler;
private SignedHttpRequestDescriptor _signedHttpRequestDescriptor;
private SignedHttpRequestValidationContext _validationContext;
private string _signedHttpRequest;

[GlobalSetup]
public void Setup()
{
var requestData = new HttpRequestData();
_signedHttpRequestHandler = new SignedHttpRequestHandler();
_signedHttpRequestDescriptor = new SignedHttpRequestDescriptor(
SignedHttpRequestTestUtils.DefaultEncodedAccessToken,
requestData,
SignedHttpRequestTestUtils.DefaultSigningCredentials,
new SignedHttpRequestCreationParameters()
{
CreateM = false,
CreateP = false,
CreateU = false
});
_signedHttpRequest = _signedHttpRequestHandler.CreateSignedHttpRequest(_signedHttpRequestDescriptor);
_validationContext = new SignedHttpRequestValidationContext(
_signedHttpRequest,
requestData,
SignedHttpRequestTestUtils.DefaultTokenValidationParameters);
}

[Benchmark]
public async Task<SignedHttpRequestValidationResult> SHRHandler_ValidateSignedHttpRequestAsync() => await _signedHttpRequestHandler.ValidateSignedHttpRequestAsync(_validationContext, CancellationToken.None);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,52 @@ namespace Microsoft.IdentityModel.Benchmarks
{
[Config(typeof(AntiVirusFriendlyConfig))]
[HideColumns("Type", "Job", "WarmupCount", "LaunchCount")]
[MemoryDiagnoser]
public class ValidateTokenAsyncTests
{
JsonWebTokenHandler jsonWebTokenHandler;
JwtSecurityTokenHandler jwtSecurityTokenHandler;
SecurityTokenDescriptor tokenDescriptor;
string jsonWebToken;
TokenValidationParameters validationParameters;
private JsonWebTokenHandler _jsonWebTokenHandler;
private JwtSecurityTokenHandler _jwtSecurityTokenHandler;
private SecurityTokenDescriptor _tokenDescriptor;
private string _jsonWebToken;
private TokenValidationParameters _validationParameters;

[GlobalSetup]
public void Setup()
{
tokenDescriptor = new SecurityTokenDescriptor
_tokenDescriptor = new SecurityTokenDescriptor
{
Claims = BenchmarkUtils.SimpleClaims,
SigningCredentials = KeyingMaterial.JsonWebKeyRsa256SigningCredentials,
};
jsonWebToken = jsonWebTokenHandler.CreateToken(tokenDescriptor);
validationParameters = new TokenValidationParameters()
_jsonWebToken = _jsonWebTokenHandler.CreateToken(_tokenDescriptor);
_validationParameters = new TokenValidationParameters()
{
ValidAudience = "http://Default.Audience.com",
ValidAudience = Default.Audience,
ValidateLifetime = true,
ValidIssuer = "http://Default.Issuer.com",
ValidIssuer = Default.Issuer,
IssuerSigningKey = KeyingMaterial.JsonWebKeyRsa256SigningCredentials.Key,
};
}

[GlobalSetup(Targets = new[] { nameof(JsonWebTokenHandler_ValidateTokenAsync) })]
public void JsonWebTokenSetup()
{
jsonWebTokenHandler = new JsonWebTokenHandler();
jsonWebTokenHandler.SetDefaultTimesOnTokenCreation = false;
_jsonWebTokenHandler = new JsonWebTokenHandler();
_jsonWebTokenHandler.SetDefaultTimesOnTokenCreation = false;
}

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

[GlobalSetup(Targets = new[] { nameof(JwtSecurityTokenHandler_ValidateTokenAsync) })]
public void JwtSecurityTokenSetup()
{
jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
jwtSecurityTokenHandler.SetDefaultTimesOnTokenCreation = false;
_jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
_jwtSecurityTokenHandler.SetDefaultTimesOnTokenCreation = false;
}

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

}
}
Loading

0 comments on commit d7f2e2d

Please sign in to comment.