2017-05-26 115 views
-1

我工作的一个在线编程项目法官像HackerRank,Codeforces等等如何测量由Java中的进程执行的代码的执行时间?

我有线程池,当请求到来时,Web服务从线程池中获取一个线程,该线程编译使用的ProcessBuilder代码(直到这里所有东西都是okey),在编译之后,该线程通过再次使用新的Processbuilder启动执行部分。但我的“超过时限”部分计算不正确。当请求数量增加时,我认为这个过程运行缓慢,因此任何基本代码都会超时。我怎样才能测量一个进程执行的代码的执行时间?(测量不应该受到请求数的影响)

编辑:我的进程应该等待进程的用户时间。但我不知道如何去做这个。

我执行的代码是在这里:

package org.anil.CodeChecker.process; 

import java.io.BufferedReader; 
import java.io.BufferedWriter; 
import java.io.File; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStream; 
import java.io.OutputStreamWriter; 
import java.util.concurrent.TimeUnit; 

import org.anil.CodeChecker.model.ExecutorModel; 

public class Executor { 
    private ProcessBuilder p; 
    private String path; 
    private String input; 
    private String output; 
    private String lang; 
    private long timeInMillis; 

    public Executor(String path,String input, String output,String lang,long timeInMillis){ 
     this.path = path; 
     this.input = input; 
     this.output = output; 
     this.lang = lang; 
     this.timeInMillis = timeInMillis; 

    } 


    public ExecutorModel execute(){ 
     ExecutorModel model = new ExecutorModel(); 

     System.out.println("Code started executing"); 

     if(lang.equals("java")){ 
      p = new ProcessBuilder("java","Solution"); 
     } 
     else if(lang.equals("c")){ 
      p = new ProcessBuilder("./a.out"); 
     } 
     else if(lang.equals("c++")){ 
      p = new ProcessBuilder("./a.out"); 
     } 
     else{ 
      System.out.println("language is not correct..."); 
      p = null; 
     } 

     p.directory(new File(path)); 

     p.redirectErrorStream(true); 

     // System.out.println("Current directory "+System.getProperty("user.dir")); 

     try{ 
      Process pp = p.start(); 

      BufferedReader reader = 
        new BufferedReader(new InputStreamReader(pp.getInputStream())); 
      StringBuilder builder = new StringBuilder(); 
      String line = null; 



      /*process e input veriliyor bu kısımda */ 
      OutputStream outputstream = pp.getOutputStream(); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputstream)); 
      writer.write(input); 

      writer.flush(); 
      writer.close(); 


      if(!pp.waitFor(timeInMillis, TimeUnit.MILLISECONDS)){ 
       System.out.println("TİME LİMİT EXCEED !!!! "); 
       model.setTimelimit(true);  
       return model; 
      } 

      else{ 
       model.setTimelimit(false); 
       int exitCode = pp.exitValue(); 
       System.out.println("Exit Value = "+pp.exitValue()); 

       if(exitCode != 0){ 
        System.out.println("RUNTIME ERROR !!!!!!"); 

        model.setSuccess(false); 
        model.setRuntimeerror(true); 
        return model; 
       } 
      } 



      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
       //builder.append(System.getProperty("line.separator")); 
      } 
      String result = builder.toString(); 
      System.out.println(" output:"+result+" input:"+input); 
      if(result.charAt(result.length()-1) == ' ') 
       result = result.substring(0, result.length()-1); 
      if(result.equals(output)){ 
       model.setSuccess(true); 
       model.setWronganswer(false); 
       System.out.println("OUTPUT (SUCCESS) = "+result); 
       return model; 
      } 
      else{ 
       model.setSuccess(false); 
       model.setWronganswer(true); 
       System.out.println("OUTPUTTT (FAIL) = "+result); 
       return model; 
      } 



     }catch(IOException ioe){ 
      System.err.println("in execute() "+ioe); 
     }catch (InterruptedException ex){ 
      System.err.println(ex); 
     } 



     System.out.println("CODE EXECUTION FINISHED !"); 


     return model; 
    } 



} 
+0

[为方法/线程设置最大执行时间]的可能副本(https://stackoverflow.com/questions/20500003/setting-a-maximum-execution-time-for-a-method-thread) –

+0

只是一个附注:'lang.equals(“java”)'不安全,''java“.equals(lang)'是。而且这个力量可能会与你同在! – AxelH

+0

@Sanket Makani,我接受了它,我已经使用它。问题在于测量由进程执行的代码的执行时间,而不是进程的执行时间。 –

回答

0

你有没有tryed到:

public long getCpuTime() { 
    ThreadMXBean bean = ManagementFactory.getThreadMXBean(); 
    return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadCpuTime() : 0L; 
} 

使用方法线程开始一个进程启动,然后再次使用该线程结束时(因为它然后也按照我的理解结束进程),然后检查delta(也就是差异,方法的最后一次使用减去第一次使用),以获得该线​​程已经运行多长时间,从而间接获得进程的时间拿?

如果我没有误会,那么使用System.currentTimeMillis()方法会更好,因为它计算给定特定线程的cpu时间,排除其他线程使用的时间以及运行在其他线程中的其他系统进程背景。