Example #1
0
        public void AggregationTest()
        {
            var random = new Random(
                int.Parse(Guid.NewGuid().ToString().Replace("-", "").Substring(0, 7), NumberStyles.HexNumber)
                );

            //параметры теста
            var writeThreadCount = 10;// Environment.ProcessorCount + 2;
            var readThreadCount = 6;//Environment.ProcessorCount;// * 2;
            var itemCount = 100000;
            var maxValue = 1000;

            //подготовка данных
            var correctAccumulator = 0L;

            _datas = new Item[writeThreadCount][];
            for (var di = 0; di < writeThreadCount; di++)
            {
                _datas[di] = new Item[itemCount];

                for (var ii = 0; ii < itemCount; ii++)
                {
                    var v = random.Next(maxValue) + 1;

                    _datas[di][ii] = new Item(v);

                    //Debug.WriteLine("Generated {0}", v);

                    correctAccumulator += v;

                    _totalCount++;
                }
            }

            Debug.WriteLine("CorrectAccumulator {0}", correctAccumulator);

            var writeThreads = new Thread[writeThreadCount];
            for (var ti = 0; ti < writeThreads.Length; ti++)
            {
                var wt = new Thread(WriteThread);
                //wt.Priority = ThreadPriority.AboveNormal;

                writeThreads[ti] = wt;
            }

            var readThreads = new Thread[readThreadCount];
            for (var ti = 0; ti < readThreads.Length; ti++)
            {
                readThreads[ti] = new Thread(ReadThread);
            }

            _allThreads = writeThreads.ToList();
            _allThreads.AddRange(readThreads);

            //run the test
            for (var ti = 0; ti < writeThreads.Length; ti++)
            {
                writeThreads[ti].Start(ti);
            }
            for (var ti = 0; ti < readThreads.Length; ti++)
            {
                readThreads[ti].Start(ti);
            }

            _threadWorkEvent.Set();

            var before = DateTime.Now;

            //все заеблось!

            for (var ti = 0; ti < _allThreads.Count; ti++)
            {
                _allThreads[ti].Join();
            }

            var after = DateTime.Now;

            Item r;
            if (_itemWaitProvider.GetItem(TimeSpan.Zero, out r) == OperationResultEnum.Success)
            {
                Assert.Fail("чота недочитали!");
            }

            ((IDisposable)_itemWaitProvider).Dispose();

            Debug.WriteLine("Total count {0}, written count {1}, read count {2}", _totalCount, _writeCount, _readCount);
            Debug.WriteLine("Expected {0}, taken {1}", correctAccumulator, _accumulator);
            Debug.WriteLine("Time taken {0}", after - before);

            if (string.IsNullOrEmpty(_forceAbortReason))
            {
                Assert.AreEqual(correctAccumulator, _accumulator);
            }
            else
            {
                Debug.WriteLine("FORCE ABORT: " + _forceAbortReason);

                Assert.Fail();

            }
        }