From 3580c18af960e7fdc963d871c09328087a2a9210 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Thu, 30 May 2019 16:15:59 -0400 Subject: [PATCH 1/5] Add micro benchmarks for (de)serializing dictionaries in S.T.Json --- .../Serializer/ReadJson.Dictionary.cs | 58 +++++++++++++++ .../Serializer/WriteJson.Dictionary.cs | 72 +++++++++++++++++++ 2 files changed, 130 insertions(+) create mode 100644 src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs create mode 100644 src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs new file mode 100644 index 00000000000..d0b1301bae5 --- /dev/null +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs @@ -0,0 +1,58 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using BenchmarkDotNet.Attributes; +using MicroBenchmarks; +using System.Collections.Generic; +using System.Collections.Immutable; + +namespace System.Text.Json.Serialization.Tests +{ + public class ReadDictionary + { + private const string _jsonString = @"{""Hello"":""World"",""Hello2"":""World2""}"; + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public Dictionary DeserializeDict() + { + return JsonSerializer.Parse>(_jsonString); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public IDictionary DeserializeIDict() + { + return JsonSerializer.Parse>(_jsonString); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public IReadOnlyDictionary DeserializeIReadOnlyDict() + { + return JsonSerializer.Parse>(_jsonString); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public ImmutableDictionary DeserializeImmutableDict() + { + return JsonSerializer.Parse>(_jsonString); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public IImmutableDictionary DeserializeIImmutableDict() + { + return JsonSerializer.Parse>(_jsonString); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public ImmutableSortedDictionary DeserializeImmutableSortedDict() + { + return JsonSerializer.Parse>(_jsonString); + } + } +} diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs new file mode 100644 index 00000000000..747c4eb34f2 --- /dev/null +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs @@ -0,0 +1,72 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using BenchmarkDotNet.Attributes; +using MicroBenchmarks; +using System.Collections.Generic; +using System.Collections.Immutable; + +namespace System.Text.Json.Serialization.Tests +{ + public class WriteDictionary + { + private Dictionary _dict = new Dictionary() { { "Hello", "World" }, { "Hello2", "World2" } }; + private static IDictionary _iDict = new Dictionary() { { "Hello", "World" }, { "Hello2", "World2" } }; + private IReadOnlyDictionary _iReadOnlyDict = new Dictionary() { { "Hello", "World" }, { "Hello2", "World2" } }; + + private static ImmutableDictionary _immutableDict; + private static IImmutableDictionary _iimmutableDict; + private static ImmutableSortedDictionary _immutableSortedDict; + + [GlobalSetup] + public void Setup() + { + _immutableDict = ImmutableDictionary.CreateRange(_dict); + _iimmutableDict = ImmutableDictionary.CreateRange(_dict); + _immutableSortedDict = ImmutableSortedDictionary.CreateRange(_dict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeDict() + { + return JsonSerializer.ToUtf8Bytes(_dict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeIDict() + { + return JsonSerializer.ToUtf8Bytes(_iDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeIReadOnlyDict() + { + return JsonSerializer.ToUtf8Bytes(_iReadOnlyDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeImmutableDict() + { + return JsonSerializer.ToUtf8Bytes(_immutableDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeIImmutableDict() + { + return JsonSerializer.ToUtf8Bytes(_iimmutableDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeImmutableSortedDict() + { + return JsonSerializer.ToUtf8Bytes(_immutableSortedDict); + } + } +} From e5727add3f65757993616a87468725fff54ab508 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Fri, 31 May 2019 10:28:47 -0400 Subject: [PATCH 2/5] Add serialize ToString benchmarks --- .../Serializer/WriteJson.Dictionary.cs | 60 ++++++++++++++++--- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs index 747c4eb34f2..1dc8147ebd4 100644 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs @@ -29,44 +29,86 @@ public void Setup() [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public byte[] SerializeDict() + public byte[] SerializeDict_ToUtf8Bytes() { return JsonSerializer.ToUtf8Bytes(_dict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public byte[] SerializeIDict() + public byte[] SerializeIDict_ToUtf8Bytes() { return JsonSerializer.ToUtf8Bytes(_iDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public byte[] SerializeIReadOnlyDict() + public byte[] SerializeIReadOnlyDict_ToUtf8Bytes() { return JsonSerializer.ToUtf8Bytes(_iReadOnlyDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public byte[] SerializeImmutableDict() + public string SerializeDict_ToString() { - return JsonSerializer.ToUtf8Bytes(_immutableDict); + return JsonSerializer.ToString(_dict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public byte[] SerializeIImmutableDict() + public string SerializeIDict_ToString() { - return JsonSerializer.ToUtf8Bytes(_iimmutableDict); + return JsonSerializer.ToString(_iDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public byte[] SerializeImmutableSortedDict() + public string SerializeIReadOnlyDict_ToString() { - return JsonSerializer.ToUtf8Bytes(_immutableSortedDict); + return JsonSerializer.ToString(_iReadOnlyDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeImmutableDict_ToBytes() + { + return System.Text.Json.Serialization.JsonSerializer.ToUtf8Bytes(_immutableDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeIImmutableDict_ToBytes() + { + return System.Text.Json.Serialization.JsonSerializer.ToUtf8Bytes(_iimmutableDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public byte[] SerializeImmutableSortedDict_ToBytes() + { + return System.Text.Json.Serialization.JsonSerializer.ToUtf8Bytes(_immutableSortedDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public string SerializeImmutableDict_ToString() + { + return System.Text.Json.Serialization.JsonSerializer.ToString(_immutableDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public string SerializeIImmutableDict_ToString() + { + return System.Text.Json.Serialization.JsonSerializer.ToString(_iimmutableDict); + } + + [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] + [Benchmark] + public string SerializeImmutableSortedDict_ToString() + { + return System.Text.Json.Serialization.JsonSerializer.ToString(_immutableSortedDict); } } } From b0bc1b84bf5ae9cff64275810b729f189c5e0535 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Mon, 3 Jun 2019 11:36:59 -0400 Subject: [PATCH 3/5] Remove extraneous namespace prefix --- .../Serializer/WriteJson.Dictionary.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs index 1dc8147ebd4..e95bd1416c3 100644 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs @@ -73,42 +73,42 @@ public string SerializeIReadOnlyDict_ToString() [Benchmark] public byte[] SerializeImmutableDict_ToBytes() { - return System.Text.Json.Serialization.JsonSerializer.ToUtf8Bytes(_immutableDict); + return JsonSerializer.ToUtf8Bytes(_immutableDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public byte[] SerializeIImmutableDict_ToBytes() { - return System.Text.Json.Serialization.JsonSerializer.ToUtf8Bytes(_iimmutableDict); + return JsonSerializer.ToUtf8Bytes(_iimmutableDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public byte[] SerializeImmutableSortedDict_ToBytes() { - return System.Text.Json.Serialization.JsonSerializer.ToUtf8Bytes(_immutableSortedDict); + return JsonSerializer.ToUtf8Bytes(_immutableSortedDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public string SerializeImmutableDict_ToString() { - return System.Text.Json.Serialization.JsonSerializer.ToString(_immutableDict); + return JsonSerializer.ToString(_immutableDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public string SerializeIImmutableDict_ToString() { - return System.Text.Json.Serialization.JsonSerializer.ToString(_iimmutableDict); + return JsonSerializer.ToString(_iimmutableDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public string SerializeImmutableSortedDict_ToString() { - return System.Text.Json.Serialization.JsonSerializer.ToString(_immutableSortedDict); + return JsonSerializer.ToString(_immutableSortedDict); } } } From 0f279910918ff8d3bedffcbd7ce760b638cda3d2 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 18 Jul 2019 12:37:32 +0200 Subject: [PATCH 4/5] reflect the API changes --- .../Serializer/ReadJson.Dictionary.cs | 12 +++---- .../Serializer/WriteJson.Dictionary.cs | 36 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs index d0b1301bae5..ed135dad011 100644 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs @@ -17,42 +17,42 @@ public class ReadDictionary [Benchmark] public Dictionary DeserializeDict() { - return JsonSerializer.Parse>(_jsonString); + return JsonSerializer.Deserialize>(_jsonString); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public IDictionary DeserializeIDict() { - return JsonSerializer.Parse>(_jsonString); + return JsonSerializer.Deserialize>(_jsonString); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public IReadOnlyDictionary DeserializeIReadOnlyDict() { - return JsonSerializer.Parse>(_jsonString); + return JsonSerializer.Deserialize>(_jsonString); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public ImmutableDictionary DeserializeImmutableDict() { - return JsonSerializer.Parse>(_jsonString); + return JsonSerializer.Deserialize>(_jsonString); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public IImmutableDictionary DeserializeIImmutableDict() { - return JsonSerializer.Parse>(_jsonString); + return JsonSerializer.Deserialize>(_jsonString); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public ImmutableSortedDictionary DeserializeImmutableSortedDict() { - return JsonSerializer.Parse>(_jsonString); + return JsonSerializer.Deserialize>(_jsonString); } } } diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs index e95bd1416c3..ae4ddc1d158 100644 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs @@ -31,84 +31,84 @@ public void Setup() [Benchmark] public byte[] SerializeDict_ToUtf8Bytes() { - return JsonSerializer.ToUtf8Bytes(_dict); + return JsonSerializer.SerializeToUtf8Bytes(_dict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public byte[] SerializeIDict_ToUtf8Bytes() { - return JsonSerializer.ToUtf8Bytes(_iDict); + return JsonSerializer.SerializeToUtf8Bytes(_iDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public byte[] SerializeIReadOnlyDict_ToUtf8Bytes() { - return JsonSerializer.ToUtf8Bytes(_iReadOnlyDict); + return JsonSerializer.SerializeToUtf8Bytes(_iReadOnlyDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public string SerializeDict_ToString() + public string SerializeDict_Serialize() { - return JsonSerializer.ToString(_dict); + return JsonSerializer.Serialize(_dict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public string SerializeIDict_ToString() + public string SerializeIDict_Serialize() { - return JsonSerializer.ToString(_iDict); + return JsonSerializer.Serialize(_iDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public string SerializeIReadOnlyDict_ToString() + public string SerializeIReadOnlyDict_Serialize() { - return JsonSerializer.ToString(_iReadOnlyDict); + return JsonSerializer.Serialize(_iReadOnlyDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public byte[] SerializeImmutableDict_ToBytes() { - return JsonSerializer.ToUtf8Bytes(_immutableDict); + return JsonSerializer.SerializeToUtf8Bytes(_immutableDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public byte[] SerializeIImmutableDict_ToBytes() { - return JsonSerializer.ToUtf8Bytes(_iimmutableDict); + return JsonSerializer.SerializeToUtf8Bytes(_iimmutableDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] public byte[] SerializeImmutableSortedDict_ToBytes() { - return JsonSerializer.ToUtf8Bytes(_immutableSortedDict); + return JsonSerializer.SerializeToUtf8Bytes (_immutableSortedDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public string SerializeImmutableDict_ToString() + public string SerializeImmutableDict_Serialize() { - return JsonSerializer.ToString(_immutableDict); + return JsonSerializer.Serialize(_immutableDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public string SerializeIImmutableDict_ToString() + public string SerializeIImmutableDict_Serialize() { - return JsonSerializer.ToString(_iimmutableDict); + return JsonSerializer.Serialize(_iimmutableDict); } [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] [Benchmark] - public string SerializeImmutableSortedDict_ToString() + public string SerializeImmutableSortedDict_Serialize() { - return JsonSerializer.ToString(_immutableSortedDict); + return JsonSerializer.Serialize(_immutableSortedDict); } } } From 5f71efa0e5b1c2a145886a59ceccc0d97ca98309 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 18 Jul 2019 12:56:59 +0200 Subject: [PATCH 5/5] use existing ReadJson and WriteJson generic classes instead od introducing new types --- .../micro/Serializers/DataGenerator.cs | 8 ++ .../Serializer/ReadJson.Dictionary.cs | 58 --------- .../System.Text.Json/Serializer/ReadJson.cs | 5 + .../Serializer/WriteJson.Dictionary.cs | 114 ------------------ .../System.Text.Json/Serializer/WriteJson.cs | 5 + 5 files changed, 18 insertions(+), 172 deletions(-) delete mode 100644 src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs delete mode 100644 src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs diff --git a/src/benchmarks/micro/Serializers/DataGenerator.cs b/src/benchmarks/micro/Serializers/DataGenerator.cs index 852a5ef98f7..07efd7e49cf 100644 --- a/src/benchmarks/micro/Serializers/DataGenerator.cs +++ b/src/benchmarks/micro/Serializers/DataGenerator.cs @@ -4,9 +4,11 @@ using System; using System.Collections.Generic; +using System.Collections.Immutable; using System.Linq; using System.Xml; using System.Xml.Serialization; +using BenchmarkDotNet.Extensions; using MessagePack; using ProtoBuf; @@ -34,6 +36,12 @@ internal static T Generate() return (T)(object)new SimpleStructWithProperties { Num = 1, Text = "Foo" }; if (typeof(T) == typeof(ClassImplementingIXmlSerialiable)) return (T)(object)new ClassImplementingIXmlSerialiable { StringValue = "Hello world" }; + if (typeof(T) == typeof(Dictionary)) + return (T)(object)ValuesGenerator.ArrayOfUniqueValues(100).ToDictionary(value => value); + if (typeof(T) == typeof(ImmutableDictionary)) + return (T)(object)ImmutableDictionary.CreateRange(ValuesGenerator.ArrayOfUniqueValues(100).ToDictionary(value => value)); + if (typeof(T) == typeof(ImmutableSortedDictionary)) + return (T)(object)ImmutableSortedDictionary.CreateRange(ValuesGenerator.ArrayOfUniqueValues(100).ToDictionary(value => value)); throw new NotImplementedException(); } diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs deleted file mode 100644 index ed135dad011..00000000000 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.Dictionary.cs +++ /dev/null @@ -1,58 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using BenchmarkDotNet.Attributes; -using MicroBenchmarks; -using System.Collections.Generic; -using System.Collections.Immutable; - -namespace System.Text.Json.Serialization.Tests -{ - public class ReadDictionary - { - private const string _jsonString = @"{""Hello"":""World"",""Hello2"":""World2""}"; - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public Dictionary DeserializeDict() - { - return JsonSerializer.Deserialize>(_jsonString); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public IDictionary DeserializeIDict() - { - return JsonSerializer.Deserialize>(_jsonString); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public IReadOnlyDictionary DeserializeIReadOnlyDict() - { - return JsonSerializer.Deserialize>(_jsonString); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public ImmutableDictionary DeserializeImmutableDict() - { - return JsonSerializer.Deserialize>(_jsonString); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public IImmutableDictionary DeserializeIImmutableDict() - { - return JsonSerializer.Deserialize>(_jsonString); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public ImmutableSortedDictionary DeserializeImmutableSortedDict() - { - return JsonSerializer.Deserialize>(_jsonString); - } - } -} diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.cs index aed4ef2a2cd..13845006fdf 100644 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.cs +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.cs @@ -5,6 +5,8 @@ using BenchmarkDotNet.Attributes; using MicroBenchmarks; using MicroBenchmarks.Serializers; +using System.Collections.Generic; +using System.Collections.Immutable; using System.IO; using System.Threading.Tasks; @@ -15,6 +17,9 @@ namespace System.Text.Json.Serialization.Tests [GenericTypeArguments(typeof(IndexViewModel))] [GenericTypeArguments(typeof(MyEventsListerViewModel))] [GenericTypeArguments(typeof(BinaryData))] + [GenericTypeArguments(typeof(Dictionary))] + [GenericTypeArguments(typeof(ImmutableDictionary))] + [GenericTypeArguments(typeof(ImmutableSortedDictionary))] public class ReadJson { private string _serialized; diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs deleted file mode 100644 index ae4ddc1d158..00000000000 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.Dictionary.cs +++ /dev/null @@ -1,114 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using BenchmarkDotNet.Attributes; -using MicroBenchmarks; -using System.Collections.Generic; -using System.Collections.Immutable; - -namespace System.Text.Json.Serialization.Tests -{ - public class WriteDictionary - { - private Dictionary _dict = new Dictionary() { { "Hello", "World" }, { "Hello2", "World2" } }; - private static IDictionary _iDict = new Dictionary() { { "Hello", "World" }, { "Hello2", "World2" } }; - private IReadOnlyDictionary _iReadOnlyDict = new Dictionary() { { "Hello", "World" }, { "Hello2", "World2" } }; - - private static ImmutableDictionary _immutableDict; - private static IImmutableDictionary _iimmutableDict; - private static ImmutableSortedDictionary _immutableSortedDict; - - [GlobalSetup] - public void Setup() - { - _immutableDict = ImmutableDictionary.CreateRange(_dict); - _iimmutableDict = ImmutableDictionary.CreateRange(_dict); - _immutableSortedDict = ImmutableSortedDictionary.CreateRange(_dict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public byte[] SerializeDict_ToUtf8Bytes() - { - return JsonSerializer.SerializeToUtf8Bytes(_dict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public byte[] SerializeIDict_ToUtf8Bytes() - { - return JsonSerializer.SerializeToUtf8Bytes(_iDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public byte[] SerializeIReadOnlyDict_ToUtf8Bytes() - { - return JsonSerializer.SerializeToUtf8Bytes(_iReadOnlyDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public string SerializeDict_Serialize() - { - return JsonSerializer.Serialize(_dict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public string SerializeIDict_Serialize() - { - return JsonSerializer.Serialize(_iDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public string SerializeIReadOnlyDict_Serialize() - { - return JsonSerializer.Serialize(_iReadOnlyDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public byte[] SerializeImmutableDict_ToBytes() - { - return JsonSerializer.SerializeToUtf8Bytes(_immutableDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public byte[] SerializeIImmutableDict_ToBytes() - { - return JsonSerializer.SerializeToUtf8Bytes(_iimmutableDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public byte[] SerializeImmutableSortedDict_ToBytes() - { - return JsonSerializer.SerializeToUtf8Bytes (_immutableSortedDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public string SerializeImmutableDict_Serialize() - { - return JsonSerializer.Serialize(_immutableDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public string SerializeIImmutableDict_Serialize() - { - return JsonSerializer.Serialize(_iimmutableDict); - } - - [BenchmarkCategory(Categories.CoreFX, Categories.JSON)] - [Benchmark] - public string SerializeImmutableSortedDict_Serialize() - { - return JsonSerializer.Serialize(_immutableSortedDict); - } - } -} diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.cs index 1afceb3c188..e0ca8ad8c3b 100644 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.cs +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.cs @@ -5,6 +5,8 @@ using BenchmarkDotNet.Attributes; using MicroBenchmarks; using MicroBenchmarks.Serializers; +using System.Collections.Generic; +using System.Collections.Immutable; using System.IO; using System.Threading.Tasks; @@ -15,6 +17,9 @@ namespace System.Text.Json.Serialization.Tests [GenericTypeArguments(typeof(IndexViewModel))] [GenericTypeArguments(typeof(MyEventsListerViewModel))] [GenericTypeArguments(typeof(BinaryData))] + [GenericTypeArguments(typeof(Dictionary))] + [GenericTypeArguments(typeof(ImmutableDictionary))] + [GenericTypeArguments(typeof(ImmutableSortedDictionary))] public class WriteJson { private T _value;