Ejemplo n.º 1
0
        public void RingQueue()
        {
            Action <int> action = i => {
                Debug.WriteLine("a:" + i);
                Thread.Sleep(1000);
            };
            var rq = new RingQueue <Action>(5);

            for (int i = 1; i < 6; i++)
            {
                var i1 = i;
                rq.Enqueue(() => action(i1));
            }
            bool isCompleted = false;

            rq.ToObservable(NewThreadScheduler.Default).Subscribe(a => a(), () => isCompleted = true);
            var ii = 6;

            while (!isCompleted)
            {
                var i1 = ++ii;
                rq.Enqueue(() => action(i1));
                Thread.Sleep(1000);
            }
        }
Ejemplo n.º 2
0
 public void ShouldThrowInvalidOperationExceptionWhenQueueIsFullForEnqueue() {
   var queue = new RingQueue<int>(10);
   for (int i = 0; i < 10; i++) {
     queue.Enqueue(i);
   }
   Assert.That(() => queue.Enqueue(10), Throws.InvalidOperationException);
 }
Ejemplo n.º 3
0
        public void RingQueueLostTest()
        {
            const int maxCount = 20;
            const int size     = 18;
            var       ring     = new RingQueue <int>(size);

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();

            for (int i = 0; i < maxCount; i++)
            {
                ring.Enqueue(i);
                ring.Count.Should().Be(Math.Min(size, i + 1));
            }
            ring.Count.Should().Be(size);
            ring.IsFull.Should().BeTrue();
            ring.LostCount.Should().Be(maxCount - size);

            for (int i = maxCount - size; i < maxCount; i++)
            {
                int value = ring.Dequeue();
                value.Should().Be(i);
            }

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();
        }
Ejemplo n.º 4
0
        public void RingQueueMaxTest()
        {
            const int maxCount = 10;
            var       ring     = new RingQueue <int>(maxCount);

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();

            for (int i = 0; i < maxCount; i++)
            {
                ring.Enqueue(i);
                ring.Count.Should().Be(i + 1);
            }
            ring.Count.Should().Be(maxCount);
            ring.IsFull.Should().BeTrue();
            ring.LostCount.Should().Be(0);

            for (int i = 0; i < maxCount; i++)
            {
                int value = ring.Dequeue();
                value.Should().Be(i);
            }

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();
        }
Ejemplo n.º 5
0
        public void RingQueueGetListTest()
        {
            const int loopCount = 10;
            const int size      = 1000;
            const int batchSize = 250;
            var       ring      = new RingQueue <int>(size);

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();

            int index = 0;

            for (int i = 0; i < loopCount; i++)
            {
                for (int j = 0; j < batchSize; j++)
                {
                    ring.Enqueue(index);
                    ring.Count.Should().Be(Math.Min(size, index + 1));
                    index++;
                }

                IList <int> list = ring.ToList();
                list.Count.Should().Be(Math.Min(size, batchSize * (i + 1)));
            }
        }
Ejemplo n.º 6
0
        public void RingQueueTest()
        {
            const int size = 3;
            var       ring = new RingQueue <int>(10);

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();

            for (int i = 0; i < size; i++)
            {
                ring.Enqueue(i);
                ring.Count.Should().Be(i + 1);
            }
            ring.Count.Should().Be(size);
            ring.IsFull.Should().BeFalse();
            ring.LostCount.Should().Be(0);

            for (int i = 0; i < size; i++)
            {
                ring.Count.Should().Be(size - i);
                int value = ring.Dequeue();
                value.Should().Be(i);
            }

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();
        }
Ejemplo n.º 7
0
        public void RingQueueTryPeekTest()
        {
            const int maxCount = 2000;
            const int size     = 20;
            var       ring     = new RingQueue <int>(size);

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();

            for (int i = 0; i < maxCount; i++)
            {
                ring.Enqueue(i);
                ring.Count.Should().Be(Math.Min(size, i + 1));
            }
            ring.Count.Should().Be(size);
            ring.IsFull.Should().BeTrue();
            ring.LostCount.Should().Be(maxCount - size);

            for (int i = maxCount - size; i < maxCount; i++)
            {
                (bool success, int peekValue)rtn = ring.TryPeek();
                rtn.success.Should().BeTrue();
                int value = ring.Dequeue();

                value.Should().Be(rtn.peekValue);
                value.Should().Be(i);
            }

            ring.IsEmpty.Should().BeTrue();
            ring.IsFull.Should().BeFalse();
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Enqueue item
        /// </summary>
        /// <param name="value"></param>
        public void OnNext(T value)
        {
            Verify.Assert <InvalidOperationException>(Running, "must be running");

            _queue.Enqueue(value);
            _notifyNewMessage.Set();
        }
Ejemplo n.º 9
0
    private IEnumerator Poll()
    {
        var procCount = Environment.ProcessorCount;

        var prevSysTime = DateTime.Now;
        var prevCpuTime = GetCpuTime();

        while (true)
        {
            yield return(new WaitForSeconds(pollingFrequency));

            var currSysTime = DateTime.Now;
            var currCpuTime = GetCpuTime();

            var sysTime = currSysTime - prevSysTime;
            var cpuTime = currCpuTime - prevCpuTime;

            var cpu = cpuTime * 100.0 / (procCount * sysTime.TotalMilliseconds);

            _history.Enqueue(cpu);

            if (tmpText)
            {
                tmpText.SetText($"CPU: {cpu:F1}% ({_history.Average():F1}%)");
            }

            prevSysTime = currSysTime;
            prevCpuTime = currCpuTime;
        }
    }
Ejemplo n.º 10
0
        public void GivenQueuePolicy_WhenEqualMaxAdded_ShouldNoDataLoss()
        {
            const int max   = 10;
            var       queue = new RingQueue <int>(max);

            Enumerable.Range(0, max).ForEach(x => queue.Enqueue(x));

            queue.Count.Should().Be(max);
            queue.LostCount.Should().Be(0);

            Enumerable.Range(0, max)
            .Zip(queue, (o, i) => (o, i))
            .All(x => x.o == x.i)
            .Should().BeTrue();
        }
Ejemplo n.º 11
0
        public void GivenQueuePolicy_WhenMoreThenMaxAdded_ShouldHaveDataLoss()
        {
            const int max   = 10;
            const int count = 11;
            var       queue = new RingQueue <int>(max);

            Enumerable.Range(0, count).ForEach(x => queue.Enqueue(x));

            queue.Count.Should().Be(max);
            queue.LostCount.Should().Be(count - max);

            Enumerable.Range(1, max)
            .Zip(queue, (o, i) => (o, i))
            .All(x => x.o == x.i)
            .Should().BeTrue();
        }
Ejemplo n.º 12
0
        public void GivenRingBuffer_WhenLessThenMaxAdded_NoDataLoss()
        {
            const int max   = 10;
            const int count = 5;
            var       queue = new RingQueue <int>(max);

            Enumerable.Range(0, count).ForEach(x => queue.Enqueue(x));

            queue.Count.Should().Be(count);
            queue.LostCount.Should().Be(0);

            Enumerable.Range(0, count)
            .Zip(queue, (o, i) => (o, i))
            .All(x => x.o == x.i)
            .Should().BeTrue();
        }
Ejemplo n.º 13
0
    public void StopBenchmark()
    {
        // if not active with frames, exit
        if (!(_active && _frames > 0))
        {
            return;
        }

        // add to history
        _history.Enqueue(_frames);

        // log result
        Debug.Log($"[Benchmark] Score: {_frames}");

        // calculate average
        _average = _history.Average();

        // set as inactive
        _active = false;
    }
Ejemplo n.º 14
0
    private IEnumerator Poll()
    {
        const float wait = 0.5f;

        while (true)
        {
            yield return(new WaitForSeconds(wait));

            var fps = _frames / wait;

            _history.Enqueue(fps);

            if (tmpText)
            {
                tmpText.SetText($"FPS: {fps:F1} ({_history.Average():F1})");
            }

            _frames = 0;
        }
    }
        static void Main()
        {
            RingQueue <int> ringQueue = new RingQueue <int>(new DoublingStrategy());

            ringQueue.Enqueue(0);
            ringQueue.Enqueue(1);
            ringQueue.Enqueue(2);
            ringQueue.Enqueue(3);
            ringQueue.Enqueue(4);
            ringQueue.Enqueue(5);
            ringQueue.Enqueue(6);
            ringQueue.Enqueue(7);
            ringQueue.Enqueue(8);
            ringQueue.Enqueue(9);

            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine(ringQueue.Dequeue());
            }

            ringQueue.Enqueue(0);
            ringQueue.Enqueue(1);
            ringQueue.Enqueue(2);
            ringQueue.Enqueue(3);
            ringQueue.Enqueue(4);
            ringQueue.Enqueue(5);
            ringQueue.Enqueue(6);
            ringQueue.Enqueue(7);
            ringQueue.Enqueue(8);
            ringQueue.Enqueue(9);
            ringQueue.Enqueue(0);
            ringQueue.Enqueue(1);
            ringQueue.Enqueue(2);
            ringQueue.Enqueue(3);
            ringQueue.Enqueue(4);
            ringQueue.Enqueue(5);
            ringQueue.Enqueue(6);
            ringQueue.Enqueue(7);
            ringQueue.Enqueue(8);
            ringQueue.Enqueue(9);

            for (int i = ringQueue.Size; i > 0; i--)
            {
                Console.WriteLine(ringQueue.Dequeue());
            }

            ringQueue.Enqueue(1);

            for (int i = ringQueue.Size; i > 0; i--)
            {
                Console.WriteLine(ringQueue.Dequeue());
            }

            Console.ReadKey();
        }
Ejemplo n.º 16
0
 protected override void OnNextCore(EventData value)
 {
     _eventRing.Enqueue(value);
 }