Skip to content

Create microbenchmark for ValidateTokenAsync #2235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.5" />
</ItemGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<Optimize>true</Optimize>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\test\Microsoft.IdentityModel.TestUtils\Microsoft.IdentityModel.TestUtils.csproj" />
<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" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions benchmark/MIcrosoft.IdentityModel.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using BenchmarkDotNet.Running;

namespace Microsoft.IdentityModel.Benchmarks
{
public static class Program
{
public static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# multi-line script to build the project
components:
jwt:
script: |
call dotnet build -c release --framework net8.0 .\src\Microsoft.IdentityModel.JsonWebTokens\Microsoft.IdentityModel.JsonWebTokens.csproj

arguments:
--application.options.outputFiles .\src\Microsoft.IdentityModel.JsonWebTokens\bin\release\net8.0\

# default arguments that are always used on crank commands
defaults: --config https://raw.githubusercontent.com/aspnet/Benchmarks/main/scenarios/aspnet.profiles.standard.yml --application.framework net8.0

# the first vaule is the default if none is specified
profiles:
windows:
description: INTEL/Windows 28 Cores
arguments: --profile aspnet-citrine-win

benchmarks:
NoMvcAuth:
description: NoMvcAuth
arguments: --config https://raw.githubusercontent.com/aspnet/Benchmarks/main/src/BenchmarksApps/Mvc/benchmarks.jwtapi.yml --scenario NoMvcAuth
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Toolchains.InProcess.Emit;

namespace Microsoft.IdentityModel.Benchmarks
{
public class AntiVirusFriendlyConfig : ManualConfig
{
public AntiVirusFriendlyConfig()
{
AddJob(Job.MediumRun
.WithToolchain(InProcessEmitToolchain.Instance));
}
}
}
32 changes: 32 additions & 0 deletions benchmark/Microsoft.IdentityModel.Benchmarks/CreateTokenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

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

namespace Microsoft.IdentityModel.Benchmarks
{
public class CreateTokenTests
{
JsonWebTokenHandler jsonWebTokenHandler;
SecurityTokenDescriptor tokenDescriptor;

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

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

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

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

namespace Microsoft.IdentityModel.Benchmarks
{
[Config(typeof(AntiVirusFriendlyConfig))]
public class ValidateTokenAsyncTests
{
JsonWebTokenHandler jsonWebTokenHandler;
JwtSecurityTokenHandler jwtSecurityTokenHandler;
SecurityTokenDescriptor tokenDescriptor;
string jsonWebToken;
TokenValidationParameters validationParameters;

[GlobalSetup]
public void Setup()
{
tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(Default.PayloadClaims),
SigningCredentials = KeyingMaterial.JsonWebKeyRsa256SigningCredentials,
};
jsonWebToken = jsonWebTokenHandler.CreateToken(tokenDescriptor);
validationParameters = new TokenValidationParameters()
{
ValidAudience = "http://Default.Audience.com",
ValidateLifetime = true,
ValidIssuer = "http://Default.Issuer.com",
IssuerSigningKey = KeyingMaterial.JsonWebKeyRsa256SigningCredentials.Key,
};
}

[GlobalSetup(Targets = new[] { nameof(JsonWebTokenHandler) })]
public void JsonWebTokenSetup()
{
jsonWebTokenHandler = new JsonWebTokenHandler();
}

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

[GlobalSetup(Targets = new[] { nameof(JwtSecurityTokenHandler) })]
public void JwtSecurityTokenSetup()
{
jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
}

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

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
imports:
- https://github.com/aspnet/Benchmarks/blob/main/scenarios/aspnet.profiles.standard.yml?raw=true

jobs:
benchmarks:
source:
repository: https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet
project: benchmark\Microsoft.IdentityModel.Benchmarks\Microsoft.IdentityModel.Benchmarks.csproj
variables:
filterArg: "*"
jobArg: short
arguments: --job {{jobArg}} --filter {{filterArg}} --memory
options:
benchmarkDotNet: true

scenarios:
ValidateTokenAsyncTests:
application:
job: benchmarks

CreateTokenTests:
application:
job: benchmarks

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>Microsoft.IdentityModel.Abstractions</PackageId>
<PackageTags>.NET;Windows;Authentication;Identity;Abstractions</PackageTags>
<IsTrimmable>true</IsTrimmable>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
<Nullable>enable</Nullable>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageId>Microsoft.IdentityModel.Logging</PackageId>
<PackageTags>.NET;Windows;Authentication;Identity;Logging</PackageTags>
<IsTrimmable>true</IsTrimmable>
<IsTrimmable Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">true</IsTrimmable>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Debug'">
Expand Down