Example #1
0
        public void ServiceCouldGrowBucketsWithResize()
        {
            // 2 counters per bucket
            Settings.AtomicCounterPoolBucketSize = 4;

            // 4 buckets initially
            var count = 100;

            var counters = new List <AtomicCounter>();

            for (int i = 0; i < count; i++)
            {
                counters.Add(AtomicCounterService.AcquireCounter());
            }

            foreach (var atomicCounter in counters)
            {
                atomicCounter.Dispose();
                AtomicCounterService.ReleaseCounter(atomicCounter);
            }

            Assert.AreEqual(64, AtomicCounterService.Buckets.Length);
            Assert.AreEqual(50, AtomicCounterService.Buckets.Where(x => x != null).Count());
            Assert.AreEqual(50 * 2, AtomicCounterService.Buckets.Where(x => x != null).Select(x => x.FreeCount).Sum());
        }
Example #2
0
        // ReSharper disable once InconsistentNaming
        public void ServiceCouldCreateACPoolAndAcquireRelease()
        {
            Settings.AtomicCounterPoolBucketSize = 4;
            var ac  = AtomicCounterService.AcquireCounter();
            var ac1 = AtomicCounterService.AcquireCounter();

            Assert.IsTrue(ac.IsValid);

            Assert.AreEqual(1, ac.Increment());
            Assert.AreEqual(0, ac.Decrement());
            Assert.AreEqual(1, ac.Increment());

            Assert.Throws <InvalidOperationException>(() => ac.Dispose());

            Assert.AreEqual(0, ac.Decrement());

            Assert.Throws <InvalidOperationException>(() => AtomicCounterService.ReleaseCounter(ac));

            ac.Dispose();
            ac1.Dispose();

            Assert.Throws <ObjectDisposedException>(() => ac.Increment());
            Assert.Throws <ObjectDisposedException>(() => ac1.Increment());

            AtomicCounterService.ReleaseCounter(ac);
        }
Example #3
0
 internal OffHeapMemory(RetainableMemoryPool <T> pool, int minLength) :
     // NOTE: this object must release _counter when finalized or not pooled
     // Base class calls _counter.Dispose in it's dispose method and this is enough
     // for shared-memory backed counters. Base class doesn't know about ACS.
     base(AtomicCounterService.AcquireCounter())
 {
     _pool = pool;
     Init(minLength);
 }
Example #4
0
        public void ServiceBenchmarkNoRelease()
        {
            var count = 100000;

            Settings.AtomicCounterPoolBucketSize = count;

            var rounds   = 50;
            var counters = new List <AtomicCounter>();

            using (Benchmark.Run("Service FullCycle", count * (long)rounds))
            {
                for (int r = 0; r < rounds; r++)
                {
                    for (int i = 0; i < count; i++)
                    {
                        counters.Add(AtomicCounterService.AcquireCounter());
                    }

                    counters.Clear();
                }
            }
        }
Example #5
0
        public void ServiceBenchmark()
        {
            var count = 10000;

            Settings.AtomicCounterPoolBucketSize = count;

            var rounds   = 5000;
            var counters = new List <AtomicCounter>();

            using (Benchmark.Run("Service FullCycle", count * (long)rounds))
            {
                for (int r = 0; r < rounds; r++)
                {
                    for (int i = 0; i < count; i++)
                    {
                        counters.Add(AtomicCounterService.AcquireCounter());
                    }

                    for (int i = counters.Count - 1; i >= 0; i--)
                    {
                        var atomicCounter = counters[i];
                        atomicCounter.Dispose();
                        AtomicCounterService.ReleaseCounter(atomicCounter);
                    }

                    //foreach (var atomicCounter in counters)
                    //{
                    //    atomicCounter.Dispose();
                    //    AtomicCounterService.ReleaseCounter(atomicCounter);
                    //}

                    counters.Clear();
                }
            }

            Assert.AreEqual(4, AtomicCounterService.Buckets.Length);
            Assert.AreEqual(1, AtomicCounterService.Buckets.Where(x => x != null).Count());
        }
Example #6
0
 protected ArrayMemory() : base(AtomicCounterService.AcquireCounter())
 {
 }