dimanche 1 novembre 2015

UnitTest Async Method

using System.Net.Http;  
using Microsoft.VisualStudio.TestTools.UnitTesting;  
using System.Threading.Tasks;  
using System.Diagnostics;  

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public async Task ExceuteMultipleRequestsInParallel()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start(); 
        HttpClient client = new HttpClient();
        Task<string> ms = client.GetStringAsync("http://ift.tt/hSyg9Q");
        Task<string> msdn = client.GetStringAsync("http://ift.tt/rnTyPu");
        Task<string> blogs = client.GetStringAsync("http://blogs.msdn.com");
        await Task.WhenAll(ms, msdn, blogs);
        sw.Stop();
        var result = sw.Elapsed.ToString();
    }

    [TestMethod]
    public async Task ExcecuteMultipleRequests()
    {
        Stopwatch sw = new Stopwatch();
        HttpClient client = new HttpClient();
        string ms = await client.GetStringAsync("http://ift.tt/hSyg9Q");
        string msdn = await client.GetStringAsync("http://ift.tt/rnTyPu");
        string blogs = await client.GetStringAsync("http://blogs.msdn.com");
        sw.Stop();
        var result = sw.Elapsed.ToString();
    }
}

At first I only used one HttpClient. My expectation was that one call to await Task.WhenAll(...) would take about the time the longest task would take.
My expectation was also that this would take measurably less than calling each of the three tasks with an await.
However, this didn't seem to be the case. Also I always had to comment one as otherwise the cache would distort things.
Then I used three HttpClients and surprisingly the method using three times await took less time then the one using await Task.WhenAll.
This seems strange.
Thanks for ideas.

[TestMethod]
    public async Task ExceuteMultipleRequestsInParallel()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        HttpClient client = new HttpClient();
        HttpClient client2 = new HttpClient();
        HttpClient client3 = new HttpClient();
        Task<string> ms = client.GetStringAsync("http://ift.tt/hSyg9Q");
        Task<string> msdn = client2.GetStringAsync("http://ift.tt/rnTyPu");
        Task<string> blogs = client3.GetStringAsync("http://blogs.msdn.com");
        await Task.WhenAll(ms, msdn, blogs);
        sw.Stop();
        var result = sw.Elapsed.ToString();
    }

    [TestMethod]
    public async Task ExcecuteMultipleRequests()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        HttpClient client = new HttpClient();
        HttpClient client2 = new HttpClient();
        HttpClient client3 = new HttpClient();
        string ms = await client.GetStringAsync("http://ift.tt/hSyg9Q");
        string msdn = await client2.GetStringAsync("http://ift.tt/rnTyPu");
        string blogs = await client3.GetStringAsync("http://blogs.msdn.com");
        sw.Stop();
        var result = sw.Elapsed.ToString();
    }

Aucun commentaire:

Enregistrer un commentaire