public void Judge_run_InfinateLoop_should_exit() { // Arrange var compiler = new CSharpCodeProvider(); var options = new CompilerParameters { GenerateExecutable = true }; var asm = compiler.CompileAssemblyFromSource(options, InfinateLoopSource); var ji = new JudgeInfo { Input = null, MemoryLimitMb = 10.0f, Path = asm.PathToAssembly, TimeLimitMs = 100 }; // Act var result = Sandbox.Judge(ji); // Assert result.Succeed.Should().BeTrue(); result.ExitCode.Should().NotBe(0); result.TimeMs.Should().BeGreaterOrEqualTo(100); }
public void Judge_run_many_times_should_not_leak_memory(int times) { // Arrange var compiler = new CSharpCodeProvider(); var options = new CompilerParameters { GenerateExecutable = true }; var asm = compiler.CompileAssemblyFromSource(options, Code); asm.Errors.HasErrors.Should().BeFalse(); var info = new JudgeInfo { Input = "Flash", MemoryLimitMb = 10.0f, Path = asm.PathToAssembly, TimeLimitMs = 150 }; var parallelOption = new ParallelOptions { MaxDegreeOfParallelism = 4 }; // Act & Assert Console.WriteLine(GC.GetTotalMemory(true)); //for (int i = 0; i < times; ++i) Sandbox.Judge(info); Parallel.For(0, times, parallelOption, (i) => Sandbox.Judge(info)); Console.WriteLine(GC.GetTotalMemory(true)); Parallel.For(0, times, parallelOption, (i) => Sandbox.Judge(info)); Console.WriteLine(GC.GetTotalMemory(true)); Parallel.For(0, times, parallelOption, (i) => Sandbox.Judge(info)); Console.WriteLine(GC.GetTotalMemory(true)); }
public void Process_should_not_terminate_other_process() { // Arrange var compiler = new CSharpCodeProvider(); var options = new CompilerParameters { GenerateExecutable = true }; options.ReferencedAssemblies.Add("System.dll"); var asm = compiler.CompileAssemblyFromSource(options, TerminateProcessByIdSource); asm.Errors.HasErrors.Should().BeFalse(); var ps = Process.Start("calc"); var info = new JudgeInfo { Input = ps.Id.ToString(CultureInfo.InvariantCulture), MemoryLimitMb = 10.0f, Path = asm.PathToAssembly, TimeLimitMs = 100 }; // Act var result = NativeDll.Judge(info); var that = Process.GetProcessById(ps.Id); // Assert result.ExitCode.Should().NotBe(0); that.Should().NotBeNull(); // Clean up ps.Kill(); }
public static OperationResult <JudgeResult> JudgeExercise(Exercise exercise, ExerciseResult exerciseResult, User user) { JudgeResult judgeResult = null; try { judgeResult = ExerciseGeneratorFactory.DriveGenerator().JudgeExercise(exercise, exerciseResult); JudgeInfo judgeInfo = DataBaseFactory.DriveDataBase().GetJudgeInfo(user.UserID); if (judgeInfo == null) { return(new OperationResult <JudgeResult>("批改失败!", true, null)); } judgeInfo.QuestionNum += judgeResult.TotalNum; judgeInfo.QuestionErrorNum += judgeResult.ErrorNum; judgeInfo.LastestCompleteTime = DateTime.Now; if (!JudgeInfoLogic.UpdateJudgeInfo(judgeInfo, user)) { return(new OperationResult <JudgeResult>("更改批改信息失败!", true, null)); } } catch (Exception e) { return(new OperationResult <JudgeResult>("批改失败!" + e.Message, true, null)); } return(new OperationResult <JudgeResult>("批改成功!", true, judgeResult)); }
public void UpdateJudgeInfo(JudgeInfo judgeInfo, int userID) { using (SqlConnection connection = new SqlConnection(ConnectionString)) { SqlCommand command = new SqlCommand( string.Format( @" UPDATE dbo.judgeinformation_table SET totalNum = {0} , errorNum = {1}, latestTime = N'{2}' WHERE userid = {3} ", judgeInfo.QuestionNum, judgeInfo.QuestionErrorNum, judgeInfo.LastestCompleteTime, userID), connection); command.Connection.Open(); command.ExecuteNonQuery(); } }
public void Judge_run_MemoryLimitTest_should_be_limited() { // Arrange var compiler = new CSharpCodeProvider(); var options = new CompilerParameters { GenerateExecutable = true }; var asm = compiler.CompileAssemblyFromSource(options, MemoryLimitTestSource); var info = new JudgeInfo { Input = null, MemoryLimitMb = 10.0f, Path = asm.PathToAssembly, TimeLimitMs = 200 }; // Act var result = NativeDll.Judge(info); // Assert result.Succeed.Should().BeTrue(); result.ExitCode.Should().NotBe(0); result.MemoryMb.Should().BeGreaterOrEqualTo(20.0f); }
public JudgeInfo GetJudgeInfo(int userID) { JudgeInfo userInformation = null; using (SqlConnection connection = new SqlConnection(ConnectionString)) { SqlCommand command = new SqlCommand( string.Format( @" SELECT totalNum, errorNum, latestTime FROM dbo.judgeinformation_table WHERE userID = {0} ", userID), connection); command.Connection.Open(); var reader = command.ExecuteReader(); if (reader.Read() && reader.HasRows) { userInformation = new JudgeInfo( reader.GetInt32(0) , reader.GetInt32(1) , reader.IsDBNull(2) ? (DateTime?)null : reader.GetDateTime(2)); } } return(userInformation); }
public Result Judge([FromForm] JudgeInfo info) { var user_id = HttpContext.Session.GetInt32("UserId"); if (user_id == null) { throw new UserException(UserException.Type.NotSiginIn); } SubmissionServices.Judge(info.Code, user_id.Value, info.Title, info.Lang); return(new Result()); }
public static OperationResult UpdateJudgeInfo(JudgeInfo judgeInfo, User user) { try { DataBaseFactory.DriveDataBase().UpdateJudgeInfo(judgeInfo, user.UserID); } catch (Exception e) { return(new OperationResult("修改失败!" + e.Message, false)); } return(new OperationResult("修改成功!", true)); }
public static ObservableCollection <JudgeInfo> GetMatchOfficials(int matchID, int serverGroupID) { if (!CheckDBConnection()) { return(null); } SqlDataReader sr = null; ObservableCollection <JudgeInfo> judgeInfos = new ObservableCollection <JudgeInfo>(); try { SqlCommand sqlCmd = DVCommon.g_DataBaseCon.CreateCommand(); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.CommandText = "Proc_JudgePoint_GetMatchOfficial"; sqlCmd.Parameters.Add("@MatchID", SqlDbType.Int).Value = matchID; sqlCmd.Parameters.Add("@LanguageCode", SqlDbType.NVarChar, 10).Value = "ENG"; sqlCmd.Parameters.Add("@ServerGroupID", SqlDbType.Int).Value = serverGroupID; sr = sqlCmd.ExecuteReader(); while (sr.Read()) { JudgeInfo judge = new JudgeInfo(); judge.Function = (string)GetNoNullValue(sr, "F_Function", ""); judge.Name = (string)GetNoNullValue(sr, "F_RegisterName", ""); judge.NOC = (string)GetNoNullValue(sr, "NOC", ""); judge.FunctionID = (int)GetNoNullValue(sr, "F_FunctionID", -1); judge.RegisterID = (int)GetNoNullValue(sr, "F_RegisterID", -1); judge.Position = (string)GetNoNullValue(sr, "F_Position", ""); judge.ServantNum = (int)GetNoNullValue(sr, "F_ServantNum", -1); judge.PositionID = (int)GetNoNullValue(sr, "F_PositionID", -1); judge.GroupID = (int)GetNoNullValue(sr, "F_ServantGroupID", -1); judge.Group = (string)GetNoNullValue(sr, "F_ServantGroup", ""); judgeInfos.Add(judge); } return(judgeInfos); } catch (System.Exception ex) { AthleticsCommon.LastErrorMsg = "GetMatchOfficials():" + ex.Message; return(null); } finally { if (sr != null) { sr.Close(); } } }
protected void Page_Load(object sender, EventArgs e) { username = Request.QueryString["username"]; id = Request.QueryString["id"]; Debug.WriteLine(username); Debug.WriteLine(id); user = new CalculateOnlineJudge.Entity.User(Convert.ToInt32(id), username); OperationResult <JudgeInfo> judgeInfoOR = JudgeInfoLogic.GetJudgeInfo(user); JudgeInfo judgeInfo = judgeInfoOR.Result; Label1.Text = "总答题数" + judgeInfo.QuestionNum.ToString(); Label2.Text = "错题数" + judgeInfo.QuestionErrorNum.ToString(); Label3.Text = "正确率" + judgeInfo.CorrectRate.ToString(); Label4.Text = "错误率" + judgeInfo.ErrorRate.ToString(); Label5.Text = "最后答题时间\n" + judgeInfo.LastestCompleteTime.ToString(); }
public static OperationResult <JudgeInfo> GetJudgeInfo(User user) { JudgeInfo judgeInfo = null; try { judgeInfo = DataBaseFactory.DriveDataBase().GetJudgeInfo(user.UserID); if (judgeInfo == null) { return(new OperationResult <JudgeInfo>("获取失败!", false, null)); } } catch (Exception e) { return(new OperationResult <JudgeInfo>("获取失败!" + e.Message, false, null)); } return(new OperationResult <JudgeInfo>("获取成功!", true, judgeInfo)); }
public void Create_calc_should_return_success() { // arrange var calc = new JudgeInfo { Path = "calc.exe", MemoryLimitMb = 10.0f, TimeLimitMs = 100, Input = null }; // act var result = NativeDll.Judge(calc); // assert result.Succeed.Should().BeTrue(); result.ErrorCode.Should().Be(0); result.MemoryMb.Should().BeGreaterThan(0); }
public static JudgeResult Judge(JudgeInfo ji, Encoding languageEncoding) { var info = new SandboxRunInfo { LimitProcessCount = 1, MemoryLimitMb = ji.MemoryLimitMb, Path = ji.Path, TimeLimitMs = ji.TimeLimitMs }; SandboxIoResult ior = BeginRun(info); if (!ior.Succeed) { EndRun(ior.InstanceHandle); return((JudgeResult)ior); } ior.ErrorReadStream.Dispose(); var writeTask = Task.Run(async() => { using (var writer = new StreamWriter(ior.InputWriteStream, languageEncoding)) { await writer.WriteAsync(ji.Input); } }); var readTask = ReadToEndFrom(ior.OutputReadStream, languageEncoding); SandboxRunResult res = EndRun(ior.InstanceHandle); writeTask.Wait(); readTask.Wait(); var jr = (JudgeResult)res; jr.Output = readTask.Result; return(jr); }
public void Process_should_return_expected_output() { // Arrange var compiler = new CSharpCodeProvider(); var options = new CompilerParameters { GenerateExecutable = true }; var res = compiler.CompileAssemblyFromSource(options, Code); var info = new JudgeInfo { Input = "Flash", MemoryLimitMb = 10, Path = res.PathToAssembly, TimeLimitMs = 1000 }; // Act var result = NativeDll.Judge(info); // Assert result.Succeed.Should().BeTrue(); result.Output.Should().Be("Hey Flash!"); }
public void Ten_kb_input_should_work_well() { // Arrange var compiler = new CSharpCodeProvider(); var options = new CompilerParameters { GenerateExecutable = true }; var res = compiler.CompileAssemblyFromSource(options, Code); var info = new JudgeInfo { Input = new string('F', 10 * 1024), MemoryLimitMb = 10, Path = res.PathToAssembly, TimeLimitMs = 1000 }; var expectedOutput = string.Format("Hey {0}!", info.Input); // Act var result = NativeDll.Judge(info); // Assert result.Succeed.Should().BeTrue(); result.Output.Should().Be(expectedOutput); }
void XML_LoadJudgeData() { string readingNode; string readingAttr; do { TextAsset txt = Resources.Load(Global.judgeInfoPath) as TextAsset; XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(txt.text); readingNode = "JudgeData"; XmlNodeList tmpNodeList = xmldoc.SelectNodes(readingNode); if (tmpNodeList == null) { Debug.Log("Node not found : " + readingNode); break; } tmpNodeList = tmpNodeList[0].ChildNodes; Global.judgeInfo = new Dictionary <JudgeType, JudgeInfo>(); for (int i = 0; i < tmpNodeList.Count; ++i) { JudgeInfo tmpJudgeInfo = new JudgeInfo(); readingAttr = "idx"; if (tmpNodeList[i].Attributes[readingAttr] == null) { Debug.Log("Attribute not found : " + readingAttr); break; } tmpJudgeInfo.id = XmlConvert.ToInt32(tmpNodeList[i].Attributes[readingAttr].Value); readingAttr = "judge_name"; if (tmpNodeList[i].Attributes[readingAttr] == null) { Debug.Log("Attribute not found : " + readingAttr); break; } tmpJudgeInfo.judge_name = tmpNodeList[i].Attributes[readingAttr].Value; readingAttr = "hp"; if (tmpNodeList[i].Attributes[readingAttr] == null) { Debug.Log("Attribute not found : " + readingAttr); break; } tmpJudgeInfo.hp = XmlConvert.ToInt32(tmpNodeList[i].Attributes[readingAttr].Value); readingAttr = "hpvalue"; if (tmpNodeList[i].Attributes[readingAttr] == null) { Debug.Log("Attribute not found : " + readingAttr); break; } tmpJudgeInfo.hpValue = XmlConvert.ToSingle(tmpNodeList[i].Attributes[readingAttr].Value); readingAttr = "term"; if (tmpNodeList[i].Attributes[readingAttr] == null) { Debug.Log("Attribute not found : " + readingAttr); break; } tmpJudgeInfo.term = XmlConvert.ToSingle(tmpNodeList[i].Attributes[readingAttr].Value); readingAttr = "score"; if (tmpNodeList[i].Attributes[readingAttr] == null) { Debug.Log("Attribute not found : " + readingAttr); break; } tmpJudgeInfo.score = XmlConvert.ToInt32(tmpNodeList[i].Attributes[readingAttr].Value); Global.judgeInfo[(JudgeType)i] = tmpJudgeInfo; } readingNode = null; } while (false); if (readingNode != null) { Debug.Log("Failed to reading Node : " + readingNode); return; } }
private async Task Judge(IEnumerable <QuestionData> datas, CompileResult asm, Encoding languageEncoding) { int runTimeMs = 0; float peakMemoryMb = 0; foreach (QuestionData data in datas) { if (_spush.Language == Languages.Java) { data.MemoryLimitMb += AppSettings.JavaGivenMemoryMb; } var info = new JudgeInfo { Input = data.Input, MemoryLimitMb = data.MemoryLimitMb, Path = asm.PathToAssembly, TimeLimitMs = data.TimeLimit, }; _log.DebugExt("NativeDll Juding..."); var result = Sandbox.Judge(info, languageEncoding); _log.DebugExt("NativeDll Judged..."); runTimeMs += result.TimeMs; peakMemoryMb = Math.Max(peakMemoryMb, result.MemoryMb); if (!result.IsDone) { await _client.Update(ClientJudgeModel.Create(_spush.Id, SolutionState.RuntimeError, runTimeMs, peakMemoryMb)); // system error return; } if (result.TimeMs >= data.TimeLimit) { await _client.Update(ClientJudgeModel.Create(_spush.Id, SolutionState.TimeLimitExceed, runTimeMs, peakMemoryMb)); return; } if (result.MemoryMb >= data.MemoryLimitMb) { await _client.Update(ClientJudgeModel.Create(_spush.Id, SolutionState.MemoryLimitExceed, runTimeMs, peakMemoryMb)); return; } if (result.ExitCode != 0) { await _client.Update(ClientJudgeModel.Create(_spush.Id, SolutionState.RuntimeError, runTimeMs, peakMemoryMb)); // application error return; } var trimed = result.Output.TrimEnd(); if (trimed != data.Output) { _log.DebugExt(() => $"\r\nExpected: \r\n{data.Output} \r\nActual: \r\n{result.Output}"); await _client.Update(ClientJudgeModel.CreateWrongAnswer(_spush.Id, runTimeMs, peakMemoryMb, data.Id, trimed)); return; } } await _client.Update(ClientJudgeModel.Create(_spush.Id, SolutionState.Accepted, runTimeMs, peakMemoryMb)); }