.Net Core, run async methods in a loop

Here is a Unittest running async method which needs to show how async can benefit when long running methods are run. It should complete in about same amount of seconds as the longest delay that was set randomly during the execution of the test.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Xunit;
using System.Diagnostics;

namespace Some.test
{
  public class TestAsyncTasks
  {
    // Randomizer
    Random _rnd = new Random();

    [Fact]
    /// <summary>
    /// Run fake tasks with random delay async
    /// </summary>
    public async Task RunRandomTasksAsync()
    {
      var series = Enumerable.Range(1, 5).ToList();
      var tasks = new List<Task<Tuple<int, bool>>>();
      foreach (var i in series)
      {
        Debug.WriteLine(“Starting Process=” + i);
        tasks.Add(DoWorkAsync(i));
      }

      // Loop to process the tasks one at a time until none remain
      while (tasks.Count > 0)
      {
        // Get the first task that completes
        var thetask = await Task.WhenAny(tasks);

        // Remove the selected task from the list only process it more than once.
        tasks.Remove(thetask);

        // Await the completed task
        Tuple<int, bool> tupl = await thetask;
        Debug.WriteLine(“Process=” + tupl.Item1 + ” ended”);
      }
    }

    /// <summary>
    /// Complete a fake task using random delay
    /// </summary>
    public async Task<Tuple<int, bool>> DoWorkAsync(int i)
    {
      int delay = _rnd.Next(25);
      Debug.WriteLine(“working.. process=” + i + “, delay=” + delay);
      await Task.Delay(1000 * delay);
      return Tuple.Create(i, true);
    }
  }
}

New rig, build results

I saw a tweet from Scott Hanselman on his new rig and he has a blog post about it. Then I realized I hadn’t posted about my new rig that I built in January. I wanted something fast for development and also for video editing. I went with Linux Mint as I have been running Mint happily on my home computers for a few years now. I chose to go unconventionally with an older server motherboard and some older components that fit. The Motherboard is a Supermicro and holds a paired Intel Xeon E5-26780 server processors, they have 10 cores each. That gives me 40 threads. 16GB of DDR3 memory and a Pro SATA III SSD. I re-used the graphics card from my old rig, I use xBox for gaming anyway. The total was about $1500, bought from Newegg and Ebay.

Let’s take a look at the numbers,

The machine Scott is building ( before over-clocking )
– Warm build just under 10 seconds.
– Cold build takes 33.3 seconds.

My desktop server
– Warm build takes 16.16 seconds.
– Warm build with -no-restore takes 9.35 seconds
– Cold build takes 27.2 seconds

Interestingly my cold build is faster, maybe because of more threads ? Warm is a lot slower on my rig. Nice to see .net use all the available threads during the build. Do your own build see what your rig can do, maybe it’s time for a new one ?

UnitTest MsSql database using Slacker, SlackerRunner

Our open source MsSql UnitTest framework Slacker and SlackerRunner was featured on Microsoft Channel 9. Now it’s easier than ever to UnitTest your database and add it into your CI,CD build pipeline.

Watch Eric Kang from Microsoft explain in detail what you need and how it works.

Nlog configuration

I needed to add Nlog tracing that must be configurable from the config file. Finding a simple sample took a me a while. Therefor I’m putting the needed pieces here.

App.config

<?xml version=“1.0” encoding=“utf-8” ?>
<configuration>
  <configSections>
    <section name=“nlog” type=“NLog.Config.ConfigSectionHandler, NLog”/>
  </configSections>
  <nlog xmlns=“http://www.nlog-project.org/schemas/NLog.xsd”
    xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
    autoReload=“true”
    throwExceptions=“false”>

    <variable name=“appName” value=“sample” />
    <targets async=“true”>
      <target xsi:type=“File”
                name=“default”
                fileName=“sample.log”
                keepFileOpen=“false”
                DeleteOldFileOnStartup=“true”
            />

    </targets>
    <rules>
      <logger name=“*” writeTo=“default” minlevel=“Info” />
    </rules>
  </nlog>
</configuration>

And then in code, you can just do the following.

using NLog;
        Logger logger = LogManager.GetLogger(“mylogger”);
        logger.Log(LogLevel.Debug, “Hellow world”);

.Net Core 1.1 – WebApi Integration testing

Things have changed a bit since the DNX .Net WebApi Integration testing post that I wrote about a year ago. This post will show updated code for .Net Core 1.1, I’m using the TestHost library as before.

I’m using these Nuget packages
xunit 2.3.0-beta1-build3642
Microsoft.AspNet.TestHost 1.1.1

Here is the UnitTest itself

using System;
using System.Diagnostics;
using System.Net.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.AspNetCore.Hosting;
using Xunit;
using DevOps.Notes.Core.Util;

namespace DevOps.Notes.Web.Spec
{
  public class ValuseControllerTest
  {
    [Fact]
    public async void PingTheServerRepeat()
    {
      // Setup client & server
      TestServer server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
      HttpClient client = server.CreateClient();
      // Make a request and check response
      var getResponse = await client.GetAsync(“api/values/”);
      var response = await getResponse.Content.ReadAsStringAsync();
      TraceLog.Log(“web api, response=” + response);

      // Hit the webapi, repeatedly
      Stopwatch watch = Stopwatch.StartNew();
      for (int i = 0; i < Util.Repeat; i++)
      {
        getResponse = await client.GetAsync(“/api/values/”+i);
        response = await getResponse.Content.ReadAsStringAsync();
      }
      watch.Stop();
      TraceLog.Log($“WepApi, round trips ={Util.Repeat}, Execution time, millisec={watch.ElapsedMilliseconds}, average roundtrip=” + ((double)watch.ElapsedMilliseconds / Util.Repeat));
      TraceLog.Log(“web api, response=” + response);

      // Done, release resources
      server.Dispose();
      client.Dispose();

    }

  }
}

Linux tools on Windows

If you are like me, you don’t want to be bothered to remember if you are working on a Windows or a Linux box. Things like ls, curl etc, should just work. So if you are missing the Linux tools on Windows and you don’t want to install the heavy Cygwin, then the is an alternative, Gow is a lightweight set of Linux Tools for Windows.

Gow can be found here