Ejemplo n.º 1
0
        internal static ResultInformation Run(string path, string arguments, int timeLimit = 1000)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new Exception("Invalid file path format.");
            }

            if (!File.Exists(path))
            {
                throw new Exception("The file does not exist.");
            }

            ResultInformation result = new ResultInformation();

            result.TimeLimit = timeLimit;

            ProcessStartInfo startInfo = new ProcessStartInfo();

            startInfo.FileName               = path;
            startInfo.Arguments              = arguments;
            startInfo.CreateNoWindow         = true;
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError  = true;
            startInfo.UseShellExecute        = false;

            using (Process process = Process.Start(startInfo))
            {
                if (!process.HasExited)
                {
                    process.Refresh();

                    result.PhysicalMemoryUsage     = process.WorkingSet64;
                    result.BasePriority            = process.BasePriority;
                    result.UserProcessorTime       = process.UserProcessorTime;
                    result.PrivilegedProcessorTime = process.PrivilegedProcessorTime;
                    result.TotalProcessorTime      = process.TotalProcessorTime;
                    result.PagedSystemMemorySize   = process.PagedSystemMemorySize64;
                    result.PagedMemorySize         = process.PagedMemorySize64;

                    result.PeakPagedMemorySize   = process.PeakPagedMemorySize64;
                    result.PeakVirtualMemorySize = process.PeakVirtualMemorySize64;
                    result.PeakWorkingSet        = process.PeakWorkingSet64;

                    System.Threading.Thread.Sleep(timeLimit);
                }
                process.Kill();

                result.ExitCode     = process.ExitCode;
                result.OutputResult = process.StandardOutput.ReadToEnd();
                result.ErrorResult  = process.StandardError.ReadToEnd();
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates model classes to \Assets\Models directory.
        /// </summary>
        /// <param name="provider">Database provider.</param>
        /// <param name="connectionString">Connection string from database.</param>
        public ResultInformation Create(DatabaseProvider provider, string connectionString)
        {
            var psi = new ProcessStartInfo(this._powerShellPath, ConfigurationConstants.InternalConstants.MODEL_SCRIPT_PATH)
            {
                UseShellExecute        = false,
                CreateNoWindow         = true,
                WindowStyle            = ProcessWindowStyle.Hidden,
                RedirectStandardError  = true,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true
            };

            var process = new Process()
            {
                StartInfo = psi
            };

            // redirect stdout and stderr
            process.OutputDataReceived += (sender, args) =>
                                          OnOutputReceived(new OutputEventArgs(OutputType.StandardOutput, args.Data));
            process.ErrorDataReceived += (sender, args) =>
                                         OnOutputReceived(new OutputEventArgs(OutputType.StandardError, args.Data));
            var result  = new ResultInformation();
            var started = false;

            try
            {
                started = process.Start();
            }
            catch (Exception e)
            {
                result.ExitStatus   = ExitStatus.ExitFailure;
                result.ErrorMessage = e.Message;
            }

            if (started)
            {
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();

                // write to stdin
                // first argument is connection string
                process.StandardInput.WriteLine(connectionString);
                // second argument is DB provider
                process.StandardInput.WriteLine(provider.ToString());
                process.StandardInput.Flush();

                process.WaitForExit();

                if (process.ExitCode != 0)
                {
                    result.ExitStatus   = ExitStatus.ExitFailure;
                    result.ErrorMessage = this._errorBuilder.ToString();
                }
                else
                {
                    result.ExitStatus   = ExitStatus.ExitSuccess;
                    result.ErrorMessage = string.Empty;
                }
            }

            process.StandardInput.Close();

            return(result);
        }
Ejemplo n.º 3
0
        static void Scoring(string path = null, string setCode = null, bool inputMode = true)
        {
            // If the input mode is true.
            if (inputMode == true)
            {
                Console.WriteLine("Enter the path that contains the file to be scored.");
                Console.Write("PATH>");
                path = Console.ReadLine();
                Console.WriteLine("Enter the setcode in question.");
                Console.Write("SET-CODE>");
                setCode = Console.ReadLine();
            }

            // Exception handling.
            if (string.IsNullOrEmpty(setCode))
            {
                throw new Exception("Invalid setcode format.");
            }


            // Split set code
            string set  = setCode.Split('-')[0];
            int    code = int.Parse(setCode.Split('-')[1]);

            // Problems Database initialize.
            if (DatabaseManager.IsOpen())
            {
                DatabaseManager.RestartDatabase(@"probs.db");
            }
            else
            {
                DatabaseManager.OpenDatabase(@"probs.db");
            }

            Problem prob = new Problem();

            // Print problem information.
            if (DatabaseManager.IsTableExists(set))
            {
                prob = DatabaseManager.GetProblem(set, code);
                Console.WriteLine("--------------------------------------------------\n" +
                                  $"TITLE : [{setCode}]{prob.Title}\n" +
                                  $"DESCRIPTION : {prob.Description}\n" +
                                  $"MEMORY LIMIT : {prob.Memory} Byte.\n" +
                                  $"TIME LIMIT : {prob.TimeLimit} Milliseconds.\n" +
                                  $"--------------------------------------------------");
            }
            else
            {
                Console.WriteLine("Problem set does not exist.");
            }

            // Answers Database initialize.
            if (DatabaseManager.IsOpen())
            {
                DatabaseManager.RestartDatabase(@"answers.db");
            }
            else
            {
                DatabaseManager.OpenDatabase(@"answers.db");
            }

            // Scoring process.
            if (DatabaseManager.IsTableExists(setCode))
            {
                List <Answer> answerList = DatabaseManager.GetAnswerList(setCode);

                foreach (Answer answer in answerList)
                {
                    ResultInformation result = ProcessRunner.Run(path, answer.Input, prob.TimeLimit); // Run program.

                    if (result.OutputResult != answer.Output)                                         // If the output value is different.
                    {
                        Console.WriteLine($"Invalid output value(Normal:{answer.Output}, Current:{result.OutputResult}).");
                    }

                    if (result.ErrorResult != null) // If an error has occurred.
                    {
                        Console.WriteLine($"An error occured while running process({result.ErrorResult}).");
                    }

                    if (result.PrivilegedProcessorTime.TotalMilliseconds > prob.TimeLimit) // If the time is exceeded.
                    {
                        Console.WriteLine($"The timeout has been exceeded.\n({result.PrivilegedProcessorTime})");
                    }

                    if (result.PeakWorkingSet > prob.Memory) // If memory usage is exceeded.
                    {
                        Console.WriteLine($"Memory usage exceeded({result.PeakWorkingSet}).");
                    }

                    if (result.ExitCode != 0) // If it is not a normal exit code.
                    {
                        Console.WriteLine($"The exit code is not 0(Exit code : {result.ExitCode}).");
                    }
                }
            }
            else
            {
                Console.WriteLine("Answer set does not exist.");
            }

            Console.WriteLine();
            SwitchToConsole();
        }