From 6cbdcae8723db9afe00afd23ff46d7027d6fb9e6 Mon Sep 17 00:00:00 2001 From: chenzo Date: Thu, 4 Oct 2012 09:09:06 +0000 Subject: [PATCH] Adds extra function to export the total harvesting time. --- src/org/ice4j/ice/Agent.java | 73 ++++++++++++++++++- .../ice/harvest/CandidateHarvesterSet.java | 10 --- 2 files changed, 71 insertions(+), 12 deletions(-) diff --git a/src/org/ice4j/ice/Agent.java b/src/org/ice4j/ice/Agent.java index d68f073..d5c06a5 100644 --- a/src/org/ice4j/ice/Agent.java +++ b/src/org/ice4j/ice/Agent.java @@ -89,6 +89,18 @@ public class Agent private final CandidateHarvesterSet harvesters = new CandidateHarvesterSet(); + /** + * The last harvest start time. -1 if this agent is not currently + * harvesting. + */ + private long lastStartHarvestingTime = -1; + + /** + * The last ended harvesting time. -1 if this agent has never harvested yet. + */ + private long lastHarvestingTime = -1; + + /** * We use the FoundationsRegistry to keep track of the foundations * we assign within a session (i.e. the entire life time of an @@ -431,6 +443,8 @@ public class Agent logger.info("Gather candidates for component " + component.toShortString()); + this.startHarvesting(); + hostCandidateHarvester.harvest( component, preferredPort, minPort, maxPort, Transport.UDP); @@ -448,6 +462,12 @@ public class Agent //apply other harvesters here harvesters.harvest(component); + this.stopHarvesting(); + logger.info( + "End candidate harvest for all harvesters within " + + this.getTotalHarvestingTime() + " ms" + + ", component: " + component.getComponentID()); + logger.fine("host+harvested candidate count: " + component.getLocalCandidateCount()); @@ -2176,8 +2196,8 @@ public class Agent * @param harvesterName The class name if the harvester. * * @return The harvesting time (in ms) for the harvester given in parameter. - * -1 if this harvester does not exists, if the ICE agent is null, or if the - * agent is not currently harvesting with this harvester. + * -1 if this harvester does not exists, or if the + * agent has not yet harvested with this harvester. */ public long getHarvestingTime(String harvesterName) { @@ -2207,4 +2227,53 @@ public class Agent return -1; } + + /** + * Starts the harvesting timer. Called when the harvest begins. + */ + public void startHarvesting() + { + // Remember the start date of this harvester. + this.lastStartHarvestingTime = System.currentTimeMillis(); + // Reset the last harvesting time. + this.lastHarvestingTime = -1; + } + + /** + * Stops the harvesting timer. Called when the harvest ends. + */ + public void stopHarvesting() + { + // Remember the last harvesting time. + this.lastHarvestingTime = this.getTotalHarvestingTime(); + // Stops the current timer. + this.lastStartHarvestingTime = -1; + } + + /** + * Returns the current harvesting time in ms. If this agent is not currently + * harvesting, then returns the value of the last total harvesting time for + * all the harvesters. -1 if this agent has nerver harvested. + * + * @return The current harvesting time in ms. If this agent is not currently + * harvesting, then returns the value of the last total harvesting time for + * all the harvesters. -1 if this agent has nerver harvested. + */ + public long getTotalHarvestingTime() + { + if(this.lastStartHarvestingTime != -1) + { + long currentHarvestingTime + = System.currentTimeMillis() - lastStartHarvestingTime; + // Retest here, while the harvesting may be end while computing the + // harvsting time. + if(this.lastStartHarvestingTime != -1) + { + return currentHarvestingTime; + } + } + // If we are ont currently harvesting, then returns the value of the + // last harvesting time. + return this.lastHarvestingTime; + } } diff --git a/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java b/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java index a6e0e60..20ae4ba 100644 --- a/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java +++ b/src/org/ice4j/ice/harvest/CandidateHarvesterSet.java @@ -107,8 +107,6 @@ public class CandidateHarvesterSet final Component component, ExecutorService executorService) { - long startCandidateHarvestTime = System.currentTimeMillis(); - /** * Represents a task to be executed by the specified executorService and * to call {@link CandidateHarvester#harvestComponent} on the specified @@ -279,14 +277,6 @@ public class CandidateHarvesterSet } taskIter.remove(); } - - long stopCandidateHarvestTime = System.currentTimeMillis(); - long candidateHarvestTime - = stopCandidateHarvestTime - startCandidateHarvestTime; - logger.info( - "End candidate harvest for all harvesters within " - + candidateHarvestTime + " ms" - + ", component: " + component.getComponentID()); } /**