Performance Impact of Using Spring.NET Dependency Injection
Spring.NET provides Dependency Injection (DI)/Inversion of Control (IoC) as well as several other capabilities including Data, Messaging, etc. While DI provides a lot of flexibility, it comes at a cost. There is some added complexity; however, the real cost is performance.
To test the performance impact of Spring.NET DI, we need to create a series of interfaces and concrete classes that implement the interfaces. Then we will create two console apps to run the tests. The first console application will be a standard C# .NET console app without Spring.Net performing native functions. The second console application will perform the same set of functions, but using Spring.NET and DI. Within these two application we will add a series of timing functions to keep track of how long each tasks takes to run.
Interfaces
Starting off we need to create the interfaces we will need to use in the project as listed below:
iSimpleMath
public interface iSimpleMath
{
double Add(double a, double b);
double Subtract(double a, double b);
}
iComplexMath
public interface iComplexMath
{
double Multiply(double a, double b);
double Divide(double a, double b);
}
iCalculate
interface iCalculate
{
double Calculate(double starting);
}
These interfaces are extremely simple and the concrete classes are just as simple.
Concrete Classes
For these tests, we need to create a couple of interfaces/classes which we can use to judge the performance impact. Next we have the concrete classes used to implement the interfaces.
ConcreteSimpleMath
class ConcreteSimpleMath : Interfaces.iSimpleMath
{
public double Add(double a, double b)
{
return a + b;
}
public double Subtract(double a, double b)
{
return a - b;
}
}
ConcreteComplexMath
class ConcreteComplexMath : Interfaces.iComplexMath
{
public double Multiply(double a, double b)
{
return a * b;
}
public double Divide(double a, double b)
{
return a / b;
}
}
ConcreteCalculate
class ConcreteCalculate : Interfaces.iCalculate
{
private Interfaces.iSimpleMath simpleMath;
private Interfaces.iComplexMath complexMath;
public ConcreteCalculate(Interfaces.iSimpleMath sMath, Interfaces.iComplexMath cMath)
{
simpleMath = sMath;
complexMath = cMath;
}
public double Calculate(double starting)
{
starting = simpleMath.Add(starting, 15);
starting = complexMath.Multiply(starting, 30);
starting = simpleMath.Subtract(starting, 5);
starting = complexMath.Divide(starting, 2);
return starting;
}
}
The first two classes ConcreteSimpleMath and
ConcreteComplexMath provide a simple implementation
of the corresponding interface. The
ConcreteCalculate class does implement the
iCalculate interface, but it also a constructor accepting
members for the iSimpleMath and
iComplexMath interfaces. The Calculate
method does perform 4 calls to the simple/complex math
functions; however, the calls are not really important as the
point is to perform a minimal amount of work.
Now that we have a series of class we can create the application. First we can create the native .NET application. as listed below to give us some base timing information of native .NET.
