From easyant-commits-return-223-apmail-incubator-easyant-commits-archive=incubator.apache.org@incubator.apache.org Wed Apr 13 06:23:28 2011 Return-Path: Delivered-To: apmail-incubator-easyant-commits-archive@minotaur.apache.org Received: (qmail 14228 invoked from network); 13 Apr 2011 06:23:28 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 13 Apr 2011 06:23:28 -0000 Received: (qmail 60342 invoked by uid 500); 13 Apr 2011 06:23:27 -0000 Delivered-To: apmail-incubator-easyant-commits-archive@incubator.apache.org Received: (qmail 60320 invoked by uid 500); 13 Apr 2011 06:23:27 -0000 Mailing-List: contact easyant-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: easyant-dev@incubator.apache.org Delivered-To: mailing list easyant-commits@incubator.apache.org Received: (qmail 60313 invoked by uid 99); 13 Apr 2011 06:23:26 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Apr 2011 06:23:26 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 13 Apr 2011 06:23:24 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 462012388A36; Wed, 13 Apr 2011 06:23:04 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1091665 - in /incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners: BuildExecutionTimer.java SubBuildExecutionTimer.java Date: Wed, 13 Apr 2011 06:23:04 -0000 To: easyant-commits@incubator.apache.org From: kpsiddharth@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110413062304.462012388A36@eris.apache.org> Author: kpsiddharth Date: Wed Apr 13 06:23:03 2011 New Revision: 1091665 URL: http://svn.apache.org/viewvc?rev=1091665&view=rev Log: EASYANT-9: Build listeners to keep track of execution time stats Added: incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/BuildExecutionTimer.java (with props) incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/SubBuildExecutionTimer.java (with props) Added: incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/BuildExecutionTimer.java URL: http://svn.apache.org/viewvc/incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/BuildExecutionTimer.java?rev=1091665&view=auto ============================================================================== --- incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/BuildExecutionTimer.java (added) +++ incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/BuildExecutionTimer.java Wed Apr 13 06:23:03 2011 @@ -0,0 +1,195 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.easyant.core.ant.listerners; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.ivy.util.StringUtils; +import org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.BuildListener; +import org.apache.tools.ant.util.DateUtils; + +/* + * This listener is created so that it may be used to provide all statistic + * collection for easyant builds. + */ + +/** + * Build listener to time execution of builds. + */ +public class BuildExecutionTimer implements BuildListener { + // build start time + // to be initialized in buildStarted method + protected long buildStartTime; + + public static class ExecutionResult { + /** + * Name of the unit whose execution is timed + */ + private String unitName; + + /** + * Time taken to execute the unit + */ + private long elapsedTime; + + /** + * Formatted representation of the execution time + */ + private String formattedElapsedTime; + + /** + * Execution result + */ + private int execResult; + + /** + * Value to be assigned to execResult if failure + */ + public static int FAILURE_RESULT = 0; + + /** + * Value to be assigned to execResult if success + */ + public static int SUCCESS_RESULT = 1; + + public ExecutionResult(String unitName, long elapsedTime, + int buildResult) { + this.unitName = unitName; + this.elapsedTime = elapsedTime; + this.formattedElapsedTime = DateUtils + .formatElapsedTime(elapsedTime); + this.execResult = buildResult; + } + + public String getUnitName() { + return this.unitName; + } + + public long getElapsedTime() { + return this.elapsedTime; + } + + public String getFormattedElapsedTime() { + return this.formattedElapsedTime; + } + + public int getResult() { + return this.execResult; + } + } + + /** + * stops the timer and stores the result as a project reference by the key + * 'referenceName' + */ + protected void stopTimer(BuildEvent arg0, String referenceName, + long startTime) { + List results = (List) arg0 + .getProject().getReference(referenceName); + if (results == null) { + results = new ArrayList(); + arg0.getProject().addReference(referenceName, results); + } + + ExecutionResult execResult = new ExecutionResult(arg0.getProject() + .getName(), System.currentTimeMillis() - startTime, arg0 + .getException() == null ? ExecutionResult.SUCCESS_RESULT + : ExecutionResult.FAILURE_RESULT); + + results.add(execResult); + + } + + public void buildFinished(BuildEvent arg0) { + stopTimer(arg0, EXECUTION_TIMER_BUILD_RESULTS, buildStartTime); + } + + public void buildStarted(BuildEvent arg0) { + buildStartTime = System.currentTimeMillis(); + } + + public void messageLogged(BuildEvent arg0) { + // leaving empty till better use is found + } + + public void targetFinished(BuildEvent arg0) { + // leaving empty till better use is found + } + + /* following methods may be implemented for more comprehensive stats */ + public void targetStarted(BuildEvent arg0) { + // leaving empty till better use is found + } + + public void taskFinished(BuildEvent arg0) { + // leaving empty till better use is found + } + + public void taskStarted(BuildEvent arg0) { + // leaving empty till better use is found + } + + /** + * Returns a string containing results of execution timing for display on + * console in a tabular fashion + * + * @param results + * @return + */ + public static String formatExecutionResults(List results) { + String formattedResults = ""; + int constantSpaces = 10; + int maxUnitNameLength = 0; + int maxExecTimeLength = 0; + for (int i = 0; i < results.size(); i++) { + ExecutionResult result = results.get(i); + maxUnitNameLength = result.getUnitName().length() > maxUnitNameLength ? result + .getUnitName().length() + : maxUnitNameLength; + maxExecTimeLength = result.getFormattedElapsedTime().length() > maxExecTimeLength ? result + .getFormattedElapsedTime().length() + : maxExecTimeLength; + } + StringBuffer sb = new StringBuffer( + org.apache.tools.ant.util.StringUtils.LINE_SEP); + for (int i = 0; i < results.size(); i++) { + ExecutionResult result = results.get(i); + String moduleName = result.getUnitName(); + int variableSpaces = maxUnitNameLength - moduleName.length() + + constantSpaces; + sb.append(" * ").append(result.getUnitName()).append( + StringUtils.repeat(" ", variableSpaces)); + // keeping both success and failed strings of equal length + String execResult = result.getResult() == ExecutionResult.SUCCESS_RESULT ? "SUCCESS " + : "FAILED "; + sb.append(execResult).append("[took ").append( + result.getFormattedElapsedTime()).append("]").append( + org.apache.tools.ant.util.StringUtils.LINE_SEP); + } + + formattedResults = sb.toString(); + return formattedResults; + } + + /** + * Reference key against which build execution results will be stored + */ + public static final String EXECUTION_TIMER_BUILD_RESULTS = "execution.timer.build.results"; +} Propchange: incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/BuildExecutionTimer.java ------------------------------------------------------------------------------ svn:eol-style = native Added: incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/SubBuildExecutionTimer.java URL: http://svn.apache.org/viewvc/incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/SubBuildExecutionTimer.java?rev=1091665&view=auto ============================================================================== --- incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/SubBuildExecutionTimer.java (added) +++ incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/SubBuildExecutionTimer.java Wed Apr 13 06:23:03 2011 @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.easyant.core.ant.listerners; + +import org.apache.tools.ant.BuildEvent; +import org.apache.tools.ant.SubBuildListener; + +/** + * Sub build listener to time execution for sub-module builds + */ +public class SubBuildExecutionTimer extends BuildExecutionTimer implements + SubBuildListener { + + public void subBuildFinished(BuildEvent arg0) { + stopTimer(arg0, EXECUTION_TIMER_SUBBUILD_RESULTS, buildStartTime); + } + + public void subBuildStarted(BuildEvent arg0) { + buildStartTime = System.currentTimeMillis(); + } + + /** + * Reference key against which sub-build execution times will be stored + */ + public static final String EXECUTION_TIMER_SUBBUILD_RESULTS = "execution.timer.subbuild.results"; +} Propchange: incubator/easyant/core/trunk/src/main/java/org/apache/easyant/core/ant/listerners/SubBuildExecutionTimer.java ------------------------------------------------------------------------------ svn:eol-style = native