Click here to Skip to main content
15,896,207 members
Articles / Programming Languages / C#

Dynamic/Transparent Proxy using DynamicProxy.NET

Rate me:
Please Sign up or sign in to vote.
4.90/5 (19 votes)
21 Oct 20039 min read 209.4K   3.9K   78  
Learn how easy it is to create Dynamic/Transparent proxies in .NET using DynamicProxy.NET
using System;
using System.Collections;
using System.Threading;

namespace DynamicProxyTutorial
{
	/// <summary>
	/// Proxy Performance tester
	/// </summary>
	public class Performance
	{
		public int instancesToTest = 1000000;
		public int gcWaitTime = 5000;
		public double milliSecondsToCreateHandProxy;
		public double milliSecondsToCreateDynamicProxy;
		public double milliSecondsToCallGetContentHandProxy;
		public double milliSecondsToCallGetContentDynamicProxy;

		public Performance() {
		}

		public void test() {
			testHandProxyInstances();
			testDynamicProxyInstances();
			testHandProxyInvocations();
			testDynamicProxyInvocations();
		}

		private void prepareForTest() {
			// Run the GC and wait for it to (hopefully) be done
			System.GC.Collect();
			Thread.Sleep(gcWaitTime);
		}

		private void testHandProxyInstances() {
			prepareForTest();

			DateTime startTime = DateTime.Now;
			for (int count = 0; count < instancesToTest; count++) {
				IFileViewer fileViewer = FileViewerProxyFactory.createProxy("", "", false);
				fileViewer = null;
			}
			DateTime endTime = DateTime.Now;
			TimeSpan diff = endTime.Subtract(startTime);
			milliSecondsToCreateHandProxy = timeDiff(startTime, endTime);
		}

		private void testDynamicProxyInstances() {
			prepareForTest();

			DateTime startTime = DateTime.Now;
			for (int count = 0; count < instancesToTest; count++) {
				IFileViewer fileViewer = FileViewerProxyFactory.createProxy("", "", true);
				fileViewer = null;
			}
			DateTime endTime = DateTime.Now;
			TimeSpan diff = endTime.Subtract(startTime);
			milliSecondsToCreateDynamicProxy = timeDiff(startTime, endTime);
		}

		private void testHandProxyInvocations() {
			prepareForTest();

			DateTime startTime = DateTime.Now;
			IFileViewer fileViewer = FileViewerProxyFactory.createProxy("", "", false);
			String valueHolder;
			for (int count = 0; count < instancesToTest; count++) {
				valueHolder = fileViewer.Content;
			}
			DateTime endTime = DateTime.Now;
			TimeSpan diff = endTime.Subtract(startTime);
			milliSecondsToCallGetContentHandProxy = timeDiff(startTime, endTime);
		}

		private void testDynamicProxyInvocations() {
			prepareForTest();

			DateTime startTime = DateTime.Now;
			IFileViewer fileViewer = FileViewerProxyFactory.createProxy("", "", true);
			String valueHolder;
			for (int count = 0; count < instancesToTest; count++) {
				valueHolder = fileViewer.Content;
			}
			DateTime endTime = DateTime.Now;
			TimeSpan diff = endTime.Subtract(startTime);
			milliSecondsToCallGetContentDynamicProxy = timeDiff(startTime, endTime);
		}

		private double timeDiff(DateTime startTime, DateTime endTime) {
			TimeSpan diff = endTime.Subtract(startTime);
			return (double)((double)(diff.Hours*60*60*1000 +  diff.Minutes*60*1000 + diff.Seconds*1000 + diff.Milliseconds))/(double)instancesToTest;
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Denmark Denmark
10 years experience in software design. In my job as a software architect I work with Java and J2EE. .net is so far only a hobby project, but nonetheless interesting Wink | ;-)

Comments and Discussions