From 68fa401d70fae728d4b6eef9e58b6cf4f11742a3 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 25 Dec 2015 20:01:13 +1300 Subject: [PATCH] -Array pool perf test --- Src/Newtonsoft.Json.TestConsole/Program.cs | 13 ++++++-- ...ewtonsoft.Json.Tests.Portable.project.json | 2 +- .../Newtonsoft.Json.Tests.project.json | 3 +- Src/Newtonsoft.Json.Tests/PerformanceTests.cs | 32 ++++++++++++++++--- Src/Newtonsoft.Json.Tests/project.json | 3 +- 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/Src/Newtonsoft.Json.TestConsole/Program.cs b/Src/Newtonsoft.Json.TestConsole/Program.cs index c223d819..5a467dc5 100644 --- a/Src/Newtonsoft.Json.TestConsole/Program.cs +++ b/Src/Newtonsoft.Json.TestConsole/Program.cs @@ -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(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); + + Console.WriteLine("Wait to do stuff again"); + Console.ReadKey(); + + t.BenchmarkDeserializeMethod(PerformanceTests.SerializeMethod.JsonNet, PerformanceTests.JsonText); + + //ReadLargeJson(); + //WriteLargeJson(); //DeserializeJson(); //ReadLargeJson(); //ReadLargeJsonJavaScriptSerializer(); diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.project.json b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.project.json index 693a3fa0..b370a58b 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.project.json +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.project.json @@ -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": {} diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.project.json b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.project.json index 1ff92079..b13cce53 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.project.json +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.project.json @@ -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": {} diff --git a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs index 5ffdb084..281effde 100644 --- a/Src/Newtonsoft.Json.Tests/PerformanceTests.cs +++ b/Src/Newtonsoft.Json.Tests/PerformanceTests.cs @@ -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 = @"2010-01-21T11:12:16.0809174+13:00(503) 814-6335fff Street
2009-12-31T11:12:16.0809174+13:00(503) 814-6335array<address
2009-12-30T11:12:16.0809174+13:00(503) 814-6335array 2 address
34123123123.121Rick2010-01-01T12:12:16.0809174+13:00Val & asd11Val2 & asd13Val3 & asd14Markus egger ]><[, (2nd)
"; - 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(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 + { + public static readonly JsonArrayPool Instance = new JsonArrayPool(); + + public char[] Rent(int minimumLength) + { + return ArrayPool.Shared.Rent(minimumLength); + } + + public void Return(char[] array) + { + ArrayPool.Shared.Return(array); + } } public TestClass DeserializeJsonNetManual(string json) diff --git a/Src/Newtonsoft.Json.Tests/project.json b/Src/Newtonsoft.Json.Tests/project.json index e51f4689..6bb0ab8a 100644 --- a/Src/Newtonsoft.Json.Tests/project.json +++ b/Src/Newtonsoft.Json.Tests/project.json @@ -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": {