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

View File

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

View File

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