c# - 如何配置 asp.net kestrel 以实现低延迟?

标签 c# linux .net-core low-latency kestrel

我正在尝试实现一个 asp.net 2.2 应用程序,以尽可能低的延迟 服务 HTTP 请求(不是吞吐量,它不是用于生产,而是某种竞争)。该应用程序应该在具有 4 个内核的 Linux docker 容器环境中运行,并且我的处理程序受 CPU 限制,每个处理程序为 0.2..3 毫秒。连接是预先创建的并保持事件状态,但我目前为空处理程序获得大约 0.6..0.8 毫秒的处理时间(回复 200 OK),有明显的抖动和偶尔的峰值到 20-50 毫秒,我不能解释一下。

Kestrel/Sockets/Threads/CLR 是否有任何特定设置可以帮助最小化每个请求的响应时间?或者如果我想将它降低到 0.1..0.2 毫秒,那么使用 EPOLL 的 C/C++ 路线是我唯一的选择?

最佳答案

Low latency is certainly possible with ASP.NET Core / Kestrel.

这是一个微型网络应用程序来演示这一点......

using System.Net;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

public static void Main(string[] args)
{
    IWebHost host = new WebHostBuilder()
        .UseKestrel()
        .Configure(app =>
        {
            // notice how we don't have app.UseMvc()?
            app.Map("/hello", SayHello);  // <-- ex: "http://localhost/hello"
        })
        .Build();

    host.Run();
}

private static void SayHello(IApplicationBuilder app)
{
    app.Run(async context =>
    {
        // implement your own response
        await context.Response.WriteAsync("Hello World!");
    });
}

类似的问题我在herehere之前已经回答过很多次了。

If you wish to compare the ASP.NET Core framework against others this is a great visual https://www.techempower.com/benchmarks/#section=data-r16&hw=ph&test=plaintext. As you can see, ASP.NET Core is has exceptional results and is the leading framework for C#.

在我上面的代码块中,我注意到缺少 app.UseMvc()。如果你确实需要它,我在这个答案中做了一个关于获得更好延迟的非常详细的答案:What is the difference between AddMvc() and AddMvcCore()?


.NET 核心运行时 (CoreRT)

如果您仍然需要更多性能,我建议您查看 .Net Core Runtime (CoreRT)

请注意,在撰写本文时,可能需要更详细地审查此选项,然后再将其用于生产系统。

"CoreRT brings much of the performance and all of the deployment benefits of native compilation, while retaining your ability to write in your favorite .NET programming language."

CoreRT 提供了对许多应用至关重要的巨大优势。

  • native 编译器生成单个文件,包括应用程序、托管依赖项和 CoreRT。
  • native 编译的应用程序启动速度更快,因为它们执行已编译的代码。他们不需要在运行时生成机器代码,也不需要加载 JIT 编译器。
  • 原生编译应用可以使用优化编译器,从而通过更高质量的代码(C++ 编译器优化)提高吞吐量。 LLILLC 和 IL 到 CPP 编译器都依赖于优化编译器。

这些好处为 .NET 开发人员开辟了一些新场景

  • 从一台机器复制单个文件可执行文件并在另一台机器(同类机器)上运行,而无需安装 .NET 运行时。
  • 创建并运行包含单个文件可执行文件的 docker 镜像(例如,除 Ubuntu 14.04 之外的一个文件)。

Linux 特定的优化

有一个很好的库试图处理非常特殊的情况。特别是对于 Linux(但此代码对于其他操作系统是安全的)。此优化背后的原理是将 libuv 传输库(ASP.NET Core 使用)替换为另一种特定于 Linux 的优化。

它直接使用内核原语来实现传输 API。这减少了堆分配对象的数量(例如 uv_buf_tSocketAsyncEventArgs),这意味着 GC 压力更小。建立在 xplat API 之上的实现将汇集对象来实现这一点。

using RedHat.AspNetCore.Server.Kestrel.Transport.Linux; // <--- note this !

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseLinuxTransport()     // <--- and note this !!!
        .UseStartup()
        .Build();

// note: It's safe to call UseLinuxTransport on non-Linux platforms, it will no-op

You can take a look at the repository for that middleware on GitHub here https://github.com/redhat-developer/kestrel-linux-transport

enter image description here

enter image description here

来源:https://developers.redhat.com/blog/2018/07/24/improv-net-core-kestrel-performance-linux/

关于c# - 如何配置 asp.net kestrel 以实现低延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54103309/

相关文章:

C# 为什么从类中调用接口(interface)成员会产生错误?

asp.net-core - 在 Linux 中绕过 ASP.NET Core 中的 SSL 验证

c# - 使用多种语言编写应用程序

linux - 在 bash 中使用 awk 变量

linux - 混合远程期望脚本和本地 bash 命令的最佳方法是什么?

linux - 用于删除小于 xMB 的文件的 Shell 脚本

c# - 由于 SSL 握手异常,.NET Core 3.0 上的 WebRequest 第二次失败

entity-framework - EF Core 2.0 scaffold-dbcontext 在另一个项目中查找 ConnectionString

c# - 如何在不获取所有版本的情况下复制/复制项目?

c# - 如何从 Java 调用 C# Web 服务