TypedFactory vs Performance Monitor

时间:2012-11-23 11:03:41

标签: castle-windsor performancecounter typed-factory-facility

我对新的windsor3 perfmonace计数器如何跟踪通过TyepedFactory生成的对象感到困惑。

考虑以下情景

public interface IBFactory
{
    IB[] GetAll();
    void FreeUp(IB cmps);
}

public class B1 : IB, IDisposable
{
    public void Add(int i){}

    public void Dispose()
    {
        Console.WriteLine("Disposing " + GetType().Name);
    }
}

public class B2 : IB, IDisposable
{
    public void Add(int i){}

    public void Dispose()
    {
        Console.WriteLine("Disposing " + GetType().Name);
    }
}

public class B3 : IB
{
    public void Add(int i){}

    public void Dispose()
    {
        Console.WriteLine("Disposing " + GetType().Name);
    }
}


 var container = new WindsorContainer();

 var diagnostic = LifecycledComponentsReleasePolicy.GetTrackedComponentsDiagnostic(container.Kernel);
 var counter = LifecycledComponentsReleasePolicy.GetTrackedComponentsPerformanceCounter(new PerformanceMetricsFactory());
 container.Kernel.ReleasePolicy = new LifecycledComponentsReleasePolicy(diagnostic, counter);

 Console.WriteLine("Enter number of iterations:");
 int iterations = int.Parse(Console.ReadLine());

 container.AddFacility<TypedFactoryFacility>();
 container.Register
 (
    Component.For<IBFactory>()
        .AsFactory()
        .LifeStyle.Transient,

    Classes.FromAssemblyContaining<IB>()
        .BasedOn(typeof(IB))
        .WithService.Base()
        .Configure(c => c.LifestyleTransient())
 );

 Console.WriteLine("Create Memory Leak Y or N?");
 var leak = Console.ReadLine().ToUpper() == "Y";


 var sleepFor = 100;// int.Parse(Console.ReadLine());

 for (var i = 1; i < iterations+1; i++)
 {
    var factory = container.Resolve<IBFactory>();
    Console.WriteLine("Factory created.");

    var cmp = factory.GetAll();
    foreach (var b in cmp)
    {
        b.Add(i);
    }

    Console.WriteLine("Iteration {0} completed", i);

    Thread.Sleep(sleepFor);

    if (!leak)
    {
        foreach (var b in cmp)
        {
            factory.FreeUp(b);
        }
    }

    Console.WriteLine("Releasing factory.");
    container.Release(factory);     
 }

 Console.WriteLine("container disposing.....");
 container.Dispose();
 Console.WriteLine("container disposed");
 Console.ReadLine();

如果我通过FreeUp工厂方法处理对象,则perf计数器会显示预期的跟踪。

相反,如果我没有明确处置对象,但是如果我将隐式处理工厂,为了测试目的而创建为瞬态,则在我处置工厂时(根据文档)处理IB实例,但是perf计数器不会得到更新并显示IB实例仍然被跟踪...

这意味着什么? Perf计数器尚未更新或对象仍然被跟踪(这将是非常可怕的),即使由于工厂处置已在IB实例上调用Dispose。

0 个答案:

没有答案