ThreeMammals / Ocelot

.NET API Gateway
https://www.nuget.org/packages/Ocelot
MIT License
8.32k stars 1.63k forks source link

A lot of useless log.Debug in Ocelot #1744

Closed RaynaldM closed 10 months ago

RaynaldM commented 11 months ago

Discussed in https://github.com/ThreeMammals/Ocelot/discussions/1736

Originally posted by **RaynaldM** October 16, 2023 We're looking to speed up Ocelot a bit, and gain a few extra requests/second. Using the benchmark, we ran a small test on [DownstreamRouteFinderMiddlewareBenchmarks](/search?q=repo%3AThreeMammals%2FOcelot%20DownstreamRouteFinderMiddlewareBenchmarks&type=code) In [DownstreamRouteFinderMiddleware](/search?q=repo%3AThreeMammals%2FOcelot+DownstreamRouteFinderMiddleware&type=code), we put the `log.Debug` in `#if DEBUG` like this: ```csharp public async Task Invoke(HttpContext httpContext) { var upstreamUrlPath = httpContext.Request.Path.ToString(); var upstreamQueryString = httpContext.Request.QueryString.ToString(); var hostHeader = httpContext.Request.Headers["Host"].ToString(); var upstreamHost = hostHeader.Contains(':') ? hostHeader.Split(':')[0] : hostHeader; #if DEBUG Logger.LogDebug($"Upstream url path is {upstreamUrlPath}"); #endif var internalConfiguration = httpContext.Items.IInternalConfiguration(); var provider = _factory.Get(internalConfiguration); var response = provider.Get(upstreamUrlPath, upstreamQueryString, httpContext.Request.Method, internalConfiguration, upstreamHost); if (response.IsError) { Logger.LogWarning($"{MiddlewareName} setting pipeline errors. IDownstreamRouteFinder returned {response.Errors.ToErrorString()}"); httpContext.Items.UpsertErrors(response.Errors); return; } #if DEBUG var downstreamPathTemplates = string.Join(", ", response.Data.Route.DownstreamRoute.Select(r => r.DownstreamPathTemplate.Value)); Logger.LogDebug($"downstream templates are {downstreamPathTemplates}"); #endif // why set both of these on HttpContext httpContext.Items.UpsertTemplatePlaceholderNameAndValues(response.Data.TemplatePlaceholderNameAndValues); httpContext.Items.UpsertDownstreamRoute(response.Data); await _next.Invoke(httpContext); } ``` and the result was surprising: With `log.Debug` ``` | Method | Mean | Error | StdDev | StdErr | Min | Q1 | Median | Q3 | Max | Op/s | Ratio | Gen 0 | Allocated | |--------- |---------:|----------:|----------:|----------:|---------:|---------:|---------:|---------:|---------:|----------:|------:|-------:|----------:| | Baseline | 1.232 us | 0.1523 us | 0.0236 us | 0.0118 us | 1.206 us | 1.217 us | 1.232 us | 1.248 us | 1.258 us | 811,506.6 | 1.00 | 0.3033 | 4 KB | ``` without ``` | Method | Mean | Error | StdDev | StdErr | Min | Q1 | Median | Q3 | Max | Op/s | Ratio | Gen 0 | Allocated | |--------- |---------:|---------:|--------:|--------:|---------:|---------:|---------:|---------:|---------:|------------:|------:|-------:|----------:| | Baseline | 890.6 ns | 28.16 ns | 4.36 ns | 2.18 ns | 884.2 ns | 889.7 ns | 892.4 ns | 893.3 ns | 893.4 ns | 1,122,855.0 | 1.00 | 0.2213 | 3 KB | ``` We gained around 300 request/s, i.e. a 38% improvement - not bad, eh? I wonder if we couldn't do the same for others, or even all of them. What do you think?
raman-m commented 11 months ago

@ggnaegi It is accepted and assigned to you! 😉 Sorry, cannot assign contributors directly until you'll write in this thread.

ggnaegi commented 11 months ago

@ggnaegi It is accepted and assigned to you! 😉 Sorry, cannot assign contributors directly until you'll write in this thread.

Hello @raman-m there I am!