From 4c447c314d91c0351a9c114e046707e7c0f48c79 Mon Sep 17 00:00:00 2001 From: Layomi Akinrinade Date: Tue, 4 Jun 2019 11:18:58 -0400 Subject: [PATCH 1/2] Add benchmarks for ISet (de)serialization and general framework for generic collections --- .../Serializer/ReadJson.GenericCollection.cs | 32 +++++++++++++++++ .../Serializer/WriteJson.GenericCollection.cs | 35 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.GenericCollection.cs create mode 100644 src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.GenericCollection.cs diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.GenericCollection.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.GenericCollection.cs new file mode 100644 index 00000000000..03bfac2be30 --- /dev/null +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.GenericCollection.cs @@ -0,0 +1,32 @@ +// 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 System.Collections.Generic; + +namespace System.Text.Json.Serialization.Tests +{ + public class WriteGenericCollection + { + private static readonly ISet _iset = new HashSet(); + + [Params(2, 50, 100)] + public int ElementCount; + + [GlobalSetup] + public void Setup() + { + for (int i = 0; i < ElementCount; i++) + { + _iset.Add($"hello{i}"); + } + } + + [Benchmark] + public string SerializeISet_ToBytes() + { + return JsonSerializer.ToString(_iset); + } + } +} \ No newline at end of file diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.GenericCollection.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.GenericCollection.cs new file mode 100644 index 00000000000..9d3d0b39f78 --- /dev/null +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.GenericCollection.cs @@ -0,0 +1,35 @@ +// 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 System.Collections.Generic; + +namespace System.Text.Json.Serialization.Tests +{ + public class ReadGenericCollection + { + private static string _jsonString; + + [Params(2, 50, 100)] + public int ElementCount; + + [GlobalSetup] + public void Setup() + { + ISet _iset = new HashSet(); + for (int i = 0; i < ElementCount; i++) + { + _iset.Add($"hello{i}"); + } + + _jsonString = JsonSerializer.ToString(_iset); + } + + [Benchmark] + public ISet DeserializeISet() + { + return JsonSerializer.Parse>(_jsonString); + } + } +} \ No newline at end of file From 0dcc16b4f879a400b9276c3e7bb29dfa8a83e4f7 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Thu, 18 Jul 2019 13:08:01 +0200 Subject: [PATCH 2/2] use existing ReadJson and WriteJson generic classes instead od introducing new types --- .../micro/Serializers/DataGenerator.cs | 3 ++ .../Serializer/ReadJson.GenericCollection.cs | 32 ----------------- .../System.Text.Json/Serializer/ReadJson.cs | 1 + .../Serializer/WriteJson.GenericCollection.cs | 35 ------------------- .../System.Text.Json/Serializer/WriteJson.cs | 1 + 5 files changed, 5 insertions(+), 67 deletions(-) delete mode 100644 src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.GenericCollection.cs delete mode 100644 src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.GenericCollection.cs diff --git a/src/benchmarks/micro/Serializers/DataGenerator.cs b/src/benchmarks/micro/Serializers/DataGenerator.cs index 07efd7e49cf..677ee8828da 100644 --- a/src/benchmarks/micro/Serializers/DataGenerator.cs +++ b/src/benchmarks/micro/Serializers/DataGenerator.cs @@ -42,6 +42,9 @@ internal static T Generate() 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)); + if (typeof(T) == typeof(HashSet)) + return (T)(object)new HashSet(ValuesGenerator.ArrayOfUniqueValues(100)); + throw new NotImplementedException(); } diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.GenericCollection.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.GenericCollection.cs deleted file mode 100644 index 03bfac2be30..00000000000 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.GenericCollection.cs +++ /dev/null @@ -1,32 +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 System.Collections.Generic; - -namespace System.Text.Json.Serialization.Tests -{ - public class WriteGenericCollection - { - private static readonly ISet _iset = new HashSet(); - - [Params(2, 50, 100)] - public int ElementCount; - - [GlobalSetup] - public void Setup() - { - for (int i = 0; i < ElementCount; i++) - { - _iset.Add($"hello{i}"); - } - } - - [Benchmark] - public string SerializeISet_ToBytes() - { - return JsonSerializer.ToString(_iset); - } - } -} \ No newline at end of file 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 13845006fdf..0c518457054 100644 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.cs +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/ReadJson.cs @@ -20,6 +20,7 @@ namespace System.Text.Json.Serialization.Tests [GenericTypeArguments(typeof(Dictionary))] [GenericTypeArguments(typeof(ImmutableDictionary))] [GenericTypeArguments(typeof(ImmutableSortedDictionary))] + [GenericTypeArguments(typeof(HashSet))] public class ReadJson { private string _serialized; diff --git a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.GenericCollection.cs b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.GenericCollection.cs deleted file mode 100644 index 9d3d0b39f78..00000000000 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.GenericCollection.cs +++ /dev/null @@ -1,35 +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 System.Collections.Generic; - -namespace System.Text.Json.Serialization.Tests -{ - public class ReadGenericCollection - { - private static string _jsonString; - - [Params(2, 50, 100)] - public int ElementCount; - - [GlobalSetup] - public void Setup() - { - ISet _iset = new HashSet(); - for (int i = 0; i < ElementCount; i++) - { - _iset.Add($"hello{i}"); - } - - _jsonString = JsonSerializer.ToString(_iset); - } - - [Benchmark] - public ISet DeserializeISet() - { - return JsonSerializer.Parse>(_jsonString); - } - } -} \ No newline at end of file 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 e0ca8ad8c3b..26e0ebfbddb 100644 --- a/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.cs +++ b/src/benchmarks/micro/corefx/System.Text.Json/Serializer/WriteJson.cs @@ -20,6 +20,7 @@ namespace System.Text.Json.Serialization.Tests [GenericTypeArguments(typeof(Dictionary))] [GenericTypeArguments(typeof(ImmutableDictionary))] [GenericTypeArguments(typeof(ImmutableSortedDictionary))] + [GenericTypeArguments(typeof(HashSet))] public class WriteJson { private T _value;