3

我正在使用实体框架 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)

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

4

1 回答 1

7

您是在测试异步调用多久返回以便您可以完成其他工作,还是在等待工作完成?异步不会神奇地让工作更快地完成。它只是解锁您的方法调用,以便您在工作完成时可以做其他事情。

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

你真正应该衡量的是可扩展性,而不是速度。同步网站将更快地处理前几个调用,但随着您添加额外负载而减慢。一个编写良好的异步网站应该更一致、更可靠地处理更多的调用,因为您可以更有效地使用处理器内核。

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

于 2013-04-15T14:36:52.267 回答