Exemplo n.º 1
0
        public void GlobalSetup()
        {
            message = new ReadOnlyMemory <byte>(System.Text.Encoding.UTF8.GetBytes("0"));

            connectionFactory = new ConnectionFactory()
            {
                Port        = 5672,
                HostName    = "localhost",
                UserName    = "******",
                Password    = "******",
                VirtualHost = "EnterpriseLog",
                AutomaticRecoveryEnabled = true,
                RequestedHeartbeat       = TimeSpan.FromMinutes(1)
            };

            Func <IConnection> getConnection = () => connectionFactory.CreateConnection();
            Func <IModel>      getModel      = () =>
            {
                using var connection = connectionRingBuffer.Accquire();
                return(connection.Current.CreateModel());
            };

            connectionRingBuffer = new DisposableRingBuffer <IConnection>(3, getConnection, TimeSpan.FromMilliseconds(10));
            modelRingBuffer      = new DisposableRingBuffer <IModel>(10, getModel, TimeSpan.FromMilliseconds(10));
        }
Exemplo n.º 2
0
        public int WithRingBuffer()
        {
            var message = new ReadOnlyMemory <byte>(System.Text.Encoding.UTF8.GetBytes("0"));

            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            for (var i = 0; i < 100; i++)
            {
                for (var j = 0; j < 30; j++)
                {
                    using (var data = modelRingBuffer.Accquire())
                    {
                        var prop = data.Current.CreateBasicProperties();
                        prop.DeliveryMode = 1;
                        data.Current.BasicPublish("amq.fanout", "none", prop, message);
                    }
                }
            }

            stopwatch.Stop();


            Console.WriteLine($"Test 1 | com RingBuffer | {stopwatch.Elapsed:G}");
            return(0);
        }
        public void TestInline()
        {
            RingBuffer.RingBuffer <DisposableTestableClass> buffer = null;
            Queue <DisposableTestableClass> queue = new Queue <DisposableTestableClass>();

            for (var i = 0; i < bufferSize; i++)
            {
                queue.Enqueue(new DisposableTestableClass());
            }

            using (DisposableRingBuffer <DisposableTestableClass> disposableBuffer = new DisposableRingBuffer <DisposableTestableClass>(
                       bufferSize,
                       () => queue.Dequeue(),
                       waitTime))
            {
                buffer = disposableBuffer;
                var         factory = new TaskFactory();
                List <Task> tasks   = new List <Task>();
                for (int i = 0; i < runCount; i++)
                {
                    using var bufferedItem = disposableBuffer.Accquire();
                    System.Threading.Thread.Sleep(workTime);
                }
                Assert.Equal(bufferSize, disposableBuffer.Available);
                Assert.Equal(disposableBuffer.Capacity, disposableBuffer.Available);
            }


            Assert.All(queue, it => Assert.True(it.IsDisposed));
            Assert.Empty(queue);
            Assert.Equal(0, buffer.Available);
            Assert.Equal(bufferSize, buffer.Capacity);
        }
Exemplo n.º 4
0
        public int WithRingBuffer()
        {
            string queueName = $"WithRingBuffer-{Guid.NewGuid().ToString("D")}";

            using (var accquisiton = modelRingBuffer.Accquire())
            {
                CreateQueue(accquisiton.Current, queueName);
            }

            for (var i = 0; i < 5; i++)
            {
                for (var j = 0; j < 1000; j++)
                {
                    using (var accquisiton = modelRingBuffer.Accquire())
                    {
                        Send(accquisiton.Current, queueName, message);
                    }
                }
            }
            return(0);
        }
        public void TestMultiThreadDisposableTest()
        {
            RingBuffer.RingBuffer <DisposableTestableClass> buffer = null;
            Queue <DisposableTestableClass> queue = new Queue <DisposableTestableClass>();

            for (var i = 0; i < bufferSize; i++)
            {
                queue.Enqueue(new DisposableTestableClass());
            }

            using (DisposableRingBuffer <DisposableTestableClass> disposableBuffer = new DisposableRingBuffer <DisposableTestableClass>(
                       bufferSize,
                       () => queue.Dequeue(),
                       waitTime))
            {
                buffer = disposableBuffer;
                List <Thread> threads = new List <Thread>();

                for (int i = 0; i < runCount; i++)
                {
                    Thread thread = new Thread(() => {
                        using var bufferedItem = disposableBuffer.Accquire();
                        System.Threading.Thread.Sleep(workTime);
                        System.Diagnostics.Debug.WriteLine($"Item {bufferedItem.Current} - processado!");
                    });
                    thread.Start();
                    threads.Add(thread);
                }

                foreach (Thread thread in threads)
                {
                    thread.Join();
                }

                Assert.Equal(bufferSize, disposableBuffer.Available);
                Assert.Equal(disposableBuffer.Capacity, disposableBuffer.Available);
            }


            Assert.All(queue, it => Assert.True(it.IsDisposed));
            Assert.Empty(queue);
            Assert.Equal(0, buffer.Available);
            Assert.Equal(bufferSize, buffer.Capacity);
        }