autofac / Autofac.Extras.DynamicProxy

Interceptor and decorator support for Autofac IoC via Castle DynamicProxy
MIT License
106 stars 33 forks source link

The execution time of the EnableClassInterceptors method is gradually increasing. #36

Closed maliming closed 4 years ago

maliming commented 4 years ago

TargetFramework: netcoreapp3.1

Autofac.Extensions.DependencyInjection version 6.0.0 Autofac.Extras.DynamicProxy version 5.0.0

The steps to reproduce the problem are:

The source code of the project that can reproduce the problem: https://github.com/maliming/ClassInterceptorTest

Create a simple mvc project and add a lot of controllers.

Controller.cs

public abstract class MyControllerBase : Controller
{
}

[Route("api/testapi/test0api")]
public class Test0ControllerBase : MyControllerBase
{
    [HttpGet]
    [Route("Get")]
    public string TestMethod()
    {
        return "test";
    }
}

[Route("api/testapi/test1api")]
public class Test1ControllerBase : MyControllerBase
{
    [HttpGet]
    [Route("Get")]
    public string TestMethod()
    {
        return "test";
    }
}

//Test100ControllerBase 

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        var host = Host.CreateDefaultBuilder(args)
            .UseServiceProviderFactory(new AutofacServiceProviderFactory())
            .ConfigureWebHostDefaults(webHostBuilder =>
            {
                webHostBuilder.UseStartup<Startup>();
            })
            .Build();

        host.Run();
    }
}

Startup.cs

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers();
    }

    public void ConfigureContainer(ContainerBuilder builder)
    {
        foreach (var type in Assembly.GetExecutingAssembly().GetTypes()
        .Where(x => typeof(MyControllerBase).IsAssignableFrom(x)))
        {
            var stopwatch = Stopwatch.StartNew();

            builder.RegisterType(type)
                .As(type)
                .EnableClassInterceptors();

            stopwatch.Stop();
            Console.WriteLine(type.Name + " => ElapsedMilliseconds:" + stopwatch.ElapsedMilliseconds);
        }
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
    }
}

My code enables class interception for each controller.

builder.RegisterType(type)
        .As(type)
        .EnableClassInterceptors();

Ideally, the execution time of the EnableClassInterceptorsmethod should be about the same.

But the result is that time is gradually increasing.

Console output:

MyControllerBase => ElapsedMilliseconds:147
Test0ControllerBase => ElapsedMilliseconds:73
Test1ControllerBase => ElapsedMilliseconds:83
Test2ControllerBase => ElapsedMilliseconds:130
Test3ControllerBase => ElapsedMilliseconds:156
Test4ControllerBase => ElapsedMilliseconds:174
Test5ControllerBase => ElapsedMilliseconds:197
Test6ControllerBase => ElapsedMilliseconds:224
Test7ControllerBase => ElapsedMilliseconds:243
Test8ControllerBase => ElapsedMilliseconds:286
Test9ControllerBase => ElapsedMilliseconds:293
Test10ControllerBase => ElapsedMilliseconds:319
Test11ControllerBase => ElapsedMilliseconds:341
Test12ControllerBase => ElapsedMilliseconds:374
Test13ControllerBase => ElapsedMilliseconds:399
Test14ControllerBase => ElapsedMilliseconds:415
Test15ControllerBase => ElapsedMilliseconds:447
Test16ControllerBase => ElapsedMilliseconds:484
Test17ControllerBase => ElapsedMilliseconds:496
Test18ControllerBase => ElapsedMilliseconds:524
Test19ControllerBase => ElapsedMilliseconds:552
Test20ControllerBase => ElapsedMilliseconds:570
Test21ControllerBase => ElapsedMilliseconds:642
Test22ControllerBase => ElapsedMilliseconds:635
Test23ControllerBase => ElapsedMilliseconds:658
Test24ControllerBase => ElapsedMilliseconds:677
Test26ControllerBase => ElapsedMilliseconds:787
Test27ControllerBase => ElapsedMilliseconds:792
Test28ControllerBase => ElapsedMilliseconds:811
Test29ControllerBase => ElapsedMilliseconds:831
Test30ControllerBase => ElapsedMilliseconds:848
Test31ControllerBase => ElapsedMilliseconds:912
Test32ControllerBase => ElapsedMilliseconds:917
Test33ControllerBase => ElapsedMilliseconds:945
Test34ControllerBase => ElapsedMilliseconds:973
Test35ControllerBase => ElapsedMilliseconds:1028
Test36ControllerBase => ElapsedMilliseconds:1024
Test37ControllerBase => ElapsedMilliseconds:1056
Test38ControllerBase => ElapsedMilliseconds:1082
Test39ControllerBase => ElapsedMilliseconds:1107
Test40ControllerBase => ElapsedMilliseconds:1132
Test41ControllerBase => ElapsedMilliseconds:1195
Test42ControllerBase => ElapsedMilliseconds:1215
Test43ControllerBase => ElapsedMilliseconds:1236
maliming commented 4 years ago

This seems to be a problem with castle https://github.com/castleproject/Core/issues/486

maliming commented 4 years ago

Closed by https://github.com/castleproject/Core/issues/486#issuecomment-603502736