本文介绍了ASP.NET Core 2.2 Kestrel服务器的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我面临着Kestrel服务器性能的问题.我有以下情况:

I'm facing problem with kestrel server's performance. I have following scenario :

    TestClient(JMeter) -> DemoAPI-1(Kestrel) -> DemoAPI-2(IIS)

我正在尝试创建一个示例应用程序,该应用程序可以在请求时获取文件内容.TestClient(100个线程)向DemoAPI-1请求,而后者又向DemoAPI-2请求.DemoAPI-2读取一个固定的XML文件(最大1 MB)并返回其内容作为响应(在生产中,DemoAPI-2不会暴露给外界).

I'm trying to create a sample application that could get the file content as and when requested.TestClient(100 Threads) requests to DemoAPI-1 which in turn request to DemoAPI-2. DemoAPI-2 reads a fixed XML file(1 MB max) and returns it's content as a response(In production DemoAPI-2 is not going to be exposed to outside world).

当我测试了从TestClient-> DemoAPI-2的直接访问时,得到了以下预期结果(好):

When I tested direct access from TestClient -> DemoAPI-2 I got expected result(good) which is following :

  1. 平均:368ms
  2. 最少:40ms
  3. 最长:1056ms
  4. 吞吐量:40.1/秒

但是当我尝试通过DemoAPI-1访问它时,得到了以下结果:

But when I tried to access it through DemoAPI-1 I got following result :

  1. 平均:48232ms
  2. 最少:21095ms
  3. 最长:49377ms
  4. 吞吐量:2.0/秒

如您所见,两者之间存在巨大差异.我什至没有获得10%的DemoAPI-2吞吐量.我被告知,与传统的IIS相比,茶est具有更高的效率和速度.另外,由于直接访问没有问题,我认为我们可以消除在DemoAPI-2上出现问题的可能性.

As you can see there is a huge difference.I'm not getting even the 10% throughput of DemoAPI-2. I was told has kestrel is more efficient and fast compared to traditional IIS. Also because there is no problem in direct access, I think we can eliminate the possible of problem on DemoAPI-2.

※DemoAPI-1的代码:

※Code of DemoAPI-1 :

string base64Encoded = null;
            var request = new HttpRequestMessage(HttpMethod.Get, url);
            var response = await this.httpClient.SendAsync(request, HttpCompletionOption.ResponseContentRead).ConfigureAwait(false);

            if (response.StatusCode.Equals(HttpStatusCode.OK))
            {
                var content = await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
                base64Encoded = Convert.ToBase64String(content);
            }
return base64Encoded;

※DemoAPI-2的代码:

※Code of DemoAPI-2 :

[HttpGet("Demo2")]
    public async Task<IActionResult> Demo2Async(int wait)
    {
        try
        {
            if (wait > 0)
            {
                await Task.Delay(wait);
            }
            var path = Path.Combine(Directory.GetCurrentDirectory(), "test.xml");
            var file = System.IO.File.ReadAllText(path);
            return Content(file);
        }
        catch (System.Exception ex)
        {
            return StatusCode(500, ex.Message);
        }
    }

一些其他信息:

  1. 两个API都是异步的.
  2. 这两个API都托管在不同的EC2实例(C5.xlarge Windows Server 2016)上.
  3. DemoAPI-1(kestrel)是一个自包含的API(没有反向代理)
  4. TestClient(jMeter)设置为此测试的100个线程.
  5. 到目前为止,尚未对kestrel服务器进行其他配置.
  6. 到目前为止,还没有可以影响性能的操作过滤器,中间件或日志记录.
  7. 使用SSL在5001端口上进行通信.
  8. 到目前为止,DemoAPI2的等待参数已设置为0.
  9. DEMOAPI-1的CPU使用率不超过40%.

推荐答案

该问题归因于HttpClient的端口耗尽问题.我能够通过使用IHttpClientFactory解决此问题.下一篇文章可能会帮助遇到类似问题的人.

The problem was due to HttpClient's port exhaustion issue.I was able to solve this problem by using IHttpClientFactory.Following article might help someone who faces similar problem.

https://www.stevejgordon.co.uk/httpclient-creation-and-disposal-internals-should-i-dispose-of-httpclient

这篇关于ASP.NET Core 2.2 Kestrel服务器的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-09 03:05