Skip to content

Commit

Permalink
Merge pull request theburningmonk#8 from davidgrenier/develop
Browse files Browse the repository at this point in the history
Addition of BinaryReader + fix to a typo
  • Loading branch information
theburningmonk committed Feb 8, 2014
2 parents 82d8d20 + e082374 commit 6f516f6
Show file tree
Hide file tree
Showing 13 changed files with 182 additions and 16 deletions.
2 changes: 1 addition & 1 deletion RunBenchmarks.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ open System.Collections.Generic
open FSharp.Charting
open FSharp.Charting.ChartTypes

open SimlpeSpeedTester.Example
open SimpleSpeedTester.Example
open SimpleSpeedTester.Interfaces

let prettyPrint (results : Dictionary<string, ITestResultSummary * ITestResultSummary * double>) =
Expand Down
12 changes: 12 additions & 0 deletions SimpleSpeedTester.sln
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JsonSerializersBenchmark",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CollectionBenchmark", "examples\CollectionBenchmark\CollectionBenchmark.csproj", "{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "TestRecord", "examples\TestRecord\TestRecord.fsproj", "{088C6FFB-7156-4172-96D8-32111C602067}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -103,6 +105,16 @@ Global
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{94F57EB2-21B7-42B3-A885-B2DE5EE74A10}.Release|x86.ActiveCfg = Release|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Debug|Any CPU.Build.0 = Debug|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Debug|x86.ActiveCfg = Debug|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Release|Any CPU.ActiveCfg = Release|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Release|Any CPU.Build.0 = Release|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{088C6FFB-7156-4172-96D8-32111C602067}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<Reference Include="FluorineFx.ServiceBrowser">
<HintPath>..\..\packages\fluorinefx.1.2.4\lib\net40\FluorineFx.ServiceBrowser.dll</HintPath>
</Reference>
<Reference Include="FSharp.Core, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="FsPickler">
<HintPath>..\..\packages\FsPickler.0.8.5.1\lib\net45\FsPickler.dll</HintPath>
</Reference>
<Reference Include="log4net">
<HintPath>..\..\packages\log4net.1.2.10\lib\2.0\log4net.dll</HintPath>
</Reference>
Expand Down Expand Up @@ -84,6 +88,10 @@
<Project>{87b76a3e-3932-47f1-924d-c80a1ae92787}</Project>
<Name>SimpleSpeedTester</Name>
</ProjectReference>
<ProjectReference Include="..\TestRecord\TestRecord.fsproj">
<Project>{088c6ffb-7156-4172-96d8-32111c602067}</Project>
<Name>TestRecord</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
Expand Down
82 changes: 74 additions & 8 deletions examples/BinarySerializersBenchmark/BinarySerializersSpeedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
using JsonNetBsonWriter = Newtonsoft.Json.Bson.BsonWriter;
using JsonNetJsonSerializer = Newtonsoft.Json.JsonSerializer;

namespace SimlpeSpeedTester.Example
namespace SimpleSpeedTester.Example
{
using Filbert.Core;

Expand All @@ -34,6 +34,7 @@ public static class BinarySerializersSpeedTest

// the objects to perform the tests with
private static readonly List<SimpleObject> SimpleObjects = Enumerable.Range(1, ObjectsCount).Select(GetSimpleObject).ToList();
private static readonly List<TestRecords.SimpleRecord> SimpleRecords = Enumerable.Range(1, ObjectsCount).Select(GetSimpleRecord).ToList();
private static readonly List<Bert> BertSimpleObjects = Enumerable.Range(1, ObjectsCount).Select(GetSimpleObjectBert).ToList();
private static readonly List<SimpleObjectWithFields> SimpleObjectsWithFields = Enumerable.Range(1, ObjectsCount).Select(GetSimpleObjectWithFields).ToList();
private static readonly List<IserializableSimpleObject> IserializableSimpleObjects = Enumerable.Range(1, ObjectsCount).Select(GetSerializableSimpleObject).ToList();
Expand All @@ -58,30 +59,34 @@ public static Dictionary<string, Tuple<ITestResultSummary, ITestResultSummary, d
results.Add(
"Protobuf-Net (with properties)",
DoSpeedTest("Protobuf-Net (with properties)", SimpleObjects, SerializeWithProtobufNet, DeserializeWithProtobufNet<SimpleObject>));

results.Add(
"Protobuf-Net (with fields)",
DoSpeedTest("Protobuf-Net (with fields)", SimpleObjectsWithFields, SerializeWithProtobufNet, DeserializeWithProtobufNet<SimpleObjectWithFields>));

// speed test binary writer (only for reference, won't be able to deserialize)
results.Add(
"BinaryWriter",
DoSpeedTest("BinaryWriter", SimpleObjects, SerializeWithBinaryWriter, null));
DoSpeedTest("BinaryWriter", SimpleObjects, SerializeWithBinaryWriter, DeserializeWithBinaryReader));

results.Add(
"FsPickler (Simple Records)",
DoSpeedTest("FsPickler", SimpleRecords, SerializeWithFsPickler, DeserializeWithFsPickler<TestRecords.SimpleRecord>));

results.Add(
"MessagePack (with properties)",
DoSpeedTest(
"MessagePack (with properties)",
SimpleObjects,
lst => SerializeWithMessagePack(lst, true),
"MessagePack (with properties)",
SimpleObjects,
lst => SerializeWithMessagePack(lst, true),
lst => DeserializeWithMessagePack<SimpleObject>(lst, true)));

results.Add(
"MessagePack (with fields)",
DoSpeedTest(
"MessagePack (with fields)",
SimpleObjectsWithFields,
lst => SerializeWithMessagePack(lst, false),
SimpleObjectsWithFields,
lst => SerializeWithMessagePack(lst, false),
lst => DeserializeWithMessagePack<SimpleObjectWithFields>(lst, false)));

results.Add(
Expand All @@ -103,6 +108,35 @@ public static Dictionary<string, Tuple<ITestResultSummary, ITestResultSummary, d
return results;
}

private static List<T> DeserializeWithFsPickler<T>(List<byte[]> payloads)
{
var fsp = new FsPickler.FsPickler();

return
payloads.Select(payload =>
{
using (var ms = new MemoryStream(payload))
{
return fsp.Deserialize<T>(ms);
}
}).ToList();
}

private static List<byte[]> SerializeWithFsPickler<T>(List<T> objects)
{
var fsp = new FsPickler.FsPickler();

return
objects.Select(o =>
{
using (var ms = new MemoryStream())
{
fsp.Serialize(ms, o);
return ms.ToArray();
}
}).ToList();
}

private static Tuple<ITestResultSummary, ITestResultSummary, double> DoSpeedTest<T>(
string testGroupName, List<T> objects, Func<List<T>, List<byte[]>> serializeFunc, Func<List<byte[]>, List<T>> deserializeFunc)
{
Expand Down Expand Up @@ -162,6 +196,11 @@ private static SimpleObject GetSimpleObject(int id)
};
}

private static TestRecords.SimpleRecord GetSimpleRecord(int id)
{
return new TestRecords.SimpleRecord(100000, "Simple", "Planet Earth", Enumerable.Range(0, 10).ToArray());
}

private static SimpleObjectWithFields GetSimpleObjectWithFields(int id)
{
return new SimpleObjectWithFields
Expand Down Expand Up @@ -250,6 +289,31 @@ private static T DeserializeWithProtobufNet<T>(byte[] byteArray)

#region Binary Writer

private static int[] ReadScores(BinaryReader reader)
{
var count = reader.ReadInt32();
return Enumerable.Range(0, count).Select(_ => reader.ReadInt32()).ToArray();
}

private static List<SimpleObject> DeserializeWithBinaryReader(List<byte[]> payloads)
{
return
payloads.Select(o =>
{
using (var ms = new MemoryStream(o))
{
var reader = new BinaryReader(ms);
return new SimpleObject
{
Id = reader.ReadInt32(),
Name = reader.ReadString(),
Address = reader.ReadString(),
Scores = ReadScores(reader)
};
}
}).ToList();
}

private static List<byte[]> SerializeWithBinaryWriter(List<SimpleObject> objects)
{
return objects.Select(SerializeWithBinaryWriter).ToList();
Expand All @@ -264,7 +328,9 @@ private static byte[] SerializeWithBinaryWriter(SimpleObject obj)
binaryWriter.Write(obj.Id);
binaryWriter.Write(obj.Name);
binaryWriter.Write(obj.Address);
binaryWriter.Write(obj.Scores.Length);
Array.ForEach(obj.Scores, binaryWriter.Write);

binaryWriter.Flush();

return memStream.ToArray();
Expand Down
1 change: 1 addition & 0 deletions examples/BinarySerializersBenchmark/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<packages>
<package id="Filbert" version="0.2.0" targetFramework="net45" />
<package id="fluorinefx" version="1.2.4" targetFramework="net45" />
<package id="FsPickler" version="0.8.5.1" targetFramework="net45" />
<package id="log4net" version="1.2.10" targetFramework="net45" />
<package id="MsgPack" version="0.1.0.2011042300" targetFramework="net45" />
<package id="Newtonsoft.Json" version="5.0.8" targetFramework="net45" />
Expand Down
2 changes: 1 addition & 1 deletion examples/CollectionBenchmark/CollectionSpeedTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using SimpleSpeedTester.Core;
using SimpleSpeedTester.Interfaces;

namespace SimlpeSpeedTester.Example
namespace SimpleSpeedTester.Example
{
public class CollectionSpeedTest
{
Expand Down
2 changes: 1 addition & 1 deletion examples/CollectionBenchmark/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace SimlpeSpeedTester.Example
namespace SimpleSpeedTester.Example
{
public static class EnumerableExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
using JsonFxReader = JsonFx.Json.JsonReader;
using JsonFxWriter = JsonFx.Json.JsonWriter;

namespace SimlpeSpeedTester.Example
namespace SimpleSpeedTester.Example
{
/// <summary>
/// Demo program which compares the serializatoin and deserialization speed of 4 popular JSON serializers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using SimpleSpeedTester.Core;
using SimpleSpeedTester.Interfaces;

namespace SimlpeSpeedTester.Example
namespace SimpleSpeedTester.Example
{
public sealed class ExceptionOnlyTestOutcomeFilter : ITestOutcomeFilter
{
Expand Down
2 changes: 1 addition & 1 deletion examples/SimpleSpeedTester.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using SimpleSpeedTester.Core;
using SimpleSpeedTester.Core.OutcomeFilters;

namespace SimlpeSpeedTester.Example
namespace SimpleSpeedTester.Example
{
class Program
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<ProjectGuid>{7ADA7026-5757-4E93-AC72-D7E69DC08433}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SimlpeSpeedTester.Example</RootNamespace>
<AssemblyName>SimlpeSpeedTester.Example</AssemblyName>
<RootNamespace>SimpleSpeedTester.Example</RootNamespace>
<AssemblyName>SimpleSpeedTester.Example</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile>
</TargetFrameworkProfile>
Expand Down
70 changes: 70 additions & 0 deletions examples/TestRecord/TestRecord.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>088c6ffb-7156-4172-96d8-32111c602067</ProjectGuid>
<OutputType>Library</OutputType>
<RootNamespace>TestRecord</RootNamespace>
<AssemblyName>TestRecord</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFSharpCoreVersion>4.3.1.0</TargetFSharpCoreVersion>
<Name>TestRecord</Name>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<Tailcalls>false</Tailcalls>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>bin\Debug\TestRecord.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<Tailcalls>true</Tailcalls>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<WarningLevel>3</WarningLevel>
<DocumentationFile>bin\Release\TestRecord.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="mscorlib" />
<Reference Include="FSharp.Core, Version=$(TargetFSharpCoreVersion), Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Numerics" />
</ItemGroup>
<ItemGroup>
<Compile Include="TestRecords.fs" />
</ItemGroup>
<PropertyGroup>
<MinimumVisualStudioVersion Condition="'$(MinimumVisualStudioVersion)' == ''">11</MinimumVisualStudioVersion>
</PropertyGroup>
<Choose>
<When Condition="'$(VisualStudioVersion)' == '11.0'">
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets')">
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\..\Microsoft SDKs\F#\3.0\Framework\v4.0\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</When>
<Otherwise>
<PropertyGroup Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets')">
<FSharpTargetsPath>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\FSharp\Microsoft.FSharp.Targets</FSharpTargetsPath>
</PropertyGroup>
</Otherwise>
</Choose>
<Import Project="$(FSharpTargetsPath)" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
9 changes: 9 additions & 0 deletions examples/TestRecord/TestRecords.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace TestRecords

type SimpleRecord =
{
Id: int
Name: string
Address: string
Scores: int[]
}

0 comments on commit 6f516f6

Please sign in to comment.