|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using Microsoft.IdentityModel.JsonWebTokens; |
| 7 | +using Microsoft.IdentityModel.TestUtils; |
| 8 | +using Microsoft.IdentityModel.Tokens; |
| 9 | + |
| 10 | +namespace Microsoft.IdentityModel.Benchmarks |
| 11 | +{ |
| 12 | + public class ReadJsonWebTokenBenchmarks |
| 13 | + { |
| 14 | + private JsonWebTokenHandler _handler; |
| 15 | + private string _smallToken; |
| 16 | + private string _largeToken; |
| 17 | + |
| 18 | + [GlobalSetup] |
| 19 | + public void Setup() |
| 20 | + { |
| 21 | + _handler = new JsonWebTokenHandler(); |
| 22 | + |
| 23 | + // Small token with minimal claims |
| 24 | + _smallToken = _handler.CreateToken( |
| 25 | + Default.PayloadString, |
| 26 | + KeyingMaterial.JsonWebKeyRsa256SigningCredentials); |
| 27 | + |
| 28 | + // Large token with many claims |
| 29 | + var claims = Default.PayloadClaims; |
| 30 | + for (int i = 0; i < 50; i++) |
| 31 | + claims.Add(new System.Security.Claims.Claim($"claim{i}", $"value{i}")); |
| 32 | + |
| 33 | + _largeToken = _handler.CreateToken( |
| 34 | + new SecurityTokenDescriptor |
| 35 | + { |
| 36 | + Subject = new CaseSensitiveClaimsIdentity(claims), |
| 37 | + SigningCredentials = KeyingMaterial.JsonWebKeyRsa256SigningCredentials |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + [Benchmark(Baseline = true)] |
| 42 | + public JsonWebToken ReadJsonWebToken_String_Small() |
| 43 | + { |
| 44 | + return _handler.ReadJsonWebToken(_smallToken); |
| 45 | + } |
| 46 | + |
| 47 | + [Benchmark] |
| 48 | + public JsonWebToken ReadJsonWebToken_Span_Small() |
| 49 | + { |
| 50 | + return _handler.ReadJsonWebToken(_smallToken.AsMemory()); |
| 51 | + } |
| 52 | + |
| 53 | + [Benchmark] |
| 54 | + public JsonWebToken ReadJsonWebToken_String_Large() |
| 55 | + { |
| 56 | + return _handler.ReadJsonWebToken(_largeToken); |
| 57 | + } |
| 58 | + |
| 59 | + [Benchmark] |
| 60 | + public JsonWebToken ReadJsonWebToken_Span_Large() |
| 61 | + { |
| 62 | + return _handler.ReadJsonWebToken(_largeToken.AsMemory()); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments