gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:06:15 27 4
gpt4 key购买 nike

我正在使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com