// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Threading; using DBCache.DAL; namespace DBCache.PerfTest { public class Setup { internal const int NREC = 10000; // all work done in Java } public class Program { private const int NIT = 5; private const int REP = 10; private const int N = REP * Setup.NREC; private const int NTHREADS = 10; private static readonly Random rng = new Random(); public static void Test(string lbl, IDAL dal) { Console.WriteLine(lbl + ":"); for(int k = 0; k < NIT; k++) { DateTime dt1 = DateTime.Now; Thread[] t = new Thread[NTHREADS]; for(int j = 0; j < t.Length; j++) { t[j] = new Thread(() => { for(int i = 0; i < N; i++) { int id = rng.Next(Setup.NREC) + 1; Big o = dal.GetBig(id); if(o.Id != id) throw new Exception("Ooops"); } }); } for(int j = 0; j < t.Length; j++) t[j].Start(); for(int j = 0; j < t.Length; j++) t[j].Join(); DateTime dt2 = DateTime.Now; Console.WriteLine("{0} random record reads in {1:F1} seconds = {2} records per second", N, (dt2 - dt1).TotalSeconds, (int)(N / (dt2 - dt1).TotalSeconds)); } dal.Close(); } public static void Main(string[] args) { StackExchange.Redis.ConnectionMultiplexer con = StackExchange.Redis.ConnectionMultiplexer.Connect("localhost:6379"); NHibernate.Caches.Redis.RedisCacheProvider.SetConnectionMultiplexer(con); ////log4net.Config.XmlConfigurator.Configure(); Test("ADO.NET - no app cache", new DAL_ADONET("MySql.Data.MySqlClient", "Server=localhost;Database=test;User Id=root;Password=")); Test("ADO.NET - local app cache", new DAL_LocalCache(new DAL_ADONET("MySql.Data.MySqlClient", "Server=localhost;Database=test;User Id=root;Password="))); Test("ADO.NET - remote app cache", new DAL_RemoteCache(new DAL_ADONET("MySql.Data.MySqlClient", "Server=localhost;Database=test;User Id=root;Password="))); Test("EF - no app cache", new DAL_EF("MySQL_test")); Test("EF - local app cache", new DAL_LocalCache(new DAL_EF("MySQL_test"))); Test("EF - remote app cache", new DAL_RemoteCache(new DAL_EF("MySQL_test"))); Test("NHibernate - no app cache, no Hibernate level 2 cache", new DAL_NHib(@"C:\Work\nhibcfg_nocache.xml", @"C:\Work\nhibmap.xml")); Test("NHibernate - local app cache, no Hibernate level 2 cache", new DAL_LocalCache(new DAL_NHib(@"C:\Work\nhibcfg_nocache.xml", @"C:\Work\nhibmap.xml"))); Test("NHibernate - remote app cache, no Hibernate level 2 cache", new DAL_RemoteCache(new DAL_NHib(@"C:\Work\nhibcfg_nocache.xml", @"C:\Work\nhibmap.xml"))); Test("NHibernate - no app cache, local Hibernate level 2 cache", new DAL_NHib(@"C:\Work\nhibcfg_localcache.xml", @"C:\Work\nhibmap.xml")); Test("NHibernate - no app cache, remote Hibernate level 2 cache", new DAL_NHib(@"C:\Work\nhibcfg_remotecache.xml", @"C:\Work\nhibmap.xml")); } } }