- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们最近开发了一个基于 SOA 的站点,但是这个站点在负载过重时最终会出现严重的负载和性能问题。我在这里发布了一个与此问题相关的问题:
ASP.NET website becomes unresponsive under load
该站点由托管在 4 节点集群上的 API(WEB API)站点和托管在另一个 4 节点集群上并调用 API 的网站组成。两者都是使用 ASP.NET MVC 5 开发的,所有操作/方法都基于 async-await 方法。
在 NewRelic 等监控工具下运行该站点,调查几个转储文件并分析工作进程后,事实证明,在非常轻的负载(例如 16 个并发用户)下,我们最终拥有大约 900 个线程,这些线程占用了 100% 的 CPU并填满了 IIS 线程队列!
尽管我们通过引入大量缓存和性能修正来设法将站点部署到生产环境,但我们团队中的许多开发人员认为我们必须删除所有异步方法并将 API 和网站转换为普通的 Web API 和操作方法只需返回一个 Action 结果。
我个人对这种方法不满意,因为我的直觉是我们没有正确使用异步方法,否则这意味着微软引入了一个基本上具有破坏性且无法使用的功能!
您是否知道任何引用资料可以清楚地说明应该/可以在何处以及如何使用异步方法?我们应该如何使用它们来避免这样的戏剧?例如根据我在 MSDN 上阅读的内容,我认为 API 层应该是异步的,但该网站可能是一个普通的非异步 ASP.NET MVC 站点。
更新:
这是与 API 进行所有通信的异步方法。
public static async Task<T> GetApiResponse<T>(object parameters, string action, CancellationToken ctk)
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(BaseApiAddress);
var formatter = new JsonMediaTypeFormatter();
return
await
httpClient.PostAsJsonAsync(action, parameters, ctk)
.ContinueWith(x => x.Result.Content.ReadAsAsync<T>(new[] { formatter }).Result, ctk);
}
}
public async static Task<IList<DownloadType>> GetSupportedContentTypes()
{
string userAgent = Request.UserAgent;
var parameters = new { Util.AppKey, Util.StoreId, QueryParameters = new { UserAgent = userAgent } };
var taskResponse = await Util.GetApiResponse<ApiResponse<SearchResponse<ProductItem>>>(
parameters,
"api/Content/ContentTypeSummary",
default(CancellationToken));
return task.Data.Groups.Select(x => x.DownloadType()).ToList();
}
public async Task<ActionResult> DownloadTypes()
{
IList<DownloadType> supportedTypes = await ContentService.GetSupportedContentTypes();
最佳答案
Is there anything silly with this method? Note that when we converted all method to non-async methods we got a heaps better performance.
public static async Task<T> GetApiResponse<T>(object parameters, string action, CancellationToken ctk)
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(BaseApiAddress);
var formatter = new JsonMediaTypeFormatter();
return
await
httpClient.PostAsJsonAsync(action, parameters, ctk)
.ContinueWith(x => x.Result.Content
.ReadAsAsync<T>(new[] { formatter }).Result, ctk);
}
}
ContinueWith
的 lambda 是阻塞的:
x => x.Result.Content.ReadAsAsync<T>(new[] { formatter }).Result
x => {
var task = x.Result.Content.ReadAsAsync<T>(new[] { formatter });
task.Wait();
return task.Result;
};
ReadAsAsync
API 的优势,并降低了 Web 应用程序的可伸缩性。在您的代码中注意其他类似的地方。
AspNetSynchronizationContext
的服务器线程处理。当您使用
await
进行延续时,延续回调将被发布到相同的同步上下文,编译器生成的代码将处理这一点。 OTOH,当您使用
ContinueWith
时,这不会自动发生。
.Result
(这将返回一个任务)并
Unwrap
嵌套任务:
return
await
httpClient.PostAsJsonAsync(action, parameters, ctk).ContinueWith(
x => x.Result.Content.ReadAsAsync<T>(new[] { formatter }),
ctk,
TaskContinuationOptions.None,
TaskScheduler.FromCurrentSynchronizationContext()).Unwrap();
ContinueWith
的复杂性:
var x = await httpClient.PostAsJsonAsync(action, parameters, ctk);
return await x.Content.ReadAsAsync<T>(new[] { formatter });
If I have to call an async method in a sync context, where using await is not possible, what is the best way of doing it?
await
和
ContinueWith
,你应该坚持使用
await
。基本上,如果你使用
async
,它必须是 async
"all the way" 。
async
并返回一个
Task
或
Task<>
,检查
this 。 ASP.NET 跟踪给定 HTTP 请求的挂起任务。在完成所有任务之前,该请求不会完成。
async
方法,可以使用
AsyncManager
类似
this 来注册挂起的任务。对于经典的 ASP.NET,您可以使用
PageAsyncTask
。
task.Wait()
并阻止,因为否则您的任务可能会在该特定 HTTP 请求的边界之外继续进行。
async
方法可能会有一些不同的场景。例如,您可以使用
ContinueWith(action, TaskScheduler.FromCurrentSynchronizationContext())
并从
action
触发完成事件(如
this )。
关于performance - 异步和等待 : are they bad?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21771219/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 8年前关闭。 Improve t
暂时忘记能力的定义,只关注能力的“检查”(使用“授权!”),我看到 CanCan 添加了大约 400 毫秒,用于简单地检查用户是否具有特定的能力主题/模型。 这是预期的吗(我假设不是)?或者,有没有可
我正在阅读有关 Swift 的教程 ( http://www.raywenderlich.com/74438/swift-tutorial-a-quick-start ),它预定义为不显式设置类型,因
这主要是由于对 SQL 问题的回答。由于性能原因,有意省略了 UDF 和子查询。我没有包括可靠性并不是说它应该被视为理所当然,但代码必须工作。 性能永远是第一位的吗?提供了许多以性能为主要优先事项的答
我已经编写了一个简单的测试平台来测量三种阶乘实现的性能:基于循环的,非尾递归的和尾递归的。 Surprisingly to me the worst performant was the loop o
我已将 ui-performance 插件应用到我的应用程序中。不幸的是,在开发模式下运行应用程序时它似乎不起作用。例如,我的 javascript 导入是用“vnull”版本呈现的。 例如 不会
我有一个我操作的 F# 引用(我在各处添加对象池以回收经常创建和删除的短期对象)。我想运行结果报价;现在我使用了 F# PowerPack,它提供了将引用转换为表达式树和委托(delegate)的方法
我正在尝试在 Spark 服务器上运行 SparklyR 库中的机器学习算法。 1 个簇 8 核 24G内存 Ubuntu 16.04 星火2.2 独立配置 1名师傅/2名 worker 每个执行器的
我有一个数据库(准确地说是在 postgres 上运行),具有以下结构: user1 (schema) | - cars (table) - airplanes (table, again) .
我的应用程序在我的 iPad 上运行。但它的表现非常糟糕——我的速度低于 15fps。谁能帮我优化一下? 它基本上是一个轮子(派生自 UIView),包含 12 个按钮(派生自 UIControl)。
在完成“Scala 中的函数式编程原则”@coursera 类(class)第 3 周的作业时,我发现当我实现视频类(class)中所示的函数联合时: override def union(tha
我正在重构我的一个 Controller 以使其成为一项服务,我想知道不将整个服务容器注入(inject)我的 Controller 是否会对性能产生影响。 这样效率更高吗: innova.path.
我有一个要显示的内容很大的文件。例如在显示用户配置文件时, 中的每个 EL 表达式需要一个 userId 作为 bean 的参数,该参数取自 session 上下文。我在 xhtml 文件中将这个 u
我非常了解 mipmapping。我不明白(在硬件/驱动程序级别)是 mipmapping 如何提高应用程序的性能(至少这是经常声称的)。在执行片段着色器之前,驱动程序不知道要访问哪个 mipmap
这个问题在这里已经有了答案: 10年前关闭。 Possible Duplicate: What's the (hidden) cost of lazy val? (Scala) Scala 允许定义惰
一些文章建议现在 build() 包含在 perform() 本身中,而其他人则建议当要链接多个操作时使用 build().perform()一起。 最佳答案 build() 包含在 perform(
Postgres docs说 For best optimization results, you should label your functions with the strictest vol
阅读Zero-cost abstractions看着 Introduction to rust: a low-level language with high-level abstractions我尝
我想在 MQ 服务器上部署 SSL,但我想知道我当前的 CPU 容量是否支持 SSL。 (我没有预算增加 CPU 内核和 MQ PVU 的数量) 我的规范: Windows 2003 服务器 SP2,
因此,我在 Chrome 开发者工具 的性能 选项卡内的时间 部分成功地监控了我的 React Native 应用程序的性能。 突然在应用程序的特定重新加载时,Timings 标签丢失。 我已尝试重置
我是一名优秀的程序员,十分优秀!