public void AddTest() { var dict = new ShieldedDict <int, object>(); Assert.Throws <InvalidOperationException>(() => dict.Add(1, new object())); Assert.Throws <InvalidOperationException>(() => ((ICollection <KeyValuePair <int, object> >)dict).Add( new KeyValuePair <int, object>(1, new object()))); var objectA = new object(); var objectB = new object(); Shield.InTransaction(() => { dict.Add(1, objectA); ((ICollection <KeyValuePair <int, object> >)dict).Add( new KeyValuePair <int, object>(2, objectB)); Assert.AreEqual(2, dict.Count); Assert.AreEqual(objectA, dict[1]); Assert.AreEqual(objectB, dict[2]); }); Assert.AreEqual(2, dict.Count); Assert.AreEqual(objectA, dict[1]); Assert.AreEqual(objectB, dict[2]); }
public void ContainsTest() { var dict = new ShieldedDict <string, object>(new KeyValuePair <string, object>[] { new KeyValuePair <string, object>("key a", null), new KeyValuePair <string, object>("key b", null), new KeyValuePair <string, object>("key c", null), }); Assert.IsTrue(dict.ContainsKey("key a")); Assert.IsTrue(((ICollection <KeyValuePair <string, object> >)dict).Contains( new KeyValuePair <string, object>("key a", null))); Assert.IsFalse(dict.ContainsKey("not me")); Assert.IsFalse(((ICollection <KeyValuePair <string, object> >)dict).Contains( new KeyValuePair <string, object>("not me", null))); Shield.InTransaction(() => { dict.Add("new key", null); dict.Remove("key a"); Assert.IsFalse(dict.ContainsKey("key a")); Assert.IsFalse(((ICollection <KeyValuePair <string, object> >)dict).Contains( new KeyValuePair <string, object>("key a", null))); Assert.IsTrue(dict.ContainsKey("new key")); Assert.IsTrue(((ICollection <KeyValuePair <string, object> >)dict).Contains( new KeyValuePair <string, object>("new key", null))); }); }
public void EnumerationTest() { var ordinaryDict = new Dictionary <int, object>() { { 1, new object() }, { 101, new object() }, { 666999, new object() } }; var dict = new ShieldedDict <int, object>(ordinaryDict); var addedObject = new object(); // in preparation for the same changes done to dict inside transaction ordinaryDict.Add(2, addedObject); ordinaryDict.Remove(666999); Shield.InTransaction(() => { // as an IShielded implementor, the Dict is more complex and needs to be more carefully // tested for how well he manages thread-local data. So, we add some changes here. dict.Add(2, addedObject); dict.Remove(666999); int count = 0; var checkSet = new HashSet <int>(); foreach (var kvp in dict) { Assert.IsTrue(checkSet.Add(kvp.Key)); Assert.IsTrue(ordinaryDict.ContainsKey(kvp.Key)); Assert.AreEqual(ordinaryDict[kvp.Key], kvp.Value); count++; } Assert.AreEqual(3, count); }); }
public void AddTest() { var dict = new ShieldedDict<int, object>(); Assert.Throws<InvalidOperationException>(() => dict.Add(1, new object())); Assert.Throws<InvalidOperationException>(() => ((ICollection<KeyValuePair<int, object>>)dict).Add( new KeyValuePair<int, object>(1, new object()))); var objectA = new object(); var objectB = new object(); Shield.InTransaction(() => { dict.Add(1, objectA); ((ICollection<KeyValuePair<int, object>>)dict).Add( new KeyValuePair<int, object>(2, objectB)); Assert.AreEqual(2, dict.Count); Assert.AreEqual(objectA, dict[1]); Assert.AreEqual(objectB, dict[2]); }); Assert.AreEqual(2, dict.Count); Assert.AreEqual(objectA, dict[1]); Assert.AreEqual(objectB, dict[2]); }
public void EnumerationTest() { var ordinaryDict = new Dictionary <int, object>() { { 1, new object() }, { 101, new object() }, { 666999, new object() } }; var dict = new ShieldedDict <int, object>(ordinaryDict); var addedObject = new object(); // in preparation for the same changes done to dict inside transaction ordinaryDict.Add(2, addedObject); ordinaryDict.Remove(666999); Shield.InTransaction(() => { // as an IShielded implementor, the Dict is more complex and needs to be more carefully // tested for how well he manages thread-local data. So, we add some changes here. dict.Add(2, addedObject); dict.Remove(666999); Assert.IsTrue(dict.OrderBy(kvp => kvp.Key).SequenceEqual(ordinaryDict.OrderBy(kvp => kvp.Key))); }); }
public static void TreePoolTest() { int numThreads = 4; int numItems = 200000; // for some reason, if this is replaced with ShieldedDict, KeyAlreadyPresent // exception is thrown. under one key you can then find an entity which does // not have that key. complete mystery. var tree = new ShieldedDict<Guid, TreeItem>(); var barrier = new Barrier(numThreads + 1); int reportEvery = 10000; Shielded<int> lastReport = new Shielded<int>(0); Shielded<DateTime> lastTime = new Shielded<DateTime>(DateTime.UtcNow); Shield.Conditional(() => tree.Count >= lastReport + reportEvery, () => { DateTime newNow = DateTime.UtcNow; int count = tree.Count; int speed = (count - lastReport) * 1000 / (int)newNow.Subtract(lastTime).TotalMilliseconds; lastTime.Assign(newNow); lastReport.Modify((ref int n) => n += reportEvery); Shield.SideEffect(() => { Console.Write("\n{0} at {1} item/s", count, speed); }); return true; }); TreeItem x = new TreeItem(); _timer = new Stopwatch(); _timer.Start(); var time = _timer.ElapsedMilliseconds; foreach (var k in Enumerable.Repeat(1, numItems)) Shield.InTransaction(() => x.Id); time = _timer.ElapsedMilliseconds - time; Console.WriteLine("1 read transactions in {0} ms.", time); var bags = new List<Action>[numThreads]; var threads = new Thread[numThreads]; for (int i = 0; i < numThreads; i++) { var bag = bags[i] = new List<Action>(); threads[i] = new Thread(() => { foreach (var a in bag) { try { a(); } catch { Console.Write(" * "); } } barrier.SignalAndWait(); }); } foreach (var i in Enumerable.Range(0, numItems)) { var item1 = new TreeItem(); bags[i % numThreads].Add(() => Shield.InTransaction(() => { tree.Add(item1.Id, item1); })); } for (int i = 0; i < numThreads; i++) threads[i].Start(); barrier.SignalAndWait(); time = _timer.ElapsedMilliseconds; Console.WriteLine(" {0} ms.", time); Console.WriteLine("\nReading sequentially..."); time = _timer.ElapsedMilliseconds; var keys = Shield.InTransaction(() => tree.Keys); time = _timer.ElapsedMilliseconds - time; Console.WriteLine("Keys read in {0} ms.", time); time = _timer.ElapsedMilliseconds; Shield.InTransaction(() => { foreach (var kvp in tree) x = kvp.Value; }); time = _timer.ElapsedMilliseconds - time; Console.WriteLine("Items read by enumerator in {0} ms.", time); time = _timer.ElapsedMilliseconds; Shield.InTransaction(() => { foreach (var k in keys) x = tree[k]; }); time = _timer.ElapsedMilliseconds - time; Console.WriteLine("Items read by key in one trans in {0} ms.", time); time = _timer.ElapsedMilliseconds; foreach (var k in keys) x = tree[k]; time = _timer.ElapsedMilliseconds - time; Console.WriteLine("Items read by key separately in {0} ms.", time); time = _timer.ElapsedMilliseconds; keys.AsParallel().ForAll(k => x = tree[k]); time = _timer.ElapsedMilliseconds - time; Console.WriteLine("Items read by key in parallel in {0} ms.", time); time = _timer.ElapsedMilliseconds; foreach (var k in Enumerable.Repeat(1, numItems)) Shield.InTransaction(() => x.Id); time = _timer.ElapsedMilliseconds - time; Console.WriteLine("1 read transactions in {0} ms.", time); }
public void EnumerationTest() { var ordinaryDict = new Dictionary<int, object>() { { 1, new object() }, { 101, new object() }, { 666999, new object() } }; var dict = new ShieldedDict<int, object>(ordinaryDict); var addedObject = new object(); // in preparation for the same changes done to dict inside transaction ordinaryDict.Add(2, addedObject); ordinaryDict.Remove(666999); Shield.InTransaction(() => { // as an IShielded implementor, the Dict is more complex and needs to be more carefully // tested for how well he manages thread-local data. So, we add some changes here. dict.Add(2, addedObject); dict.Remove(666999); int count = 0; var checkSet = new HashSet<int>(); foreach (var kvp in dict) { Assert.IsTrue(checkSet.Add(kvp.Key)); Assert.IsTrue(ordinaryDict.ContainsKey(kvp.Key)); Assert.AreEqual(ordinaryDict[kvp.Key], kvp.Value); count++; } Assert.AreEqual(3, count); }); }
public void ContainsTest() { var dict = new ShieldedDict<string, object>(new KeyValuePair<string, object>[] { new KeyValuePair<string, object>("key a", null), new KeyValuePair<string, object>("key b", null), new KeyValuePair<string, object>("key c", null), }); Assert.IsTrue(dict.ContainsKey("key a")); Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)dict).Contains( new KeyValuePair<string, object>("key a", null))); Assert.IsFalse(dict.ContainsKey("not me")); Assert.IsFalse(((ICollection<KeyValuePair<string, object>>)dict).Contains( new KeyValuePair<string, object>("not me", null))); Shield.InTransaction(() => { dict.Add("new key", null); dict.Remove("key a"); Assert.IsFalse(dict.ContainsKey("key a")); Assert.IsFalse(((ICollection<KeyValuePair<string, object>>)dict).Contains( new KeyValuePair<string, object>("key a", null))); Assert.IsTrue(dict.ContainsKey("new key")); Assert.IsTrue(((ICollection<KeyValuePair<string, object>>)dict).Contains( new KeyValuePair<string, object>("new key", null))); }); }