Skip to content

Commit

Permalink
Performance optimizations and support nullable types and honor NullIm…
Browse files Browse the repository at this point in the history
…plication specification
  • Loading branch information
Claytonious committed May 1, 2014
1 parent d765aea commit 47e13a9
Show file tree
Hide file tree
Showing 16 changed files with 424 additions and 220 deletions.
57 changes: 57 additions & 0 deletions PerfTest/PerfTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{F9E4A0A8-0657-461D-AAD7-E78B97EBE22E}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>PerfTest</RootNamespace>
<AssemblyName>PerfTest</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;COEXIST_CLI</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Externalconsole>true</Externalconsole>
<DefineConstants>COEXIST_CLI</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="MsgPack">
<HintPath>..\lib\MsgPack.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="..\msgpack-sharp-tests\AnimalMessage.cs">
<Link>AnimalMessage.cs</Link>
</Compile>
<Compile Include="..\msgpack-sharp-tests\AnimalColor.cs">
<Link>AnimalColor.cs</Link>
</Compile>
<Compile Include="..\msgpack-sharp-tests\Habitat.cs">
<Link>Habitat.cs</Link>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
<ProjectReference Include="..\msgpack-sharp\msgpack-sharp.csproj">
<Project>{765753B8-A9EB-4DD1-B0F0-D478E54E9ACB}</Project>
<Name>msgpack-sharp</Name>
</ProjectReference>
</ItemGroup>
</Project>
67 changes: 67 additions & 0 deletions PerfTest/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using scopely.msgpacksharp.tests;
using scopely.msgpacksharp;
using System.IO;

namespace PerfTest
{
class MainClass
{
private const int numMessagesToTest = 10000;
private static AnimalMessage testMsg;

public static void Main(string[] args)
{
testMsg = AnimalMessage.CreateTestMessage();

// Warm-up for both
//TestCli();
//TestMsgPackSharp();

// Actual tests...
TimeTest(TestCli, "Serialize and Deserialize with Official CLI");
TimeTest(TestMsgPackSharp, "Serialize and Deserialize with MsgPack-Sharp");
}

private static void TimeTest(Action test, string testName)
{
DateTime start = DateTime.Now;
test();
DateTime end = DateTime.Now;
TimeSpan diff = end.Subtract(start);
double numPerSecond = (double)numMessagesToTest / diff.TotalSeconds;
Console.Out.WriteLine(testName + ":\n\t{0:n0} messages per second", numPerSecond);
}

private static void TestMsgPackSharp()
{
for (int i = 0; i < numMessagesToTest; i++)
{
byte[] buffer = MsgPackSerializer.SerializeObject(testMsg);
#pragma warning disable 0219
var deserialized = MsgPackSerializer.Deserialize<AnimalMessage>(buffer);
#pragma warning restore 0219
}
}

private static void TestCli()
{
var serializer = MsgPack.Serialization.MessagePackSerializer.Create<AnimalMessage>();
for (int i = 0; i < numMessagesToTest; i++)
{
byte[] buffer = null;
using (MemoryStream outStream = new MemoryStream())
{
serializer.Pack(outStream, testMsg);
buffer = outStream.ToArray();
}
using (MemoryStream stream = new MemoryStream(buffer))
{
#pragma warning disable 0219
var deserialized = serializer.Unpack(stream);
#pragma warning restore 0219
}
}
}
}
}
22 changes: 22 additions & 0 deletions PerfTest/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Reflection;
using System.Runtime.CompilerServices;

// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("PerfTest")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("Clay")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.*")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]

File renamed without changes.
8 changes: 5 additions & 3 deletions msgpack-sharp-tests/AnimalColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ public AnimalColor()
{
}

[MessagePackMember(10)]
[MessagePackMember(0)]
public float Red { get; set; }
[MessagePackMember(20)]

[MessagePackMember(1)]
public float Green { get; set; }
[MessagePackMember(30)]

[MessagePackMember(2)]
public float Blue { get; set; }

public override bool Equals(object obj)
Expand Down
33 changes: 22 additions & 11 deletions msgpack-sharp-tests/AnimalMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,34 @@ public AnimalMessage()
public DateTime BirthDay { get; set; }

[MessagePackMember( 6 )]
public int[] SomeNumbers { get; set; }

[MessagePackMember( 7 )]
public List<AnimalColor> SpotColors { get; set; }

[MessagePackMember( 8 )]
[MessagePackMember( 7 )]
public List<AnimalColor> MoreColors { get; set; }

[MessagePackMember( 9 )]
[MessagePackMember( 8 )]
public Dictionary<string,string> Metadata { get; set; }

[MessagePackMember( 10 )]
[MessagePackMember( 9 )]
public List<int> ListOfInts { get; set; }

[MessagePackMember( 11 )]
[MessagePackMember( 10 )]
public Habitat CurrentHabitat { get; set; }

[MessagePackMember( 12 )]
[MessagePackMember( 11 )]
public string TheLongString { get; set; }

#if !COEXIST_CLI
[MessagePackMember( 12, NilImplication = NilImplication.Null )]
public int? NullableIntOne { get; set; }

[MessagePackMember( 13, NilImplication = NilImplication.MemberDefault )]
public int? NullableIntTwo { get; set; }

[MessagePackMember( 14, NilImplication = NilImplication.MemberDefault )]
public int? NullableIntThree { get; set; }
#endif

public static AnimalMessage CreateTestMessage()
{
AnimalMessage msg = new AnimalMessage();
Expand All @@ -58,9 +66,6 @@ public static AnimalMessage CreateTestMessage()
msg.AnimalName = "Lunchbox";
msg.AnimalColor = new AnimalColor() { Red = 1.0f, Green = 0.1f, Blue = 0.1f };
msg.BirthDay = new DateTime(1974, 1, 4);
msg.SomeNumbers = new int[5];
for (int i = 0; i < msg.SomeNumbers.Length; i++)
msg.SomeNumbers[i] = i * 2;
msg.SpotColors = new List<AnimalColor>();
for (int i = 0; i < 3; i++)
{
Expand All @@ -82,6 +87,12 @@ public static AnimalMessage CreateTestMessage()
msg.TheLongString += "+";
}

#if !COEXIST_CLI
msg.NullableIntOne = null;
msg.NullableIntTwo = null;
msg.NullableIntThree = 1;
#endif

return msg;
}
}
Expand Down
9 changes: 4 additions & 5 deletions msgpack-sharp-tests/SerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ private void VerifyAnimalMessage(AnimalMessage msg, AnimalMessage restored)
Assert.AreEqual(msg.AnimalColor.Green, restored.AnimalColor.Green);
Assert.AreEqual(msg.AnimalColor.Blue, restored.AnimalColor.Blue);
Assert.AreEqual(msg.BirthDay, restored.BirthDay);
Assert.AreEqual(msg.SomeNumbers.Length, restored.SomeNumbers.Length);
for (int i = 0; i < msg.SomeNumbers.Length; i++)
{
Assert.AreEqual(msg.SomeNumbers[i], restored.SomeNumbers[i]);
}
Assert.AreEqual(msg.SpotColors.Count, restored.SpotColors.Count);
for (int i = 0; i < msg.SpotColors.Count; i++)
{
Expand All @@ -124,6 +119,10 @@ private void VerifyAnimalMessage(AnimalMessage msg, AnimalMessage restored)

Assert.AreEqual(msg.CurrentHabitat, restored.CurrentHabitat);
Assert.AreEqual(msg.TheLongString, restored.TheLongString);

Assert.IsFalse(restored.NullableIntOne.HasValue);
Assert.IsTrue(restored.NullableIntTwo.HasValue);
Assert.IsTrue(restored.NullableIntThree.HasValue && msg.NullableIntThree.Value == 1);
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions msgpack-sharp-verifier/msgpack-sharp-verifier.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<CustomCommands>
<CustomCommands>
<Command type="AfterBuild" command="cp ${TargetFile} ${SolutionDir}/msgpack-sharp-tests/bin/Debug/" />
<Command type="AfterBuild" command="cp ${ProjectDir}/MsgPack.dll ${SolutionDir}/msgpack-sharp-tests/bin/Debug/" />
<Command type="AfterBuild" command="cp ${ProjectDir}/../lib/MsgPack.dll ${SolutionDir}/msgpack-sharp-tests/bin/Debug/" />
</CustomCommands>
</CustomCommands>
</PropertyGroup>
Expand All @@ -37,7 +37,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="MsgPack">
<HintPath>MsgPack.dll</HintPath>
<HintPath>..\lib\MsgPack.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand All @@ -49,9 +49,6 @@
<Compile Include="..\msgpack-sharp-tests\AnimalColor.cs">
<Link>AnimalColor.cs</Link>
</Compile>
<Compile Include="..\msgpack-sharp\MsgPackAttribute.cs">
<Link>MsgPackAttribute.cs</Link>
</Compile>
<Compile Include="..\msgpack-sharp-tests\Habitat.cs">
<Link>Habitat.cs</Link>
</Compile>
Expand Down
8 changes: 7 additions & 1 deletion msgpack-sharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "msgpack-sharp-tests", "msgp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "msgpack-sharp-verifier", "msgpack-sharp-verifier\msgpack-sharp-verifier.csproj", "{5A7A433B-0B08-4F87-A526-CE1B71EE4CD6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerfTest", "PerfTest\PerfTest.csproj", "{F9E4A0A8-0657-461D-AAD7-E78B97EBE22E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,8 +27,12 @@ Global
{765753B8-A9EB-4DD1-B0F0-D478E54E9ACB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{765753B8-A9EB-4DD1-B0F0-D478E54E9ACB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{765753B8-A9EB-4DD1-B0F0-D478E54E9ACB}.Release|Any CPU.Build.0 = Release|Any CPU
{F9E4A0A8-0657-461D-AAD7-E78B97EBE22E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F9E4A0A8-0657-461D-AAD7-E78B97EBE22E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F9E4A0A8-0657-461D-AAD7-E78B97EBE22E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F9E4A0A8-0657-461D-AAD7-E78B97EBE22E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
StartupItem = msgpack-sharp\msgpack-sharp.csproj
StartupItem = PerfTest\PerfTest.csproj
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions msgpack-sharp/MessagePackMemberAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class MessagePackMemberAttribute : Attribute
public MessagePackMemberAttribute(int id)
{
this.id = id;
NilImplication = NilImplication.MemberDefault;
}

public int Id
Expand Down
Loading

0 comments on commit 47e13a9

Please sign in to comment.