Exemplo n.º 1
0
        // MILLI seconds
        public static CpuLoader Run(int minDuration = 1, int minCpuUsage = 1, bool needKernelLoad = true)
        {
            CpuLoader ret = new CpuLoader();

            ret.LoadCpu(minDuration, minCpuUsage, needKernelLoad);
            return(ret);
        }
Exemplo n.º 2
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.");
        }
        public void Grow_Usage()
        {
            if (CrossInfo.ThePlatform == CrossInfo.Platform.Windows)
            {
                return;
            }

            CpuLoader.Run(1, 1, true);
            CpuUsageScope scope = CrossInfo.ThePlatform == CrossInfo.Platform.MacOSX
                ? CpuUsageScope.Process
                : CpuUsageScope.Thread;

            Console.WriteLine($"Supported scope: {scope}");
            LinuxResourceUsageReader.GetByScope(scope);
            var prev = LinuxResourceUsageReader.GetByScope(scope);

            for (int i = 0; i < 10; i++)
            {
                CpuLoader.Run(1, 1, true);
                CpuUsage?next = LinuxResourceUsageReader.GetByScope(scope);
                Console.WriteLine($" {i} -> {next}");
                Assert.GreaterOrEqual(next.Value.KernelUsage.TotalMicroSeconds, prev.Value.KernelUsage.TotalMicroSeconds);
                Assert.GreaterOrEqual(next.Value.UserUsage.TotalMicroSeconds, prev.Value.UserUsage.TotalMicroSeconds);
                prev = next;
            }
        }
Exemplo n.º 4
0
        public void Show_Precision_Histogram(PrecisionCase precisionCase)
        {
            PosixProcessPriority.ApplyMyPriority(precisionCase.Priority);
            var preJit = CpuLoader.Run(11, 1, true).IncrementsCount;

            Console.WriteLine($"OS: {CrossFullInfo.OsDisplayName}");
            Console.WriteLine($"CPU: {CrossFullInfo.ProcessorName}");
            var actualCase = new PrecisionCase(Process.GetCurrentProcess().PriorityClass, precisionCase.IncludeKernelLoad);

            Console.WriteLine($"Granularity[{actualCase}] (it may vary if Intel SpeedStep, TurboBoost, etc are active):");
            int count = CrossFullInfo.IsMono ? 1 : 9;

            for (int i = 1; i <= count; i++)
            {
                var    cpuLoadResult = CpuLoader.Run(minDuration: 1000, needKernelLoad: precisionCase.IncludeKernelLoad);
                long   granularity   = cpuLoadResult.IncrementsCount;
                double microSeconds  = 1000000d / granularity;
                Console.WriteLine($" #{i}: {granularity} increments a second, eg {microSeconds:n1} microseconds in average");

                // Percentile report
                PercentileCalc <long> percentileCalc = new PercentileCalc <long>(cpuLoadResult.Population, x => x, x => x.ToString("n3"));
                double[] percents = new[] { 1d, 5, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 99 };
                // double[] percents = new[] {1d, 5, 10, 90, 95, 99}.Reverse().ToArray();
                StringBuilder pcReport = new StringBuilder();
                for (int p = 0; p < percents.Length; p++)
                {
                    pcReport.Append($"  {percents[p].ToString("f0").PadLeft(3)}%: {percentileCalc[percents[p]].ToString("n3"),-12}");
                    if ((p + 1) % 5 == 0)
                    {
                        pcReport.AppendLine();
                    }
                }

                Console.WriteLine(pcReport);

                // Histogram report
                Statistica <long> stat = new Statistica <long>(cpuLoadResult.Population, x => (double)x, x => x, x => x.ToString("n3"));
                var histogram          = stat.BuildReport(12, 3);
                Console.WriteLine(histogram.ToConsole("CPU Usage increments (microseconds)", 42));
            }
        }
Exemplo n.º 5
0
 // Load CPU by number of milliseconds
 private void LoadCpu(int milliseconds = 42) => CpuLoader.Run(minDuration: milliseconds, minCpuUsage: milliseconds, needKernelLoad: true);