Exemple #1
0
        public async Task AwaitForEachTests(AsyncSchedulerCase testEnvironment)
        {
            // Pre JIT
            await testEnvironment.Factory.StartNew(() => CpuLoader.Run(minDuration: 1, minCpuUsage: 1, needKernelLoad: true));

            // Act (durations are for debugging)
            CpuUsageAsyncWatcher watcher = new CpuUsageAsyncWatcher();
            long expectedMicroseconds    = 0;

            await foreach (var milliseconds in GetLoadings())
            {
                await testEnvironment.Factory.StartNew(() => CpuLoader.Run(minDuration: milliseconds, minCpuUsage: milliseconds, needKernelLoad: true));

                expectedMicroseconds += milliseconds * 1000L;
            }

            var  totals             = watcher.Totals;
            long actualMicroseconds = totals.GetSummaryCpuUsage().TotalMicroSeconds;

            Console.WriteLine($"Expected usage: {(expectedMicroseconds/1000d):n3}, Actual usage: {(actualMicroseconds/1000d):n3} milliseconds");
            Console.WriteLine(totals.ToHumanString(taskDescription: "ParallelTests()"));

            // Assert
            Assert.GreaterOrEqual(totals.Count, 8, "Number of context switches should be 8 at least");
            // 0.15 for qemu armhf, less for rest
            Assert.AreEqual(expectedMicroseconds, actualMicroseconds, 0.15d * expectedMicroseconds, "Actual CPU Usage should be about as expected.");
        }
Exemple #2
0
        public async Task ParallelTests(AsyncSchedulerCase testEnvironment)
        {
            if (!IsSupported())
            {
                return;
            }

            // Act (durations are for debugging)
            CpuUsageAsyncWatcher watcher = new CpuUsageAsyncWatcher();
            TaskFactory          tf      = new TaskFactory();
            var task4 = testEnvironment.Factory.StartNew(() => LoadCpu(milliseconds: 2400));
            var task3 = testEnvironment.Factory.StartNew(() => LoadCpu(milliseconds: 2100));
            var task2 = testEnvironment.Factory.StartNew(() => LoadCpu(milliseconds: 1800));
            var task1 = testEnvironment.Factory.StartNew(() => LoadCpu(milliseconds: 1500));
            await Task.WhenAll(task1, task2, task3, task4);

            await NotifyFinishedTasks();

            watcher.Stop();
            var totals = watcher.Totals;

            // Assert
            long actualMicroseconds   = totals.GetSummaryCpuUsage().TotalMicroSeconds;
            long expectedMicroseconds = 1000L * (2400 + 2100 + 1800 + 1500);

            Console.WriteLine($"Expected usage: {(expectedMicroseconds/1000d):n3}, Actual usage: {(actualMicroseconds/1000d):n3} milliseconds");
            Console.WriteLine(totals.ToHumanString(taskDescription: "ParallelTests()"));
            // 7 for windows 8 cores, rarely 6 for slow 2 core machine, but 7 for single core armv5
            Assert.GreaterOrEqual(totals.Count, 6, "Number of context switches should be 6 at least");
            Assert.AreEqual(expectedMicroseconds, actualMicroseconds, 0.1d * expectedMicroseconds, "Actual CPU Usage should be about as expected.");
        }
Exemple #3
0
        public async Task ConcurrentTest(AsyncSchedulerCase testEnvironment)
        {
            if (!IsSupported())
            {
                return;
            }
            // second context switch is lost for ThreadPerTaskScheduler
            if (testEnvironment.Scheduler is ThreadPerTaskScheduler)
            {
                return;
            }

            IList <Task> tasks      = new List <Task>();
            int          errors     = 0;
            int          maxThreads = Math.Min(Environment.ProcessorCount + 9, 16);

            // hypothesis: each thread load should be exponential
            for (int i = 1; i <= maxThreads; i++)
            {
                var iCopy = i;
                tasks.Add(testEnvironment.Factory.StartNew(async() =>
                {
                    var expectedMilliseconds = iCopy * 400;
                    if (!await CheckExpectedCpuUsage(expectedMilliseconds, testEnvironment.Factory))
                    {
                        Interlocked.Increment(ref errors);
                    }
                }, TaskCreationOptions.LongRunning).Unwrap());
            }

            await Task.WhenAll(tasks);

            Assert.IsTrue(errors == 0, "Concurrent CpuUsageAsyncWatchers should not infer on each other. See details above");
        }
Exemple #4
0
        public async Task SimpleTests(AsyncSchedulerCase testEnvironment)
        {
            if (!IsSupported())
            {
                return;
            }

            // Act (durations are for debugging)
            CpuUsageAsyncWatcher watch = new CpuUsageAsyncWatcher();
            await testEnvironment.Factory.StartNew(() => LoadCpu(milliseconds : 200));

            await testEnvironment.Factory.StartNew(() => LoadCpu(milliseconds : 500));

            await testEnvironment.Factory.StartNew(() => LoadCpu(milliseconds : 800));

            await NotifyFinishedTasks();

            watch.Stop();
            var totals = watch.Totals;

            // Assert
            long actualMicroseconds   = totals.GetSummaryCpuUsage().TotalMicroSeconds;
            long expectedMicroseconds = 1000L * (200 + 500 + 800);

            Console.WriteLine($"Expected usage: {(expectedMicroseconds/1000d):n3}, Actual usage: {(actualMicroseconds/1000d):n3} milliseconds");
            Console.WriteLine(watch.ToHumanString(taskDescription: "SimpleTests()"));

            Assert.GreaterOrEqual(totals.Count, 6, "Number of context switches should be 6 at least");
            Assert.AreEqual(expectedMicroseconds, actualMicroseconds, 0.1d * expectedMicroseconds, "Actual CPU Usage should be about as expected.");
        }