Author: chatellier Date: 2009-09-02 13:47:36 +0000 (Wed, 02 Sep 2009) New Revision: 2576 Modified: isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/SimulationInformation.java isis-fish/trunk/src/test/java/fr/ifremer/isisfish/datastore/SimulationInformationTest.java Log: Add rule time on simulation information. 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-02 13:47:17 UTC (rev 2575) +++ isis-fish/trunk/src/main/java/fr/ifremer/isisfish/datastore/SimulationInformation.java 2009-09-02 13:47:36 UTC (rev 2576) @@ -30,8 +30,10 @@ import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Properties; +import java.util.Set; import org.apache.commons.lang.time.DurationFormatUtils; import org.apache.commons.logging.Log; @@ -58,24 +60,26 @@ private static final String END_SIMULATION = "simulationEnd"; private static final String EXPORT_TIME = "exportTime"; private static final String RULE_TIME = "ruleTime"; + private static final String RULE_TIME_INIT = RULE_TIME + ".init"; + private static final String RULE_TIME_PRE = RULE_TIME + ".pre"; + private static final String RULE_TIME_POST = RULE_TIME + ".post"; private static final String OTHER_INFO = "otherInfo"; private static final String STATISTIC = "statistic"; private static final String OPTIMIZATION_USAGE = "optimizationUsage"; private static final String SIMULATION_EXCEPTION = "exception"; - private static final String RULE_TIME_INIT = "init"; - private static final String RULE_TIME_PRE = "pre"; - private static final String RULE_TIME_POST = "post"; - private static final SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy.MM.dd HH:mm:ss"); protected Properties info = new Properties(); + protected File file = null; /** * Constructor. * + * If file already exists, load his content into current instance. + * * @param file simulation information output file */ public SimulationInformation(File file) { @@ -100,9 +104,13 @@ @Override public String toString() { String result = "Simulation Information:\n"; + + // date start/end result += "Start: " + dateFormat.format(getSimulationStart()) + " End: " + dateFormat.format(getSimulationEnd()) + "\n"; - Map<String, Long> exportTime = getExportTime(); + + // exports + Map<String, Long> exportTime = getExportTimes(); if (exportTime.size() > 0) { result += "Export time:\n"; for (Map.Entry<String, Long> entry : exportTime.entrySet()) { @@ -114,19 +122,56 @@ } } + // rules + Set<String> ruleNames = getRuleNames(); + if (ruleNames.size() > 0) { + result += "Rule time:\n"; + for (String ruleName : ruleNames) { + long time = getRuleInitTime(ruleName); + long total = time; + if (time > 0) { + + String details = ""; + 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"); + } + time = getRulePostTime(ruleName); + if (time > 0) { + total += time; + details += ", post:" + DurationFormatUtils.formatDuration(time, "s'.'S"); + } + + result += "\t" + ruleName + " : " + DurationFormatUtils.formatDuration(total, "s'.'S"); + result += " (" + details + ")"; + result += "\n"; + } + } + } + + // general information String info = getInformation(); if (info != null && !"".equals(info)) { result += "Information:\n" + info + "\n"; } + // Statistic String v = getStatistic(); if (v != null) { result += "Statistic:\n" + v + "\n"; } + + // Optimisation usage v = getOptimizationUsage(); if (v != null) { result += "Optimisation usage:\n" + v + "\n"; } + + // Exception v = getException(); if (v != null) { result += "Exception:\n" + v + "\n"; @@ -181,8 +226,9 @@ } /** - * Get the date of simulation start - * @return + * Get the date of simulation start. + * + * @return simulation end date */ public Date getSimulationEnd() { String d = info.getProperty(END_SIMULATION); @@ -206,11 +252,16 @@ setInfo(END_SIMULATION, dateFormat.format(date)); } + public void addExportTime(String exportName, long time) { + setInfo(EXPORT_TIME + "." + exportName, String.valueOf(time)); + } + /** - * get all export time in map - * @return + * Get all export time in map. + * + * @return a map with all export time */ - public Map<String, Long> getExportTime() { + protected Map<String, Long> getExportTimes() { Map<String, Long> result = new HashMap<String, Long>(); for (String key : info.stringPropertyNames()) { if (key.startsWith(EXPORT_TIME + ".")) { @@ -235,6 +286,16 @@ return result; } + /** + * Get all export time in map. + * + * @return a map with all export time + * @deprecated since 3.2.0.5, use {@link #getExportTimes()} instead + */ + public Map<String, Long> getExportTime() { + return getExportTimes(); + } + public long getExportTime(String exportName) { String t = info.getProperty(EXPORT_TIME + "." + exportName); long result = 0; @@ -250,10 +311,152 @@ return result; } - public void addExportTime(String exportName, long time) { - setInfo(EXPORT_TIME + "." + exportName, String.valueOf(time)); + /** + * Add rule time. + * + * If a time already exists for ruleName, add time to previous time. + * (usefull because pre/post action are called multiples time) + * + * @param keyName (ie {@link #RULE_TIME_INIT}, {@link #RULE_TIME_PRE}, {@link #RULE_TIME_POST}) + * @param ruleName rule name + * @param time time to add + * + * @since 3.2.0.5 + */ + protected void addRuleTime(String keyName, String ruleName, long time) { + + // get previous time + String previousTimeAsString = info + .getProperty(keyName + "." + ruleName); + long previousTime = 0; + if (previousTimeAsString != null) { + try { + previousTime = Long.parseLong(previousTimeAsString); + } catch (NumberFormatException eee) { + if (log.isWarnEnabled()) { + log.warn(_("isisfish.error.parse.long", + previousTimeAsString), eee); + } + } + } + + // add + long newTime = previousTime + time; + + // set new time in + setInfo(keyName + "." + ruleName, String.valueOf(newTime)); } + /** + * Add rule init time. + * + * If a time already exists for ruleName, add time to previous time. + * + * @param ruleName rule name + * @param time time to add + * @since 3.2.0.5 + */ + public void addRuleInitTime(String ruleName, long time) { + addRuleTime(RULE_TIME_INIT, ruleName, time); + } + + /** + * Add rule pre operation time time. + * + * If a time already exists for ruleName, add time to previous time. + * + * @param ruleName rule name + * @param time time to add + * @since 3.2.0.5 + */ + public void addRulePreTime(String ruleName, long time) { + addRuleTime(RULE_TIME_PRE, ruleName, time); + } + + /** + * Add rule post operation time. + * + * If a time already exists for ruleName, add time to previous time. + * + * @param ruleName rule name + * @param time time to add + * @since 3.2.0.5 + */ + public void addRulePostTime(String ruleName, long time) { + addRuleTime(RULE_TIME_POST, ruleName, time); + } + + /** + * Get rule init operation time. + * + * @param ruleName rule name + * @return time + * @since 3.2.0.5 + */ + public long getRuleInitTime(String ruleName) { + return getRuleTime(RULE_TIME_INIT, ruleName); + } + + /** + * Get rule pre operation time. + * + * @param ruleName rule name + * @return time + * @since 3.2.0.5 + */ + public long getRulePreTime(String ruleName) { + return getRuleTime(RULE_TIME_PRE, ruleName); + } + + /** + * Get rule post operation time. + * + * @param ruleName rule name + * @return time + * @since 3.2.0.5 + */ + public long getRulePostTime(String ruleName) { + return getRuleTime(RULE_TIME_POST, ruleName); + } + + /** + * Get rule operation time. + * + * @param ruleName rule name + * @since 3.2.0.5 + */ + protected long getRuleTime(String prefixName, String ruleName) { + String t = info.getProperty(prefixName + "." + ruleName); + long result = 0; + if (t != null) { + try { + result = Long.parseLong(t); + } catch (NumberFormatException eee) { + if (log.isWarnEnabled()) { + log.warn(_("isisfish.error.parse.long", t), eee); + } + } + } + return result; + } + + /** + * Get rules names. + * + * @return a map with all export time + */ + protected Set<String> getRuleNames() { + Set<String> result = new HashSet<String>(); + for (String key : info.stringPropertyNames()) { + if (key.startsWith(RULE_TIME + ".")) { + // recupere le nom apres le deuxieme "." + String ruleName = key.substring(key.indexOf('.', RULE_TIME.length() + 1) + 1); + result.add(ruleName); + } + } + return result; + } + public String getStatistic() { String result = info.getProperty(STATISTIC); return result; @@ -297,6 +500,7 @@ /** * @deprecated since 3.2.0.5, use {@link #getInformation()} instead + * @return other information */ public String getInfomation() { return getInformation(); Modified: isis-fish/trunk/src/test/java/fr/ifremer/isisfish/datastore/SimulationInformationTest.java =================================================================== --- isis-fish/trunk/src/test/java/fr/ifremer/isisfish/datastore/SimulationInformationTest.java 2009-09-02 13:47:17 UTC (rev 2575) +++ isis-fish/trunk/src/test/java/fr/ifremer/isisfish/datastore/SimulationInformationTest.java 2009-09-02 13:47:36 UTC (rev 2576) @@ -26,8 +26,9 @@ import java.text.SimpleDateFormat; import java.util.Date; -import junit.framework.Assert; - +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; import org.junit.Test; /** @@ -43,6 +44,9 @@ */ public class SimulationInformationTest { + /** Log. */ + protected static Log log = LogFactory.getLog(SimulationInformationTest.class); + /** * Test simulation information with no data. * @@ -60,6 +64,7 @@ Assert.assertFalse(info.hasError()); Assert.assertNull(info.getException()); Assert.assertEquals(0, info.getExportTime("test")); + Assert.assertNotNull(info.toString()); } /** @@ -84,6 +89,7 @@ Assert.assertFalse(info.hasError()); Assert.assertTrue(info.getOptimizationUsage().indexOf("Cache used") > 0); Assert.assertNull(info.getStatistic()); + Assert.assertTrue(info.toString().indexOf("VesselMargin.java") > 0); } /** @@ -101,6 +107,7 @@ SimulationInformation info = new SimulationInformation(file); Assert.assertTrue(info.hasError()); Assert.assertTrue(info.getException().indexOf("TopiaException") > 0); + Assert.assertTrue(info.toString().indexOf("TopiaException") > 0); } /** @@ -130,6 +137,7 @@ SimulationInformation testInfo = new SimulationInformation(file); Assert.assertEquals(d1, testInfo.getSimulationStart()); Assert.assertEquals(d2, testInfo.getSimulationEnd()); + Assert.assertTrue(testInfo.toString().indexOf("15:50:47") > 0); } /** @@ -152,6 +160,7 @@ SimulationInformation testInfo = new SimulationInformation(file); Assert.assertTrue(testInfo.hasError()); Assert.assertTrue(testInfo.getException().indexOf("grave") > 0); + Assert.assertTrue(testInfo.toString().indexOf("grave") > 0); } @@ -178,5 +187,45 @@ Assert.assertEquals(40, testInfo.getExportTime("export2")); Assert.assertEquals(50, testInfo.getExportTime("export3")); Assert.assertEquals(60, testInfo.getExportTime("export4")); + Assert.assertTrue(testInfo.toString().indexOf("export1") > 0); } + + /** + * Test simulation rule time. + * + * @throws IOException + */ + @Test + public void testSimulationRuleTime() throws IOException { + File file = File.createTempFile("information-rule", ".txt"); + file.deleteOnExit(); + + SimulationInformation info = new SimulationInformation(file); + info.addRuleInitTime("rule1", 30); + info.addRuleInitTime("rule2", 8); + info.addRuleInitTime("rule3", 321); + info.addRuleInitTime("rule4", 321); + + // time must be added + info.addRulePreTime("rule1", 20); + info.addRulePostTime("rule1", 25); + info.addRulePreTime("rule1", 20); + info.addRulePostTime("rule1", 25); + + info.addRulePreTime("rule2", 20); + info.addRulePostTime("rule2", 25); + + info.addRulePreTime("rule3", 20); + // no additional time for rule 4 + + info.store(); + + SimulationInformation testInfo = new SimulationInformation(file); + Assert.assertFalse(testInfo.hasError()); + //Assert.assertEquals(30, testInfo.getExportTime("export1")); + //Assert.assertEquals(40, testInfo.getExportTime("export2")); + //Assert.assertEquals(50, testInfo.getExportTime("export3")); + //Assert.assertEquals(60, testInfo.getExportTime("export4")); + //Assert.assertTrue(testInfo.toString().indexOf("export1") > 0); + } }
participants (1)
-
chatellierï¼ users.labs.libre-entreprise.org