Top 5 Fastest WCF Serializers

Recently have been using WCF again at work, I wondered how slow (or fast) the latest DataContractSerailizer is. I was pleasantly surprised that it was fairly similar in speed to the JSon Serializer.

Time-100k

Memoryusage-100k

I have run some benchmarks and run speed tests on the following 5 serializers:

DataContractSerializer

This is the default WCF serializer and is used by specifying the DataContract and DataMember attributes on your classes such as the following:

[DataContract]
public class DtoDataContract : IDataContract
{
    [DataMember]
    public long Id { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Description { get; set; }

    // Other Members
}

This generates XML with each Class and Field as Tags, such as the following:

<DtoDataContract i:type="ArrayOfDtoDataContract" xmlns="http://schemas.datacontract.org/2004/07/Benchmark" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <DtoDataContract>
        <Balance>736.13</Balance>
        <Created>2017-02-22T09:04:29.0656576+00:00</Created>
        <Description>Open-architected responsive service-desk</Description>
        <Id>0</Id>
        <Interest>506.00</Interest>
        <Limit>230.27</Limit>
        <Name>Bernier Group</Name>
        <Transaction>260.22</Transaction>
    </DtoDataContract>
</DtoDataContract>

Size: 473 Bytes

NetDataContractSerializer

This is an alternative Serializer that comes out of the box with WCF, and it is very similar to DataContractSerializer but adds additional information to each tag that can be used by the client.

This serializer generates the following output:

<ArrayOfDtoDataContract z:Id="1" z:Type="System.Collections.Generic.List`1[[Benchmark.DtoDataContract, Benchmark, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" z:Assembly="0" xmlns="http://schemas.datacontract.org/2004/07/Benchmark" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
    <_items z:Id="2" z:Size="4">
        <DtoDataContract z:Id="3">
            <Balance>736.13</Balance>
            <Created>2017-02-22T09:04:29.0656576+00:00</Created>
            <Description z:Id="4">Open-architected responsive service-desk</Description>
            <Id>0</Id>
            <Interest>506.00</Interest>
            <Limit>230.27</Limit>
            <Name z:Id="5">Bernier Group</Name>
            <Transaction>260.22</Transaction>
        </DtoDataContract>
        <DtoDataContract i:nil="true" />
        <DtoDataContract i:nil="true" />
        <DtoDataContract i:nil="true" />
    </_items>
    <_size>1</_size>
    <_version>1</_version>
</ArrayOfDtoDataContract>

Size: 874 Bytes

Newtonsoft.JSON

[  
   {  
      "Id":0,
      "Name":"Bernier Group",
      "Description":"Open-architected responsive service-desk",
      "Created":"2017-02-22T09:04:29.0656576+00:00",
      "Balance":736.13,
      "Transaction":260.22,
      "Interest":506.00,
      "Limit":230.27
   }
]

Size: 207 Bytes

ProtoBuf

I decided to compare Google ProtocalBuffers .NET implementation by Marc Gravell – protobufnet.

To use ProtoBuf, you need to decorate the class with ProtoContract and each member with ProtoMember(x) where x is the position of the field when serializing.

[ProtoContract]
public class DtoDataContract : IDataContract
{
    [ProtoMember(1)]
    public long Id { get; set; }

    [ProtoMember(2)]
    public string Name { get; set; }

    [ProtoMember(3)]
    public string Description { get; set; }

    // Other members
}

Result is Raw Binary Stream such as:

f\u0012
Bernier Group\u001a(Open-architected responsive service-desk"\u000b\u0008�Ũ����4\u0010\u0005*\u0006\u0008��\u0004\u0018\u00042\u0006\u0008��\u0001\u0018\u0004:\u0006\u0008��\u0003\u0018\u0004B\u0006\u0008�\u0001\u0018\u0004

Size: 104 B

BinaryWriter

This is a custom hand rolled BinaryWriter for my class using BinaryWriter and BinaryReader such as the following:

Serializing

// Count of items in list to help deserialize
writer.Write(_items.Count);

foreach (var item in _items)
{
    writer.Write(item.Id);
    writer.Write(item.Balance);
    writer.Write(item.Description);
    writer.Write(item.Interest);
    writer.Write(item.Transaction);
    writer.Write(item.Limit);
    writer.Write(item.Name);
    writer.Write(item.Created.ToBinary());
}

Deserializing

var reader = new BinaryReader(stream);

var count = reader.ReadInt32();
var list = new List<DtoDataContract>(count);

while (stream.Position < stream.Length)
{
    var item = new DtoDataContract
    {
        Id = reader.ReadInt64(),
        Balance = reader.ReadDecimal(),
        Description = reader.ReadString(),
        Interest = reader.ReadDecimal(),
        Transaction = reader.ReadDecimal(),
        Limit = reader.ReadDecimal(),
        Name = reader.ReadString(),
        Created = DateTime.FromBinary(reader.ReadInt64())
    };

    list.Add(item);
}

This produces a 30% longer binary stream than protobuf (surprisingly) but is faster

\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000�\u001f\u0001\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0002\u0000(Open-architected responsive service-desk��\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0002\u0000�e\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0002\u0000�Y\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0002\u0000

Size: 139 B

Migrating to Hexo Blog

Ever wondered what how hard it is to migrate away from WordPress. For example Hexo which builds static HTML files from markdown posts and pages?

My journey of migrating from WordPress to Hexo.io

Installation

Installation was easy, and straight forward.

  1. Install node.js
  2. Install git
  3. Run npm install hexo-cli -g

Then followed up with running the following in a folder where I want the blog source files to be created:

  1. hexo init
  2. npm install
  3. hexo generate
  4. hexo server

And it was accessible via http://localhost:4000

{% asset_img 1.Post-Installation.jpg Post Installation %}

Configuration

Blog Config

Setup _config.yml. Changed the following for this blog:

`\

Site

title: codePERF [dot] NET subtitle: code is art description: .NET Blog focusing on performance and architecture by Michal Ciechan author: Michal Ciechan …

URL

If your site is put in a subdirectory, set url as ‘http://yoursite.com/child’ and root as ‘/child/’

url: http://www.codeperf.net … `\

Also changed the following

`\

Writing

… post_asset_folder: true … highlight: enable: true line_number: false … `\ Hexo will create a folder every time you make a new post with the hexo new [layout] <title> command. This asset folder will have the same name as the markdown file associated with the post. Place all assets related to your post into the associated folder, and you will be able to reference them using a relative path, making for an easier and more convenient workflow.

The line_number: false property removes line numbers from code blocks, so that it is easier for readers to copy code from your site. I hate sites with line numbers enabled!

Theme

Install theme by pasting the themes source into \themes\ folder (e.g. BootstrapBlog theme) and changing _config.yml theme property to same name as the folder (BootstrapBlog) that you just pasted into.

`\

Extensions

Plugins: https://hexo.io/plugins/

Themes: https://hexo.io/themes/

theme: BootstrapBlog … `\

The site now looked like the following:

{% asset_img BootstrapBlog.Theme.jpg Post Installation %}

Writing a Blog post

Create an empty post by using the CLI:

hexo new "Migrating from WordPress to Hexo"

This generates the following MarkDown file representing the post

blog root
+---source
| +---_posts
| | Migrating-from-WordPress-to-Hexo.md

Open the file up and start writing your MarkDown. If you don’t know all of the MarkDown syntax the following couple of help pages might help:

The most common ones I use are:

Headers

# Header
## Sub Header

Links Click this Inline Link for more info

[Click this ReferencedLink](1) for more info.

... somewhere else in the document, usually at the bottom

[1]: http://someotherlink/

Lists 1. First Numbered Item 2. Second Numbered Item

- Bullet List Item 1
- Bullet List Item 2

Code

This is some `inline` piece of code

`Or just a single line of code`

Code Block or use back ticks to denote code

Image

{% asset_img image.name.jpg Some Title %}

Read More

Read more links can be achieved by adding <!-- more --> to the article. Anything after this tag, will be truncated in post excerpts such as on my homepage.

Testing & Preview

For Previewing my Markdown posts I used Atom editor with a number of Markdown plugins including Live Preview. I found this worked best for me after trying a number of editors.

I will make a future post and possibly a video series of the editors I tried and my reviews.

Atom Editor – Markdown Live Preview

{% asset_img Atom-LivePreview.jpg Atom Editor Markdown Live preview %}

Testing

For testing I ended up going with Hexo server by executing the following in the project dir:

> hexo generate
> hexo server

{% asset_img HexoCliOutput.jpg Hexo CLI Output%}

Whenever I changed a file, it would automatically notice, regenerate latest files and all I had to do was refresh the browser.

Althought whenever I changed any of the config, I had to close the server (Ctrl + C) and then run the following commands

\project> hexo clean
\project> hexo generate
\project> hexo server

But I had this as one command using the & as a in my server window, so all I had to do in the end, was stop the server, press Up key, and hit enter.

 \project> hexo clean & hexo generate & hexo server

Migrating posts

Watch out for part 2 of this series which will discuss in depth my journey of Migrating all posts into Hexo!

ASP.NET Core – MVC xUnit Integration Testing

Recently I wanted to test my new ASP.NET Core MVC app, so that I can be sure my startup.cs configuration is correct, and especially focusing on the JSON parsing of it.

I straight away run into a few basic stumbling blocks that might help a few people out there!

Test Discovery

The first stumbling block was trying to get the tests discovered at all.

I had to select a test runner and add this to the root of my project.json:

{
  "testRunner": "xunit",
  ....
}

I tried to initially use xunit and dotnet-test-xunit versions 2.1.0 which is the officially released version at the time of writing.

But as this does not support Microsoft.NETCore.App 1.0.1, after a bit off Bing’ing around, I figured out that I need to use "dotnet-test-xunit": "2.2.0-preview2-build1029"

Referencing the MVC Project

The second stumbling block was trying to simply reference the MVC project, in this case, “Taskily”. So I added the following line:

"dependencies": {
  .....
  "Taskily": "1.0.0*"
}

But this was giving me a “runtimes” is an unsupported framework error, which I couldn’t for the life of me figure out. In the end, it turned out to be that because the Taskily project (or at least it was called Taskily in the .sln) actually resided in a folder called WebApplication1 so my solution structure looked like this

root
|global.json
|---src
    |---WebApplication1  <-- renamed to Taskily in .sln
    |---Taskily.Tests    <-- new test project

The way I got this to compile is

  1. Close the .sln
  2. Rename WebApplicaiton1 folder to Taskily (same as project name in .sln)
  3. Re-Open .sln (Taskily won’t load as it is looking for src/WebApplication1/Taskily.xproj
  4. Remove the reference to the Taskily project from the .sln.
  5. Re-Add the Taskily project which should now be in src/Taskily/Taskily.xproj
  6. Compile

The error message was so not intuitive,and was bugging me for ages!

MVC Integration Test

Next, I needed to somehow integrate the startup configuration, which contains the JSON.Net serializer settings, etc. in my tests so I followed the Microsofts Integration Testing Article which allows you to create an in memory host of your MVC app with custom such as the following:

public class ApiTests
{
    private readonly HttpClient _client;

    public ApiTests()
    {
        var server = new TestServer(new WebHostBuilder()
                .UseStartup<Startup>()
        );

        _client = server.CreateClient();
    }

    [Fact]
    public async void Get_Returns2Values()
    {
        var response = await _client.GetAsync("https://localhost/api/Alexa/");

        var res = await response.Content.ReadAsStringAsync();

        res.Should().Be(@"[""value1"",""value2""]");
    }
}

In order for this to run, I had to add the following NuGet package dependency in order to be able to use the AspNetCore TestServer class.

"Microsoft.AspNetCore.TestHost": "1.1.0-preview1-final", 

UserSecrets

This allowed me to compile and run the test, but straight away the test failed as I was using UserSecrets, as specified in Microsoft’s Safe storage of app secrets during development article, and one of my Middleware was throwing an exception that the value cannot be null.

So I added a user secrets Id into the test project.json:

{
  ....
  "userSecretsId": "aspnet-WebApplication1-19b75b8b-b10e-4fea-94e8-17c9f955732e"
}

and also added the SecretManager tool into the project via project.json which allows me to manage the user secrets for the project via command line:

{
  .....
  "tools": {
    "Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final"
  },
  .....
}

I then set my 2 user secrets that one of my AspnetCore middleware’s needed by

  1. dotnet restore (Ctrl+Shift+K, Ctrl+Shift+R in VS on the project)
  2. Open Command Prompt in the test folder
  3. run the following command

    dotnet user-secrets set ClientID MySecretClientIdValue dotnet user-secrets set ClientIDPassword PasswordThatYouWillNeverGuess

Environment

Now that the UserSecrets were setup, my test was still failing with the same error (that the ClientID is null, which is retrieved from UserSecrets).

After a bit of digging around I noticed the following:

if (env.IsDevelopment())
{
    // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
    builder.AddUserSecrets();

    // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
    builder.AddApplicationInsightsSettings(developerMode: true);
}

which meant, that ASP.NET Core will only use UserSecrets when the server is running in Development environment. I could change this but it’s probably like that for a reason! So instead decided to set the environment variable on the TestServer by using the WebHostBuilder like the following:

var server = new TestServer(new WebHostBuilder()
        .UseEnvironment(EnvironmentName.Development) //<-- Setting Environment Variable
        .UseStartup<Startup>()
);

Now my test was running successfully and Bobs your uncle! 😀

Happy Coding

SQL – Union vs Join Performance

Precursor

Following on from a comment on the answer I gave for this question on StackOverflow I was asked to comment by @seebiscuit on the following answer by someone else:

a well written join will be faster than a union

So me being me, I decided to look into this issue and do some testing myself.

Enviornment

So first off I will be running this on the following:

  • Local version of SQL Server Express 2014 (12.0.2269.0)
  • Using SSMS to run my queries.
  • Windows 10 x64
  • i7 930 1st Gen CPU | 16GB Ram | Samsung Evo 950 Pro SATA3 SSD

I decided to start of with the trusted Microsft Adventure Works SQL Server 2014 Sample Database available here:

Test Method

In my SSMS session run the following to turn on statistics

SET STATISTICS TIME ON

then record the CPU time and elapsed time for each run. Run each query 10 times, discard the 1 Minimum, 1 Maximum and average the rest (8)

Results

2016-04-14 00_39_00-Book1 - Excel

Queries – PK Clustered Index <int>

Wrote the following queries on the Person.Person table which was the biggest ( ~20k Records / 30mb Data )

Query 1 – UNION ALL:

SELECT BusinessEntityID, ModifiedDate, rowguid FROM Person.Person
UNION ALL
SELECT BusinessEntityID, ModifiedDate, rowguid FROM Person.Person    

2016-04-06 21_20_25-Start

Query 2 – UNION

SELECT BusinessEntityID, ModifiedDate, rowguid FROM Person.Person
UNION
SELECT BusinessEntityID, ModifiedDate, rowguid FROM Person.Person    

2016-04-06 21_23_43-Start

Query 3 – INNER JOIN

SELECT p1.BusinessEntityID, p1.ModifiedDate, p1.rowguid
    ,  p2.BusinessEntityID, p2.ModifiedDate, p2.rowguid 
FROM Person.Person p1 
INNER JOIN Person.Person p2 
    ON p2.BusinessEntityID = p1.BusinessEntityID

2016-04-06 21_25_35-Start

Query 4 – LEFT JOIN

SELECT p1.BusinessEntityID, p1.ModifiedDate, p1.rowguid
    ,  p2.BusinessEntityID, p2.ModifiedDate, p2.rowguid 
FROM Person.Person p1 
LEFT JOIN Person.Person p2 
    ON p2.BusinessEntityID = p1.BusinessEntityID    

2016-04-06 21_26_21-Start

Query 5 – RIGHT JOIN

SELECT p1.BusinessEntityID, p1.ModifiedDate, p1.rowguid
    ,  p2.BusinessEntityID, p2.ModifiedDate, p2.rowguid 
FROM Person.Person p1 
LEFT JOIN Person.Person p2 
    ON p2.BusinessEntityID = p1.BusinessEntityID    

2016-04-06 21_26_21-Start

Query 6 – FULL OUTER JOIN

SELECT p1.BusinessEntityID, p1.ModifiedDate, p1.rowguid
    ,  p2.BusinessEntityID, p2.ModifiedDate, p2.rowguid 
FROM Person.Person p1 
FULL OUTER JOIN Person.Person p2 
    ON p2.BusinessEntityID = p1.BusinessEntityID

2016-04-06 21_30_47-Start

Results – Table

MethodMedianStdDevCPUTime(ms)
1. Union All21.8937 ms6.7085 ms18
2. Union19.1815 ms2.2459 ms16
3. Inner Join24.5083 ms4.8172 ms21
4. Left Join23.3757 ms4.2216 ms23
5. Right Join25.7348 ms5.0168 ms23
6. Full Outer Join21.8262 ms2.2162 ms20

Comments

I was planning to do more and different types of queries and compare how Unions vs Joins compare, as well as much larger datasets (close to 2gb) but this has taken longer than I hope already (a week) and if there is more interest oleave a comment and it wil motivate me to delve into the issue.

In the end, it is hard to think of a scenario where you could use 1 or the other as they serve very different purposes! And you are best of using the right tool for the job.

If someone has some interesting scenario’s where you could actually use both then let me know!

NuGet – Find Project referencing Version Y of Pkg M – Part 1 of 2

Had an issue at work where Visual Studio Solution (.sln) was constantly showing that a NuGet package can be updated, even though all of the individual projects where up-to date, and greyed out.

I did some digging and had to learn PowerShell in order to find this out, as this can be used in the NuGet Package Manager Console.

First of all you will need to run the Get-Project -All to list all of the projects in the solution.

This returns a List<System.__ComObject> (C# Equivalent)

Finding out properties of an object

To Find out what variables are available in that object, I piped this output into the following commands:

  • | Select-Object -First 1 in order to only get the first object in the list
  • | Select * in order to select all properties of that object.
  • | Write-Host command in order to output all variables

This returned a list of all the available variables in the piped System.__ComObject

E.g. `@{Name=ABC; FullName=C:\SomeDir\SomeSln\ABC.csprod; …..}

Find installed packages using Get-Package command

NuGet has a command called Get-Package which lets you list all installed packages inside a Solution/Project.

It has 2 parameters which will help us:

  • -Filter M Which allows us to filter according to some string. E.g. Get-Package -Filter M to get all the packages with M in its name.
  • -First x which allows us to get the top x number of packages. E.g. Get-Package -First 1
  • -Project ABC Which allows us to filter for installed packages only in the specified project ABC

First I run Get-Package -Filter M to get a list of all the M packages installed.

This returned the following values

Id   Version
---  ------
M    1.0.0.0
M    2.0.0.0

As I am interested only in the older version. I knew I could use -First 1 in order to just get the first value.

Therefore Get-Package -Filter M -First 1 returned only the older package M(1.0.0.0)

Looping through each project

Powershell has a ForEach{} statement which you can use to loop over a collection, such as the List of System.__ComObjects returned from Get-Project -All

E.g. Get-Project | ForEach{ WriteHost }

This will loop over each returned object from Get-Project and do a Console.WriteLine(object). This basically just prints out a System.__ComObject\r\n per project.

There is a shorthand for writing ForEach{} statements in PowerShell which is %{}. Making the following 2 statements identical

Get-Project | ForEach{ Write Host }
Get-Project | %{ Write Host }    

I will use the shorthand %{} ForEach notation from now on.

Accessing the Piped Object

You can access the current piped variable using the $_ notation

Therefore you can access all the properties of this object inside the loop by $_.Property

E.g. Get-Package | %{ $_.Name } will return a list of all Project Names.(As Name is a property on the returned object from Get-Package

To Be Continued

In the next part I will talk about how using all of these, you can find out which project is referencing an older version of a NuGet package.

This should be available by 6th March 2016. Keep tuned in!

.NET No-Op Delegate vs if vs Rx

Recently I’ve had on my mind, what is the most performant way to do optional logging, e.g. Tracing in particular, where by default it is turned off, but by using some sort of switch/command line argument, I can turn it on for extra info when trying to debug something on a users machine, and therefore cannot recompile.

In this post I will be concentrating on comparing the performance for the following methods of achieving this:

  • IObservable // RxIObservable
  • if (LogLevel <= LogLevel.Trace) // IfMoreThanOrEq
  • if (LogLevel < LogLevel.Debug) // IfMoreThanEnum
  • _traceDelegate = new delegate { } // NoOpDelegate
  • if (_traceDelegate != null) // IfTraceDelegate
  • if (IsTraceEnabled) // IfIsTraceProp
  • if (IsTraceEnabled) // IfIsTraceField

The reason I am interested is because there may arise a situation where I may occasionally want to turn on tracing, for operations which happen 1000’s of times per second. Of course once you turn it on it would be negligible which method you choose as the time taken for output of the log would be far higher, but ideally I would never be turning this logging on, only in rare occasions.

First suite of tests I did was run it with Logging Off, so I could see the effect that each method would have on the performance. I run each method 1,000,000 times as well as doing a multiplication and keeping a total to mimic some very light work.

If-Vs-NoOp-Results-LoggingOff

As you can see using a boolean field to represent whether it should log or not is fastest, and almost as fast as doing nothing where as using a property for this, took the longest. Don’t get me wrong, for doing these checks 1 million times, it is pretty pointless going and changing your loggers to use this if they are not already, but it is worth noting should you need to optimize some mission critical code in the future.

I also run the tests with the Trace turned on for completeness. The following is a relative representation of how long each test method took.

If-vs-NoOp-Results-Main.xlsx - Excel

Let me start of with the code that was being run 1,000,000 times:

for (int i = 0; i < Size; i++)
{
    methodToLog(i);
    total += DoSomeWork(i);
}

and

public int DoSomeWork(int i)
{
    return i*2;
}

So as you can see it was really simple stuff. Ofcourse normally you would have heavier work load and the impact would be marginal. But none the less, let me continue.

The implementations of each logging method are as follows in relative performance impact ascending when logging was turned of.

public class IfIsTraceField // 6% Impact
{
    public static bool IsTraceEnabled;

    public static void Trace(int x)
    {
        if (IsTraceEnabled) TraceLog.Add(x);
    }
}

Followed by

public class IfTraceDelegate // 16% Impact
{
    public static Log TraceHandler = null;

    public static void Trace(int x)
    {
        if (TraceHandler != null) TraceHandler(x);
    }
}

In Third Place:

public class RxIObservable // 36% Impact
{
    private static readonly Subject<int> TraceSubject = new Subject<int>();

    public static IObservable<int> TraceObservable
    {
        get { return TraceSubject; }
    }

    public static void Trace(int x)
    {
        TraceSubject.OnNext(x);
    }
}

And the remainder as they were all very close:

public class IfLessThanEnum // 64%
{
    public static LogLevel LogLevel { get; set; }

    public static void Trace(int x)
    {
        if (LogLevel < LogLevel.Debug) TraceLog.Add(x);
    }
}

public class NoOpDelegate // 66% Impact
{
    public static Log TraceHandler = delegate { };

    public static void Trace(int x)
    {
        TraceHandler(x);
    }
}

public class IfLessThanOrEq // 69% Impact
{
    public static LogLevel LogLevel { get; set; }

    public static void Trace(int x)
    {
        if (LogLevel <= LogLevel.Trace) TraceLog.Add(x);
    }
}

public class IfIsTraceProp // 69% Impact
{
    public static bool IsTraceEnabled { get; set; }

    public static void Trace(int x)
    {
        if (IsTraceEnabled) TraceLog.Add(x);
    }
}

Conclusions, a private field is fastest, where as property is slowest (I am assuming that this is due to the stack push/pop and/or context switching. Will need to look into the generated IL and blog about it one day.

Anyhow the code will be available on GitHub shortly.

TDD – Mock.Throw Interface Call Verification Technique

First of all I did not know what to call this post, so it is what it is. If you have any better suggestions leave a comment!

Often I find myself writing a class which ties multiple classes together. I like to call these controllers. Therefore using a MVVMC (Model-View-ViewModel-Controller) pattern which is a combination of MVVM and MVC as I believe they suit each other very well.

One problem that bugs when I am doing this using a TDD approach is that my interface call verification methods fail because of either NotImplementedException or NullReferenceException because as I test the first interface call, the rest is not yet implemented, or the mocks return null.

Rant

For the TDD purists out there, no I do not write a non compilable test first. I develop using the following cycle:

  1. Think about what I need and how I would test it
  2. Create an empty class to represent what I need
  3. Create an empty method
  4. Add comments of what this method should do
  5. Change the comments to empty implementations (often another method which throws new NotImplementedException)
  6. Write a test for the first fake implementation
  7. Code the implementation
  8. Repeat 6-7 until no empty implementations left.
  9. Repeat steps 1-8 for each interface I used.

I guess you can call it Test Driven Design, rather than Development. Maybe one day it will change, who knows, but currently that is how I enjoy coding the most, makes me most productive as well as churn out decent quality code. I guess that is an idea for another blog post 😀

Rant Over

The Technique

[Test]
[ExpectedException(typeof(MockCalled))]
public void ProcessData_CallsWorker()
{
    var list = new List {1};
 
    // Setup worker
    _worker.Setup(w => w.DoWork(list))
           .Throws(); // Technique
 
    // Act
    _controller.ProcessData(list);
}

 The Reason

The reason for adding throws is to stop the flow of the program as soon as it hits that mock. I often use this when I want to verify a call was made to a mock, and I don’t want to test the remaining logic or flow of the class under test.

 

Method Under Test

 

The above logic can be represented by the following code:

internal void ProcessData(List data)
{
    var newData = _worker.DoWork(data);
 
    if (newData.Count == 0)
        throw new NotImplementedException(); // Do Something else
 
    SaveData(newData);
}

Having such a method, my first test would be to make sure my worker gets called. The conventional way of testing this would be something along these lines (I am using Moq, but most Mocking frameworks are similar)

[Test]
public void ProcessData_CallsWorker()
{
    var list = new List {1};
 
    // Setup worker
    _worker.Setup(w => w.DoWork(list))
        .Verifiable();
 
    // Act
    _controller.ProcessData(list);
 
    _worker.VerifyAll();
}

But using this method, the code blows up with a NullReferenceException.

NullReference

Yes you could make the worker.DoWork(list) return a list but that would just make the test throw an NotImplementedException as neither of the other paths are implemented yet.

A better option is to make the mocked worker.DoWork(list) throw an exception:

_worker.Setup(w => w.DoWork(list))
    .Throws(new MockCalled());

You then let the MockCalled exception bubble up the call stack, and get handled by you inside your test:

try
{
    _controller.ProcessData(list); // Will throw when calls worker
}
catch (MockCalled)
{
}

You should throw a custom exception ( I have used MockCalled : Exception ) so that you know for a fact that when you receive a MockCalled exception, it could of only come from your mocked method.

If your testing framework allows for annotating a test with ExpectedType (NUnit and MSTest do, haven’t tried any others), then this becomes even cleaner:

[Test]
[ExpectedException(typeof(MockCalled))]
public void ProcessData_CallsWorker()
{
    var list = new List {1};
 
    // Setup worker
    _worker.Setup(w => w.DoWork(list))
        .Throws();
 
    // Act
    _controller.ProcessData(list);
}

In the above method, I wrapped the Throws(new MockCalled()) in an extension method .Throws()  for Moq as below:

public static void Throws(this ISetup mock)
    where TMock : class
{
    mock.Throws(new MockCalled());
}

 Or you could go even further and wrap the Setup call as well:

public static ISetup ThrowsOn(this Mock mock, Expression> expression) 
    where T : class
{
    return mock.Setup(expression).Throws();
}

 Giving you a slightly neater interface:

[Test]
[ExpectedException(typeof(MockCalled))]
public void ProcessData_CallsWorker()
{
    var list = new List {1};
 
    // Setup worker
    _worker.ThrowsOn(w => w.DoWork(list));
 
    // Act
    _controller.ProcessData(list);
}

 Other Usages

Imagine the following scenario

Same Class Usage

This represents a scenario where you want to test that another method on the class under test was called. The usual way of testing something like this would be to mark your ProcessData() method as public virtual, and then Mock the class under test verifying  that the ProcessData method was called.

public virtual List ProcessData(List data) { ... }
 
[Test]
public void Iteration_HasData_CallsProcessData()
{
    // Setup repo
    var list = new List {1};
    _repo.Setup(r => r.GetData()).Returns(list);
 
    _mock.Setup(x => x.ProcessData(list))
        .Verifiable();
 
    // Act
    _mock.Object.Iteration();
 
    // Assert
    _mock.VerifyAll();
}

There are 2 issues with that:

  1. You have to make the ProcessData method public virtual so that Moq can provide an implementation that registers the call.
  2. DataChanged logic will throw a NullReferenceException if it depends on the result of ProcessData, so you have to return a valid result from ProcessData.

The first point has never sat well with me, so this is where you can again use this technique.

The second point, well thats a pain each time, so rather than setting up a verifiable virtual implementation for ProcessData that return valid data for the method to progress gracefully why not make it throw the MockCalled exception?

[Test]
[ExpectedException(typeof(MockCalled))]
public void Iteration_HasData_CallsProcessDataThrow()
{
    // Setup repo
    var list = new List {1};
    _repo.Setup(r => r.GetData()).Returns(list);
    // Setup mock
    _mock.ThrowsOn(x => x.ProcessData(list));
        
    // Act
    _mock.Object.Iteration();
}

 This makes the test a bit cleaner, but there is still a flaw which does not sit well with me. That you have to mark the method as public virtual. This is probably something that I could take a look at and implement in Moq, but I will leave that for another day. At the moment what I have been doing which is working quite well, is making the first logical interface interaction throw the exception.

In the scenario above, the first call which happens right at the start of ProcessData is the IWorker.DoWork(), so I can make this method throw the exception, knowing that it should get to there gracefully at which point the exception will be thrown.

This allows the method to stay private or internal, and does not need to be virtual.

[Test]
[ExpectedException(typeof(MockCalled))]
public void Iteration_HasData_CallsProcessDataWhichCallsWorker()
{
    // Setup repo
    var list = new List {1};
    _repo.Setup(r => r.GetData()).Returns(list);
    // Setup Worker
    _worker.ThrowsOn(x => x.DoWork(list));
        
    // Act
    _controller.Iteration();
}

As always you can find the code @ GitHub on codePerf/20150319-TddMockThrowInterfaceCallVerificationTechnique

WPF – MVVM Animated Dialogs

Recently someone asked what is a good way to show animated dialog boxes/controls while keeping the views and view models separated in an MVVM fashion.

My answer was to use data templates! Such that, you have a view model to represent a dialog box/control (I will refer to these as simply dialogs), whether it be an alert, modal view to edit properties, informational or anything else, and let the view decide how and where to show it by using a common service to which you can pass these alerts in.

Data templates allowed me to get an affect like this:

AnimatedDialogs

The view model layer could be represented using the following dependency graph:

IDIalogService

The IDialogService exposes 1 method (ShowDialog) which is used by the view model in order to show any IDialog, with an optional generic continuation Action<TDialog> so some more logic can be carried out after the dialog closes. The IDialogService also exposes 2 events (DialogShown / DialogClosed) which allow any interested parties to carry out some action whenever a dialog is shown or closed. This is used by the MainViewModel, which hosts the CurrentDialog property, in order to show and hide dialogs.

public interface IDialogService
{
    IContinueWith ShowDialog(TDialog dialog) where TDialog : IDialog;

    event EventHandler DialogShown;
    event EventHandler DialogClosed;
}

The MainViewModel which is used as the DataContext for the MainWindow, looks like the following:

public class MainViewModel : ViewModelBase
{
    public MainViewModel(IDialogService dialogService) { ... }
    public RelayCommand CommandAboutMe { ... }
    public IDialog CurrentDialog { ... }
    public ObservableCollection Posts { ... }
}

The MainViewModel, has the IDialogService injected into it, and it subscribes to the IDialogService.DialogShown and IDialogService.DialogClosed events, which in turn set the CurrentDialog to a given IDialog (on shown) or null (on closed). This allows the external sources to show and hide dialogs on the MainWindow via the IDialogService.

MainWindow.xaml:

<Window x:Class="MvvmAnimatedDialogs.MainWindow" ... >
    <Grid>
        <DockPanel>
             /* Content of window */
        </DockPanel>
        <ContentControl Content="{Binding CurrentDialog}" />
    </Grid>
</Window>

In the MainWindow, the DockPanel contains all of the UI layout for the main window, and the ContentControl is a place-holder for the dialogs, which is bound the the MainViewModel.CurrentDialog. It uses the power of WPF data templates in order to decide how to display whatever IDialog object it is bound to (via the CurrentDialog property) and will display nothing if it is null. It is important to either make the ContentControl the last element, or set its Z-Index to a value higher than any other Z-Index, in order for the dialogs to be shown on top of everything else.

The ContentControl will traverse up the tree, trying to find data templates for an object of the current type that its Content property is bound to, so the data templates are placed as a ResourceDictionary in the App.xaml file.

<Application x:Class="MvvmAnimatedDialogs.App" ....
             xmlns:viewModel="clr-namespace:MvvmAnimatedDialogs.ViewModel">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Dialogs/DialogTemplates.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

And inside the DialogTemplates.xaml, I have DataTemplates for each concrete implementation of IDialog.

DialogTemplates.xaml

<ResourceDictionary>
    <DataTemplate DataType="{x:Type dialogs:SimpleDialog}" Resources="{StaticResource DialogResources}">
        <Border>            
            <Border>                
                <Grid Margin="20">
                    <DockPanel Grid.Column="1">
                        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
                            <Button Content="OK" Command="{Binding CommandOk}" />
                        </StackPanel>
                        <TextBlock Text="{Binding Message}" />
                    </DockPanel>
                </Grid>
            </Border>
        </Border>
    </DataTemplate>
    <DataTemplate DataType="{x:Type dialogs:OkCancelDialog}" Resources="{StaticResource DialogResources}">
        <Border>
            <Border>
                <Grid Margin="20">
                    <DockPanel Grid.Column="1">
                        <StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
                            <Button Content="OK" Command="{Binding CommandOk}" />
                            <Button Content="Cancel" Command="{Binding CommandCancel}" />
                        </StackPanel>
                        <TextBlock Text="{Binding Message}" />
                    </DockPanel>
                </Grid>
            </Border>
        </Border>
    </DataTemplate>
</ResourceDictionary>

Yes I know the plumbing code for each dialog should be abstracted into its own UserControl/CustomControl but for this exercise copy + paste will have to suffice.

As usual, you can find all the code on GitHub @ codePerf/20150315-WpfMvvmAnimatedDialogs

Rx Request Response Throttle

My most recent project at work was to introduce a Request Response messaging API into one our systems, as previously we were receiving a daily batch file with upwards of 4m entries when we only needed around 500-1,000 of them.

One of the restrictions was that we had to have mechanisms in place to ensure we did not flood the Service API with 1,000’s of messages all in one go, as this would cause delays for all other systems using the same API, as this was a widely used and critical service.

We had to make sure we do not have more than 4 requests queued at any one time (the API had 4 worker processes) so that everyone gets their fair share of the service. This made a great exercise for Rx 😀

The system could be represented via this diagram 

2015-03-07 13_47_38-RxRequestResponse-OverviewOne of my favourite things that Rx brought to the table, was this concept of virtual time using schedulers which you can learn about in Lee Campbells – Intro to Rx free on-line book under Part 4 – Testing Rx.

I started with an initial controller, which subscribes to an injected IObservable<Request> and calls the service straight away for each request. Responses are handled similarly, the controller subscribes to an IObservable<Response> on the IApiService using the injected IObserver<Response>

public class RequestResponseController
{
    public RequestResponseController(IRequester requester, IResponseHandler responseHandler, 
        IApiService service, ISchedulers schedulers)
    {
        requester
            .ObserveOn(schedulers.ThreadPool)
            .Subscribe(request => service.SendRequest(request));
            
        service.Responses
            .ObserveOn(schedulers.ThreadPool)
            .Subscribe(responseHandler);
    }
}

With a simple test to prove this

[Test]
public void Should_get_respponse_when_request_received()
{
    _requests.OnNext(new Request(1));
    
    _schedulers.ThreadPool.AdvanceBy(1); // Send Request
    _requestsReceived.Should().HaveCount(1);
    
    _serviceScheduler.AdvanceBy(1); // Process Request
    
    _schedulers.ThreadPool.AdvanceBy(1); // Process Response
    _responsesProcessed.Should().HaveCount(1);
}

Which passed with the following output:

Advancing ThreadPool by 1
Received Request: 1
  
Advancing ServiceScheduler by 1
Sending Response: 1
   
Advancing ThreadPool by 1
Processed Response: 1

I then went on to publish 5 requests from the requester, of which only 4 should be sent to the service before receiving a response.

[Test]
public void Should_send_max_4_requests_at_one_time()
{
    // Schedule 5 requests
    _requests.OnNext(new Request(1));
    _requests.OnNext(new Request(2));
    _requests.OnNext(new Request(3));
    _requests.OnNext(new Request(4));
    _requests.OnNext(new Request(5));
    
    _schedulers.ThreadPool.AdvanceBy(5); // Attempt to send all 5 requests
    _requestsReceived.Should().HaveCount(4); // Check if only 4 requests received by service
}

 This test failed as expected, and gave the following results:

Advancing ThreadPool by 5
Received Request: 1
Received Request: 2
Received Request: 3
Received Request: 4
Received Request: 5
Expected collection to contain 4 item(s), but found 5.

 In order to achieve the throttling, I added a new Subject

  field which acts as a throttle to the requester, so that only 4 requests are active at any time.

private readonly Subject _throttle = new Subject();

This could be achieve by using the  IObservable.Zip() extension, which brings 2 sequences together, and publishes at the rate of the slowest sequence. I also immediately published 4 objects into the  _throttle sequence so that the first 4 requests would immediately fire.

public RequestResponseController(...)
{
    // Service Subscription stayed the same
 
    requester
        .ObserveOn(schedulers.ThreadPool)
        .Zip(_throttle,(request, i) => request) // <---------Added Zip
        .Subscribe(request => service.SendRequest(request));
 
    for (int i = 0; i < 4; i++) // Init _throttle with 4 requests
        _throttle.OnNext(i);
}

 This made the test pass, and could be represented by the following marble diagram: 

Throttle  |1234----------------------------
Requester |----1-2-3-4-5-------------------
Result    |----1-2-3-4---------------------

 The missing piece of the puzzle was to ensure that queued requests would be sent out as soon as a response comes back. This could be represented by the following test:

[Test]
public void Should_send__5th_queued_request_after_receiving_1st_response()
{
    // Schedule 5 requests
    _requests.OnNext(new Request(1));
    _requests.OnNext(new Request(2));
    _requests.OnNext(new Request(3));
    _requests.OnNext(new Request(4));
    _requests.OnNext(new Request(5));
    _requests.OnNext(new Request(6));
 
    _schedulers.ThreadPool.AdvanceBy(5); // Attempt to send all 5 requests
 
    _serviceScheduler.AdvanceBy(1);
    _requestsReceived.Should().HaveCount(5); // Check that 5th request received by service
}

 Which failed and gave the following output:

Advancing ThreadPool by 5
Received Request: 1
Received Request: 2
Received Request: 3
Received Request: 4
 
Advancing ServiceScheduler by 1
Sending Response: 1
Expected collection to contain 5 item(s), but found 4.

 The implementation for this was easy, just subscribe to the  IApiService.Response  sequence, with an Action that pushes a value onto the _throttle sequence.

public RequestResponseController(...)
{
    service.Responses
        .Subscribe(r => _throttle.OnNext(0));
        
    // Rest of the Constructor
}

 And could be represented by the following marble diagram:

Throttle  |1234--------------0--0--0--0--0--0--0-
Requester |----1-2-3-4-5-6-7----------------89---
Result    |----1-2-3-4-------5--6--7--------89---
Response  |------------------1--2--3--4--5--6--7-

Stay tuned as I will be blogging about the Time-out -> Retry implementation for this controller soon. Where if we don't receive a response within 30 seconds, we retry, and to handle multiple responses for the same request (e.g. due to the retry) so that we do not process duplicate responses.

As always the code can be found on GitHub @ michal-ciechan/codePerf/20150308-RxRequestResponse/