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 1 commit
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
43 changes: 43 additions & 0 deletions benchmark/MIcrosoft.IdentityModel.Benchmarks/JsonWebTokenTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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.Security.Claims;
using System.Threading.Tasks;

namespace Microsoft.IdentityModel.Benchmarks
{
public class JsonWebTokenTests
{

JsonWebTokenHandler tokenHandler;
SecurityTokenDescriptor tokenDescriptor;
string token;
TokenValidationParameters validationParameters;

[GlobalSetup(Targets = new[] { nameof(ValidateTokenAsync) })]
public void ValidateTokenSetup()
{
tokenHandler = new JsonWebTokenHandler();
tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(Default.PayloadClaims),
SigningCredentials = KeyingMaterial.JsonWebKeyRsa256SigningCredentials,
};
token = tokenHandler.CreateToken(tokenDescriptor);
validationParameters = new TokenValidationParameters()
{
ValidAudience = "http://Default.Audience.com",
ValidateLifetime = false,
ValidIssuer = "http://Default.Issuer.com",
IssuerSigningKey = KeyingMaterial.JsonWebKeyRsa256SigningCredentials.Key,
};
}

[Benchmark]
public async Task<TokenValidationResult> ValidateTokenAsync() => await tokenHandler.ValidateTokenAsync(token, validationParameters).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<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" />
</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,24 @@
jobs:
benchmarks:
source:
repository: https://github.com/AzureAD/azure-activedirectory-identitymodel-extensions-for-dotnet
branchOrCommit: westinm/benchmark
project: benchmark\Microsoft.IdentityModel.Benchmarks\Microsoft.IdentityModel.Benchmarks.csproj
variables:
filterArg: "*"
jobArg: short
arguments: --job {{jobArg}} --filter {{filterArg}} --memory
options:
benchmarkDotNet: true

scenarios:
JsonWebTokenTests:
application:
job: benchmarks

profiles:
local:
jobs:
application:
endpoints:
- http://localhost:5010
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://github.com/aspnet/Benchmarks/blob/main/build/azure.profile.yml?raw=true --application.framework net7.0

# the first vaule is the defualt 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
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