public void PerformanceNewClassTest_TransientV3() { //Configuration var config = new TraceConfig { TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer }; _container.RegisterInstance(new TraceSourceSync(config)); _container.RegisterInterceptor <InterceptorDoNothing>(LifeTime.Transient); _container.Register <IClassVoidTest, ClassVoidTest>(LifeTime.Transient); _container.Intercept <IClassVoidTest>().With <InterceptorDoNothing>(); //Test const int upper = 10_000; var watch = Stopwatch.StartNew(); for (var i = 0; i < upper; i++) { var _ = _container.Resolve <IClassVoidTest>(); } watch.Stop(); //Verify Assert.IsTrue(watch.Elapsed < TimeSpan.FromSeconds(1), $"Trop lent pour créer {upper} objet transient: time = {watch.ElapsedMilliseconds} ms"); }
public async Task InterceptionTestV1() { //Configuration var count = 0; TraceData[] traceReceived = { }; using (var tracer = new TraceSourceSync(new TraceConfig { TraceBehavior = TraceStaticBehavior.AddListenerToStaticAccessAndRemoveDefaultTracer, PageSize = 2 })) { _container.RegisterInstance(tracer); _container.RegisterInterceptor <InterceptorTraceInjectTracer>(LifeTime.Singleton); _container.RegisterInterceptor <InterceptorTraceStatic>(LifeTime.Singleton); _container.Register <IClassVoidTest, ClassVoidTest>(LifeTime.Singleton); _container.Intercept <IClassVoidTest>().With <InterceptorTraceInjectTracer>(); _container.Intercept <IClassVoidTest>().With <InterceptorTraceStatic>(); tracer.OnTracesSent += trace => { traceReceived = trace; Interlocked.Increment(ref count); }; //Test var instance = _container.Resolve <IClassVoidTest>(); instance.MethodCall(); //Verify await tracer.FlushAndCompleteAddingAsync().ConfigureAwait(false); } // un seul retour (avec les deux traces des intercepteurs) Assert.AreEqual(1, count); Assert.IsNotNull(traceReceived.Single(o => o.Message == "IClassVoidTest_InterceptorTraceInjectTracer")); Assert.IsNotNull(traceReceived.Single(o => o.Message == "IClassVoidTest_InterceptorTraceStatic")); }
/// <summary> /// 注册某一类的单例 /// </summary> /// <typeparam name="T">实例类型</typeparam> /// <param name="instance">注册实例</param> /// <param name="nameID">实例ID</param> public static void RegisterInstance <T>(T instance, string nameID = null) { mContainer.RegisterInstance <T>(instance, nameID); }