-
Notifications
You must be signed in to change notification settings - Fork 273
/
Xml_ToStream.cs
76 lines (67 loc) · 2.88 KB
/
Xml_ToStream.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
// 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_ToStream<T>
{
private T value;
private XmlSerializer xmlSerializer;
private DataContractSerializer dataContractSerializer;
private XmlDictionaryWriter xmlDictionaryWriter;
private MemoryStream memoryStream;
[GlobalSetup]
public void Setup()
{
value = DataGenerator.Generate<T>();
memoryStream = new MemoryStream(capacity: short.MaxValue);
xmlSerializer = new XmlSerializer(typeof(T));
dataContractSerializer = new DataContractSerializer(typeof(T));
xmlDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(memoryStream, null, null, ownsStream: false);
}
[BenchmarkCategory(Categories.Libraries, Categories.Runtime)]
[Benchmark(Description = nameof(XmlSerializer))]
public void XmlSerializer_()
{
memoryStream.Position = 0;
xmlSerializer.Serialize(memoryStream, value);
}
[BenchmarkCategory(Categories.Libraries)]
[Benchmark(Description = nameof(DataContractSerializer))]
public void DataContractSerializer_()
{
memoryStream.Position = 0;
dataContractSerializer.WriteObject(memoryStream, value);
}
[BenchmarkCategory(Categories.Libraries)]
[Benchmark(Description = nameof(XmlDictionaryWriter))]
public void DataContractSerializer_BinaryXml_()
{
memoryStream.Position = 0;
((IXmlBinaryWriterInitializer)xmlDictionaryWriter).SetOutput(memoryStream, null, null, ownsStream: false);
dataContractSerializer.WriteObject(xmlDictionaryWriter, value);
}
// YAXSerializer is not included in the benchmarks because it does not allow to serialize to stream (only to file and string)
[GlobalCleanup]
public void Dispose()
{
xmlDictionaryWriter?.Dispose();
memoryStream?.Dispose();
}
}
}