如何衡量Java中进程执行代码的执行时间?

时间:2017-05-26 10:44:30

标签: java concurrency processbuilder

我正在开发像HackerRank,Codeforces等编程在线评审项目...

我有线程池,当请求到来时,Web服务从线程池获取一个线程,并且该线程使用ProcessBuilder编译代码(一切都是okey直到这里),在编译之后,该线程通过再次使用a来启动执行部分新的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;
    }



}

1 个答案:

答案 0 :(得分:0)

你有没有尝试过:

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

使用该方法作为启动进程的线程启动,然后在该线程结束时再次使用它(因为它然后也按照我的理解结束了该进程),然后检查delta(也就是差异,方法的最后用法减去第一次使用),以获取该线程运行的时间,从而间接获得该进程所花费的时间?

如果我没有误解,那么使用例如System.currentTimeMillis()会更好,因为它计算给定特定线程的cpu时间,不包括其他线程以及在后台运行的其他系统进程使用的时间