-Added IJsonBufferPool, performance stuff
This commit is contained in:
@@ -44,6 +44,11 @@ using NUnit.Framework;
|
||||
#endif
|
||||
using Newtonsoft.Json;
|
||||
using System.IO;
|
||||
#if NET20
|
||||
using Newtonsoft.Json.Utilities.LinqBridge;
|
||||
#else
|
||||
using System.Linq;
|
||||
#endif
|
||||
using System.Xml;
|
||||
using Newtonsoft.Json.Utilities;
|
||||
|
||||
@@ -1168,6 +1173,74 @@ third line", jsonTextReader.Value);
|
||||
Assert.AreEqual("type", reader.Value);
|
||||
}
|
||||
|
||||
public class FakeBufferPool : IJsonBufferPool<char>
|
||||
{
|
||||
public readonly IList<char[]> FreeBuffers = new List<char[]>();
|
||||
public readonly IList<char[]> UsedBuffers = new List<char[]>();
|
||||
|
||||
public char[] RentBuffer(int minSize)
|
||||
{
|
||||
char[] buffer = FreeBuffers.FirstOrDefault(b => b.Length >= minSize);
|
||||
if (buffer != null)
|
||||
{
|
||||
FreeBuffers.Remove(buffer);
|
||||
UsedBuffers.Add(buffer);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
buffer = new char[minSize];
|
||||
UsedBuffers.Add(buffer);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public void ReturnBuffer(ref char[] buffer)
|
||||
{
|
||||
UsedBuffers.Remove(buffer);
|
||||
if (!FreeBuffers.Contains(buffer))
|
||||
{
|
||||
FreeBuffers.Add(buffer);
|
||||
}
|
||||
|
||||
buffer = null;
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void BufferTest()
|
||||
{
|
||||
string json = @"{
|
||||
""CPU"": ""Intel"",
|
||||
""Description"": ""Amazing!\nBuy now!"",
|
||||
""Drives"": [
|
||||
""DVD read/writer"",
|
||||
""500 gigabyte hard drive""
|
||||
]
|
||||
}";
|
||||
|
||||
FakeBufferPool bufferPool = new FakeBufferPool();
|
||||
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
using (JsonTextReader reader = new JsonTextReader(new StringReader(json)))
|
||||
{
|
||||
reader.BufferPool = bufferPool;
|
||||
|
||||
while (reader.Read())
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
if ((i + 1) % 100 == 0)
|
||||
{
|
||||
Console.WriteLine("Allocated buffers: " + bufferPool.FreeBuffers.Count);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.AreEqual(2, bufferPool.FreeBuffers.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ReadNullTerminatorStrings()
|
||||
{
|
||||
|
||||
@@ -1271,7 +1271,7 @@ _____'propertyName': NaN,
|
||||
{
|
||||
StringWriter swNew = new StringWriter();
|
||||
char[] buffer = null;
|
||||
JavaScriptUtils.WriteEscapedJavaScriptString(swNew, c.ToString(), '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default, ref buffer);
|
||||
JavaScriptUtils.WriteEscapedJavaScriptString(swNew, c.ToString(), '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default, null, ref buffer);
|
||||
|
||||
StringWriter swOld = new StringWriter();
|
||||
WriteEscapedJavaScriptStringOld(swOld, c.ToString(), '"', true);
|
||||
|
||||
@@ -430,7 +430,7 @@ If attributes are not mentioned, default values are used in each case.
|
||||
using (StringWriter w = StringUtils.CreateStringWriter(StringUtils.GetLength(text) ?? 16))
|
||||
{
|
||||
char[] buffer = null;
|
||||
JavaScriptUtils.WriteEscapedJavaScriptString(w, text, '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default, ref buffer);
|
||||
JavaScriptUtils.WriteEscapedJavaScriptString(w, text, '"', true, JavaScriptUtils.DoubleQuoteCharEscapeFlags, StringEscapeHandling.Default, null, ref buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -136,5 +136,60 @@ namespace Newtonsoft.Json.Tests.Utilities
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HexParse()
|
||||
{
|
||||
HexParseSame("0000");
|
||||
HexParseSame("1234");
|
||||
HexParseSame("4321");
|
||||
HexParseSame("abcd");
|
||||
HexParseSame("dcba");
|
||||
HexParseSame("ffff");
|
||||
HexParseSame("ABCD");
|
||||
HexParseSame("DCBA");
|
||||
HexParseSame("FFFF");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HexParseOffset()
|
||||
{
|
||||
int value = ConvertUtils.HexTextToInt("!0000".ToCharArray(), 1, 5);
|
||||
Assert.AreEqual(0, value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void HexParseError()
|
||||
{
|
||||
ExceptionAssert.Throws<FormatException>(() =>
|
||||
{
|
||||
ConvertUtils.HexTextToInt("-100".ToCharArray(), 0, 4);
|
||||
}, "Invalid hex character: -");
|
||||
ExceptionAssert.Throws<FormatException>(() =>
|
||||
{
|
||||
ConvertUtils.HexTextToInt("000g".ToCharArray(), 0, 4);
|
||||
}, "Invalid hex character: g");
|
||||
ExceptionAssert.Throws<FormatException>(() =>
|
||||
{
|
||||
ConvertUtils.HexTextToInt(" ssd".ToCharArray(), 0, 4);
|
||||
}, "Invalid hex character: ");
|
||||
ExceptionAssert.Throws<FormatException>(() =>
|
||||
{
|
||||
ConvertUtils.HexTextToInt("000:".ToCharArray(), 0, 4);
|
||||
}, "Invalid hex character: :");
|
||||
ExceptionAssert.Throws<FormatException>(() =>
|
||||
{
|
||||
ConvertUtils.HexTextToInt("000G".ToCharArray(), 0, 4);
|
||||
}, "Invalid hex character: G");
|
||||
}
|
||||
|
||||
private void HexParseSame(string text)
|
||||
{
|
||||
int v1 = int.Parse(text, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
|
||||
|
||||
int v2 = ConvertUtils.HexTextToInt(text.ToCharArray(), 0, 4);
|
||||
|
||||
Assert.AreEqual(v1, v2, "Invalid result when parsing hex text: " + text);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,9 @@
|
||||
"exclude": [
|
||||
],
|
||||
"dependencies": {
|
||||
"Newtonsoft.Json": ""
|
||||
"Newtonsoft.Json": {
|
||||
"target": "project"
|
||||
}
|
||||
},
|
||||
"commands": {
|
||||
"test": "xunit.runner.dnx -parallel none"
|
||||
|
||||
Reference in New Issue
Block a user