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 CpuUsageAsyncMinimal()
        {
            CpuUsageAsyncWatcher watcher = new CpuUsageAsyncWatcher();
            await Task.Run(() => "nothing to do");

            var totals = watcher.Totals.GetSummaryCpuUsage();
        }
Exemple #3
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 #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.");
        }
Exemple #5
0
        private async Task PreJit()
        {
            Console.WriteLine("Pre-Jitting CpuUsageAsyncWatcher class");
            CpuUsageAsyncWatcher watch = new CpuUsageAsyncWatcher();
            await Task.Run(() => LoadCpu(1));

            await NotifyFinishedTasks();

            watch.Stop();
            watch.ToHumanString();
            Console.WriteLine("Pre-Jitted CpuUsageAsyncWatcher class");
        }
Exemple #6
0
        async Task <bool> CheckExpectedCpuUsage(int expectedMilliseconds, TaskFactory factory)
        {
            // Act
            CpuUsageAsyncWatcher watcher = new CpuUsageAsyncWatcher();
            await factory.StartNew(() => LoadCpu(expectedMilliseconds));

            watcher.Stop();
            Console.WriteLine(watcher.ToHumanString(taskDescription: $"'Expected CPU Load is {expectedMilliseconds} milli-seconds'"));

            Assert.AreEqual(2, watcher.Totals.Count, "The CheckExpectedCpuUsage should produce exact 2 context switches");
            // Assert: CpuUsage should be expectedMilliseconds
            var  actualSeconds = watcher.GetSummaryCpuUsage().TotalMicroSeconds / 1000000d;
            bool isOk          = actualSeconds >= expectedMilliseconds / 1000d;

            return(isOk);
        }