2

Yarp 与 Nginx性能大比拼不出所料它胜利了! - tokengo

 7 months ago
source link: https://www.cnblogs.com/hejiale010426/p/17998403
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Yarp 与 Nginx 性能大比拼

测试环境:

Ubuntu 22.04.3 LTS (GNU/Linux 6.5.0-14-generic x86_64)

Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz *2

运行内存:94.3G

yarp 环境

.NET 8 SDK

Program.cs代码:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.MapReverseProxy();
app.Run();

Test.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <InvariantGlobalization>true</InvariantGlobalization>
        <PublishAot>true</PublishAot>


        <StackTraceSupport>false</StackTraceSupport>
        <OptimizationPreference>Size</OptimizationPreference>
        <PublishTrimmed>true</PublishTrimmed>
        <BlazorEnableTimeZoneSupport>false</BlazorEnableTimeZoneSupport>
        <EventSourceSupport>false</EventSourceSupport>
        <HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
        <EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
        <MetadataUpdaterSupport>false</MetadataUpdaterSupport>
        <UseNativeHttpHandler>true</UseNativeHttpHandler>
        <TrimMode>link</TrimMode>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="Yarp.ReverseProxy" Version="2.1.0" />
    </ItemGroup>

</Project>

参考 Native AOT deployment overview - .NET | Microsoft Learn 在服务器中安装 aot 环境

使用以下指令构建 aot 程序

dotnet publish -c Release -r linux-x64 --self-contained true /p:PublishAot=true --output ../output

Nginx 安装

在服务器中安装 nginx

sudo apt install nginx

/etc/nginx/conf.d目录下创建一个 wwwroot.conf

server {
    listen 7771;
    server_name localhost;
    location / {
	    add_header 'Access-Control-Allow-Origin' 'http://localhost:8088';
	    add_header 'Cache-Control' 'public, max-age=604800';
	    add_header 'Access-Control-Allow-Credentials' 'true';
	    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
	    rewrite ^/proxy/bing/(.*)$ /$1 break;
	    proxy_pass http://127.0.0.1:7777/;
    }
}

代理的服务

.NET 8 SDK

创建一个用于测试的代理服务,提供一个简单的接口,直接返回空的字符串。我们将这个服务发布成 linux-64 的程序,

Program.cs

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddEndpointsApiExplorer();

var app = builder.Build();


app.MapGet("/weatherforecast", () => string.Empty)
    .WithName("GetWeatherForecast");

app.Run();

2415052-20240131021848707-2069485113.png
chmod +x WebApplication1

然后启动我们的代理测试端点

./WebApplication1 urls="http://*:7777"

使用的测试工具

Apipost-Team/runnerGo: A tool similar to apache bench (ab) (github.com)

由 ApiPOST 开源的基于 Go 语言实现的压测工具,我们去 Release 下载发布好的 win-64 位程序,然后执行,

然后打开测试界面runnerGo UI (apipost.cn)

http://192.168.31.251:7772/weatherforecast Yarp 代理的服务

http://192.168.31.251:7771/weatherforecast Nginx 代理的服务

第一轮测试:

YARP 压测结果:

2415052-20240131021856037-1996063807.png

Nginx 压测结果:

2415052-20240131021902095-1444353300.png

第二轮测试:

Yarp 压测结果:

2415052-20240131021907767-1827068383.png

Nginx 压测结果:

2415052-20240131021915043-1047263792.png

第三轮压测:

Yarp 压测结果:

2415052-20240131021921454-1941438417.png

Nginx 压测结果:

2415052-20240131021929638-667261886.png

以上测试都是在内网测试,都属于同一个局域网,由测试结果得出 Yarp 基本完胜 Nginx,虽然说基本性能超越,但是 Yarp 也并非完全可替代 Nginx,Nginx 是支持 TCP/UDP 代理的,而 Yarp 默认是只支持 Http 协议的代理。

来自 token 的分享

技术交流群:737776595

本文作者:tokengo

本文链接:https://www.cnblogs.com/hejiale010426/p/17998403

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK