Skip to content

Commit

Permalink
Performance improvements (#247)
Browse files Browse the repository at this point in the history
***NO_CI***
  • Loading branch information
torbacz authored Sep 26, 2022
1 parent 8d88180 commit 6bf7b9b
Show file tree
Hide file tree
Showing 48 changed files with 1,660 additions and 686 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,182 +1,125 @@
using nanoFramework.Benchmark;
using System;
using System.Collections;
using nanoFramework.Benchmark.Attributes;
using nanoFramework.Json.Benchmark.Base;
using nanoFramework.Json.Test.Shared;
using System;
using System.Collections;

namespace nanoFramework.Json.Benchmark.DeserializationBenchmarks
{
[IterationCount(5)]
public class ReferenceTypesDeserializationBenchmark : BaseIterationBenchmark
{
const string testString = "TestStringToSerialize";
const short arrayElementCount = 5;
int[] intArray = new int[arrayElementCount];
short[] shortArray = new short[arrayElementCount];
private Person nestedTestClass;
private JsonTestClassComplex complexClass;
private JsonTestTown myTown;
private ArrayList arrayList;

[Setup]
public void Setup()
{
var random = new Random();
for (int i = 0; i < arrayElementCount; i++)
{
intArray[i] = random.Next();
shortArray[i] = (short)random.Next(short.MaxValue);
}

nestedTestClass = new Person()
{
FirstName = "John",
LastName = "Doe",
Birthday = new DateTime(1988, 4, 23),
ID = 27,
Address = null,
ArrayProperty = new string[] { "hello", "world" },
Friend = new Person()
{
FirstName = "Bob",
LastName = "Smith",
Birthday = new DateTime(1983, 7, 3),
ID = 2,
Address = "123 Some St",
ArrayProperty = new string[] { "hi", "planet" },
}
};

complexClass = new JsonTestClassComplex()
{
aInteger = 10,
aShort = 254,
aByte = 0x05,
aString = "A string",
aFloat = 1.2345f,
aDouble = 1.2345,
aBoolean = true,
Timestamp = DateTime.UtcNow,
FixedTimestamp = new DateTime(2020, 05, 01, 09, 30, 00),
intArray = new[] { 1, 3, 5, 7, 9 },
shortArray = new[] { (short)1, (short)3, (short)5, (short)7, (short)9 },
byteArray = new[] { (byte)0x22, (byte)0x23, (byte)0x24, (byte)0x25, (byte)0x26 },
stringArray = new[] { "two", "four", "six", "eight" },
floatArray = new[] { 1.1f, 3.3f, 5.5f, 7.7f, 9.9f },
doubleArray = new[] { 1.12345, 3.3456, 5.56789, 7.78910, 9.910111213 },
child1 = new JsonTestClassChild() { one = 1, two = 2, three = 3 },
Child = new JsonTestClassChild() { one = 100, two = 200, three = 300 },
nullObject = null,
nanFloat = float.NaN,
nanDouble = double.NaN,
};

myTown = new JsonTestTown
{
TownID = 1,
TownName = "myTown",
CompaniesInThisTown = new JsonTestCompany[]
{
new JsonTestCompany { CompanyID = 1, CompanyName = "AAA Amalgamated Industries" },
new JsonTestCompany { CompanyID = 2, CompanyName = "BBB Amalgamated Industries" },
new JsonTestCompany { CompanyID = 3, CompanyName = "CCC Amalgamated Industries" },
new JsonTestCompany { CompanyID = 4, CompanyName = "DDD Amalgamated Industries" },
new JsonTestCompany { CompanyID = 5, CompanyName = "EEE Amalgamated Industries" },
new JsonTestCompany { CompanyID = 6, CompanyName = "FFF Amalgamated Industries" },
new JsonTestCompany { CompanyID = 7, CompanyName = "GGG Amalgamated Industries" },
new JsonTestCompany { CompanyID = 8, CompanyName = "HHH Amalgamated Industries" }
},
EmployeesInThisTown = new JsonTestEmployee[]
{
new JsonTestEmployee
{
EmployeeID = 1,
EmployeeName = "John Smith",
CurrentEmployer = new JsonTestCompany { CompanyID = 3, CompanyName = "CCC Amalgamated Industries" },
FormerEmployers = new JsonTestCompany[]
{
new JsonTestCompany { CompanyID = 2, CompanyName = "BBB Amalgamated Industries" },
new JsonTestCompany { CompanyID = 5, CompanyName = "EEE Amalgamated Industries" },
const string IntArrayJson = "[405421362,1082483948,1131707654,345242860,1111968802]";
const string ShortArrayJson = "[12345,25463,22546,18879,12453]";
const string StringJson = "some string";
const string ArrayListJson = "[{\"stringtest\":\"hello world\",\"nulltest\":null,\"collection\":[-1,null,24.565657576,\"blah\",false]}]";
const string s_AzureTwinsJsonTestPayload = @"{
""deviceId"": ""nanoDeepSleep"",
""etag"": ""AAAAAAAAAAc="",
""deviceEtag"": ""Njc2MzYzMTQ5"",
""status"": ""enabled"",
""statusUpdateTime"": ""0001-01-01T00:00:00Z"",
""connectionState"": ""Disconnected"",
""lastActivityTime"": ""2021-06-03T05:52:41.4683112Z"",
""cloudToDeviceMessageCount"": 0,
""authenticationType"": ""sas"",
""x509Thumbprint"": {
""primaryThumbprint"": null,
""secondaryThumbprint"": null
},
""modelId"": """",
""version"": 381,
""properties"": {
""desired"": {
""TimeToSleep"": 30,
""$metadata"": {
""$lastUpdated"": ""2021-06-03T05:37:11.8120413Z"",
""$lastUpdatedVersion"": 7,
""TimeToSleep"": {
""$lastUpdated"": ""2021-06-03T05:37:11.8120413Z"",
""$lastUpdatedVersion"": 7
}
},
new JsonTestEmployee
{
EmployeeID = 1,
EmployeeName = "Jim Smith",
CurrentEmployer = new JsonTestCompany { CompanyID = 7, CompanyName = "GGG Amalgamated Industries" },
FormerEmployers = new JsonTestCompany[]
{
new JsonTestCompany { CompanyID = 4, CompanyName = "DDD Amalgamated Industries" },
new JsonTestCompany { CompanyID = 1, CompanyName = "AAA Amalgamated Industries" },
new JsonTestCompany { CompanyID = 6, CompanyName = "FFF Amalgamated Industries" },
},
""$version"": 7
},
""reported"": {
""Firmware"": ""nanoFramework"",
""TimeToSleep"": 30,
""$metadata"": {
""$lastUpdated"": ""2021-06-03T05:52:41.1232797Z"",
""Firmware"": {
""$lastUpdated"": ""2021-06-03T05:52:41.1232797Z""
},
""TimeToSleep"": {
""$lastUpdated"": ""2021-06-03T05:52:41.1232797Z""
}
}
},
""$version"": 374
}
};
},
""capabilities"": {
""iotEdge"": false
}
}";

arrayList = new ArrayList()
{
{ "testString" },
{ 42 },
{ null },
{ DateTime.UtcNow },
{ TimeSpan.FromSeconds(100) }
};
}
const string NestedClassJson = "{\"FirstName\":\"John\",\"LastName\":\"Doe\",\"ArrayProperty\":[\"hello\",\"world\"],\"Address\":null,\"Birthday\":\"1988-04-23T00:00:00.0000000Z\",\"ID\":27,\"Friend\":{\"FirstName\":\"Bob\",\"LastName\":\"Smith\",\"ArrayProperty\":[\"hi\",\"planet\"],\"Address\":\"123 Some St\",\"Birthday\":\"1983-07-03T00:00:00.0000000Z\",\"ID\":2,\"Friend\":null}}";
const string ComplexArrayJson = "{\"TownID\":1,\"EmployeesInThisTown\":[{\"CurrentEmployer\":{\"CompanyID\":3,\"CompanyName\":\"CCC Amalgamated Industries\"},\"EmployeeID\":1,\"FormerEmployers\":[{\"CompanyID\":2,\"CompanyName\":\"BBB Amalgamated Industries\"},{\"CompanyID\":5,\"CompanyName\":\"EEE Amalgamated Industries\"}],\"EmployeeName\":\"John Smith\"},{\"CurrentEmployer\":{\"CompanyID\":7,\"CompanyName\":\"GGG Amalgamated Industries\"},\"EmployeeID\":1,\"FormerEmployers\":[{\"CompanyID\":4,\"CompanyName\":\"DDD Amalgamated Industries\"},{\"CompanyID\":1,\"CompanyName\":\"AAA Amalgamated Industries\"},{\"CompanyID\":6,\"CompanyName\":\"FFF Amalgamated Industries\"}],\"EmployeeName\":\"Jim Smith\"}],\"TownName\":\"myTown\",\"CompaniesInThisTown\":[{\"CompanyID\":1,\"CompanyName\":\"AAA Amalgamated Industries\"},{\"CompanyID\":2,\"CompanyName\":\"BBB Amalgamated Industries\"},{\"CompanyID\":3,\"CompanyName\":\"CCC Amalgamated Industries\"},{\"CompanyID\":4,\"CompanyName\":\"DDD Amalgamated Industries\"},{\"CompanyID\":5,\"CompanyName\":\"EEE Amalgamated Industries\"},{\"CompanyID\":6,\"CompanyName\":\"FFF Amalgamated Industries\"},{\"CompanyID\":7,\"CompanyName\":\"GGG Amalgamated Industries\"},{\"CompanyID\":8,\"CompanyName\":\"HHH Amalgamated Industries\"}]}";

protected override int IterationCount => 20;

[Benchmark]
public void IntArray()
{
RunInIteration(() =>
{
JsonConvert.SerializeObject(intArray);
JsonConvert.DeserializeObject(IntArrayJson, typeof(int[]));
});
}

[Benchmark]
public void ShortArray()
public void ArrayList()
{
RunInIteration(() =>
{
JsonConvert.SerializeObject(shortArray);
JsonConvert.DeserializeObject(ArrayListJson, typeof(ArrayList));
});
}

[Benchmark]
public void String()
public void ComplexObjectAzureTwinPayload()
{
RunInIteration(() =>
{
JsonConvert.SerializeObject(testString);
});
JsonConvert.DeserializeObject(s_AzureTwinsJsonTestPayload, typeof(TwinPayload));
}

[Benchmark]
public void NestedClass()
public void ShortArray()
{
JsonConvert.SerializeObject(nestedTestClass);
RunInIteration(() =>
{
JsonConvert.DeserializeObject(ShortArrayJson, typeof(short[]));
});
}

[Benchmark]
public void ComplexObject()
public void String()
{
JsonConvert.SerializeObject(complexClass);
RunInIteration(() =>
{
JsonConvert.DeserializeObject(StringJson, typeof(string));
});
}

[Benchmark]
public void ComplexArrayObject()
public void NestedClass()
{
JsonConvert.SerializeObject(myTown);
JsonConvert.DeserializeObject(NestedClassJson, typeof(Person));
}

[Benchmark]
public void ArrayList()
public void ComplexArrayObject()
{
RunInIteration(() =>
{
JsonConvert.SerializeObject(arrayList);
});
JsonConvert.DeserializeObject(ComplexArrayJson, typeof(JsonTestTown));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void Short()
RunInIteration(() =>
{
// TODO: Return value should be of type short
var value = (int)JsonConvert.DeserializeObject(ShortJson, typeof(short));
JsonConvert.DeserializeObject(ShortJson, typeof(short));
});
}

Expand All @@ -34,7 +34,7 @@ public void TimeSpanT()
RunInIteration(() =>
{
// TODO: Return value should be of type timespan
var value = JsonConvert.DeserializeObject(TimeSpanJson, typeof(TimeSpan));
JsonConvert.DeserializeObject(TimeSpanJson, typeof(TimeSpan));
});
}

Expand All @@ -44,7 +44,7 @@ public void Float()
RunInIteration(() =>
{
// TODO: Return value should be of type float
var value = (double)JsonConvert.DeserializeObject(FloatJson, typeof(float));
JsonConvert.DeserializeObject(FloatJson, typeof(float));
});
}

Expand All @@ -53,7 +53,7 @@ public void Double()
{
RunInIteration(() =>
{
var value = (double)JsonConvert.DeserializeObject(DoubleJson, typeof(double));
JsonConvert.DeserializeObject(DoubleJson, typeof(double));
});
}

Expand All @@ -62,7 +62,7 @@ public void DateTimeT()
{
RunInIteration(() =>
{
var value = (DateTime)JsonConvert.DeserializeObject(DateTimeJson, typeof(DateTime));
JsonConvert.DeserializeObject(DateTimeJson, typeof(DateTime));
});
}

Expand All @@ -72,7 +72,7 @@ public void Long()
RunInIteration(() =>
{
// TODO: Return value should be of type long
var value = (int)JsonConvert.DeserializeObject(LongJson, typeof(long));
JsonConvert.DeserializeObject(LongJson, typeof(long));
});
}

Expand All @@ -88,7 +88,7 @@ public void EnumBoxed()
{
RunInIteration(() =>
{
var value = (Hashtable)JsonConvert.DeserializeObject(BoxedEnumJson, typeof(Hashtable));
JsonConvert.DeserializeObject(BoxedEnumJson, typeof(Hashtable));
});
}
}
Expand Down
Loading

0 comments on commit 6bf7b9b

Please sign in to comment.