Skip to content

Commit

Permalink
zeroformatter
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeralsing committed Nov 14, 2016
1 parent 63428f4 commit 5939875
Show file tree
Hide file tree
Showing 8 changed files with 6,549 additions and 41 deletions.
24 changes: 12 additions & 12 deletions Wire.PerfTest/Program.cs
Expand Up @@ -18,22 +18,22 @@ private static void Main(string[] args)

private static void Run()
{
var largeStructTest = new LargeStructTest();
largeStructTest.Run(1000000);
//var largeStructTest = new LargeStructTest();
//largeStructTest.Run(1000000);

var guidArrayTest = new GuidArrayTest();
guidArrayTest.Run(30000);
//var guidArrayTest = new GuidArrayTest();
//guidArrayTest.Run(30000);

var guidTest = new GuidTest();
guidTest.Run(1000000);
var typicalPersonArrayTest = new TypicalPersonArrayTest();
typicalPersonArrayTest.Run(1000);
//var guidTest = new GuidTest();
//guidTest.Run(1000000);
//var typicalPersonArrayTest = new TypicalPersonArrayTest();
//typicalPersonArrayTest.Run(1000);

var typicalPersonTest = new TypicalPersonTest();
typicalPersonTest.Run(100000);
//var typicalPersonTest = new TypicalPersonTest();
//typicalPersonTest.Run(100000);

var typicalMessageArrayTest = new TypicalMessageArrayTest();
typicalMessageArrayTest.Run(10000);
//var typicalMessageArrayTest = new TypicalMessageArrayTest();
//typicalMessageArrayTest.Run(10000);

var typicalMessageTest = new TypicalMessageTest();
typicalMessageTest.Run(1000000);
Expand Down
77 changes: 67 additions & 10 deletions Wire.PerfTest/Tests/TestBase.cs
Expand Up @@ -8,9 +8,36 @@
using Newtonsoft.Json;
using NFX.IO;
using NFX.Serialization.Slim;
using ZeroFormatter;
using ZeroFormatter.Formatters;
using ZeroFormatter.Internal;
using ZeroFormatter.Segments;

namespace Wire.PerfTest.Tests
{

public class GuidFormatter : Formatter<Guid>
{
public override int? GetLength()
{
// If size is fixed, return fixed size.
return 16;
}

public override int Serialize(ref byte[] bytes, int offset, Guid value)
{
// BinaryUtil is helpers of byte[] operation
return BinaryUtil.WriteBytes(ref bytes, offset, value.ToByteArray());
}

public override Guid Deserialize(ref byte[] bytes, int offset, DirtyTracker tracker, out int byteSize)
{
byteSize = 16;
var guidBytes = BinaryUtil.ReadBytes(ref bytes, offset, 16);
return new Guid(guidBytes);
}
}

class TestResult
{
public string TestName { get; set; }
Expand All @@ -35,6 +62,7 @@ internal abstract class TestBase<T>

public void Run(int repeat)
{
ZeroFormatter.Formatters.Formatter<Guid>.Register(new GuidFormatter());
Repeat = repeat;
Value = GetValue();
Console.WriteLine();
Expand Down Expand Up @@ -62,7 +90,7 @@ public void Run(int repeat)

var fastestSerializer = _results.OrderBy(r => r.SerializationTime).First();
var fastestDeserializer = _results.OrderBy(r => r.DeserializationTime).First();
var fastestRoundtrip = _results.OrderBy(r => r.DeserializationTime).First();
var fastestRoundtrip = _results.OrderBy(r => r.SerializationTime + r.DeserializationTime).First();
var smallestPayload = _results.OrderBy(r => r.PayloadSize).First();

Console.WriteLine($"* **Fastest Serializer**: {fastestSerializer.TestName} - {(long)fastestSerializer.SerializationTime.TotalMilliseconds} ms");
Expand All @@ -78,18 +106,20 @@ public void Run(int repeat)

protected virtual void TestAll()
{
SerializeZeroFormatter();
SerializeZeroFormatterWithPooling();
SerializeKnownTypesReuseSession();
SerializeKnownTypes();
SerializeDefault();
SerializeVersionTolerant();
SerializeVersionPreserveObjects();
SerializeNFXSlim();
SerializeNFXSlimPreregister();
SerializeSSText();
SerializeNetSerializer();
SerializeProtoBufNet();
SerializeJsonNet();
SerializeBinaryFormatter();
//SerializeVersionTolerant();
//SerializeVersionPreserveObjects();
//SerializeNFXSlim();
//SerializeNFXSlimPreregister();
//SerializeSSText();
//SerializeNetSerializer();
//SerializeProtoBufNet();
//SerializeJsonNet();
//SerializeBinaryFormatter();
}

private void SaveTestResult(string testName, IEnumerable<TestResult> result )
Expand Down Expand Up @@ -334,6 +364,31 @@ private void SerializeBinaryFormatter()
}, bytes.Length);
}

private void SerializeZeroFormatter()
{
var bytes = ZeroFormatter.ZeroFormatterSerializer.Serialize(Value);
RunTest("ZeroFormatter", () =>
{
ZeroFormatter.ZeroFormatterSerializer.Serialize(Value);
}, () =>
{
ZeroFormatter.ZeroFormatterSerializer.Deserialize<T>(bytes);
}, bytes.Length);
}

private void SerializeZeroFormatterWithPooling()
{
var bytes = ZeroFormatter.ZeroFormatterSerializer.Serialize(Value);
RunTest("ZeroFormatterWithPooling", () =>
{
ZeroFormatter.ZeroFormatterSerializer.Serialize(ref bytes, 0, Value);
}, () =>
{
ZeroFormatter.ZeroFormatterSerializer.Deserialize<T>(bytes);
}, bytes.Length);
}


private void SerializeKnownTypesReuseSession()
{
var types = typeof(T).Namespace.StartsWith("System") ? null : new[] { typeof(T) };
Expand All @@ -343,6 +398,7 @@ private void SerializeKnownTypesReuseSession()
var s = new MemoryStream();
serializer.Serialize(Value, s,ss);
var bytes = s.ToArray();

RunTest("Wire - KnownTypes + Reuse Sessions", () =>
{
var stream = new MemoryStream();
Expand All @@ -361,6 +417,7 @@ private void SerializeKnownTypes()
var s = new MemoryStream();
serializer.Serialize(Value, s);
var bytes = s.ToArray();

RunTest("Wire - KnownTypes", () =>
{
var stream = new MemoryStream();
Expand Down
14 changes: 10 additions & 4 deletions Wire.PerfTest/Types/TypicalMessage.cs
Expand Up @@ -4,23 +4,29 @@
using System.Text;
using System.Threading.Tasks;
using ProtoBuf;
using ZeroFormatter;

namespace Wire.PerfTest.Types
{
[ProtoContract]
[Serializable]
[ZeroFormattable]
public class TypicalMessage
{
[ProtoMember(1)]
public string StringProp { get; set; }
[Index(0)]
public virtual string StringProp { get; set; }

[ProtoMember(2)]
public int IntProp { get; set; }
[Index(1)]
public virtual int IntProp { get; set; }

[ProtoMember(3)]
public Guid GuidProp { get; set; }
[Index(2)]
public virtual Guid GuidProp { get; set; }

[ProtoMember(4)]
public DateTime DateProp { get; set; }
[Index(3)]
public virtual DateTime DateProp { get; set; }
}
}
9 changes: 9 additions & 0 deletions Wire.PerfTest/Wire.PerfTest.csproj
Expand Up @@ -34,6 +34,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Bond, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
Expand Down Expand Up @@ -211,6 +212,14 @@
<HintPath>..\packages\System.Xml.XPath.XDocument.4.0.1\lib\net46\System.Xml.XPath.XDocument.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ZeroFormatter, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ZeroFormatter.1.3.0\lib\net45\ZeroFormatter.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="ZeroFormatter.Interfaces, Version=1.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\ZeroFormatter.Interfaces.1.3.0\lib\net45\ZeroFormatter.Interfaces.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ManualSerializer\CustomBinaryFormatter.cs" />
Expand Down
2 changes: 2 additions & 0 deletions Wire.PerfTest/packages.config
Expand Up @@ -59,4 +59,6 @@
<package id="System.Xml.XmlDocument" version="4.0.1" targetFramework="net46" />
<package id="System.Xml.XPath" version="4.0.1" targetFramework="net46" />
<package id="System.Xml.XPath.XDocument" version="4.0.1" targetFramework="net46" />
<package id="ZeroFormatter" version="1.3.0" targetFramework="net46" />
<package id="ZeroFormatter.Interfaces" version="1.3.0" targetFramework="net46" />
</packages>
24 changes: 10 additions & 14 deletions Wire/ValueSerializers/UnsupportedTypeSerializer.cs
@@ -1,10 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Wire.Compilation;
using Wire.Internal;

Expand All @@ -20,36 +16,36 @@ public UnsupportedTypeException(Type t, string msg):base(msg)
}
public class UnsupportedTypeSerializer:ValueSerializer
{
private string errorMessage = "";
private Type invalidType;
private readonly string _errorMessage;
private readonly Type _invalidType;
public UnsupportedTypeSerializer(Type t, string msg)
{
errorMessage = msg;
invalidType = t;
_errorMessage = msg;
_invalidType = t;
}
public override int EmitReadValue([NotNull] ICompiler<ObjectReader> c, int stream, int session, [NotNull] FieldInfo field)
{
throw new UnsupportedTypeException(invalidType, errorMessage);
throw new UnsupportedTypeException(_invalidType, _errorMessage);
}
public override void EmitWriteValue(ICompiler<ObjectWriter> c, int stream, int fieldValue, int session)
{
throw new UnsupportedTypeException(invalidType, errorMessage);
throw new UnsupportedTypeException(_invalidType, _errorMessage);
}
public override object ReadValue([NotNull] Stream stream, [NotNull] DeserializerSession session)
{
throw new UnsupportedTypeException(invalidType, errorMessage);
throw new UnsupportedTypeException(_invalidType, _errorMessage);
}
public override void WriteManifest([NotNull] Stream stream, [NotNull] SerializerSession session)
{
throw new UnsupportedTypeException(invalidType, errorMessage);
throw new UnsupportedTypeException(_invalidType, _errorMessage);
}
public override void WriteValue([NotNull] Stream stream, object value, [NotNull] SerializerSession session)
{
throw new UnsupportedTypeException(invalidType, errorMessage);
throw new UnsupportedTypeException(_invalidType, _errorMessage);
}
public override Type GetElementType()
{
throw new UnsupportedTypeException(invalidType, errorMessage);
throw new UnsupportedTypeException(_invalidType, _errorMessage);
}
}
}
37 changes: 36 additions & 1 deletion Wire/Wire.project.json
@@ -1,5 +1,40 @@
{
"version": "0.8.1-*",
"description": "Wire, fast binary POCO serializer",
"authors": [
"Roger Johansson"
],
"packOptions": {
"tags": [
"serialization",
"akka.net",
"poco"
],
"projectUrl": "https://github.com/akkadotnet/Wire",
"licenseUrl": "https://github.com/akkadotnet/Wire/blob/master/LICENSE",
"iconUrl": "https://raw.githubusercontent.com/hmemcpy/NServiceBus.Wire/master/icon.png"
},
"dependencies": {
},

"frameworks": {
"netstandard1.6": {
"imports": "dnxcore50",
"dependencies": {
"NETStandard.Library": "1.6.0",
"Microsoft.CSharp": "4.0.1"
}
},
"net450": {
"imports": "dotnet5.4",
"dependencies": {},
"define": [ "SERIALIZATION" ]
}
},
"buildOptions": {
"allowUnsafe": true
},
"runtimes": {
"win": {}
"win": {}
}
}

0 comments on commit 5939875

Please sign in to comment.