r2598 - in isis-fish/trunk: . src/main/java/fr/ifremer/isisfish/aspect src/main/java/fr/ifremer/isisfish/datastore src/main/java/fr/ifremer/isisfish/simulator src/main/java/fr/ifremer/isisfish/simulator/launcher
Author: chatellier Date: 2009-09-11 09:27:55 +0000 (Fri, 11 Sep 2009) New Revision: 2598 Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/RuleAspect.java Modified: isis-fish/trunk/changelog.txt isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/AspectClassLoader.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/SimulationInformation.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/Simulator.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/InProcessSimulatorLauncher.java isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SimulationExecutor.java Log: Add rule time aspect. Modified: isis-fish/trunk/changelog.txt =================================================================== --- isis-fish/trunk/changelog.txt 2009-09-11 09:27:25 UTC (rev 2597) +++ isis-fish/trunk/changelog.txt 2009-09-11 09:27:55 UTC (rev 2598) @@ -1,5 +1,7 @@ isis-fish (3.2.0.6) stable; urgency=low + * Add aspect to get rule time (init/pre/post) in simulation summary + -- isis-fish (3.2.0.5) stable; urgency=low Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/AspectClassLoader.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/AspectClassLoader.java 2009-09-11 09:27:25 UTC (rev 2597) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/AspectClassLoader.java 2009-09-11 09:27:55 UTC (rev 2598) @@ -43,8 +43,10 @@ import sun.misc.URLClassPath; /** - * AspectClassLoader. + * Aspectwerkz class loader. * + * http://aspectwerkz.codehaus.org/xref/org/codehaus/aspectwerkz/transform/inli... + * * Created: 30 mars 07 00:31:22 * * @author poussin @@ -54,12 +56,20 @@ * by : $Author$ */ public class AspectClassLoader extends URLClassLoader { + + /** + * Class loader constructor. + * + * @param urls parent urlclassloader url + * @param parent parent class loader + */ public AspectClassLoader(URL[] urls, ClassLoader parent) { super(urls, parent); ClassPreProcessorHelper.initializePreProcessor(); } - protected Class findClass(String name) throws ClassNotFoundException { + @Override + protected Class<?> findClass(String name) throws ClassNotFoundException { String path = name.replace('.', '/').concat(".class"); URLClassPath ucp = new URLClassPath(getURLs()); Resource res = ucp.getResource(path, false); @@ -70,47 +80,48 @@ byte[] transformed = ClassPreProcessorHelper.defineClass0Pre(this, name, b, 0, b.length, null); return defineClass(name, transformed, 0, transformed.length); } catch (IOException e) { - throw new ClassNotFoundException(e.getMessage()); + throw new ClassNotFoundException("Can't build aspect for class", e); } } else { throw new ClassNotFoundException(name); } } - public void deploy(Class aspectClass) { + /** + * Register new aspect. + * + * @param aspectClass aspect class to register + */ + public void deploy(Class<?> aspectClass) { String className = aspectClass.getName(); try { aspectClass = Class.forName(className, false, this); } catch (ClassNotFoundException e) { - throw new RuntimeException( - "could not load class [" + className + "] in class loader [" + this + "]" - ); + throw new RuntimeException("could not load class [" + className + "] in class loader [" + this + "]"); } - final ClassInfo aspectClassInfo = JavaClassInfo.getClassInfo(aspectClass); // create a new aspect def and fill it up with the annotation def from the aspect class final SystemDefinition systemDef = SystemDefinitionContainer.getVirtualDefinitionAt(this); final AspectDefinition newAspectDef = new AspectDefinition(className, aspectClassInfo, systemDef); - final Set newExpressions = getNewExpressionsForAspect( - aspectClass, newAspectDef, systemDef, DeploymentScope.MATCH_ALL); - + getNewExpressionsForAspect(aspectClass, newAspectDef, systemDef, DeploymentScope.MATCH_ALL); } /** * Returns a set with the new expressions for the advice in the aspect to deploy. * - * @param aspectClass s * @param newAspectDef + * @param aspectClass * @param newAspectDef + * @param newAspectDef * @param systemDef * @param deploymentScope * @return a set with the new expressions */ private Set getNewExpressionsForAspect(final Class aspectClass, - final AspectDefinition newAspectDef, - final SystemDefinition systemDef, - final DeploymentScope deploymentScope) { + final AspectDefinition newAspectDef, + final SystemDefinition systemDef, + final DeploymentScope deploymentScope) { final ClassLoader aspectLoader = aspectClass.getClassLoader(); final String aspectName = aspectClass.getName(); @@ -136,7 +147,7 @@ if (oldExpression == null) { continue; } -// deploymentHandle.registerDefinitionChange(adviceDef, oldExpression); + // deploymentHandle.registerDefinitionChange(adviceDef, oldExpression); final ExpressionInfo newExpression = deploymentScope.newExpressionInfo(oldExpression); adviceDef.setExpressionInfo(newExpression); @@ -144,5 +155,4 @@ } return newExpressions; } - } Added: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/RuleAspect.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/RuleAspect.java (rev 0) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/RuleAspect.java 2009-09-11 09:27:55 UTC (rev 2598) @@ -0,0 +1,177 @@ +/* *##% + * Copyright (C) 2009 Ifremer, Code Lutin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +package fr.ifremer.isisfish.aspect; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codehaus.aspectwerkz.annotation.Around; +import org.codehaus.aspectwerkz.annotation.Aspect; +import org.codehaus.aspectwerkz.joinpoint.JoinPoint; + +import fr.ifremer.isisfish.datastore.SimulationInformation; +import fr.ifremer.isisfish.datastore.SimulationStorage; +import fr.ifremer.isisfish.entities.Metier; +import fr.ifremer.isisfish.rule.Rule; +import fr.ifremer.isisfish.simulator.SimulationContext; +import fr.ifremer.isisfish.types.Date; + +/** + * Aspect utiliser pour intersepecter les appels a + * {@link Rule#init(SimulationContext)}, {@link Rule#preAction(SimulationContext, Date, Metier)}, + * {@link Rule#postAction(SimulationContext, Date, Metier)} et memoriser + * le temps mit. + * + * @author chatellier + * @version $Revision$ + * + * Last update : $Date$ + * By : $Author$ + */ + at Aspect("perInstance") +public class RuleAspect { + + /** + * State phase to register. + */ + protected static enum State { + INIT, PRE, POST + } + + /** Log. */ + private static Log log = LogFactory.getLog(RuleAspect.class); + + /** + * Aspect around {@link Rule#init(SimulationContext)} in packages "rules". + * + * @param jp join point + * @return init result + * @throws Throwable + */ + @Around("execution(* rules.*.init(..))") + public Object initCall(final JoinPoint jp) throws Throwable { + + Object result = makeTimedCall(jp, State.INIT); + return result; + + } + + /** + * Aspect around {@link Rule#preAction(SimulationContext, Date, Metier)} in packages "rules". + * + * @param jp join point + * @return preAction result + * @throws Throwable + */ + @Around("execution(* rules.*.preAction(..))") + public Object initPreCall(final JoinPoint jp) throws Throwable { + + Object result = makeTimedCall(jp, State.PRE); + return result; + } + + /** + * Aspect around {@link Rule#postAction(SimulationContext, Date, Metier)} in packages "rules". + * + * @param jp join point + * @return preAction result + * @throws Throwable + */ + @Around("execution(* rules.*.postAction(..))") + public Object initPostCall(final JoinPoint jp) throws Throwable { + + Object result = makeTimedCall(jp, State.POST); + return result; + } + + /** + * Effectue l'appel reel en calculant le temps pris. + * + * @param jp join point + * @param state + * @param aspectized class return object + * @throws Throwable + */ + protected Object makeTimedCall(JoinPoint jp, State state) throws Throwable { + + if (log.isTraceEnabled()) { + log.trace("Rule aspect called : " + jp.getCalleeClass().getSimpleName()); + } + + // get time before + long timeBeforeCall = System.currentTimeMillis(); + + // make real call + Object result = jp.proceed(); + + // get time after + long timeAfterCall = System.currentTimeMillis(); + + // get real time + long timeTaken = timeAfterCall - timeBeforeCall; + + registerTime(jp, state, timeTaken); + + return result; + + } + + /** + * Add time (init/pre/post) to simulation information in + * current {@link SimulationContext}. + * + * @param jp join point + * @param state state + * @param timeTaken time taken by {@code state} + */ + protected void registerTime(JoinPoint jp, State state, long timeTaken) { + SimulationContext context = SimulationContext.get(); + + SimulationStorage simulation = context.getSimulationStorage(); + + // happen if called outside a simulation + // so ... can't happen :) + if (simulation != null) { + + // get rule information + Class<?> calleeClass = jp.getCalleeClass(); + String ruleName = calleeClass.getSimpleName(); + + SimulationInformation info = simulation.getInformation(); + + switch (state) { + case INIT: + info.addRuleInitTime(ruleName, timeTaken); + break; + case PRE: + info.addRulePreTime(ruleName, timeTaken); + break; + case POST: + info.addRulePostTime(ruleName, timeTaken); + break; + default: + break; + } + } + else { + if (log.isWarnEnabled()) { + log.warn("Time register called outside a simulation context"); + } + } + } +} Property changes on: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/aspect/RuleAspect.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL" Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/SimulationInformation.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/SimulationInformation.java 2009-09-11 09:27:25 UTC (rev 2597) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/SimulationInformation.java 2009-09-11 09:27:55 UTC (rev 2598) @@ -127,25 +127,35 @@ if (ruleNames.size() > 0) { result += "Rule time:\n"; for (String ruleName : ruleNames) { + + String details = ""; + long total = 0; + long time = getRuleInitTime(ruleName); - long total = time; if (time > 0) { - - String details = ""; + total += time; details += "init:" + DurationFormatUtils.formatDuration(time, "s'.'S"); - - // can be 0 if condition always return false, never entrer pre/post - time = getRulePreTime(ruleName); - if (time > 0) { - total += time; - details += ", pre:" + DurationFormatUtils.formatDuration(time, "s'.'S"); + } + + // can be 0 if condition always return false, never entrer pre/post + time = getRulePreTime(ruleName); + if (time > 0) { + if (total > 0) { + details += ", "; } - time = getRulePostTime(ruleName); - if (time > 0) { - total += time; - details += ", post:" + DurationFormatUtils.formatDuration(time, "s'.'S"); + total += time; + details += "pre:" + DurationFormatUtils.formatDuration(time, "s'.'S"); + } + time = getRulePostTime(ruleName); + if (time > 0) { + if (total > 0) { + details += ", "; } + total += time; + details += "post:" + DurationFormatUtils.formatDuration(time, "s'.'S"); + } + if (total > 0) { result += "\t" + ruleName + " : " + DurationFormatUtils.formatDuration(total, "s'.'S"); result += " (" + details + ")"; result += "\n"; Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/Simulator.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/Simulator.java 2009-09-11 09:27:25 UTC (rev 2597) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/Simulator.java 2009-09-11 09:27:55 UTC (rev 2598) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2006 + * Copyright (C) 2006 - 2009 * Ifremer, Code Lutin, Cédric Pineau, Benjamin Poussin * * This program is free software; you can redistribute it and/or @@ -17,9 +17,11 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *##%*/ -/* * - * Simulator.java - * +package fr.ifremer.isisfish.simulator; + +/** + * This interface is used when simulator have all code for simulation. + * * Created: 5 juil. 2006 17:57:46 * * @author poussin @@ -28,17 +30,13 @@ * Last update: $Date$ * by : $Author$ */ - -package fr.ifremer.isisfish.simulator; - - -/** - * This interface is used when simulator have all code for simulation. - * - * @author poussin - */ public interface Simulator { + + /** + * Run simulation. + * + * @param context simulation context to use + * @throws Exception + */ public void simulate(SimulationContext context) throws Exception; } - - Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/InProcessSimulatorLauncher.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/InProcessSimulatorLauncher.java 2009-09-11 09:27:25 UTC (rev 2597) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/InProcessSimulatorLauncher.java 2009-09-11 09:27:55 UTC (rev 2598) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2002-2009 Code Lutin, Benjamin Poussin + * Copyright (C) 2002 - 2009 Ifremer, Code Lutin, Benjamin Poussin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@ -47,6 +47,7 @@ import fr.ifremer.isisfish.IsisFishRuntimeException; import fr.ifremer.isisfish.aspect.AspectClassLoader; import fr.ifremer.isisfish.aspect.Cache; +import fr.ifremer.isisfish.aspect.RuleAspect; import fr.ifremer.isisfish.aspect.Trace; import fr.ifremer.isisfish.datastore.SimulationStorage; import fr.ifremer.isisfish.datastore.SimulatorStorage; @@ -332,6 +333,9 @@ context.setSimulationControl(control != null ? control : new SimulationControl(simulation.getName())); + // Warning : Rule have to be instanciated after aspect definition + classLoader.deploy(RuleAspect.class); + SimulationParameter parameters = simulation.getParameter(); parameters.setIsisFishVersion(IsisConfig.getVersion()); // forceReload, save all modification in parameter and reread it @@ -348,6 +352,7 @@ message(control, _("isisfish.message.setting.cache.aspects")); classLoader.deploy(Cache.class); } + // recherche du simulateur a utiliser String simulatorName = parameters.getSimulatorName(); Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SimulationExecutor.java =================================================================== --- isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SimulationExecutor.java 2009-09-11 09:27:25 UTC (rev 2597) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/simulator/launcher/SimulationExecutor.java 2009-09-11 09:27:55 UTC (rev 2598) @@ -1,5 +1,5 @@ /* *##% - * Copyright (C) 2002-2009 Code Lutin, Benjamin Poussin + * Copyright (C) 2002-2009 Ifremer, Code Lutin, Benjamin Poussin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License
participants (1)
-
chatellier@users.labs.libre-entreprise.org