public void MemoryCounterConstructorTest() { Process process = new Process(); int interval = 0; //Correct interval && correct process try { MemoryCounter memoryCounter = new MemoryCounter(process, interval); Assert.AreEqual(true, true); } catch(Exception) { Assert.AreEqual(true,false); } //Correct interval && null process try { process = null; MemoryCounter memoryCounter = new MemoryCounter(process, interval); Assert.AreEqual(false, true); } catch (Exception) { Assert.AreEqual(true, true); } //Incorrect(minus eq 0) interval && correct process try { interval = 0; MemoryCounter memoryCounter = new MemoryCounter(process, interval); Assert.AreEqual(false, true); } catch (Exception) { Assert.AreEqual(true, true); } }
/// <summary> /// Runs provided .exe file and returns the resulting statistic( time usage, memory usage, output). /// </summary> static void Main() { //Set error mode, for hiding error message boxes. SetErrorMode(0x0001 | 0x0002 | 0x0004 | 0x8000); Result result = new Result(); result.ProgramStatus = Status.Running; //Read input data string exePath = Console.ReadLine(); string serializedProgram = Console.In.ReadToEnd(); //deserialize problem XmlSerializer deserializer = new XmlSerializer(typeof(Program)); MemoryStream inputStream = new MemoryStream(); byte[] buffer = new byte[serializedProgram.Length]; for (int i = 0; i < serializedProgram.Length; i++) { buffer[i] = (byte)serializedProgram[i]; } inputStream.Write(buffer, 0, buffer.Length); inputStream.Position = 0; Program program = deserializer.Deserialize(inputStream) as Program; //create new process Process exeProcess = new Process(); exeProcess.StartInfo.RedirectStandardError = true; exeProcess.StartInfo.RedirectStandardInput = true; exeProcess.StartInfo.RedirectStandardOutput = true; exeProcess.StartInfo.CreateNoWindow = true; exeProcess.StartInfo.UseShellExecute = false; //check if provided exe. is .NET based exeProcess.StartInfo.FileName = NETWrapperPath; exeProcess.StartInfo.Arguments = "\"" + exePath + "\""; try { //if true we run .NET wrapper Assembly.LoadFile(Path.GetFullPath(exePath)); if (!File.Exists(exePath)) { throw new FileNotFoundException("Can't find file", exePath); } } catch { //if not we simply run it. exeProcess.StartInfo.Arguments = ""; exeProcess.StartInfo.FileName = exePath; } //start process MemoryCounter memoryCounter = new MemoryCounter(exeProcess, 20); exeProcess.Start(); Thread.Sleep(100); //write input data exeProcess.StandardInput.Write(program.InputTest); exeProcess.StandardInput.Close(); exeProcess.WaitForExit(program.TimeLimit); if (!exeProcess.HasExited) { exeProcess.Kill(); result.ProgramStatus = Status.TimeLimit; } memoryCounter.Stop(); //get program statistic result.Error = exeProcess.StandardError.ReadToEnd(); result.Output = exeProcess.StandardOutput.ReadToEnd(); result.TimeUsed = (int)exeProcess.TotalProcessorTime.TotalMilliseconds; result.MemoryUsed = (int)memoryCounter.Memory / 1024; //some programs output 0 char that is not acceptible, we replace it with 13(return) char for (int i = 0; i < 32; i++) { if (i != 9 && i != 10 && i != 13) { result.Output = result.Output.Replace((char)i, (char)13); } } //serialize and deserialize output //this is done becase string is changed during serialization //as program.OutputTest was serilized/deserialized //we need make same chnges with result.Output XmlSerializer stringSerializer = new XmlSerializer(typeof(string)); MemoryStream stringStream = new MemoryStream(); stringSerializer.Serialize(stringStream, result.Output); stringStream.Position = 0; result.Output = stringSerializer.Deserialize(stringStream) as string; //set program status if (result.ProgramStatus != Status.TimeLimit) { if (exeProcess.ExitCode != 0) { result.ProgramStatus = Status.Crashed; } else { if (result.MemoryUsed > program.MemoryLimit) { result.ProgramStatus = Status.MemoryLimit; } else { if (result.Output == program.OutputTest) { result.ProgramStatus = Status.Accepted; } else { result.ProgramStatus = Status.WrongAnswer; } } } } //serialize the result of progarm execution XmlSerializer serializer = new XmlSerializer(typeof(Result)); MemoryStream outputStream = new MemoryStream(); serializer.Serialize(outputStream, result); //write the result to the stream Stream standardOutput = Console.OpenStandardOutput(); outputStream.WriteTo(standardOutput); standardOutput.Close(); }
/// <summary> /// Executes provided exe file and returns the result of program using. /// </summary> /// /// <param name="exePath"> /// Path of exe file to run. /// </param> /// /// <param name="program"> /// Execunitg constraints.(like memory limit, time limit) /// </param> /// /// <returns> /// Detailed result of program executing. /// </returns> /// /// <exception cref="ArgumentNullException"> /// If any argument is null. /// </exception> /// <exception cref="FileNotFoundException"> /// If provided path is invalid. /// </exception> /// <exception cref=""> /// /// </exception> public static Result ExecuteWin32(string exePath, Program program, string arguments) { //validate arguments ProjectHelper.ValidateNotNull(program, "program"); ProjectHelper.ValidateFileExists(exePath, "exePath"); //Set error mode, for hiding error message boxes. SetErrorMode(0x0001 | 0x0002 | 0x0004 | 0x8000); Result result = new Result(); result.ProgramStatus = Status.Running; //create new process using (Process exeProcess = new Process()) { exeProcess.StartInfo.RedirectStandardError = true; exeProcess.StartInfo.RedirectStandardInput = true; exeProcess.StartInfo.RedirectStandardOutput = true; exeProcess.StartInfo.CreateNoWindow = true; exeProcess.StartInfo.UseShellExecute = false; exeProcess.StartInfo.FileName = exePath; exeProcess.StartInfo.Arguments = arguments; exeProcess.StartInfo.Arguments = exeProcess.StartInfo.Arguments; //start process MemoryCounter memoryCounter = new MemoryCounter(exeProcess, 20); exeProcess.Start(); Thread.Sleep(200); //write input data exeProcess.StandardInput.Write(program.InputTest); exeProcess.StandardInput.Close(); exeProcess.WaitForExit(program.TimeLimit); if (!exeProcess.HasExited) { exeProcess.Kill(); result.ProgramStatus = Status.TimeLimit; } memoryCounter.Stop(); //get program statistic result.Error = exeProcess.StandardError.ReadToEnd(); result.Output = exeProcess.StandardOutput.ReadToEnd(); result.TimeUsed = (int)exeProcess.TotalProcessorTime.TotalMilliseconds; result.MemoryUsed = (int)memoryCounter.Memory / 1024; result.Output = result.Output.Trim(); //set program status if (result.ProgramStatus != Status.TimeLimit) { if (exeProcess.ExitCode != 0) { result.ProgramStatus = Status.Crashed; } else { if (result.MemoryUsed > program.MemoryLimit) { result.ProgramStatus = Status.MemoryLimit; } else { if (result.Output == program.OutputTest) { result.ProgramStatus = Status.Accepted; } else { result.ProgramStatus = Status.WrongAnswer; } } } } } return result; }
public void MemoryCounterConstructorTest4() { Process process = new Process(); int interval = 10000; MemoryCounter memoryCounter = new MemoryCounter(process,interval); var currentProcess = memoryCounter.Process; Assert.AreEqual(currentProcess != null,true); }
public void MemoryCounterConstructorTest3() { Process process = new Process(); int interval = 0; try { MemoryCounter memoryCounter = new MemoryCounter(process, interval); Assert.AreEqual(true, false); } catch (Exception) { Assert.AreEqual(true, true); } }
/// <summary> /// Executes provided exe file and returns the result of program using. /// </summary> /// /// <param name="exePath"> /// Path of exe file to run. /// </param> /// /// <param name="program"> /// Execunitg constraints.(like memory limit, time limit) /// </param> /// /// <returns> /// Detailed result of program executing. /// </returns> /// /// <exception cref="ArgumentNullException"> /// If any argument is null. /// </exception> /// <exception cref="FileNotFoundException"> /// If provided path is invalid. /// </exception> /// <exception cref=""> /// /// </exception> public static Result ExecuteWin32(string exePath, Program program, string arguments) { //validate arguments ProjectHelper.ValidateNotNull(program, "program"); ProjectHelper.ValidateFileExists(exePath, "exePath"); //Set error mode, for hiding error message boxes. SetErrorMode(0x0001 | 0x0002 | 0x0004 | 0x8000); Result result = new Result(); result.ProgramStatus = Status.Running; //create new process using (Process exeProcess = new Process()) { exeProcess.StartInfo.RedirectStandardError = true; exeProcess.StartInfo.RedirectStandardInput = true; exeProcess.StartInfo.RedirectStandardOutput = true; exeProcess.StartInfo.CreateNoWindow = true; exeProcess.StartInfo.UseShellExecute = false; exeProcess.StartInfo.FileName = exePath; exeProcess.StartInfo.Arguments = arguments; exeProcess.StartInfo.Arguments = exeProcess.StartInfo.Arguments; //start process MemoryCounter memoryCounter = new MemoryCounter(exeProcess, 20); exeProcess.Start(); Thread.Sleep(200); //write input data exeProcess.StandardInput.Write(program.InputTest); exeProcess.StandardInput.Close(); exeProcess.WaitForExit(program.TimeLimit); if (!exeProcess.HasExited) { exeProcess.Kill(); result.ProgramStatus = Status.TimeLimit; } memoryCounter.Stop(); //get program statistic result.Error = exeProcess.StandardError.ReadToEnd(); result.Output = exeProcess.StandardOutput.ReadToEnd(); result.TimeUsed = (int)exeProcess.TotalProcessorTime.TotalMilliseconds; result.MemoryUsed = (int)memoryCounter.Memory / 1024; result.Output = result.Output.Trim(); //set program status if (result.ProgramStatus != Status.TimeLimit) { if (exeProcess.ExitCode != 0) { result.ProgramStatus = Status.Crashed; } else { if (result.MemoryUsed > program.MemoryLimit) { result.ProgramStatus = Status.MemoryLimit; } else { if (result.Output == program.OutputTest) { result.ProgramStatus = Status.Accepted; } else { result.ProgramStatus = Status.WrongAnswer; } } } } } return(result); }