From 32a6d7875b5cf5038c363d30d94d1fb1b79f69b2 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Wed, 7 May 2014 23:45:23 +0530 Subject: [PATCH] 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 --- src/kademlia/KadStatistician.java | 81 +++++++++++++++++++++++++++++++ src/kademlia/KademliaNode.java | 4 +- src/kademlia/Statistician.java | 64 +++++++++--------------- src/kademlia/core/KadServer.java | 6 +-- 4 files changed, 108 insertions(+), 47 deletions(-) create mode 100644 src/kademlia/KadStatistician.java diff --git a/src/kademlia/KadStatistician.java b/src/kademlia/KadStatistician.java new file mode 100644 index 0000000..8fe9a0c --- /dev/null +++ b/src/kademlia/KadStatistician.java @@ -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(); +} diff --git a/src/kademlia/KademliaNode.java b/src/kademlia/KademliaNode.java index 31ed6cc..14393ed 100644 --- a/src/kademlia/KademliaNode.java +++ b/src/kademlia/KademliaNode.java @@ -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; } diff --git a/src/kademlia/Statistician.java b/src/kademlia/Statistician.java index 47ebcb1..5d1259d 100644 --- a/src/kademlia/Statistician.java +++ b/src/kademlia/Statistician.java @@ -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(); } } diff --git a/src/kademlia/core/KadServer.java b/src/kademlia/core/KadServer.java index ef5d7cd..0e82a18 100644 --- a/src/kademlia/core/KadServer.java +++ b/src/kademlia/core/KadServer.java @@ -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);