Azure / Azure/Azure-Functions

Azure Function slow HTTP requests compared to when running in localhost

Open
#2,180 0 comments 0 reactions 2 assignees Claimed by @jeffhollan View on GitHub
premium-plan
Dominant language
PowerShell
Stars
1.1k
Forks
215
Avg merge
4h 2m
Merged PRs (30d)
1

Description

I'm using a Azure Function .Net 6 Isolated.

When I run the function on localhost from VS2022, it is up to 5 times faster then when I deploy it to Azure Function. Localhost is a VM hosted in Azure in the same region as the function. I tried different Service Plans, but issue remains. (Consumption Plan, Elastic Premium EP3, Premium V2 P3v2).

The same code running in localhost Vs. Azure
LLoXd

Results in different regions vs. localhost:
![Region_Central-US-Test](https://user-images.githubusercontent.com/101572840/158197135-3739f7ae-ebad-4f92-aa94-c7b9e5082765.png)

This behaves exactly the same in my student subscription.

The code is as follows:

DI - using the IHttpClientFactory (here):

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

```

HttpClient usage:

```
private readonly HttpClient _httpClient;

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

public async Task SendPrintServiceMessage(string url, HttpMethod httpMethod, string referer, IEnumerable> 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
}
}
```
Application Insights is as follows:

AZURE hosted:
![Applicationinsights_Azure](https://user-images.githubusercontent.com/101572840/158197407-f0bf74fb-dbef-4855-a825-90b2fdf3b62e.png)

Localhost:
![Applicationinsights_Localhost](https://user-images.githubusercontent.com/101572840/158197441-2b17fe0d-5de8-46e7-b612-fe4f1c8723f7.png)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.