Improves harvesting time stats: 1) number of harvests and 2) total harvesting time for each harvesters.

This commit is contained in:
chenzo 2012-10-05 16:05:17 +00:00
parent 523236d6ce
commit 19c2d4767c
4 changed files with 96 additions and 116 deletions

View File

@ -90,16 +90,9 @@ public class Agent
= new CandidateHarvesterSet(); = new CandidateHarvesterSet();
/** /**
* The last harvest start time. -1 if this agent is not currently * Manages statisics about harvesting time.
* harvesting.
*/ */
private long lastStartHarvestingTime = -1; private HarvestingTimeStat harvestingTimeStat = new HarvestingTimeStat();
/**
* The last ended harvesting time. -1 if this agent has never harvested yet.
*/
private long lastHarvestingTime = -1;
/** /**
* We use the <tt>FoundationsRegistry</tt> to keep track of the foundations * We use the <tt>FoundationsRegistry</tt> to keep track of the foundations
@ -2199,8 +2192,8 @@ public class Agent
* @param harvesterName The class name if the harvester. * @param harvesterName The class name if the harvester.
* *
* @return The harvesting time (in ms) for the harvester given in parameter. * @return The harvesting time (in ms) for the harvester given in parameter.
* -1 if this harvester does not exists, or if the * 0 if this harvester does not exists, or if the agent has not yet
* agent has not yet harvested with this harvester. * harvested with this harvester.
*/ */
public long getHarvestingTime(String harvesterName) public long getHarvestingTime(String harvesterName)
{ {
@ -2210,7 +2203,7 @@ public class Agent
return this.hostCandidateHarvester.getHarvestingTime(); return this.hostCandidateHarvester.getHarvestingTime();
} }
long harvestingTime = -1; long harvestingTime = 0;
CandidateHarvester tmpHarvester; CandidateHarvester tmpHarvester;
Iterator<CandidateHarvester> itHarvesters = this.harvesters.iterator(); Iterator<CandidateHarvester> itHarvesters = this.harvesters.iterator();
while(itHarvesters.hasNext()) while(itHarvesters.hasNext())
@ -2219,16 +2212,54 @@ public class Agent
if(tmpHarvester.getClass().getName().endsWith(harvesterName)) if(tmpHarvester.getClass().getName().endsWith(harvesterName))
{ {
harvestingTime = tmpHarvester.getHarvestingTime(); harvestingTime = tmpHarvester.getHarvestingTime();
// Theremay be several harvester with the same class name. Thus, // There may be several harvester with the same class name.
// returns only an active one. // Thus, returns only an active one (if any).
if(harvestingTime != -1) if(harvestingTime != 0)
{ {
return harvestingTime; return harvestingTime;
} }
} }
} }
return -1; return 0;
}
/**
* Returns the number of harvesting time for the harvester given in
* parameter.
*
* @param harvesterName The class name if the harvester.
*
* @return The number of harvesting time for the harvester given in
* parameter.
*/
public int getNbHarvesting(String harvesterName)
{
if(this.hostCandidateHarvester.getClass().getName().endsWith(
harvesterName))
{
return this.hostCandidateHarvester.getNbHarvesting();
}
int nbHarvesting = 0;
CandidateHarvester tmpHarvester;
Iterator<CandidateHarvester> itHarvesters = this.harvesters.iterator();
while(itHarvesters.hasNext())
{
tmpHarvester = itHarvesters.next();
if(tmpHarvester.getClass().getName().endsWith(harvesterName))
{
nbHarvesting = tmpHarvester.getNbHarvesting();
// There may be several harvester with the same class name.
// Thus, returns only an active one (if any).
if(nbHarvesting != 0)
{
return nbHarvesting;
}
}
}
return 0;
} }
/** /**
@ -2236,10 +2267,7 @@ public class Agent
*/ */
public void startHarvesting() public void startHarvesting()
{ {
// Remember the start date of this harvester. this.harvestingTimeStat.startHarvesting();
this.lastStartHarvestingTime = System.currentTimeMillis();
// Reset the last harvesting time.
this.lastHarvestingTime = -1;
} }
/** /**
@ -2247,36 +2275,30 @@ public class Agent
*/ */
public void stopHarvesting() public void stopHarvesting()
{ {
// Remember the last harvesting time. this.harvestingTimeStat.stopHarvesting();
this.lastHarvestingTime = this.getTotalHarvestingTime();
// Stops the current timer.
this.lastStartHarvestingTime = -1;
} }
/** /**
* Returns the current harvesting time in ms. If this agent is not currently * 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 * harvesting, then returns the value of the last total harvesting time for
* all the harvesters. -1 if this agent has nerver harvested. * all the harvesters. 0 if this agent has nerver harvested.
* *
* @return The current harvesting time in ms. If this agent is not currently * @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 * harvesting, then returns the value of the last total harvesting time for
* all the harvesters. -1 if this agent has nerver harvested. * all the harvesters. 0 if this agent has nerver harvested.
*/ */
public long getTotalHarvestingTime() public long getTotalHarvestingTime()
{ {
if(this.lastStartHarvestingTime != -1) return this.harvestingTimeStat.getHarvestingTime();
{ }
long currentHarvestingTime
= System.currentTimeMillis() - lastStartHarvestingTime; /**
// Retest here, while the harvesting may be end while computing the * Returns the number of harvesting for this agent.
// harvsting time. *
if(this.lastStartHarvestingTime != -1) * @return The number of harvesting for this agent.
{ */
return currentHarvestingTime; public int getNbHarvesting()
} {
} return this.harvestingTimeStat.getNbHarvesting();
// If we are ont currently harvesting, then returns the value of the
// last harvesting time.
return this.lastHarvestingTime;
} }
} }

View File

@ -22,16 +22,9 @@ import org.ice4j.ice.*;
public abstract class CandidateHarvester public abstract class CandidateHarvester
{ {
/** /**
* The last harvest start time for this harvester. -1 if this harvester is * Manages statisics about harvesting time.
* not currently harvesting.
*/ */
private long lastStartHarvestingTime = -1; private HarvestingTimeStat harvestingTimeStat = new HarvestingTimeStat();
/**
* The last ended harvesting time for this harvester. -1 if this harvester
* has never harvested yet.
*/
private long lastHarvestingTime = -1;
/** /**
* Gathers all candidate addresses of the type that this * Gathers all candidate addresses of the type that this
@ -56,10 +49,7 @@ public abstract class CandidateHarvester
*/ */
public void startHarvesting() public void startHarvesting()
{ {
// Remember the start date of this harvester. this.harvestingTimeStat.startHarvesting();
this.lastStartHarvestingTime = System.currentTimeMillis();
// Reset the last harvesting time.
this.lastHarvestingTime = -1;
} }
/** /**
@ -67,47 +57,30 @@ public abstract class CandidateHarvester
*/ */
public void stopHarvesting() public void stopHarvesting()
{ {
// Remember the last harvesting time. this.harvestingTimeStat.stopHarvesting();
this.lastHarvestingTime = this.getHarvestingTime();
// Stops the current timer.
this.lastStartHarvestingTime = -1;
}
/**
* Disabless the harvesting timer. Called when the harvest is disabled.
*/
public void disableHarvesting()
{
// Remember the last harvesting time.
this.lastHarvestingTime = -1;
// Stops the current timer.
this.lastStartHarvestingTime = -1;
} }
/** /**
* Returns the current harvesting time in ms. If this harvester is not * Returns the current harvesting time in ms. If this harvester is not
* currently harvesting, then returns the value of the last harvesting time. * currently harvesting, then returns the value of the last harvesting time.
* -1 if this harvester has nerver harvested. * 0 if this harvester has nerver harvested.
* *
* @return The current harvesting time in ms. If this harvester is not * @return The current harvesting time in ms. If this harvester is not
* currently harvesting, then returns the value of the last harvesting time. * currently harvesting, then returns the value of the last harvesting time.
* -1 if this harvester has nerver harvested. * 0 if this harvester has nerver harvested.
*/ */
public long getHarvestingTime() public long getHarvestingTime()
{ {
if(this.lastStartHarvestingTime != -1) return this.harvestingTimeStat.getHarvestingTime();
{ }
long currentHarvestingTime
= System.currentTimeMillis() - lastStartHarvestingTime; /**
// Retest here, while the harvesting may be end while computing the * Returns the number of harvesting for this harvester.
// harvsting time. *
if(this.lastStartHarvestingTime != -1) * @return The number of harvesting for this harvester.
{ */
return currentHarvestingTime; public int getNbHarvesting()
} {
} return this.harvestingTimeStat.getNbHarvesting();
// If we are ont currently harvesting, then returns the value of the
// last harvesting time.
return this.lastHarvestingTime;
} }
} }

View File

@ -469,7 +469,6 @@ public class CandidateHarvesterSet
public void setEnabled(boolean enabled) public void setEnabled(boolean enabled)
{ {
logger.info("disabling harvester: " + harvester); logger.info("disabling harvester: " + harvester);
harvester.disableHarvesting();
this.enabled = enabled; this.enabled = enabled;
} }
} }

View File

@ -34,16 +34,9 @@ public class HostCandidateHarvester
= Logger.getLogger(HostCandidateHarvester.class.getName()); = Logger.getLogger(HostCandidateHarvester.class.getName());
/** /**
* The last harvest start time for this harvester. -1 if this harvester is * Manages statisics about harvesting time.
* not currently harvesting.
*/ */
private long lastStartHarvestingTime = -1; private HarvestingTimeStat harvestingTimeStat = new HarvestingTimeStat();
/**
* The last ended harvesting time for this harvester. -1 if this harvester
* has never harvested yet.
*/
private long lastHarvestingTime = -1;
/** /**
* Gathers all candidate addresses on the local machine, binds sockets on * Gathers all candidate addresses on the local machine, binds sockets on
@ -419,8 +412,7 @@ public class HostCandidateHarvester
*/ */
public void startHarvesting() public void startHarvesting()
{ {
// Remember the start date of this harvester. this.harvestingTimeStat.startHarvesting();
this.lastStartHarvestingTime = System.currentTimeMillis();
} }
/** /**
@ -428,36 +420,30 @@ public class HostCandidateHarvester
*/ */
public void stopHarvesting() public void stopHarvesting()
{ {
// Remember the last harvesting time. this.harvestingTimeStat.stopHarvesting();
this.lastHarvestingTime = this.getHarvestingTime();
// Stops the current timer.
this.lastStartHarvestingTime = -1;
} }
/** /**
* Returns the current harvesting time in ms. If this harvester is not * Returns the current harvesting time in ms. If this harvester is not
* currently harvesting, then returns the value of the last harvesting time. * currently harvesting, then returns the value of the last harvesting time.
* -1 if this harvester has nerver harvested. * 0 if this harvester has nerver harvested.
* *
* @return The current harvesting time in ms. If this harvester is not * @return The current harvesting time in ms. If this harvester is not
* currently harvesting, then returns the value of the last harvesting time. * currently harvesting, then returns the value of the last harvesting time.
* -1 if this harvester has nerver harvested. * 0 if this harvester has nerver harvested.
*/ */
public long getHarvestingTime() public long getHarvestingTime()
{ {
if(this.lastStartHarvestingTime != -1) return this.harvestingTimeStat.getHarvestingTime();
{ }
long currentHarvestingTime
= System.currentTimeMillis() - lastStartHarvestingTime; /**
// Retest here, while the harvesting may be end while computing the * Returns the number of harvesting for this harvester.
// harvsting time. *
if(this.lastStartHarvestingTime != -1) * @return The number of harvesting for this harvester.
{ */
return currentHarvestingTime; public int getNbHarvesting()
} {
} return this.harvestingTimeStat.getNbHarvesting();
// If we are ont currently harvesting, then returns the value of the
// last harvesting time.
return this.lastHarvestingTime;
} }
} }