c# - Asp.net MVC 异步速度很慢

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

我正在使用 Entity Framework 6.0.0 alpha1。

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

一个是没有异步的:

    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)

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

最佳答案

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

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

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

为了提高整体可扩展性和响应能力,请查找您或用户不必等待完成的长时间运行的操作,并将其设为异步。

关于c# - Asp.net MVC 异步速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16017715/

相关文章:

c# - 当继承类型具有不同的类型参数时,声明泛型基类型的变量

c# - MVC 编辑存储在隐藏字段中的复杂对象

asp.net-mvc - 捕获ASP.NET MVC错误和WebActivator

c# - 在具有内部联接的 LINQ 查询中选择最近日期

c# - 获取数组中的枚举元素

c# - Nhibernate/Linq : NHibernate. QueryException : could not resolve property: Profile. : MyNamespace. MyObject 的类

c# - Entity Framework : How to detect external changes to database

.net - EF 代码首先从 IQueryable<T> 中删除批处理?

C# 在 Main() 中等待所有线程完成

javascript - 将值推送到 jquery 中的数组