Skip to content

Commit

Permalink
Reduce allocations in System.Text.Json tests and catch OOM exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
akoeplinger committed Jul 21, 2023
1 parent e357352 commit 58375c9
Show file tree
Hide file tree
Showing 6 changed files with 397 additions and 341 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

namespace System.Text.Json.Tests
Expand Down Expand Up @@ -235,7 +236,7 @@ public static void ToStringLargeTest(int stringLength)
}
{
var message = new string('>', stringLength);
var builder = new StringBuilder();
var builder = new StringBuilder(stringLength);
for (int i = 0; i < stringLength; i++)
{
builder.Append("\\u003E");
Expand Down Expand Up @@ -302,7 +303,7 @@ public static void GetUtf8BytesLargeTest(int stringLength)
}
{
var message = new string('>', stringLength);
var builder = new StringBuilder();
var builder = new StringBuilder(stringLength);
for (int i = 0; i < stringLength; i++)
{
builder.Append("\\u003E");
Expand Down Expand Up @@ -362,7 +363,7 @@ public static void GetValueLargeTest(int stringLength)
public static void GetValueLargeEscapedTest(int stringLength)
{
var message = new string('>', stringLength);
var builder = new StringBuilder();
var builder = new StringBuilder(stringLength);
for (int i = 0; i < stringLength; i++)
{
builder.Append("\\u003E");
Expand Down Expand Up @@ -426,7 +427,7 @@ public static void InvalidLargeEncode()
}
catch (OutOfMemoryException)
{
return;
throw new SkipTestException("Out of memory allocating large objects");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text.Encodings.Web;
using System.Text.Json.Tests;
using Microsoft.DotNet.XUnitExtensions;
using Xunit;

namespace System.Text.Json.Serialization.Tests
Expand Down Expand Up @@ -161,42 +162,49 @@ public static void WriteObjectWithNumberHandling()
[OuterLoop]
public static void SerializeLargeListOfObjects()
{
Dto dto = new()
{
Prop1 = int.MaxValue,
Prop2 = int.MinValue,
Prop3 = "AC",
Prop4 = 500,
Prop5 = int.MaxValue / 2,
Prop6 = 250M,
Prop7 = 250M,
Prop8 = 250M,
Prop9 = 250M,
Prop10 = 250M,
Prop11 = 150M,
Prop12 = 150M,
Prop13 = DateTimeOffset.MaxValue,
Prop14 = DateTimeOffset.MaxValue,
Prop15 = DateTimeOffset.MaxValue,
Prop16 = DateTimeOffset.MaxValue,
Prop17 = 3,
Prop18 = DateTime.MaxValue,
Prop19 = DateTime.MaxValue,
Prop20 = 25000,
Prop21 = DateTime.MaxValue
};

// It takes a little over 4,338,000 items to reach a payload size above the Array.MaxLength value.
List<Dto> items = Enumerable.Repeat(dto, 4_338_000).ToList();

try
{
JsonSerializer.SerializeToUtf8Bytes(items);
Dto dto = new()
{
Prop1 = int.MaxValue,
Prop2 = int.MinValue,
Prop3 = "AC",
Prop4 = 500,
Prop5 = int.MaxValue / 2,
Prop6 = 250M,
Prop7 = 250M,
Prop8 = 250M,
Prop9 = 250M,
Prop10 = 250M,
Prop11 = 150M,
Prop12 = 150M,
Prop13 = DateTimeOffset.MaxValue,
Prop14 = DateTimeOffset.MaxValue,
Prop15 = DateTimeOffset.MaxValue,
Prop16 = DateTimeOffset.MaxValue,
Prop17 = 3,
Prop18 = DateTime.MaxValue,
Prop19 = DateTime.MaxValue,
Prop20 = 25000,
Prop21 = DateTime.MaxValue
};

// It takes a little over 4,338,000 items to reach a payload size above the Array.MaxLength value.
List<Dto> items = Enumerable.Repeat(dto, 4_338_000).ToList();

try
{
JsonSerializer.SerializeToUtf8Bytes(items);
}
catch (OutOfMemoryException) { }

items.AddRange(Enumerable.Repeat(dto, 1000).ToList());
Assert.Throws<OutOfMemoryException>(() => JsonSerializer.SerializeToUtf8Bytes(items));
}
catch (OutOfMemoryException)
{
throw new SkipTestException("Out of memory allocating large objects");
}
catch (OutOfMemoryException) { }

items.AddRange(Enumerable.Repeat(dto, 1000).ToList());
Assert.Throws<OutOfMemoryException>(() => JsonSerializer.SerializeToUtf8Bytes(items));
}

class Dto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using Microsoft.DotNet.XUnitExtensions;
using Newtonsoft.Json;
using Xunit;

Expand Down Expand Up @@ -453,8 +454,15 @@ public static void LongInputString(int length)
[OuterLoop]
public static void VeryLongInputString(int length)
{
// Verify that deserializer does not do any multiplication or addition on the string length
DeserializeLongJsonString(length);
try
{
// Verify that deserializer does not do any multiplication or addition on the string length
DeserializeLongJsonString(length);
}
catch (OutOfMemoryException)
{
throw new SkipTestException("Out of memory allocating large objects");
}
}

private static void DeserializeLongJsonString(int stringLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3038,7 +3038,7 @@ public static void JsonContainingOnlyCommentsIsInvalid(string jsonString, int ex
[MemberData(nameof(LotsOfCommentsTests))]
public static void SkipLotsOfComments(string valueString, bool insideArray, string expectedString)
{
var builder = new StringBuilder();
var builder = new StringBuilder(2_000_000);
if (insideArray)
{
builder.Append("[");
Expand Down Expand Up @@ -3116,7 +3116,7 @@ public static void SkipLotsOfComments(string valueString, bool insideArray, stri
[MemberData(nameof(LotsOfCommentsTests))]
public static void ConsumeLotsOfComments(string valueString, bool insideArray, string expectedString)
{
var builder = new StringBuilder();
var builder = new StringBuilder(2_000_000);
if (insideArray)
{
builder.Append("[");
Expand Down
Loading

0 comments on commit 58375c9

Please sign in to comment.