示例#1
0
        public void three_tasks_one_failing_with_allowPartialBatchResumption_with_enumerator()
        {
            using (var tp = new RTP(8))
            {
                tp.Start();
                var range = Enumerable.Range(0, 10000).ToList();

                Assert.Throws(typeof(DivideByZeroException), () =>
                {
                    tp.ExecuteBatch(range, (IEnumerator <int> input) =>
                    {
                        while (input.MoveNext())
                        {
                            const int number = 100;
                            var result       = number / input.Current;

                            if (input.Current == 1 || input.Current == 5000)
                            {
                                Thread.Sleep(1000);
                            }
                        }
                    }, database: null);
                });
            }
        }
        public void TwoLevelParallelBulkCalculationSimple()
        {
            var cts = new CancellationTokenSource();

            using (var tp = new RTP(8, cts.Token, null))
            {
                long sum = 0;
                tp.Start();
                var range = Enumerable.Range(0, 100000).ToList();


                tp.ExecuteBatch(range, (IEnumerator <int> input) =>
                {
                    var inputAsList = new List <int>();

                    while (input.MoveNext())
                    {
                        inputAsList.Add(input.Current);
                    }
                    tp.ExecuteBatch(inputAsList, x => Interlocked.Add(ref sum, (long)x));
                });


                var expectedSum = range.Sum(x => (long)x);
                Assert.Equal(expectedSum, sum);
            }
        }
        public void OneLevelParallelBulkCalculation()
        {
            var cts = new CancellationTokenSource();

            using (var tp = new RTP(8, cts.Token, null))
            {
                long sum = 0;
                tp.Start();
                var range       = Enumerable.Range(0, 100000).ToList();
                var expectedSum = range.Sum(x => (long)x);
                for (int i = 1; i <= 1024; i++)
                {
                    var sp = Stopwatch.StartNew();
                    tp.ExecuteBatch(range, (IEnumerator <int> input) =>
                    {
                        while (input.MoveNext())
                        {
                            Interlocked.Add(ref sum, (long)input.Current);
                        }
                    }, i);

                    Assert.Equal(expectedSum, sum);
                    sum = 0;
                }
            }
        }
        public void ThrottlingTest()
        {
            var cts = new CancellationTokenSource();

            using (var tp = new RTP(8, cts.Token, null).Start())
            {
                var threadPoolStats = tp.GetThreadPoolStats();

                Assert.Equal(8, threadPoolStats.ThreadsPrioritiesCounts[ThreadPriority.Normal]);
                Assert.Equal(8, threadPoolStats.ConcurrentWorkingThreadsAmount);

                for (var i = 0; i < 8; i++)
                {
                    tp.HandleHighCpuUsage();
                    int normalPrioritiesCount = 0, belowNormalPrioritiesCount = 0;
                    threadPoolStats = tp.GetThreadPoolStats();
                    var threadPrioritiesCounts = threadPoolStats.ThreadsPrioritiesCounts;
                    threadPrioritiesCounts.TryGetValue(ThreadPriority.Normal, out normalPrioritiesCount);
                    threadPrioritiesCounts.TryGetValue(ThreadPriority.BelowNormal, out belowNormalPrioritiesCount);

                    Assert.Equal(7 - i, normalPrioritiesCount);
                    Assert.Equal(i + 1, belowNormalPrioritiesCount);
                    Assert.Equal(8, threadPoolStats.ConcurrentWorkingThreadsAmount);
                }

                threadPoolStats = tp.GetThreadPoolStats();
                Assert.Equal(8, threadPoolStats.ConcurrentWorkingThreadsAmount);

                for (var i = 0; i < 7; i++)
                {
                    tp.HandleHighCpuUsage();
                    threadPoolStats = tp.GetThreadPoolStats();

                    Assert.Equal(7 - i, threadPoolStats.ConcurrentWorkingThreadsAmount);
                }

                for (var i = 1; i < 8; i++)
                {
                    tp.HandleLowCpuUsage();
                    threadPoolStats = tp.GetThreadPoolStats();

                    Assert.Equal(i + 1, threadPoolStats.ConcurrentWorkingThreadsAmount);
                }

                for (var i = 1; i <= 8; i++)
                {
                    tp.HandleLowCpuUsage();
                    int normalPrioritiesCount = 0, belowNormalPrioritiesCount = 0;
                    threadPoolStats = tp.GetThreadPoolStats();
                    var threadPrioritiesCounts = threadPoolStats.ThreadsPrioritiesCounts;
                    threadPrioritiesCounts.TryGetValue(ThreadPriority.Normal, out normalPrioritiesCount);
                    threadPrioritiesCounts.TryGetValue(ThreadPriority.BelowNormal, out belowNormalPrioritiesCount);

                    Assert.Equal(i, normalPrioritiesCount);
                    Assert.Equal(8 - i, belowNormalPrioritiesCount);
                    Assert.Equal(8, threadPoolStats.ConcurrentWorkingThreadsAmount);
                }
            }
        }
示例#5
0
 public void OneLevelParallelCalculation()
 {
     using (var tp = new RTP(8))
     {
         long sum = 0;
         tp.Start();
         var range = Enumerable.Range(0, 100000).ToList();
         tp.ExecuteBatch(range, (int input) =>
         {
             Interlocked.Add(ref sum, (long)input);
         }, null);
         var expectedSum = range.Sum(x => (long)x);
         Assert.Equal(expectedSum, sum);
     }
 }
示例#6
0
        public void three_tasks_one_failing()
        {
            using (var tp = new RTP(8))
            {
                tp.Start();
                var range = Enumerable.Range(0, 3).ToList();

                Assert.Throws(typeof(DivideByZeroException), () =>
                {
                    tp.ExecuteBatch(range, (int input) =>
                    {
                        const int number = 100;
                        var result       = number / input;
                    }, database: null);
                });
            }
        }
        public void TwoLevelParallelCalculation()
        {
            var cts = new CancellationTokenSource();

            using (var tp = new RTP(8, cts.Token, null))
            {
                long sum = 0;
                tp.Start();
                var range = Enumerable.Range(0, 1000).ToList();
                tp.ExecuteBatch(range, (int input) =>
                {
                    var innerRange = Enumerable.Range(0, 100).ToList();
                    tp.ExecuteBatch(innerRange, (int innerInput) => Interlocked.Add(ref sum, (long)input));
                });
                var expectedSum = range.Sum(x => (long)x);
                Assert.Equal(100 * expectedSum, sum);
            }
        }
示例#8
0
        public void three_tasks_one_failing()
        {
            var cts = new CancellationTokenSource();

            using (var tp = new RTP(8, cts.Token, null))
            {
                tp.Start();
                var range = Enumerable.Range(0, 3).ToList();

                Assert.Throws(typeof(DivideByZeroException), () =>
                {
                    tp.ExecuteBatch(range, (int input) =>
                    {
                        const int number = 100;
                        var result       = number / input;
                    });
                });
            }
        }
示例#9
0
        public void one_task_one_failing_with_enumerator()
        {
            using (var tp = new RTP(8))
            {
                tp.Start();
                var range = Enumerable.Range(0, 1).ToList();

                Assert.Throws(typeof(DivideByZeroException), () =>
                {
                    tp.ExecuteBatch(range, (IEnumerator <int> input) =>
                    {
                        while (input.MoveNext())
                        {
                            const int number = 100;
                            var result       = number / input.Current;
                        }
                    }, database: null);
                });
            }
        }
示例#10
0
        public void PartialMaxWaitThrottlingTest()
        {
            using (var tp = new RTP(8).Start())
            {
                var array = Enumerable.Range(0, 6).ToList();
                var stats = tp.GetThreadPoolStats();

                Debug.Print(stats.PartialMaxWait.ToString());
                for (var i = 0; i < 8; i++)
                {
                    tp.ExecuteBatch(array, (int x) =>
                    {
                        Thread.Sleep(x * 1000);
                    }, allowPartialBatchResumption: true, database: null);
                    stats = tp.GetThreadPoolStats();

                    Debug.Print(stats.PartialMaxWait.ToString());
                }
            }
        }
示例#11
0
        public void OneLevelParallelBulkCalculationSimple()
        {
            using (var tp = new RTP(8))
            {
                long sum = 0;
                tp.Start();
                var range = Enumerable.Range(0, 100000).ToList();

                tp.ExecuteBatch(range, (IEnumerator <int> input) =>
                {
                    while (input.MoveNext())
                    {
                        Interlocked.Add(ref sum, (long)input.Current);
                    }
                }, null);


                var expectedSum = range.Sum(x => (long)x);
                Assert.Equal(expectedSum, sum);
            }
        }
示例#12
0
        public void OneLevelParallelCalculationWithPartialResumption()
        {
            using (var tp = new RTP(8).Start())
            {
                long sum   = 0;
                var  range = Enumerable.Range(1, 6).ToList();
                tp.ExecuteBatch(range, (int input) =>
                {
                    Interlocked.Add(ref sum, (long)input);
                    Thread.Sleep((int)Math.Pow(input, 4) * 5);
                }, allowPartialBatchResumption: true, database: null);

                while (tp.GetAllWaitingTasks().Count() != 0 || tp.RunningTasksAmount != 0)
                {
                    Thread.Sleep(100);
                }
                var expectedSum = range.Sum(x => (long)x);
                Console.WriteLine($"expected:{expectedSum}; real:{sum}");
                Assert.Equal(expectedSum, sum);
            }
        }
示例#13
0
        public void three_tasks_one_failing_with_allowPartialBatchResumption()
        {
            using (var tp = new RTP(8))
            {
                tp.Start();
                var range = Enumerable.Range(0, 3).ToList();

                Assert.Throws(typeof(DivideByZeroException), () =>
                {
                    tp.ExecuteBatch(range, (int input) =>
                    {
                        const int number = 100;
                        var result       = number / input;

                        if (input == 1)
                        {
                            Thread.Sleep(3000);
                        }
                    }, allowPartialBatchResumption: true, database: null);
                });
            }
        }
示例#14
0
        public void three_tasks_one_failing_with_enumerator()
        {
            var cts = new CancellationTokenSource();

            using (var tp = new RTP(8, cts.Token, null))
            {
                tp.Start();
                var range = Enumerable.Range(0, 10000).ToList();

                Assert.Throws(typeof(DivideByZeroException), () =>
                {
                    tp.ExecuteBatch(range, (IEnumerator <int> input) =>
                    {
                        while (input.MoveNext())
                        {
                            const int number = 100;
                            var result       = number / input.Current;
                        }
                    });
                });
            }
        }
示例#15
0
        public void three_tasks_one_failing_with_allowPartialBatchResumption_shouldnt_throw()
        {
            var cts = new CancellationTokenSource();

            using (var tp = new RTP(8, cts.Token, null))
            {
                tp.Start();
                var range = Enumerable.Range(0, 3).ToList();
                Assert.DoesNotThrow(() =>
                {
                    tp.ExecuteBatch(range, (int input) =>
                    {
                        const int number = 100;
                        if (input == 0)
                        {
                            Thread.Sleep(30000);
                        }

                        var result = number / input;
                    }, allowPartialBatchResumption: true, maxWaitMultiplier: 2500);
                });
            }
        }
        public void OneLevelParallelCalculationWithPartialResumption()
        {
            var cts = new CancellationTokenSource();

            using (var tp = new RTP(8, cts.Token, null).Start())
            {
                long sum   = 0;
                var  range = Enumerable.Range(1, 6).ToList();
                tp.ExecuteBatch(range, (int input) =>
                {
                    Interlocked.Add(ref sum, (long)input);
                    Thread.Sleep((int)Math.Pow(input, 4) * 5);
                }, allowPartialBatchResumption: true);
                var waitingTasksAmount = tp.GetAllWaitingTasks().Count();
                var runningTasksAmount = tp.RunningTasksAmount;
                Assert.NotEqual(waitingTasksAmount + runningTasksAmount, 0);
                while (tp.GetAllWaitingTasks().Count() != 0 || tp.RunningTasksAmount != 0)
                {
                    Thread.Sleep(100);
                }
                var expectedSum = range.Sum(x => (long)x);
                Assert.Equal(expectedSum, sum);
            }
        }