Statistician

- Made it an interface
- Implemented the interface and made the changes in the code to use the interface

- We do this because we'll extend the code for SocialKad
This commit is contained in:
Joshua Kissoon 2014-05-07 23:45:23 +05:30
parent 1d5d8004df
commit 32a6d7875b
4 changed files with 108 additions and 47 deletions

View File

@ -0,0 +1,81 @@
package kademlia;
/**
* Specification for class that keeps statistics for a Kademlia instance.
*
* These statistics are temporary and will be lost when Kad is shut down.
*
* @author Joshua Kissoon
* @since 20140507
*/
public interface KadStatistician
{
/**
* Used to indicate some data is sent
*
* @param size The size of the data sent
*/
public void sentData(long size);
/**
* @return The total data sent
*/
public long getTotalDataSent();
/**
* Used to indicate some data was received
*
* @param size The size of the data received
*/
public void receivedData(long size);
/**
* @return The total data received
*/
public long getTotalDataReceived();
/**
* Sets the bootstrap time for this Kademlia Node
*
* @param time The bootstrap time in nanoseconds
*/
public void setBootstrapTime(long time);
/**
* @return How long the system took to bootstrap
*/
public long getBootstrapTime();
/**
* Add the timing for a new content lookup operation that took place
*
* @param time The time the content lookup took in nanoseconds
* @param routeLength The length of the route it took to get the content
*/
public void addContentLookup(long time, int routeLength);
/**
* @return The total number of content lookups performed.
*/
public int numContentLookups();
/**
* @return The total time spent on content lookups.
*/
public long totalContentLookupTime();
/**
* Compute the average time a content lookup took
*
* @return The average time in milliseconds
*/
public double averageContentLookupTime();
/**
* Compute the average route length of content lookup operations.
*
* @return The average route length
*/
public double averageContentLookupRouteLength();
}

View File

@ -68,7 +68,7 @@ public class KademliaNode
private final transient MessageFactory messageFactory;
/* Statistics */
private final Statistician statistician;
private final KadStatistician statistician;
{
@ -454,7 +454,7 @@ public class KademliaNode
/**
* @return The statistician that manages all statistics
*/
public Statistician getStatistician()
public KadStatistician getStatistician()
{
return this.statistician;
}

View File

@ -10,7 +10,7 @@ import java.text.DecimalFormat;
* @author Joshua Kissoon
* @since 20140505
*/
public class Statistician
public class Statistician implements KadStatistician
{
/* How much data was sent and received by the server over the network */
@ -35,65 +35,45 @@ public class Statistician
this.totalRouteLength = 0;
}
/**
* Used to indicate some data is sent
*
* @param size The size of the data sent
*/
@Override
public void sentData(long size)
{
this.totalDataSent += size;
this.numDataSent++;
}
/**
* @return The total data sent
*/
@Override
public long getTotalDataSent()
{
return this.totalDataSent;
}
/**
* Used to indicate some data was received
*
* @param size The size of the data received
*/
@Override
public void receivedData(long size)
{
this.totalDataReceived += size;
this.numDataReceived++;
}
/**
* @return The total data received
*/
@Override
public long getTotalDataReceived()
{
return this.totalDataReceived;
}
/**
* Sets the bootstrap time for this Kademlia Node
*
* @param time The bootstrap time in nanoseconds
*/
@Override
public void setBootstrapTime(long time)
{
this.bootstrapTime = time;
}
@Override
public long getBootstrapTime()
{
return this.bootstrapTime;
}
/**
* Add the timing for a new content lookup operation that took place
*
* @param time The time the content lookup took in nanoseconds
* @param routeLength The length of the route it took to get the content
*/
@Override
public void addContentLookup(long time, int routeLength)
{
this.numContentLookups++;
@ -101,21 +81,19 @@ public class Statistician
this.totalRouteLength += routeLength;
}
@Override
public int numContentLookups()
{
return this.numContentLookups;
}
@Override
public long totalContentLookupTime()
{
return this.totalContentLookupTime;
}
/**
* Compute the average time a content lookup took
*
* @return The average time in milliseconds
*/
@Override
public double averageContentLookupTime()
{
double avg = (double) ((double) this.totalContentLookupTime / (double) this.numContentLookups) / 1000000D;
@ -123,49 +101,51 @@ public class Statistician
return new Double(df.format(avg));
}
@Override
public double averageContentLookupRouteLength()
{
double avg = (double) ((double) this.totalRouteLength / (double) this.numContentLookups);
DecimalFormat df = new DecimalFormat("#.00");
return new Double(df.format(avg));
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder("Statistician: [");
sb.append("Bootstrap Time: ");
sb.append(this.getBootstrapTime());
sb.append("; ");
sb.append("Data Sent: ");
sb.append("(");
sb.append(this.numDataSent);
sb.append(") ");
sb.append(this.getTotalDataSent());
sb.append(" bytes; ");
sb.append("Data Received: ");
sb.append("(");
sb.append(this.numDataReceived);
sb.append(") ");
sb.append(this.getTotalDataReceived());
sb.append(" bytes; ");
sb.append("Num Content Lookups: ");
sb.append(this.numContentLookups());
sb.append("; ");
sb.append("Avg Content Lookup Time: ");
sb.append(this.averageContentLookupTime());
sb.append("; ");
sb.append("Avg Content Lookup Route Lth: ");
sb.append(this.averageContentLookupRouteLength());
sb.append("; ");
sb.append("]");
return sb.toString();
}
}

View File

@ -13,7 +13,7 @@ import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import kademlia.Statistician;
import kademlia.KadStatistician;
import kademlia.exceptions.KadServerDownException;
import kademlia.message.Message;
import kademlia.message.MessageFactory;
@ -47,7 +47,7 @@ public class KadServer
/* Factories */
private final MessageFactory messageFactory;
private final Statistician statistician;
private final KadStatistician statistician;
{
@ -68,7 +68,7 @@ public class KadServer
*
* @throws java.net.SocketException
*/
public KadServer(int udpPort, MessageFactory mFactory, Node localNode, KadConfiguration config, Statistician statistician) throws SocketException
public KadServer(int udpPort, MessageFactory mFactory, Node localNode, KadConfiguration config, KadStatistician statistician) throws SocketException
{
this.config = config;
this.socket = new DatagramSocket(udpPort);