Skip to content

Commit

Permalink
add binary xml version to xml benchmarks (#2557)
Browse files Browse the repository at this point in the history
* add binary xml version to xml benchmarks

* fix indentation

* Fix dispose implementation
  • Loading branch information
Daniel-Svensson committed Mar 10, 2023
1 parent 231c93e commit cc6f516
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
32 changes: 30 additions & 2 deletions src/benchmarks/micro/Serializers/Xml_FromStream.cs
Expand Up @@ -26,6 +26,8 @@ public class Xml_FromStream<T>
private XmlSerializer xmlSerializer;
private DataContractSerializer dataContractSerializer;
private MemoryStream memoryStream;
private byte[] memoryBytes;
private XmlDictionaryReader xmlDictionaryReader;

[GlobalSetup(Target = nameof(XmlSerializer_))]
public void SetupXmlSerializer()
Expand Down Expand Up @@ -63,9 +65,35 @@ public T DataContractSerializer_()
return (T)dataContractSerializer.ReadObject(memoryStream);
}

// YAXSerializer is not included in the benchmarks because it does not allow to deserialize from stream (only from file and string)
[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() => memoryStream.Dispose();
public void Cleanup()
{
xmlDictionaryReader?.Dispose();
memoryStream?.Dispose();
}
}
}
18 changes: 17 additions & 1 deletion src/benchmarks/micro/Serializers/Xml_ToStream.cs
Expand Up @@ -25,6 +25,7 @@ public class Xml_ToStream<T>
private T value;
private XmlSerializer xmlSerializer;
private DataContractSerializer dataContractSerializer;
private XmlDictionaryWriter xmlDictionaryWriter;
private MemoryStream memoryStream;

[GlobalSetup]
Expand All @@ -34,6 +35,7 @@ public void Setup()
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)]
Expand All @@ -52,9 +54,23 @@ public void DataContractSerializer_()
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() => memoryStream.Dispose();
public void Dispose()
{
xmlDictionaryWriter?.Dispose();
memoryStream?.Dispose();
}
}
}

0 comments on commit cc6f516

Please sign in to comment.