August 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Performance Impact of Using Spring.NET Dependency Injection

  • August 10, 2009
  • By Chris Bennett
  • Send Email »
  • More Articles »

.NET Test Application

  static void Main(string[] args) 
  { 
     Console.WriteLine("Testing .Net"); 
     Console.WriteLine("Running Creating Objects"); 
     DateTime start = DateTime.Now; 
  
     for (double i = 0; i < 100000; i++) 
     { 
        Interfaces.iCalculate cal = new Concrete.ConcreteCalculate(new Concrete.ConcreteSimpleMath(), new Concrete.ConcreteComplexMath()); 
        cal.Calculate(i); 
     } 
  
     Console.WriteLine("Done."); 
     Console.WriteLine(DateTime.Now.Subtract(start).TotalMilliseconds.ToString()+"ms"); 
     Console.WriteLine(); 
  
     Console.WriteLine("Running Use Single Object"); 
     start = DateTime.Now; 
     Interfaces.iCalculate cal2 = new Concrete.ConcreteCalculate(new Concrete.ConcreteSimpleMath(), new Concrete.ConcreteComplexMath()); 
  
     for (double i = 0; i < 100000; i++) 
     { 
        cal2.Calculate(i); 
     }
   
     Console.WriteLine("Done"); 
     Console.WriteLine(DateTime.Now.Subtract(start).TotalMilliseconds.ToString() + "ms"); 
     Console.ReadLine(); 
  }

This application performs two tests, first it will time how long it takes collectively to create a new CalculateObject, SimpleMath, ComplexMath and Perform the calculation 100,000 times. Then it will run the same test and use only a single set of objects to perform the calculation on. At the conclusion of each test it will display the time the test took in milliseconds. After running the application you should see something similar to the image below:


Figure 1 - Testing .NET Application Results

Depending on your machine configuration the actual timing information will vary. However, its important to note that the time in miliseconds is mainly intended to be used as a comparison point between native .NET and Spring.NET.

Testing Spring.NET

In testing the Spring.NET application we first need to create the spring configuration in the app.config. I've included the snippet from the config including this section.

  <spring> 
     <context> 
        <resource uri="config://spring/objects" /> 
     </context> 
     <objects xmlns="http://www.springframework.net"> 
        <object name="SimpleMath" 
              type="TestingSpringNet.Concrete.ConcreteSimpleMath,TestingSpringNet" 
              singleton="false" />
  
        <object name="ComplexMath" 
              type="TestingSpringNet.Concrete.ConcreteComplexMath,TestingSpringNet" 
              singleton="false" />
  
        <object name="ConcreteCalculate" 
              type="TestingSpringNet.Concrete.ConcreteCalculate,TestingSpringNet" 
              singleton="false">
              <constructor-arg name="sMath" ref="SimpleMath" /> 
              <constructor-arg name="cMath" ref="ComplexMath" /> 
        </object> 
  
        <object name="SimpleMathSingleton" 
              type="TestingSpringNet.Concrete.ConcreteSimpleMath,TestingSpringNet" 
              singleton="true" /> 
  
        <object name="ComplexMathSingleton" 
              type="TestingSpringNet.Concrete.ConcreteComplexMath,TestingSpringNet" 
              singleton="true" /> 
  
        <object name="ConcreteCalculateUsingSingletons" 
              type="TestingSpringNet.Concrete.ConcreteCalculate,TestingSpringNet" 
              singleton="false"> 
              <constructor-arg name="sMath" ref="SimpleMathSingleton" /> 
              <constructor-arg name="cMath" ref="ComplexMathSingleton" /> 
        </object> 
  
        <object name="ConcreteCalculateSingleton" 
              type="TestingSpringNet.Concrete.ConcreteCalculate,TestingSpringNet" 
              singleton="true"> 
              <constructor-arg name="sMath" ref="SimpleMathSingleton" /> 
              <constructor-arg name="cMath" ref="ComplexMathSingleton" /> 
        </object> 
  
     </objects> 
  </spring> 

This context defines not only SimpleMath, ComplexMath and Calculate objects for testing, it also defines several others which are used to test the timing of varying levels of injection. Next, we can jump into the testing Spring.NET application.

  static void Main(string[] args) 
  { 
     using (IApplicationContext ctx = ContextRegistry.GetContext()) 
     { 
        Console.WriteLine("Testing Spring.Net"); 
        Console.WriteLine("Running Creating Objects"); 
        DateTime start = DateTime.Now; 
  
        for (double i=0; i < 100000; i++) 
        { 
           // Get a new instance of the object 
           Interfaces.iCalculate cal = (Interfaces.iCalculate)ctx.GetObject("ConcreteCalculate"); 
           cal.Calculate(i); 
        } 
  
        Console.WriteLine("Done"); 
        Console.WriteLine(DateTime.Now.Subtract(start).TotalMilliseconds.ToString() +"ms"); 
        Console.WriteLine(); 
  
        Console.WriteLine("Running Use Single Object"); 
        start = DateTime.Now; 
        Interfaces.iCalculate cal2 = (Interfaces.iCalculate)ctx.GetObject("ConcreteCalculate"); 
  
        for (double i = 0; i < 100000; i++) 
        { 
           cal2.Calculate(i);    
        } 
  
        Console.WriteLine("Done"); 
        Console.WriteLine(DateTime.Now.Subtract(start).TotalMilliseconds.ToString() + "ms"); 
        Console.WriteLine(); 
  
        Console.WriteLine("Running using Singletons"); 
        start = DateTime.Now; 
  
        for (double i = 0; i < 100000; i++) 
        { 
           Interfaces.iCalculate cal3 = (Interfaces.iCalculate)ctx.GetObject("ConcreteCalculateUsingSingletons"); 
           cal3.Calculate(i); 
        } 
  
        Console.WriteLine("Done"); 
        Console.WriteLine(DateTime.Now.Subtract(start).TotalMilliseconds.ToString() + "ms"); 
        Console.WriteLine(); 
  
        Console.WriteLine("Running using only Singletons"); 
        start = DateTime.Now;
   
        for (double i = 0; i < 100000; i++) 
        { 
           Interfaces.iCalculate cal4 = (Interfaces.iCalculate)ctx.GetObject("ConcreteCalculateSingleton"); 
           cal4.Calculate(i); 
        } 
  
        Console.WriteLine("Done"); 
        Console.WriteLine(DateTime.Now.Subtract(start).TotalMilliseconds.ToString() + "ms"); 
        Console.ReadLine(); 
     } 
  }





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel