// 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 Apache.Ignite.Core; using Apache.Ignite.Core.Client; using Apache.Ignite.Core.Client.Cache; namespace Cache.IgniteThin.Concurrency { public class Program { private delegate void F(ICacheClient cache); private static void Test1(ICacheClient cache) { int? hot = cache.Get("hot"); hot = hot + 1; cache.Put("hot", hot); } private static void Test3(ICacheClient cache) { bool done = false; while(!done) { int? hot1 = cache.Get("hot"); int? hot2 = hot1 + 1; done = cache.Replace("hot", hot1, hot2); } } private static void Testn(ICacheClient 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(ICacheClient 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) { IgniteClientConfiguration cfg = new IgniteClientConfiguration { Endpoints = new List { "localhost:10800" } }; using (IIgniteClient cli = Ignition.StartClient(cfg)) { ICacheClient cache = cli.GetOrCreateCache("performance_cache"); Test(cache, "No handling", c => Test1(c)); // locking not supported using thin Test(cache, "Replace with check", c => Test3(c)); // atomic increment not supported using thin } } } }