Newtonsoft.Json: Very Slow Serialization on Azure

Viewed 155

I've stumbled upon a really strange problem. I'm trying to Serialize using Newtonsoft a quite large object. In my local machine I have tried to measure the time needed to do that and it's around 500-550miliseconds. However, in our Azure Web App Service, the exact same piece of code, with the exact same object to serialize needs 20-23 seconds to complete!

Here is my code:

public class JsonNaNConverter : JsonConverter
{
        public override bool CanRead { get { return false; } }
        public override bool CanWrite { get { return true; } }
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            var val = value as double? ?? (double?)(value as float?);
            if (val == null || Double.IsNaN((double)val) || Double.IsInfinity((double)val))
            {
                writer.WriteNull();
                return;
            }
            writer.WriteValue((double)val);
        }
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new NotImplementedException(); }
        public override bool CanConvert(Type objectType) { return objectType == typeof(double) || objectType == typeof(double?) || objectType == typeof(float); }
}

[...]

#define WRITE_TIMER_LOGS

[...]

var serializerSettings = new JsonSerializerSettings();
serializerSettings.Converters.Add(new JsonNaNConverter());
serializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

[...]

#if WRITE_TIMER_LOGS
var swSerializeTime = new System.Diagnostics.Stopwatch();
#endif

[...]

#if WRITE_TIMER_LOGS
swSerializeTime.Start();
#endif
                    
jsonString = JsonConvert.SerializeObject(responses, Formatting.None, serializerSettings);

#if WRITE_TIMER_LOGS
    swSerializeTime.Stop();
    #endif

swSerializeTime.ElapsedMiliseconds is the number I observe in both environments.

The resulting jsonString is quite large and as a string it occupies a length of 3239388 characters.

My machine specs are:

CPU: Intel Core i5 10500 RAM: 16GB DDR4 @ 2666MHz OS: Windows 10 21H2

Azure Website App Service Details:

Location: South Central US App Service Plan: P2V2

Our app runs in .NET Framework 4.6.1 and uses Newtonsoft.Json 12.0.1

Can you please advice?

0 Answers
Related