Asp.net MVC异步很慢

时间:2013-04-15 14:27:31

标签: c# asp.net-mvc entity-framework

我正在使用Entity Framework 6.0.0 alpha1。

在我的Asp.net MVC应用程序中,我有两个控制器:

一个没有异步:

    public ActionResult Index()
    {
        return View(db.Movie.ToList());
    }

一个是异步:

    public async Task<ActionResult> Index()
    {
        var model = await db.Movie.ToListAsync();
        return View(model);
    }

我使用ab工具来测试效果:

没有异步的结果:

Server Software:        Microsoft-IIS/8.0
Server Hostname:        localhost
Server Port:            60863

Document Path:          /movies
Document Length:        5724 bytes

Concurrency Level:      10
Time taken for tests:   21.229 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      6071000 bytes
HTML transferred:       5724000 bytes
Requests per second:    47.11 [#/sec] (mean)
Time per request:       212.290 [ms] (mean)
Time per request:       21.229 [ms] (mean, across all concurrent requests)
Transfer rate:          279.27 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       4
Processing:    66  211  71.3    195     806
Waiting:       66  211  71.3    195     806
Total:         67  211  71.3    196     807

Percentage of the requests served within a certain time (ms)
  50%    196
  66%    223
  75%    245
  80%    260
  90%    298
  95%    334
  98%    397
  99%    461
 100%    807 (longest request)

异步结果:

Concurrency Level:      10
Time taken for tests:   29.495 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Total transferred:      6071000 bytes
HTML transferred:       5724000 bytes
Requests per second:    33.90 [#/sec] (mean)
Time per request:       294.947 [ms] (mean)
Time per request:       29.495 [ms] (mean, across all concurrent requests)
Transfer rate:          201.01 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.5      0       2
Processing:    69  293 160.0    244    1546
Waiting:       69  293 160.0    244    1546
Total:         70  294 160.0    245    1547

Percentage of the requests served within a certain time (ms)
  50%    245
  66%    295
  75%    343
  80%    373
  90%    507
  95%    639
  98%    772
  99%    841
 100%   1547 (longest request)

我的问题是为什么异步很慢?

1 个答案:

答案 0 :(得分:6)

您是否正在测试异步调用返回的时间,以便您可以完成其他工作,或者您是否在等待工作完成? Async不会神奇地让工作变得更快;它只是解锁您的方法调用,以便您可以在工作完成时执行其他操作。

事实上,如果你正在等待工作完成,那么异步会花费更长的时间,因为它会增加一些开销。

你应该测量的是可扩展性,而不是速度。同步网站将更快地为前几个呼叫提供服务,但在添加额外负载时会减慢速度。一个编写良好的异步网站应该更加一致和可靠地处理更多的呼叫,因为您更有效地使用处理器核心。

要提高整体可伸缩性和响应能力,请查找您或用户不必等待完成的长时间运行操作,并使这些操作异步。