示例#1
0
        public static CompilerBenchmark Success(
            Compiler compiler, TimingResult timeToCompile, int numberFunctions)
        {
            if (timeToCompile.Elapsed == TimeSpan.Zero)
            {
                throw new ArgumentException(
                          "Compiling cannot take zero seconds", nameof(timeToCompile));
            }

            return(new CompilerBenchmark(compiler, timeToCompile, numberFunctions, true));
        }
示例#2
0
        private CompilerBenchmark(
            Compiler compiler, TimingResult timeToCompile, int numberFunctions, bool compiled)
        {
            if (numberFunctions < 0)
            {
                throw new ArgumentException(
                          "Cannot compile zero functions", nameof(numberFunctions));
            }

            Compiler        = compiler;
            TimeToCompile   = timeToCompile;
            NumberFunctions = numberFunctions;
            Compiled        = compiled;
        }
        static TimingResult?CmdTimeBenchmark(Compiler compiler, string args)
        {
            var sout = new List <string>();

            using (var p = new Process())
            {
                // RESULT: %x %e %M means (exit code, elapsed time in seconds, max resident set size)
                p.StartInfo.FileName  = "/usr/bin/time";
                p.StartInfo.Arguments = $@"-f ""RESULT: %x %e %M"" {compiler.Exe} {args}";

                p.StartInfo.UseShellExecute        = false;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError  = true;
                p.StartInfo.ErrorDialog            = false;

                Console.WriteLine($"\"{p.StartInfo.FileName} {p.StartInfo.Arguments}\"");
                foreach (string k in compiler.EnvironmentVariables.Keys)
                {
                    Console.Write($" and {k}=\"{compiler.EnvironmentVariables[k]}\"");
                    p.StartInfo.EnvironmentVariables[k] = compiler.EnvironmentVariables[k];
                }

                // The last line of the output will be from /usr/bin/time
                p.OutputDataReceived += (sender, outputLine) => {
                    if (outputLine.Data != null)
                    {
                        sout.Add(outputLine.Data);
                    }
                };
                p.ErrorDataReceived += (sender, errorLine) => {
                    if (errorLine.Data != null)
                    {
                        sout.Add(errorLine.Data);
                    }
                };

                p.Start();
                p.BeginOutputReadLine();
                p.BeginErrorReadLine();
                p.WaitForExit();

                if (p.ExitCode != 0)
                {
                    Thread.Sleep(2500);
                    return(null);
                }
            }

            for (var i = sout.Count - 1; i >= 0; --i)
            {
                var line = sout[i];
                if (!line.StartsWith("RESULT:"))
                {
                    continue;
                }

                var results  = line.Split(' ');
                var exitCode = results[1];
                if (int.Parse(exitCode) != 0)
                {
                    Console.WriteLine($"  ! Compilation failed for '{compiler.Exe} {args}'");
                    Thread.Sleep(2500);
                    return(null);
                }

                var elapsedSeconds     = double.Parse(results[2]);
                var maxResidentSetSize = int.Parse(results[3]);
                var timing             = new TimingResult(elapsedSeconds, maxResidentSetSize);
                Console.WriteLine($"  - Took {timing.Elapsed} MRSS {maxResidentSetSize}");
                return(timing);
            }

            throw new Exception("Result of /usr/bin/time not found in output of {" + string.Join("\n", sout) + "}");
        }