Skip to content

UseEmfMiddleware middleware allocates a new Stopwatch instance every request #61

@phalvesr

Description

@phalvesr

After taking a closer look at the UseEmfMiddleware - middleware responsable for measuring the time a request takes to be completed - I could notice that it is instanciating a new Stopwatch every request. As it can lead to potential performance issues in scenarios where the api is called hundreds or thousands of times I propose a change to use something like the ValueStopwatch struct used by the dotnet team to measure time without allocating a new object on the heap. I have already implemented those change on the PR #60 - Changes UseEmfMiddleware to avoid allocating a new stopwatch instance per request.

Middleware code as it is now:

public static void UseEmfMiddleware(this IApplicationBuilder app, Func<HttpContext, IMetricsLogger, Task> action)
{
    app.Use(async (context, next) =>
    {
        Stopwatch stopWatch = new Stopwatch();
        stopWatch.Start();
        await next.Invoke();
        var config = context.RequestServices.GetRequiredService<EMF.Config.IConfiguration>();
        var logger = context.RequestServices.GetRequiredService<IMetricsLogger>();

        if (!String.IsNullOrEmpty(config.ServiceName))
        {
            logger.SetNamespace(config.ServiceName);
        }

        await action(context, logger);
        stopWatch.Stop();
        logger.PutMetric("Time", stopWatch.ElapsedMilliseconds, Model.Unit.MILLISECONDS);
    });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions