瀏覽代碼

New test for Cache by Eyal Alayuf (Mainsoft)

svn path=/trunk/mcs/; revision=43703
Gonzalo Paniagua Javier 20 年之前
父節點
當前提交
ebf390b7ba

+ 85 - 0
mcs/class/System.Web/Test/System.Web.Caching/CacheStress.cs

@@ -0,0 +1,85 @@
+using System;
+using System.Web.Caching;
+using System.Threading;
+
+/// <summary>
+/// Summary description for Class1.
+/// </summary>
+public class CacheStress
+{
+	static int threads = 0;
+	static int KeyStart = 0;
+	static long SlidingWindow = 0;
+	static bool UseAbsoluteTime = false;
+	static int Modulo = 71;
+	static Cache c;
+	static SafeSum Sum = new SafeSum();
+
+	/// <summary>
+	/// The main entry point for the application.
+	/// </summary>
+	static void Main(string[] args)
+	{
+		if (args.Length < 2) {
+			Console.WriteLine("Usage: CacheStress <#threads> <#millis> [UseAbsoluteTime]");
+			return;
+		}
+		c = new Cache();
+		threads = System.Int32.Parse(args[0]);
+		SlidingWindow = System.Int64.Parse(args[1]);
+		UseAbsoluteTime = (args.Length > 2);
+		for (int i = 0; i < threads; i++) 
+		{
+			Thread th = new Thread(new ThreadStart(RunCycle));
+			th.Start();
+		}
+		int secs = 10;
+		for (int j = secs; ;j += secs) 
+		{
+			Thread.Sleep(1000 * secs);
+			Console.WriteLine("Executed {0} transactions in {1} seconds", Sum.Value, j);
+		}
+	}
+
+	static void RunCycle()
+	{
+		int n = Interlocked.Increment(ref KeyStart);
+		for (int i = 1; ; i++) {
+			try 
+			{
+				string key = "stam" + n;
+				object o2 = c.Get(key);
+				if (o2 == null) 
+				{
+					if (UseAbsoluteTime)
+						c.Insert(key, 1, null, DateTime.Now.AddTicks(SlidingWindow), Cache.NoSlidingExpiration);
+					else
+						c.Insert(key, 1, null, Cache.NoAbsoluteExpiration, new TimeSpan(SlidingWindow));
+				}
+				n = (n * 2 + i) % Modulo;
+			}
+			catch (Exception e) 
+			{
+				Console.WriteLine("Caught exception " + e.GetType().ToString() + ": " + e.Message + e.StackTrace);
+			}
+			if (i == 100) 
+			{
+				Sum.Add(i);
+				i = 0;
+			}
+		}
+	}
+
+	class SafeSum
+	{
+		public SafeSum()
+		{
+			_value = 0;
+		}
+
+		public int Value { get { lock(this) { return _value; } } }
+		public void Add(int i) { lock(this) { _value += i; } }
+
+		private int _value;
+	}
+}

+ 10 - 0
mcs/class/System.Web/Test/System.Web.Caching/readme.txt

@@ -0,0 +1,10 @@
+The CacheStress.cs test is a standalone test that should be compiled and run
+as a console application.
+In normal mode the test prints every 10 seconds the number of transactions it
+committed.
+In case of an exception the test prints the exception and continues. In case
+of a deadlock the transaction count will remain constant.
+Note that the test does not run in .Net on Windows since the System.Web.Caching
+of .Net cannot be used from a console application. Mono's implementation does
+not currently have this dependency. When (and if) it does this test should
+be made into a Web application.