-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved FastJsonWriter and added tests and benchmarks.
- Loading branch information
Showing
19 changed files
with
513 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<PropertyGroup> | ||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion> | ||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> | ||
</PropertyGroup> | ||
|
||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | ||
<PropertyGroup Label="Globals"> | ||
<ProjectGuid>35d73c72-267b-4598-abc0-f1e1bacde451</ProjectGuid> | ||
<RootNamespace>MirrorSharp.Benchmarks</RootNamespace> | ||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | ||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | ||
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
</PropertyGroup> | ||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
</Project> |
126 changes: 126 additions & 0 deletions
126
MirrorSharp.Benchmarks/Of.Json/ComplexObjectBenchmarks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
using System; | ||
using System.IO; | ||
using BenchmarkDotNet.Attributes; | ||
using MirrorSharp.Internal; | ||
using Newtonsoft.Json; | ||
|
||
namespace MirrorSharp.Benchmarks.Of.Json { | ||
public class ComplexObjectBenchmarks : JsonBenchmarksBase { | ||
/* | ||
{ | ||
"type":"completions", | ||
"completions":{ | ||
"span":{"start":70,"length":0}, | ||
"list":[ | ||
{ | ||
"filterText":"Equals", | ||
"displayText":"Equals", | ||
"tags":["method","public"] | ||
}, | ||
{ | ||
"filterText":"GetHashCode", | ||
"displayText":"GetHashCode", | ||
"tags":["method","public"] | ||
},{ | ||
"filterText":"GetType", | ||
"displayText":"GetType", | ||
"tags":["method","public"] | ||
},{ | ||
"filterText":"ToString", | ||
"displayText":"ToString", | ||
"tags":["method","public"] | ||
} | ||
] | ||
} | ||
} | ||
*/ | ||
|
||
[Benchmark] | ||
public ArraySegment<byte> NewtonsoftJson_JsonWriter() { | ||
_memoryStream.Seek(0, SeekOrigin.Begin); | ||
|
||
var writer = _newtonsoftJsonWriter; | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("type"); | ||
writer.WriteValue("completions"); | ||
writer.WritePropertyName("completions"); | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("span"); | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("start"); | ||
writer.WriteValue(70); | ||
writer.WritePropertyName("length"); | ||
writer.WriteValue(0); | ||
writer.WriteEndObject(); | ||
writer.WritePropertyName("list"); | ||
writer.WriteStartArray(); | ||
WriteCompletion(writer, "Equals"); | ||
WriteCompletion(writer, "GetHashCode"); | ||
WriteCompletion(writer, "GetType"); | ||
WriteCompletion(writer, "ToString"); | ||
writer.WriteEndArray(); | ||
writer.WriteEndObject(); | ||
writer.WriteEndObject(); | ||
|
||
return FlushNewtonsoftJsonWriterAndGetBuffer(); | ||
} | ||
|
||
[Benchmark] | ||
public ArraySegment<byte> MirrorSharp_FastJsonWriter() { | ||
_fastJsonWriter.Reset(); | ||
|
||
var writer = _fastJsonWriter; | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("type"); | ||
writer.WriteValue("completions"); | ||
writer.WritePropertyName("completions"); | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("span"); | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("start"); | ||
writer.WriteValue(70); | ||
writer.WritePropertyName("length"); | ||
writer.WriteValue(0); | ||
writer.WriteEndObject(); | ||
writer.WritePropertyName("list"); | ||
writer.WriteStartArray(); | ||
WriteCompletion(writer, "Equals"); | ||
WriteCompletion(writer, "GetHashCode"); | ||
WriteCompletion(writer, "GetType"); | ||
WriteCompletion(writer, "ToString"); | ||
writer.WriteEndArray(); | ||
writer.WriteEndObject(); | ||
writer.WriteEndObject(); | ||
|
||
return _fastJsonWriter.WrittenSegment; | ||
} | ||
|
||
private static void WriteCompletion(JsonTextWriter writer, string text) { | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("filterText"); | ||
writer.WriteValue(text); | ||
writer.WritePropertyName("displayText"); | ||
writer.WriteValue(text); | ||
writer.WritePropertyName("tags"); | ||
writer.WriteStartArray(); | ||
writer.WriteValue("method"); | ||
writer.WriteValue("public"); | ||
writer.WriteEndArray(); | ||
writer.WriteEndObject(); | ||
} | ||
|
||
private static void WriteCompletion(FastUtf8JsonWriter writer, string text) { | ||
writer.WriteStartObject(); | ||
writer.WritePropertyName("filterText"); | ||
writer.WriteValue(text); | ||
writer.WritePropertyName("displayText"); | ||
writer.WriteValue(text); | ||
writer.WritePropertyName("tags"); | ||
writer.WriteStartArray(); | ||
writer.WriteValue("method"); | ||
writer.WriteValue("public"); | ||
writer.WriteEndArray(); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.IO; | ||
using BenchmarkDotNet.Attributes; | ||
using MirrorSharp.Internal; | ||
using Newtonsoft.Json; | ||
|
||
namespace MirrorSharp.Benchmarks.Of.Json { | ||
public class JsonBenchmarksBase { | ||
// ReSharper disable InconsistentNaming | ||
protected MemoryStream _memoryStream; | ||
protected JsonTextWriter _newtonsoftJsonWriter; | ||
protected FastUtf8JsonWriter _fastJsonWriter; | ||
// ReSharper restore InconsistentNaming | ||
|
||
[Setup] | ||
public void Setup() { | ||
_memoryStream = new MemoryStream(); | ||
_newtonsoftJsonWriter = new JsonTextWriter(new StreamWriter(_memoryStream)) { | ||
Formatting = Formatting.None | ||
}; | ||
_fastJsonWriter = new FastUtf8JsonWriter(ArrayPool<byte>.Shared); | ||
} | ||
|
||
protected ArraySegment<byte> FlushNewtonsoftJsonWriterAndGetBuffer() { | ||
_newtonsoftJsonWriter.Flush(); | ||
ArraySegment<byte> buffer; | ||
_memoryStream.TryGetBuffer(out buffer); | ||
return buffer; | ||
} | ||
|
||
[Cleanup] | ||
public void Cleanup() { | ||
_fastJsonWriter.Dispose(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
MirrorSharp.Benchmarks/Of.Json/WriteValueInt32Benchmarks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Attributes; | ||
using MirrorSharp.Benchmarks.Of.Json; | ||
using MirrorSharp.Internal; | ||
using Newtonsoft.Json; | ||
|
||
namespace MirrorSharp.Benchmarks { | ||
public class WriteValueInt32Benchmarks : JsonBenchmarksBase { | ||
[Params(-111, 1, 1111111)] | ||
public int Value { get; set; } | ||
|
||
[Benchmark] | ||
public ArraySegment<byte> NewtonsoftJson_JsonWriter() { | ||
_memoryStream.Seek(0, SeekOrigin.Begin); | ||
_newtonsoftJsonWriter.WriteValue(Value); | ||
return FlushNewtonsoftJsonWriterAndGetBuffer(); | ||
} | ||
|
||
[Benchmark] | ||
public ArraySegment<byte> MirrorSharp_FastJsonWriter() { | ||
_fastJsonWriter.Reset(); | ||
_fastJsonWriter.WriteValue(Value); | ||
return _fastJsonWriter.WrittenSegment; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
MirrorSharp.Benchmarks/Of.Json/WriteValueStringBenchmarks.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.IO; | ||
using BenchmarkDotNet.Attributes; | ||
|
||
namespace MirrorSharp.Benchmarks.Of.Json { | ||
public class WriteValueStringBenchmarks : JsonBenchmarksBase { | ||
private const string String = @" | ||
using System; | ||
public class C { | ||
public void M() { | ||
} | ||
} | ||
"; | ||
|
||
[Benchmark] | ||
public ArraySegment<byte> NewtonsoftJson_JsonWriter() { | ||
_memoryStream.Seek(0, SeekOrigin.Begin); | ||
_newtonsoftJsonWriter.WriteValue(String); | ||
return FlushNewtonsoftJsonWriterAndGetBuffer(); | ||
} | ||
|
||
[Benchmark] | ||
public ArraySegment<byte> MirrorSharp_FastJsonWriter() { | ||
_fastJsonWriter.Reset(); | ||
_fastJsonWriter.WriteValue(String); | ||
return _fastJsonWriter.WrittenSegment; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using BenchmarkDotNet.Running; | ||
using MirrorSharp.Benchmarks.Of.Json; | ||
|
||
namespace MirrorSharp.Benchmarks { | ||
public static class Program { | ||
public static void Main(string[] args) { | ||
BenchmarkRunner.Run<WriteValueStringBenchmarks>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
// General Information about an assembly is controlled through the following | ||
// set of attributes. Change these attribute values to modify the information | ||
// associated with an assembly. | ||
[assembly: AssemblyConfiguration("")] | ||
[assembly: AssemblyCompany("")] | ||
[assembly: AssemblyProduct("MirrorSharp.Benchmarks")] | ||
[assembly: AssemblyTrademark("")] | ||
|
||
// Setting ComVisible to false makes the types in this assembly not visible | ||
// to COM components. If you need to access a type in this assembly from | ||
// COM, set the ComVisible attribute to true on that type. | ||
[assembly: ComVisible(false)] | ||
|
||
// The following GUID is for the ID of the typelib if this project is exposed to COM | ||
[assembly: Guid("35d73c72-267b-4598-abc0-f1e1bacde451")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"version": "1.0.0-*", | ||
"buildOptions": { | ||
"emitEntryPoint": true | ||
}, | ||
|
||
"dependencies": { | ||
"BenchmarkDotNet": "0.9.9", | ||
"Newtonsoft.Json": "9.0.1", | ||
"MirrorSharp.Common": "*", | ||
"Microsoft.NETCore.App": { | ||
"type": "platform", | ||
"version": "1.0.0" | ||
} | ||
}, | ||
|
||
"frameworks": { | ||
"netcoreapp1.0": { | ||
"imports": [ "dnxcore50", "portable-net45+win8+wp8+wpa81", "portable-net45+win8" ] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace MirrorSharp.Internal { | ||
public static class EncodingExtensions { | ||
public static string GetString(this Encoding encoding, ArraySegment<byte> segment) { | ||
return encoding.GetString(segment.Array, segment.Offset, segment.Count); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.