gpt4 book ai didi

与在本地主机中运行时相比,Azure Function 的 HTTP 请求速度较慢

转载 作者:行者123 更新时间:2023-12-02 05:56:14 25 4
gpt4 key购买 nike

我正在使用隔离的 Azure Function .Net 6。

当我从 VS2022 在 localhost 上运行该函数时,速度比我将其部署到 Azure Function 时快 5 倍。本地主机是在 Azure 中与该函数位于同一区域中托管的 VM。我尝试了不同的服务计划,但问题仍然存在。 (消费计划、Elastic Premium EP3、Premium V2 P3v2)

The same code running in localhost Vs. Azure

不同区域与本地主机的结果: enter image description here

代码如下:

DI - 使用 IHttpClientFactory (here):

public static class DataSourceServiceRegistration
{
public static IServiceCollection RegisterDataSourceServices(this IServiceCollection serviceCollection)
{
serviceCollection.AddHttpClient();
return serviceCollection;
}
}

HttpClient 用法:

private readonly HttpClient _httpClient;

public EsriHttpClientAdapter(HttpClient httpClient)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}

public async Task<JsonDocument> SendPrintServiceMessage(string url, HttpMethod httpMethod, string referer, IEnumerable<KeyValuePair<string, string>> content = null)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
HttpContent httpContent = null;
if (content != null)
{
httpContent = new FormUrlEncodedContent(content);
}
var msg = new HttpRequestMessage(httpMethod, url) { Content = httpContent };
_httpClient.DefaultRequestHeaders.Referrer = new Uri(referer);
_httpClient.DefaultRequestHeaders.Add("some", "config");

_logger.LogInformation($"Before SendAsync - time {watch.ElapsedMilliseconds}");
var result = await _httpClient.SendAsync(msg);
_logger.LogInformation($"After SendAsync - time {watch.ElapsedMilliseconds}");
var response = await result.Content.ReadAsStringAsync();
_logger.LogInformation($"After ReadAsStringAsync - time {watch.ElapsedMilliseconds}");

if (result.StatusCode == HttpStatusCode.OK)
{
//do some stuff here
}
}

应用洞察如下:

azure :

enter image description here

本地主机:

enter image description here

最佳答案

不确定这是否适用于您,但希望对您有所帮助。如果您在基本(消耗)计划下运行,您的函数将始终处于冷状态,并且在被 Http 触发器调用时需要启动。为了避免这种情况,如果使用应用服务环境、专用或高级计划,您可以将该功能设置为“始终开启”(如果这在您的预算和范围内)。 (换句话说,自由函数总是会冷淡。)

您可以在“配置”>“常规设置”>“始终开启”下更改此设置。

有关函数如何通过冷启动运行的详细信息,请访问:

https://azure.microsoft.com/en-us/blog/understanding-serverless-cold-start/

关于与在本地主机中运行时相比,Azure Function 的 HTTP 请求速度较慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71411842/

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