简体   繁体   English

Asp.Net HttpClient性能

[英]Asp.Net HttpClient Performance

I'm load/stress testing using Visual Studios load testing project with current users set quite low at 10. The application makes multiple System.Net.Http.HttpClient request to a WebAPI service layer/application (which in some cases calls another WebApi layer/application) and it's only getting around 30 request/second when testing a single MVC4 controller (not parsing dependent requests). 我使用Visual Studios负载测试项目加载/压力测试,当前用户设置得非常低。该应用程序向WebAPI服务层/应用程序(在某些情况下调用另一个WebApi层)发出多个System.Net.Http.HttpClient请求在测试单个MVC4控制器(不解析相关请求)时,它只获得大约30个请求/秒。

These figures are based on a sample project where no real code is executed at any of the layers other then HttpClient calls and to contruct a new collection of Models to return (so some serialization happening too as part of WebApi). 这些数据基于一个示例项目,其中除HttpClient调用之外的任何层都不执行实际代码,并构造要返回的新模型集合(因此一些序列化也作为WebApi的一部分发生)。 I've tried both with async controller and without and the results seemed pretty similar (slight improvement with async controllers) 我已经尝试使用异步控制器和没有,结果似乎非常相似(与异步控制器略有改进)

Caching the calls obviously increases it dramatically (to around 400 RPS) although the nature of the data and the traffic means that a significant amount of the calls wouldn't be cached in real world scenarios. 缓存呼叫显然会大大增加(大约400 RPS),尽管数据和流量的性质意味着大量的呼叫不会在现实场景中缓存。

Is HttpClient (or HTTP layer in general) simply too expensive or are there some ideas you have around getting higher throughput? HttpClient(或一般的HTTP层)是否过于昂贵,或者您是否有一些想法可以获得更高的吞吐量?

It might be worth noting that on my local development machine this load test also maxes out the CPU. 值得注意的是,在我的本地开发机器上,此负载测试也会最大化CPU。 Any ideas or suggestions would be very much appreciated? 任何想法或建议将非常感激?

HttpClient has a built-in default of 2 concurrent connections. HttpClient内置默认值为2个并发连接。

Have you changed the default? 你改变了默认值吗?

See this answer 看到这个答案

I believe if you chase the deps for HTTPClient, you will find that it relies on the settings for ServicePointManager. 我相信如果你追逐HTTPClient的deps,你会发现它依赖于ServicePointManager的设置。

See this link 看到这个链接

You can change the default connection limit by calling System.Net.ServicePointManager.DefaultConnectionLimit 您可以通过调用System.Net.ServicePointManager.DefaultConnectionLimit来更改默认连接限制

You can increase number of concurrent connection for http client. 您可以增加http客户端的并发连接数。 Default connection limit is 2. In order to find optimum connection try the formula 12* number of CPU on your machine. 默认连接限制为2.为了找到最佳连接,请尝试计算机上12 *的CPU数量。

In code : In code: 在代码中:在代码中:

ServicePointManager.DefaultConnectionLimit = 48;  

Or In Api.config or web.config file 或者在Api.config或web.config文件中

<system.net>
    <connectionManagement>
      <add address="*" maxconnection="48"/>
    </connectionManagement>
  </system.net>

Check this link for more detail. 查看此链接了解更多详情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM