Async vs Sync Benchmarks on MVC Controller with Questions

后端 未结 1 641
轮回少年
轮回少年 2021-02-04 14:58

I have set-up the examples from this MSDN article Using Asynchronous Methods in ASP.NET MVC 4 and have done some benchmarking to see what I come up with.

Server Configur

相关标签:
1条回答
  • 2021-02-04 15:26

    I think the clue is in your comparison

    webClient.DownloadString(uri)
    

    to

    var response = await httpClient.GetAsync(uri);
    return (await response.Content.ReadAsAsync<object>());
    

    maybe you can try

    webclient.DownloadStringAsync(uri)
    

    and you could optimize your async code to

    await Task.Run(() => {
        // just run your sync code here
        var g1 = GetGizmos("url1");
        var g2 = GetGizmos("url2");
        var g3 = GetGizmos("url3");
        return Content("");
    });
    

    It is enough to have one async yielding point here.

    Refer to this answer for details about async: Do you have to put Task.Run in a method to make it async?

    0 讨论(0)
提交回复
热议问题