// 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.Collections.Generic; using System.Threading; using Infinispan.HotRod; using Infinispan.HotRod.Config; using Infinispan.HotRod.Event; namespace Cache.Infinispan.Functional { public class Program { private delegate void F(IRemoteCache cache); private static void Test1(IRemoteCache cache) { int? hot; lock (cache) // RemoteCache is not threadsafe { hot = cache.Get("hot"); } hot = hot + 1; lock (cache) // RemoteCache is not threadsafe { cache.Put("hot", hot); } } private static void Testn(IRemoteCache cache, int nrep, F action) { for (int i = 0; i < nrep; i++) { action(cache); } } private const int NTHREADS = 100; private const int NOPS = 10000; private static void Test(IRemoteCache cache, string lbl, F action) { DateTime dt1 = DateTime.Now; cache.Put("hot", 0); Thread[] t = new Thread[NTHREADS]; for (int i = 0; i < NTHREADS; i++) { t[i] = new Thread(() => Testn(cache, NOPS / NTHREADS, action)); } for (int i = 0; i < NTHREADS; i++) { t[i].Start(); } for (int i = 0; i < NTHREADS; i++) { t[i].Join(); } int? hot = cache.Get("hot"); DateTime dt2 = DateTime.Now; Console.WriteLine("{0} : {1} = {2} ({3} ms)", lbl, hot, NOPS, (int)((dt2 - dt1).TotalMilliseconds)); } public static void Main(string[] args) { ConfigurationBuilder b = new ConfigurationBuilder(); b.AddServer().Host("localhost").Port(11222); RemoteCacheManager rcm = new RemoteCacheManager(b.Build()); IRemoteCache cache = rcm.GetCache("concurrency_cache"); Test(cache, "No handling", c => Test1(c)); // nothing available as client library is not complete in 8.2.0 RC2 rcm.Stop(); } } }