如何计算eclipse中基于Java的工具的编译时间

时间:2016-02-05 09:39:22

标签: java eclipse

在我目前的项目中,我试图在eclipse中计算基于Java的工具的编译时间。计算编译时间的一种方法是使用以下方法。

long lStartTime = new Date().getTime();
//some tasks
long lEndTime = new Date().getTime();
long difference = lEndTime - lStartTime;
System.out.println("Elapsed milliseconds: " + difference);

我也尝试过 Metric-Eclipse插件,但它不会给出编译时间。我想使用一些 eclipse插件工具或其他工具来计算编译时间。有没有可用于计算编译时间的eclipse插件/工具?

2 个答案:

答案 0 :(得分:0)

虽然你问过一种使用Eclipse IDE编写时间的方法,但注释表明你愿意接受无头构建解决方案,只要它有效。虽然你提到你试过这个没有成功,但是由于依赖性错误,如果项目设置正确,依赖错误不应该阻止你建立。

我编写了这个脚本以使用Eclipse的内置编译器。我使用它与C ++代码,但仍应使用Java。假设您使用的是Linux。我还没有测试它的时序方面,但你可以随时删除该部分并用time

调用它。

将其另存为headlessBuild.sh,并使用sh headlessBuild.sh -csh headlessBuild.sh -r进行调用。您也可以使用time headlessBuild.sh来调用它来使用linux的内置命令计时器。

#!/bin/sh

args=`getopt hrcs: $*`
set -- $args

if [ $? -ne 0 ]; then
  echo ""
  echo "Usage: headlessBuild {args}"
  echo ""
  echo "-c        cleans and rebuilds workspace"
  echo "-r        rebuilds workspace without cleaning (default)"
  echo ""
  exit 1
fi

currDir=$(dirname "${BASH_SOURCE[0]}")
WORKSPACE_ROOT=/home/user/workspace
nbr=0

PROJ_NAME[$nbr]='ProjectName1'
PROJ_EXE[$nbr]='ProjectName1/Debug'
TARGET[$nbr]='TargetName1.so'
nbr=$(( nbr+1 ))

PROJ_NAME[$nbr]='ProjectName2'
PROJ_EXE[$nbr]='ProjectName2/Debug'
TARGET[$nbr]='TargetName2.so'
nbr=$(( nbr+1 ))



buildType="build"
for i; do
  case "$i" in
    -c  ) buildType="cleanBuild"
          shift;
          ;;
    -r  ) buildType="build"
          shift;
          ;;
    --  ) shift; 
          break;
          ;;
  esac
done


echo ' '
echo 'BUILDING PROJECTS...'
echo ' '
STARTTIME=$(date +%s)

nbr=0
for proj in "${PROJ_NAME[@]}" ; do

    if [ "$buildType" = "cleanBuild" ]; then

        ####### ensure rebuild by deleting current target, if it exists
        name=$WORKSPACE_ROOT'/'"${PROJ_EXE[$nbr]}"'/'"${TARGET[$nbr]}"
        if [[ -f $name ]]; then
            echo '   Removing exising executable:' $name
            rm $name
        fi;
    fi;

    echo '   Building ' $proj
    eclipse -nosplash -data $WORKSPACE_ROOT --launcher.suppressErrors -application org.eclipse.cdt.managedbuilder.core.headlessbuild  -import $WORKSPACE_ROOT'/'$proj -$buildType "${PROJ_EXE[$nbr]}" > "${LOGFILENAME[$nbr]}" -vmargs -Declipse.exitdata=""
done 

ENDTIME=$(date +%s)
echo ' '
echo "Elapsed Time: $(($ENDTIME - $STARTTIME)) s"

答案 1 :(得分:0)

你可以用蚂蚁来做这件事。

工作区中的新Java项目(比如名称是ws-builder)

ws-builder
    src
        task.ant.BuilderWorkSpaceTask.java
    build.xml

.classpath of project :(我的eclipse版本是Mars,这三个jar版本可能在旧版本中有所不同,但没关系)

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="var" path="ECLIPSE_HOME/plugins/org.apache.ant_1.9.4.v201504302020/lib/ant.jar"/>
    <classpathentry kind="var" path="ECLIPSE_HOME/plugins/org.eclipse.core.resources_3.10.1.v20150725-1910.jar"/>
    <classpathentry kind="var" path="ECLIPSE_HOME/plugins/org.eclipse.equinox.common_3.7.0.v20150402-1709.jar"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

task.ant.BuilderWorkSpaceTask类:

package task.ant;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IncrementalProjectBuilder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

public class BuildWorkspaceTask extends Task implements IProgressMonitor {

    @Override
    public void execute() throws BuildException {

        IWorkspace workspace = ResourcesPlugin.getWorkspace();

        try {

            // IncrementalProjectBuilder.FULL_BUILD- indicates a full build.
            // IncrementalProjectBuilder.INCREMENTAL_BUILD- indicates a incremental build.
            // IncrementalProjectBuilder.CLEAN_BUILD-

            workspace.build(IncrementalProjectBuilder.CLEAN_BUILD, this);

        } catch (CoreException e) {
            e.printStackTrace();
        }

        super.execute();
    }

    @Override
    public void worked(int arg0) {
        log("worked(" + arg0 + ")");
    }

    @Override
    public void subTask(String arg0) {
        log("subTask(" + arg0 + ")");
    }

    @Override
    public void setTaskName(String arg0) {
        log("setTaskName(" + arg0 + ")");
    }

    @Override
    public void setCanceled(boolean arg0) { }

    @Override
    public boolean isCanceled() {
        return false;
    }

    @Override
    public void internalWorked(double arg0) {
        log("internalWorked(" + arg0 + ")");
    }

    @Override
    public void done() {
        log("done()");
    }

    @Override
    public void beginTask(String arg0, int arg1) {
        log("beginTask(" + arg0 + ", " + arg1 + ")");
    }

}

的build.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build-workspace" name="Test">

    <path id="task-classpath">
        <pathelement location="bin"/>
    </path>

    <taskdef name="workspaceBuild" classname="task.ant.BuildWorkspaceTask" classpathref="task-classpath" />

    <target name="build-workspace">
        <workspaceBuild />
    </target>

</project>

Right click on build.xml > Run as > 2 Ant Build > (select) JRE (tab) > (select) Run in the same JRE as the workspace > Run 那就是它。你会看到蚂蚁完成时的持续时间。