public void TestMultipleMemberAccessorFactory() { Property prop = new Property(); IGetAccessor accessor1 = factoryGet.CreateGetAccessor(typeof(Property), "Int"); IGetAccessorFactory factory2 = new GetAccessorFactory(true); IGetAccessor accessor2 = factory2.CreateGetAccessor(typeof(Property), "Int"); Assert.AreEqual(int.MinValue, accessor1.Get(prop)); Assert.AreEqual(int.MinValue, accessor2.Get(prop)); }
public void TestGetIntegerPerformance() { const int TEST_ITERATIONS = 1000000; Property prop = new Property(); int test = -1; Timer timer = new Timer(); #region Direct access (fastest) GC.Collect(); GC.WaitForPendingFinalizers(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; test = prop.Int; Assert.AreEqual(int.MinValue, test); } timer.Stop(); double directAccessDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); #endregion #region IL Property accessor GC.Collect(); GC.WaitForPendingFinalizers(); IGetAccessorFactory factory = new GetAccessorFactory(true); IGetAccessor propertyAccessor = factory.CreateGetAccessor(typeof(Property), "Int"); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; test = (int)propertyAccessor.Get(prop); Assert.AreEqual(int.MinValue, test); } timer.Stop(); double propertyAccessorDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); double propertyAccessorRatio = propertyAccessorDuration / directAccessDuration; #endregion #region IBatisNet.Common.Utilities.Object.ReflectionInfo GC.Collect(); GC.WaitForPendingFinalizers(); ReflectionInfo reflectionInfo = ReflectionInfo.GetInstance(prop.GetType()); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; PropertyInfo propertyInfo = (PropertyInfo)reflectionInfo.GetGetter("Int"); test = (int)propertyInfo.GetValue(prop, null); Assert.AreEqual(int.MinValue, test); } timer.Stop(); double reflectionInfoDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); double reflectionInfoRatio = (float)reflectionInfoDuration / directAccessDuration; #endregion #region Reflection GC.Collect(); GC.WaitForPendingFinalizers(); Type type = prop.GetType(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; PropertyInfo propertyInfo = type.GetProperty("Int", BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance); test = (int)propertyInfo.GetValue(prop, null); Assert.AreEqual(int.MinValue, test); } timer.Stop(); double reflectionDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); double reflectionRatio = reflectionDuration / directAccessDuration; #endregion #region ReflectionInvokeMember (slowest) GC.Collect(); GC.WaitForPendingFinalizers(); timer.Start(); for (int i = 0; i < TEST_ITERATIONS; i++) { test = -1; test = (int)type.InvokeMember("Int", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance, null, prop, null); Assert.AreEqual(int.MinValue, test); } timer.Stop(); double reflectionInvokeMemberDuration = 1000000 * (timer.Duration / (double)TEST_ITERATIONS); double reflectionInvokeMemberRatio = reflectionInvokeMemberDuration / directAccessDuration; #endregion // Print results Console.WriteLine("{0} property gets on integer...", TEST_ITERATIONS); Console.WriteLine("Direct access: \t\t{0} ", directAccessDuration.ToString("F3")); Console.WriteLine("IMemberAccessor: \t\t{0} Ratio: {1}", propertyAccessorDuration.ToString("F3"), propertyAccessorRatio.ToString("F3")); Console.WriteLine("IBatisNet ReflectionInfo: \t{0} Ratio: {1}", reflectionInfoDuration.ToString("F3"), reflectionInfoRatio.ToString("F3")); Console.WriteLine("ReflectionInvokeMember: \t{0} Ratio: {1}", reflectionInvokeMemberDuration.ToString("F3"), reflectionInvokeMemberRatio.ToString("F3")); Console.WriteLine("Reflection: \t\t\t{0} Ratio: {1}", reflectionDuration.ToString("F3"), reflectionRatio.ToString("F3")); }