protected override IEnumerable <TimedResult> TestOnceInternal()
        {
            var queue = Param();

            yield return(TimedResult.Measure("Fill (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => queue.Enqueue(_item));
            }));

            yield return(TimedResult.Measure("Empty (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => { queue.TryDequeue(out var _); });
            }));


            yield return(TimedResult.Measure("Mixed Enqueue/TryDequeue (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 2 == 0)
                    {
                        queue.Enqueue(_item);
                    }
                    else
                    {
                        queue.TryDequeue(out _);
                    }
                });
            }));
        }
        protected override IEnumerable <TimedResult> TestOnceInternal()
        {
            foreach (var t in base.TestOnceInternal())
            {
                yield return(t);
            }

            const int mixSize = 100;
            var       c       = (IDictionary <int, object>)Param();

            for (var i = 0; i < TestSize; i++)
            {
                c.Add(_items[i]);
            }
            var items = Enumerable.Range(0, mixSize).Select(i => new object()).ToArray();

            yield return(TimedResult.Measure("Random Set/Get", () =>
            {
                for (var i = 0; i < TestSize; i++)
                {
                    var i1 = i;
                    Parallel.For(0, mixSize, x =>
                    {
                        if (x % 2 == 0)
                        {
                            c[i1] = items[x];
                        }
                        else
                        {
                            var _ = c[i1];
                        }
                    });
                }
            }));
        }
Example #3
0
        protected override IEnumerable <TimedResult> TestOnceInternal()
        {
            var c = Param();

            yield return(TimedResult.Measure("Fill (.AddLast(item)) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => c.AddLast(_item));
            }));

            yield return(TimedResult.Measure("Empty Backwards (.RemoveLast()) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    c.RemoveLast();
                });
            }));

            yield return(TimedResult.Measure("Refill (.AddLast(item)) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => c.AddLast(_item));
            }));

            yield return(TimedResult.Measure("Mixed Add/Remove Last (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 2 == 0)
                    {
                        c.AddLast(_item);
                    }
                    else
                    {
                        c.RemoveLast();
                    }
                });
            }));

            yield return(TimedResult.Measure("Empty Forwards (.RemoveLast()) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => c.RemoveLast());
            }));
        }
        protected override IEnumerable <TimedResult> TestOnceInternal()
        {
            using var pool = Param();
            if (pool is null)
            {
                throw new NullReferenceException();
            }
            //yield return TimedResult.Measure("Take From Empty (In Parallel)", () =>
            //{
            //	Parallel.For(0, TestSize, i => _items[i] = pool.Take());
            //});

            yield return(TimedResult.Measure("Give To (In Parallel)", () =>
            {
                // ReSharper disable once AccessToDisposedClosure
                Parallel.For(0, TestSize, i => pool.Give(_items[i]));
#if DEBUG
                if (pool is DefaultObjectPool <object> )
                {
                    return;
                }
                var count = pool.Count;
                Debug.Assert(pool is OptimisticArrayObjectPool <T> || count == TestSize, $"Expected {TestSize}, acutal count: {count}");
#endif
            }));

            yield return(TimedResult.Measure("Mixed 90%-Take/10%-Give (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 10 == 0)
                    {
                        pool.Give(_items[i]);
                    }
                    else
                    {
                        _items[i] = pool.Take();
                    }
                });
            }));

            yield return(TimedResult.Measure("Mixed 50%-Take/50%-Give (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 2 == 0)
                    {
                        _items[i] = pool.Take();
                    }
                    else
                    {
                        pool.Give(_items[i]);
                    }
                });
            }));

            yield return(TimedResult.Measure("Mixed 10%-Take/90%-Give (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 10 == 0)
                    {
                        _items[i] = pool.Take();
                    }
                    else
                    {
                        pool.Give(_items[i]);
                    }
                });
            }));

            //if (pool is DefaultObjectPool<object>) yield break;
            //yield return TimedResult.Measure("Empty Pool (.TryTake())", () =>
            //{

            //	while (pool.TryTake() is not null)
            //	{
            //		// remaining++;
            //	}
            //});
        }
        protected override IEnumerable <TimedResult> TestOnceInternal()
        {
            var c = Param();

            yield return(TimedResult.Measure("Fill (.Add(item)) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => c.Add(_items[i]));
            }));

            yield return(TimedResult.Measure("Enumerate", () =>
            {
                // ReSharper disable once NotAccessedVariable
                var x = 0;
                // ReSharper disable once LoopCanBeConvertedToQuery
                foreach (var _ in c)
                {
                    x++;
                }
            }));

            // It's obvious to note that you have to 'lock' a collection or acquire a 'snapshot' before enumerating.
            yield return(TimedResult.Measure("Enumerate (In Parallel)", () =>
            {
                Parallel.ForEach(c, i => { });
            }));

            yield return(TimedResult.Measure(".Contains(item) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize * 2, i => { var _ = c.Contains(_items[i]); });
            }));

            if (c is IList <T> list)
            {
                yield return(TimedResult.Measure("IList<T> Read Access", () =>
                {
                    for (var i = 0; i < TestSize; i += 2)
                    {
                        var _ = list[i];
                    }
                }));

                yield return(TimedResult.Measure("IList<T> Read Access (In Parallel)", () =>
                {
                    Parallel.For(0, (int)TestSize, i => { var _ = list[i]; });
                }));
            }

            if (c is ISynchronizedCollection <T> syncList)
            {
                yield return(TimedResult.Measure(".Snapshot()", () =>
                {
                    var _ = syncList.Snapshot();
                }));
            }
            else
            {
                yield return(TimedResult.Measure(".Snapshot()", () =>
                {
                    var _ = c.ToArray();
                }));
            }

            yield return(TimedResult.Measure("Empty Backwards (.Remove(last)) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => { c.Remove(_items[TestSize - i - 1]); });
            }));

            yield return(TimedResult.Measure("Refill (.Add(item)) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => c.Add(_items[i]));
            }));

            yield return(TimedResult.Measure("50/50 Mixed Contains/Add (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 2 == 0)
                    {
                        c.Contains(_items[i]);
                    }
                    else
                    {
                        c.Add(_items[i]);
                    }
                });
            }));

            yield return(TimedResult.Measure("10/90 Mixed Contains/Add (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 10 == 0)
                    {
                        c.Contains(_items[i]);
                    }
                    else
                    {
                        c.Add(_items[i]);
                    }
                });
            }));

            yield return(TimedResult.Measure("90/10 Mixed Contains/Add (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 10 != 9)
                    {
                        c.Contains(_items[i]);
                    }
                    else
                    {
                        c.Add(_items[i]);
                    }
                });
            }));

            yield return(TimedResult.Measure("50/50 Mixed Add/Remove (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i =>
                {
                    if (i % 2 == 0)
                    {
                        c.Add(_items[i]);
                    }
                    else
                    {
                        c.Remove(_items[i]);
                    }
                });
            }));

            yield return(TimedResult.Measure("Empty Forwards (.Remove(first)) (In Parallel)", () =>
            {
                Parallel.For(0, TestSize, i => { c.Remove(_items[i]); });
            }));
        }
Example #6
0
        protected override IEnumerable <TimedResult> TestOnceInternal()
        {
            var c = Param();

            yield return(TimedResult.Measure("Fill (.Add(item))", () =>
            {
                for (var i = 0; i < TestSize; i++)
                {
                    c.Add(_items[i]);
                }
            }));

            yield return(TimedResult.Measure("Enumerate", () =>
            {
                // ReSharper disable once NotAccessedVariable
                var x = 0;
                // ReSharper disable once LoopCanBeConvertedToQuery
                foreach (var _ in c)
                {
                    x++;
                }
            }));

            yield return(TimedResult.Measure(".Contains(item)", () =>
            {
                for (var i = 0; i < TestSize; i++)
                {
                    var _ = c.Contains(_items[i]);
                }
            }));

            if (c is IList <T> list)
            {
                yield return(TimedResult.Measure("IList<T> Read Access", () =>
                {
                    for (var i = 0; i < TestSize; i += 2)
                    {
                        var _ = list[i];
                    }
                }));
            }

            yield return(TimedResult.Measure("Empty Backwards (.Remove(last))", () =>
            {
                for (var i = 0; i < TestSize; i++)
                {
                    c.Remove(_items[TestSize - i - 1]);
                }
            }));

            yield return(TimedResult.Measure("Refill (.Add(item))", () =>
            {
                for (var i = 0; i < TestSize; i++)
                {
                    c.Add(_items[i]);
                }
            }));

            yield return(TimedResult.Measure("Empty Forwards (.Remove(first))", () =>
            {
                for (var i = 0; i < TestSize; i++)
                {
                    c.Remove(_items[i]);
                }
            }));

            for (var i = 0; i < TestSize; i++)
            {
                c.Add(_items[i]);
            }

            yield return(TimedResult.Measure(".Clear()", () =>
            {
                c.Clear();
            }));
        }