.Net内建缓存测试代码HttpRuntime.Cache protected void Page_Load(object sender, EventArgs e) { int start = 200; int runs = 10000;
string keyBase = "testKey"; string obj = "This is a test of an object blah blah es, serialization does not seem to slow things down so much. The gzip compression is horrible horrible performance, so we only use it for very large objects. I have not done any heavy benchmarking recently";
long begin = DateTime.Now.Ticks; for(int i = start; i < start+runs; i++) { HttpRuntime.Cache.Add(keyBase + i, obj,null,System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(1),System.Web.Caching.CacheItemPriority.Normal,null); } long end = DateTime.Now.Ticks; long time = end - begin;
Response.Write(runs + " sets: " + new TimeSpan(time).ToString() + "ms<br />"); begin = DateTime.Now.Ticks; int hits = 0; int misses = 0; for(int i = start; i < start+runs; i++) { string str = (string) HttpRuntime.Cache.Get(keyBase + i); if(str != null) ++hits; else ++misses; } end = DateTime.Now.Ticks; time = end - begin;
Response.Write(runs + " gets: " + new TimeSpan(time).ToString() + "ms"); } Memcached测试代码Memcached namespace Memcached.MemcachedBench { using System; using System.Collections; using Memcached.ClientLibrary; public class MemcachedBench { [STAThread] public static void Main(String[] args) { int runs = 100; int start = 200; if(args.Length > 1) { runs = int.Parse(args[0]); start = int.Parse(args[1]); }
MemcachedClient mc = new MemcachedClient(); mc.EnableCompression = false;
string keyBase = "testKey"; string obj = "This is a test of an object blah blah es, serialization does not seem to slow things down so much. The gzip compression is horrible horrible performance, so we only use it for very large objects. I have not done any heavy benchmarking recently";
long begin = DateTime.Now.Ticks; for(int i = start; i <start+runs; i++) { mc.Set(keyBase + i, obj); } long end = DateTime.Now.Ticks; long time = end - begin;
Console.WriteLine(runs + " sets: " + new TimeSpan(time).ToString() + "ms");
begin = DateTime.Now.Ticks; int hits = 0; int misses = 0; for(int i = start; i <start+runs; i++) { string str = (string) mc.Get(keyBase + i); if(str != null) ++hits; else ++misses; } end = DateTime.Now.Ticks; time = end - begin; Console.WriteLine(runs + " gets: " + new TimeSpan(time).ToString() + "ms"); Console.WriteLine("Cache hits: " + hits.ToString()); Console.WriteLine("Cache misses: " + misses.ToString());