-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathXml_FromStream.cs
99 lines (88 loc) · 4.04 KB
/
Xml_FromStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 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 System.IO;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Attributes.Filters;
namespace MicroBenchmarks.Serializers
{
[GenericTypeArguments(typeof(LoginViewModel))]
[GenericTypeArguments(typeof(Location))]
[GenericTypeArguments(typeof(IndexViewModel))]
[GenericTypeArguments(typeof(MyEventsListerViewModel))]
[GenericTypeArguments(typeof(CollectionsOfPrimitives))]
[GenericTypeArguments(typeof(XmlElement))]
[GenericTypeArguments(typeof(SimpleStructWithProperties))]
[GenericTypeArguments(typeof(ClassImplementingIXmlSerialiable))]
[AotFilter("Currently not supported due to missing metadata.")]
public class Xml_FromStream<T>
{
private T value;
private XmlSerializer xmlSerializer;
private DataContractSerializer dataContractSerializer;
private MemoryStream memoryStream;
private byte[] memoryBytes;
private XmlDictionaryReader xmlDictionaryReader;
[GlobalSetup(Target = nameof(XmlSerializer_))]
public void SetupXmlSerializer()
{
value = DataGenerator.Generate<T>();
memoryStream = new MemoryStream(capacity: short.MaxValue);
memoryStream.Position = 0;
xmlSerializer = new XmlSerializer(typeof(T));
xmlSerializer.Serialize(memoryStream, value);
}
[BenchmarkCategory(Categories.Libraries, Categories.Runtime)]
[Benchmark(Description = nameof(XmlSerializer))]
public T XmlSerializer_()
{
memoryStream.Position = 0;
return (T)xmlSerializer.Deserialize(memoryStream);
}
[GlobalSetup(Target = nameof(DataContractSerializer_))]
public void SetupDataContractSerializer()
{
value = DataGenerator.Generate<T>();
memoryStream = new MemoryStream(capacity: short.MaxValue);
memoryStream.Position = 0;
dataContractSerializer = new DataContractSerializer(typeof(T));
dataContractSerializer.WriteObject(memoryStream, value);
}
[BenchmarkCategory(Categories.Libraries)]
[Benchmark(Description = nameof(DataContractSerializer))]
public T DataContractSerializer_()
{
memoryStream.Position = 0;
return (T)dataContractSerializer.ReadObject(memoryStream);
}
[GlobalSetup(Target = nameof(DataContractSerializer_BinaryXml_))]
public void SetupDataContractSerializer_BinaryXml_()
{
value = DataGenerator.Generate<T>();
memoryStream = new MemoryStream(capacity: short.MaxValue);
memoryStream.Position = 0;
dataContractSerializer = new DataContractSerializer(typeof(T));
using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateBinaryWriter(memoryStream, null, null, ownsStream: false))
dataContractSerializer.WriteObject(writer, value);
memoryBytes = memoryStream.ToArray();
xmlDictionaryReader = XmlDictionaryReader.CreateBinaryReader(memoryBytes, XmlDictionaryReaderQuotas.Max);
}
[BenchmarkCategory(Categories.Libraries)]
[Benchmark(Description = nameof(XmlDictionaryReader))]
public T DataContractSerializer_BinaryXml_()
{
((IXmlBinaryReaderInitializer)xmlDictionaryReader).SetInput(memoryBytes, 0, memoryBytes.Length, null, XmlDictionaryReaderQuotas.Max, null, null);
return (T)dataContractSerializer.ReadObject(xmlDictionaryReader);
}
// YAXSerializer is not included in the benchmarks because it does not allow to deserialize from stream (only from file and string)
[GlobalCleanup]
public void Cleanup()
{
xmlDictionaryReader?.Dispose();
memoryStream?.Dispose();
}
}
}