Skip to content

Commit

Permalink
Add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sruthi Keerthi Rangavajhula (from Dev Box) committed Feb 1, 2024
1 parent 0fc2dfb commit 1a96d3e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
4 changes: 4 additions & 0 deletions benchmark/Microsoft.IdentityModel.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static void Main(string[] args)
}
private static void DebugThroughTests()
{
ReadTokenTests readTokenTests = new ReadTokenTests();
readTokenTests.Setup();
readTokenTests.ReadJWE_FromSpan();

AsymmetricAdapterSignatures asymmetricAdapter = new AsymmetricAdapterSignatures();
asymmetricAdapter.Setup();
asymmetricAdapter.SignDotnetCreatingBufferRSA();
Expand Down
39 changes: 31 additions & 8 deletions benchmark/Microsoft.IdentityModel.Benchmarks/ReadTokenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,54 @@ namespace Microsoft.IdentityModel.Benchmarks
[MemoryDiagnoser]
public class ReadTokenTests
{
string _encodedToken;
string _encodedJWS;
string _encryptedJWE;

[GlobalSetup]
public void Setup()
{
var tokenDescriptor = new SecurityTokenDescriptor
var jsonWebTokenHandler = new JsonWebTokenHandler();
var jwsTokenDescriptor = new SecurityTokenDescriptor
{
Claims = BenchmarkUtils.Claims,
SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256,
TokenType = JwtHeaderParameterNames.Jwk
};

_encodedToken = new JsonWebTokenHandler().CreateToken(tokenDescriptor);
_encodedJWS = jsonWebTokenHandler.CreateToken(jwsTokenDescriptor);

var jweTokenDescriptor = new SecurityTokenDescriptor
{
SigningCredentials = BenchmarkUtils.SigningCredentialsRsaSha256,
EncryptingCredentials = BenchmarkUtils.EncryptingCredentialsAes256Sha512,
Claims = BenchmarkUtils.Claims
};

_encryptedJWE = jsonWebTokenHandler.CreateToken(jweTokenDescriptor);
}

[Benchmark]
public JsonWebToken ReadJWS_FromString()
{
return new JsonWebToken(_encodedJWS);
}

[Benchmark]
public JsonWebToken ReadJWTFromEncodedString()
[Benchmark]
public JsonWebToken ReadJWS_FromSpan()
{
return new JsonWebToken(_encodedJWS.AsSpan());
}

[Benchmark]
public JsonWebToken ReadJWE_FromString()
{
return new JsonWebToken(_encodedToken);
return new JsonWebToken(_encryptedJWE);
}

[Benchmark]
public JsonWebToken ReadJWTFromEncodedSpan()
public JsonWebToken ReadJWE_FromSpan()
{
return new JsonWebToken(_encodedToken.AsSpan());
return new JsonWebToken(_encryptedJWE.AsSpan());
}
}
}
12 changes: 4 additions & 8 deletions src/Microsoft.IdentityModel.JsonWebTokens/JsonWebToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,7 @@ internal void ReadToken(ReadOnlySpan<char> encodedJsonSpan)
throw LogHelper.LogExceptionMessage(new SecurityTokenMalformedException(LogMessages.IDX14120));

Dot2 = Dot1 + Dot2 + 1;

if (Dot2 == encodedJsonSpan.Length - 1)
Dot3 = -1;
else
Dot3 = encodedJsonSpan.Slice(Dot2 + 1).IndexOf('.');
Dot3 = (Dot2 == encodedJsonSpan.Length - 1) ? -1 : encodedJsonSpan.Slice(Dot2 + 1).IndexOf('.');

if (Dot3 == -1)
{
Expand Down Expand Up @@ -623,8 +619,8 @@ internal void ReadToken(ReadOnlySpan<char> encodedJsonSpan)
ReadOnlySpan<char> headerSpan = encodedJsonSpan.Slice(0, Dot1);
if (headerSpan.IsEmpty)
throw LogHelper.LogExceptionMessage(new ArgumentException(LogMessages.IDX14307));
// right number of dots for JWE (4)

// right number of dots for JWE (4)
byte[] headerAsciiBytes = new byte[headerSpan.Length];
Encoding.ASCII.GetBytes(headerSpan, headerAsciiBytes);

Expand Down Expand Up @@ -687,7 +683,7 @@ internal void ReadToken(ReadOnlySpan<char> encodedJsonSpan)
}
}

EncodedToken = encodedJsonSpan.ToString();
EncodedToken = encodedJsonSpan.ToString(); // TODO: Discuss: This is expensive. Is there a better way to do this?
}
#endif

Expand Down

0 comments on commit 1a96d3e

Please sign in to comment.