gpt4 book ai didi

c# - 如何提高 .NET Core Web API 的性能?

转载 作者:行者123 更新时间:2023-11-30 17:34:58 29 4
gpt4 key购买 nike

我正在尝试创建一个能够处理 10K+ 请求/秒的 Web API 应用程序。

我创建了一个空的 .NET Core Web API 项目,其中只有一个 GET 操作,它只返回一个字符串。我将自包含应用程序发布到 Debian8 服务器。

dotnet publish -r debian.8-x64 -c Release

然后我使用 Apache Benchmark 使用以下选项运行负载测试。

ab -n 50000 -c -k 200 localhost:5000/api/cnt/dummy

测试结果是4.5K req/sec。

有什么方法可以提高我的 Web API 应用程序的性能吗?

我的 project.json 文件:

{
"dependencies": {
"Microsoft.NETCore.App": {
"version": "1.0.1"
},
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.AspNetCore.ResponseCompression": "1.0.0"

},

"tools": {
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},

"frameworks": {
"netcoreapp1.1": {
"imports": [
"dnxcore50",
"portable-net45+win8"
]
}
},

"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},

"runtimeOptions": {
"configProperties": {
"System.GC.Server": true,
"System.GC.Concurrent": true
}
},

"runtimes": {
"win10-x64": {},
"debian.8-x64": {}
},

"publishOptions": {
"include": [
"wwwroot",
"**/*.cshtml",
"appsettings.json",
"web.config"
]
},

"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}

我的 Startup.cs 文件:

public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

if (env.IsEnvironment("Development"))
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}

builder.AddEnvironmentVariables();
Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);

services.AddMvc();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

app.UseApplicationInsightsRequestTelemetry();

app.UseApplicationInsightsExceptionTelemetry();

app.UseMvc();
}
}

最佳答案

根据我们的经验,ab 不会让您达到这个数字。您需要另一个负载测试工具,例如 https://github.com/wg/wrk .其他一些也可以。

您仍然可以尝试同时使用多个 ab 进程。然后在特定时间使用特定数量的客户端。例如 -c 20 -t 30 -k

关于c# - 如何提高 .NET Core Web API 的性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41760875/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com