-Array pool perf test

This commit is contained in:
James Newton-King
2015-12-25 20:01:13 +13:00
parent 58fff8c2f8
commit 68fa401d70
5 changed files with 43 additions and 10 deletions
+11 -2
View File
@@ -51,8 +51,17 @@ namespace Newtonsoft.Json.TestConsole
//PerformanceTests t = new PerformanceTests();
//t.DeserializeLargeJson();
//DeserializeLargeJson();
WriteLargeJson();
PerformanceTests t = new PerformanceTests();
t.Iterations = 10000;
t.BenchmarkDeserializeMethod<TestClass>(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText);
Console.WriteLine("Wait to do stuff again");
Console.ReadKey();
t.BenchmarkDeserializeMethod<TestClass>(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText);
//ReadLargeJson();
//WriteLargeJson();
//DeserializeJson();
//ReadLargeJson();
//ReadLargeJsonJavaScriptSerializer();
@@ -2,7 +2,7 @@
"dependencies": {
"FSharp.Core": "4.0.0.1",
"NUnit": "2.6.2",
"System.Collections.Immutable": "1.1.36"
"System.Collections.Immutable": "1.1.37"
},
"frameworks": {
"net45": {}
@@ -3,7 +3,8 @@
"Autofac": "4.0.0-beta7-130",
"FSharp.Core": "4.0.0.1",
"NUnit": "2.6.2",
"System.Collections.Immutable": "1.1.36"
"System.Buffers": "4.0.0-rc2-23623",
"System.Collections.Immutable": "1.1.37"
},
"frameworks": {
"net45": {}
+27 -5
View File
@@ -23,9 +23,10 @@
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
#if !(NET20 || NET35 || NETFX_CORE || PORTABLE || DNXCORE50)
#if !(NET20 || NET35 || NET40 || NETFX_CORE || PORTABLE || PORTABLE40 || DNXCORE50)
using System.Xml;
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
@@ -67,7 +68,7 @@ namespace Newtonsoft.Json.Tests
public class PerformanceTests : TestFixtureBase
{
public int Iterations = 100;
//private const int Iterations = 5000;
//public int Iterations = 10000;
#region Data
private const string BsonHex =
@@ -79,7 +80,7 @@ namespace Newtonsoft.Json.Tests
private const string XmlText =
@"<TestClass xmlns=""http://schemas.datacontract.org/2004/07/Newtonsoft.Json.Tests"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><Address1><Entered>2010-01-21T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>fff Street</Street></Address1><Addresses><Address><Entered>2009-12-31T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>&#x1F;array&lt;address</Street></Address><Address><Entered>2009-12-30T11:12:16.0809174+13:00</Entered><Phone>(503) 814-6335</Phone><Street>array 2 address</Street></Address></Addresses><BigNumber>34123123123.121</BigNumber><Name>Rick</Name><Now>2010-01-01T12:12:16.0809174+13:00</Now><dictionary xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:KeyValueOfstringint><a:Key>Val &amp; asd1</a:Key><a:Value>1</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>Val2 &amp; asd1</a:Key><a:Value>3</a:Value></a:KeyValueOfstringint><a:KeyValueOfstringint><a:Key>Val3 &amp; asd1</a:Key><a:Value>4</a:Value></a:KeyValueOfstringint></dictionary><strings xmlns:a=""http://schemas.microsoft.com/2003/10/Serialization/Arrays""><a:string i:nil=""true""/><a:string>Markus egger ]&gt;&lt;[, (2nd)</a:string><a:string i:nil=""true""/></strings></TestClass>";
private const string JsonText =
public const string JsonText =
@"{""strings"":[null,""Markus egger ]><[, (2nd)"",null],""dictionary"":{""Val & asd1"":1,""Val2 & asd1"":3,""Val3 & asd1"":4},""Name"":""Rick"",""Now"":""\/Date(1262301136080+1300)\/"",""BigNumber"":34123123123.121,""Address1"":{""Street"":""fff Street"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1264025536080+1300)\/""},""Addresses"":[{""Street"":""\u001farray<address"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1262211136080+1300)\/""},{""Street"":""array 2 address"",""Phone"":""(503) 814-6335"",""Entered"":""\/Date(1262124736080+1300)\/""}]}";
private const string JsonIsoText =
@@ -807,6 +808,7 @@ If attributes are not mentioned, default values are used in each case.
public void BenchmarkDeserializeMethod<T>(SerializeMethod method, object json, int? iterations = null, bool warmUp = true)
{
GC.Collect();
GC.WaitForPendingFinalizers();
if (warmUp)
{
@@ -845,8 +847,28 @@ If attributes are not mentioned, default values are used in each case.
serializer.Converters.Add(new IsoDateTimeConverter());
}
var value = (T)serializer.Deserialize(new StringReader(json), type);
return value;
using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
{
reader.ArrayPool = JsonArrayPool.Instance;
var value = (T)serializer.Deserialize(reader, type);
return value;
}
}
public class JsonArrayPool : IArrayPool<char>
{
public static readonly JsonArrayPool Instance = new JsonArrayPool();
public char[] Rent(int minimumLength)
{
return ArrayPool<char>.Shared.Rent(minimumLength);
}
public void Return(char[] array)
{
ArrayPool<char>.Shared.Return(array);
}
}
public TestClass DeserializeJsonNetManual(string json)
+2 -1
View File
@@ -32,7 +32,8 @@
"Autofac": "4.0.0-beta7-130",
"FSharp.Core": "4.0.0.1",
"NUnit": "2.6.4",
"System.Collections.Immutable": "1.1.37"
"System.Collections.Immutable": "1.1.37",
"System.Buffers": "4.0.0-rc2-23623"
}
},
"net40": {