diff --git a/src/client/Microsoft.Identity.Client/Microsoft.Identity.Client.csproj b/src/client/Microsoft.Identity.Client/Microsoft.Identity.Client.csproj index 29ae70c711..98d09a6043 100644 --- a/src/client/Microsoft.Identity.Client/Microsoft.Identity.Client.csproj +++ b/src/client/Microsoft.Identity.Client/Microsoft.Identity.Client.csproj @@ -109,7 +109,7 @@ - $(DefineConstants);PORTABLE;HAVE_ASYNC;HAVE_BIG_INTEGER;HAVE_BINARY_SERIALIZATION;HAVE_COVARIANT_GENERICS;HAVE_DATA_CONTRACTS;HAVE_DATE_TIME_OFFSET;HAVE_DYNAMIC;HAVE_EXPRESSIONS;HAVE_FSHARP_TYPES;HAVE_GUID_TRY_PARSE;HAVE_HASH_SET;HAVE_IGNORE_DATA_MEMBER_ATTRIBUTE;HAVE_INOTIFY_COLLECTION_CHANGED;HAVE_INOTIFY_PROPERTY_CHANGING;HAVE_ISET;HAVE_LINQ;HAVE_METHOD_IMPL_ATTRIBUTE;HAVE_NON_SERIALIZED_ATTRIBUTE;HAVE_READ_ONLY_COLLECTIONS;HAVE_REFLECTION_BINDER;HAVE_SERIALIZATION_BINDER_BIND_TO_NAME;HAVE_STRING_JOIN_WITH_ENUMERABLE;HAVE_TIME_SPAN_PARSE_WITH_CULTURE;HAVE_TIME_SPAN_TO_STRING_WITH_CULTURE;HAVE_TIME_ZONE_INFO;HAVE_TYPE_DESCRIPTOR;HAVE_UNICODE_SURROGATE_DETECTION;HAVE_VARIANT_TYPE_PARAMETERS;HAVE_VERSION_TRY_PARSE;HAVE_XLINQ;HAVE_OBSOLETE_FORMATTER_ASSEMBLY_STYLE;HAVE_XML_DOCUMENT;HAVE_CONCURRENT_DICTIONARY;HAVE_ICONVERTIBLE;$(AdditionalConstants) + $(DefineConstants);UNITY;PORTABLE;HAVE_ASYNC;HAVE_BIG_INTEGER;HAVE_BINARY_SERIALIZATION;HAVE_COVARIANT_GENERICS;HAVE_DATA_CONTRACTS;HAVE_DATE_TIME_OFFSET;HAVE_DYNAMIC;HAVE_EXPRESSIONS;HAVE_FSHARP_TYPES;HAVE_GUID_TRY_PARSE;HAVE_HASH_SET;HAVE_IGNORE_DATA_MEMBER_ATTRIBUTE;HAVE_INOTIFY_COLLECTION_CHANGED;HAVE_INOTIFY_PROPERTY_CHANGING;HAVE_ISET;HAVE_LINQ;HAVE_METHOD_IMPL_ATTRIBUTE;HAVE_NON_SERIALIZED_ATTRIBUTE;HAVE_READ_ONLY_COLLECTIONS;HAVE_REFLECTION_BINDER;HAVE_SERIALIZATION_BINDER_BIND_TO_NAME;HAVE_STRING_JOIN_WITH_ENUMERABLE;HAVE_TIME_SPAN_PARSE_WITH_CULTURE;HAVE_TIME_SPAN_TO_STRING_WITH_CULTURE;HAVE_TIME_ZONE_INFO;HAVE_TYPE_DESCRIPTOR;HAVE_UNICODE_SURROGATE_DETECTION;HAVE_VARIANT_TYPE_PARAMETERS;HAVE_VERSION_TRY_PARSE;HAVE_XLINQ;HAVE_OBSOLETE_FORMATTER_ASSEMBLY_STYLE;HAVE_XML_DOCUMENT;HAVE_CONCURRENT_DICTIONARY;HAVE_ICONVERTIBLE;$(AdditionalConstants) $(DefineConstants);IS_APPCENTER_BUILD diff --git a/src/client/Microsoft.Identity.Client/json/Serialization/DefaultContractResolver.cs b/src/client/Microsoft.Identity.Client/json/Serialization/DefaultContractResolver.cs index 702ba3d66d..2815833a76 100644 --- a/src/client/Microsoft.Identity.Client/json/Serialization/DefaultContractResolver.cs +++ b/src/client/Microsoft.Identity.Client/json/Serialization/DefaultContractResolver.cs @@ -1379,7 +1379,7 @@ protected virtual IValueProvider CreateMemberValueProvider(MemberInfo member) // warning - this method use to cause errors with Intellitrace. Retest in VS Ultimate after changes IValueProvider valueProvider; -#if !(PORTABLE40 || PORTABLE || DOTNET || ANDROID || iOS || MAC) +#if !(PORTABLE40 || PORTABLE || DOTNET || ANDROID || iOS || MAC || UNITY) if (DynamicCodeGeneration) { valueProvider = new DynamicValueProvider(member); @@ -1388,7 +1388,7 @@ protected virtual IValueProvider CreateMemberValueProvider(MemberInfo member) { valueProvider = new ReflectionValueProvider(member); } -#elif !PORTABLE40 +#elif !(PORTABLE40 || UNITY) valueProvider = new ExpressionValueProvider(member); #else valueProvider = new ReflectionValueProvider(member); @@ -1704,4 +1704,4 @@ public string GetResolvedPropertyName(string propertyName) return ResolvePropertyName(propertyName); } } -} \ No newline at end of file +} diff --git a/src/client/Microsoft.Identity.Client/json/Serialization/ExpressionValueProvider.cs b/src/client/Microsoft.Identity.Client/json/Serialization/ExpressionValueProvider.cs index b7a82a1550..c4a45e0177 100644 --- a/src/client/Microsoft.Identity.Client/json/Serialization/ExpressionValueProvider.cs +++ b/src/client/Microsoft.Identity.Client/json/Serialization/ExpressionValueProvider.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET20 || NET35) +#if !(NET20 || NET35 || UNITY) using System; using System.Collections.Generic; @@ -118,4 +118,4 @@ public object GetValue(object target) } } -#endif \ No newline at end of file +#endif diff --git a/src/client/Microsoft.Identity.Client/json/Serialization/JsonTypeReflector.cs b/src/client/Microsoft.Identity.Client/json/Serialization/JsonTypeReflector.cs index f752a8ee59..8e3b778579 100644 --- a/src/client/Microsoft.Identity.Client/json/Serialization/JsonTypeReflector.cs +++ b/src/client/Microsoft.Identity.Client/json/Serialization/JsonTypeReflector.cs @@ -515,17 +515,20 @@ public static ReflectionDelegateFactory ReflectionDelegateFactory { get { -#if !(PORTABLE40 || PORTABLE || DOTNET || ANDROID || iOS || MAC) +#if !(PORTABLE40 || PORTABLE || DOTNET || ANDROID || iOS || MAC || UNITY) if (DynamicCodeGeneration) { return DynamicReflectionDelegateFactory.Instance; } return LateBoundReflectionDelegateFactory.Instance; + +#elif UNITY + return LateBoundReflectionDelegateFactory.Instance; #else - return ExpressionReflectionDelegateFactory.Instance; + return ExpressionReflectionDelegateFactory.Instance; #endif } } } -} \ No newline at end of file +} diff --git a/src/client/Microsoft.Identity.Client/json/Utilities/ExpressionReflectionDelegateFactory.cs b/src/client/Microsoft.Identity.Client/json/Utilities/ExpressionReflectionDelegateFactory.cs index a5a82272fb..ad98b32ffd 100644 --- a/src/client/Microsoft.Identity.Client/json/Utilities/ExpressionReflectionDelegateFactory.cs +++ b/src/client/Microsoft.Identity.Client/json/Utilities/ExpressionReflectionDelegateFactory.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET20 || NET35) +#if !(NET20 || NET35 || UNITY) using System.Collections.Generic; using System.Linq; @@ -380,4 +380,4 @@ private Expression EnsureCastExpression(Expression expression, Type targetType, } } -#endif \ No newline at end of file +#endif diff --git a/tests/Microsoft.Identity.Test.Performance/JsonTests.cs b/tests/Microsoft.Identity.Test.Performance/JsonTests.cs new file mode 100644 index 0000000000..261d76b29f --- /dev/null +++ b/tests/Microsoft.Identity.Test.Performance/JsonTests.cs @@ -0,0 +1,123 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using BenchmarkDotNet.Attributes; +using BenchmarkDotNet.Configs; +using Microsoft.Identity.Client.Instance.Discovery; +using Microsoft.Identity.Client.OAuth2; +using Microsoft.Identity.Client.Utils; +using Microsoft.Identity.Test.Unit; + +namespace Microsoft.Identity.Test.Performance +{ + public class JsonTests + { + private readonly string _msalTokenResponseJson; + private readonly MsalTokenResponse _msalTokenResponse; + + private readonly string _instanceDiscoveryResponseJson; + private readonly InstanceDiscoveryResponse _instanceDiscoveryResponse; + + private readonly string _oAuth2ResponseBaseJson; + private readonly OAuth2ResponseBase _oAuth2ResponseBase; + + public JsonTests() + { + _oAuth2ResponseBase = InitOAuth2ResponseBase(new OAuth2ResponseBase()); + _oAuth2ResponseBaseJson = JsonHelper.SerializeToJson(_oAuth2ResponseBase); + + _msalTokenResponse = InitMsalTokenResponse(new MsalTokenResponse()); + _msalTokenResponseJson = JsonHelper.SerializeToJson(_msalTokenResponse); + + _instanceDiscoveryResponse = InitInstanceDiscoveryResponse(new InstanceDiscoveryResponse()); + _instanceDiscoveryResponseJson = JsonHelper.SerializeToJson(_instanceDiscoveryResponse); + } + + private InstanceDiscoveryResponse InitInstanceDiscoveryResponse(InstanceDiscoveryResponse instanceDiscoveryResponse) + { + int entries = 30; + instanceDiscoveryResponse.TenantDiscoveryEndpoint = TestConstants.DiscoveryEndPoint; + instanceDiscoveryResponse.Metadata = new InstanceDiscoveryMetadataEntry[entries]; + + InitOAuth2ResponseBase(instanceDiscoveryResponse); + + for (int i = 0; i < entries; i++) + { + instanceDiscoveryResponse.Metadata[i] = new InstanceDiscoveryMetadataEntry + { + Aliases = new[] { "login.windows.net", "login.microsoftonline.com" }, + PreferredCache = "login.windows.net", + PreferredNetwork = "login.microsoftonline.com" + }; + } + + return instanceDiscoveryResponse; + } + + private OAuth2ResponseBase InitOAuth2ResponseBase(OAuth2ResponseBase oAuth2ResponseBase) + { + oAuth2ResponseBase.Error = "OAuth error"; + oAuth2ResponseBase.SubError = "OAuth suberror"; + oAuth2ResponseBase.ErrorDescription = "OAuth error description"; + oAuth2ResponseBase.ErrorCodes = new[] { "error1", "error2", "error3" }; + oAuth2ResponseBase.CorrelationId = "1234-123-1234"; + oAuth2ResponseBase.Claims = "claim1 claim2"; + + return oAuth2ResponseBase; + } + + private MsalTokenResponse InitMsalTokenResponse(MsalTokenResponse msalTokenResponse) + { + msalTokenResponse.TokenType = "token type"; + msalTokenResponse.AccessToken = "access token"; + msalTokenResponse.RefreshToken = "refresh token"; + msalTokenResponse.Scope = "scope scope"; + msalTokenResponse.ClientInfo = "client info"; + msalTokenResponse.IdToken = "id token"; + msalTokenResponse.ExpiresIn = 123; + msalTokenResponse.ExtendedExpiresIn = 12345; + msalTokenResponse.RefreshIn = 12333; + msalTokenResponse.FamilyId = "family id"; + + InitOAuth2ResponseBase(msalTokenResponse); + + return msalTokenResponse; + } + + [Benchmark] + public string Serialize_MsalTokenResponse_Test() + { + return JsonHelper.SerializeToJson(_msalTokenResponse); + } + + [Benchmark] + public string Deserialize_MsalTokenResponse_Test() + { + return JsonHelper.DeserializeFromJson(_msalTokenResponseJson).CorrelationId; + } + + [Benchmark] + public string Serialize_InstanceDiscoveryResponse_Test() + { + return JsonHelper.SerializeToJson(_instanceDiscoveryResponse); + } + + [Benchmark] + public string Deserialize_InstanceDiscoveryResponse_Test() + { + return JsonHelper.DeserializeFromJson(_instanceDiscoveryResponseJson).CorrelationId; + } + + [Benchmark] + public string Serialize_OAuth2ResponseBase_Test() + { + return JsonHelper.SerializeToJson(_oAuth2ResponseBase); + } + + [Benchmark] + public string Deserialize_OAuth2ResponseBase_Test() + { + return JsonHelper.DeserializeFromJson(_oAuth2ResponseBaseJson).CorrelationId; + } + } +} diff --git a/tests/Microsoft.Identity.Test.Performance/Program.cs b/tests/Microsoft.Identity.Test.Performance/Program.cs index d9142a6c38..9fc6b52240 100644 --- a/tests/Microsoft.Identity.Test.Performance/Program.cs +++ b/tests/Microsoft.Identity.Test.Performance/Program.cs @@ -12,7 +12,7 @@ class Program { static void Main(string[] args) { - BenchmarkRunner.Run( + BenchmarkRunner.Run( DefaultConfig.Instance .WithOptions(ConfigOptions.DontOverwriteResults) .AddJob(