Working on IO loop stuff.
diff --git a/utils/misc/pom.xml b/utils/misc/pom.xml
index 899edcf..b451b50 100644
--- a/utils/misc/pom.xml
+++ b/utils/misc/pom.xml
@@ -20,7 +20,10 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava-testlib</artifactId>
- <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.onlab.onos</groupId>
+ <artifactId>onlab-junit</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
diff --git a/utils/misc/src/main/java/org/onlab/util/Counter.java b/utils/misc/src/main/java/org/onlab/util/Counter.java
new file mode 100644
index 0000000..ae97b76
--- /dev/null
+++ b/utils/misc/src/main/java/org/onlab/util/Counter.java
@@ -0,0 +1,124 @@
+package org.onlab.util;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
+
+/**
+ * Counting mechanism capable of tracking occurrences and rates.
+ */
+public class Counter {
+
+ private long total = 0;
+ private long start = System.currentTimeMillis();
+ private long end = 0;
+
+ /**
+ * Creates a new counter.
+ */
+ public Counter() {
+ }
+
+ /**
+ * Creates a new counter in a specific state. If non-zero end time is
+ * specified, the counter will be frozen.
+ *
+ * @param start start time
+ * @param total total number of items to start with
+ * @param end end time; if non-ze
+ */
+ public Counter(long start, long total, long end) {
+ checkArgument(start <= end, "Malformed interval: start > end");
+ checkArgument(total >= 0, "Total must be non-negative");
+ this.start = start;
+ this.total = total;
+ this.end = end;
+ }
+
+ /**
+ * Resets the counter, by zeroing out the count and restarting the timer.
+ */
+ public synchronized void reset() {
+ end = 0;
+ total = 0;
+ start = System.currentTimeMillis();
+ }
+
+ /**
+ * Freezes the counter in the current state including the counts and times.
+ */
+ public synchronized void freeze() {
+ end = System.currentTimeMillis();
+ }
+
+ /**
+ * Adds the specified number of occurrences to the counter. No-op if the
+ * counter has been frozen.
+ *
+ * @param count number of occurrences
+ */
+ public synchronized void add(long count) {
+ checkArgument(count >= 0, "Count must be non-negative");
+ if (end == 0L) {
+ total += count;
+ }
+ }
+
+ /**
+ * Returns the number of occurrences per second.
+ *
+ * @return throughput in occurrences per second
+ */
+ public synchronized double throughput() {
+ return total / duration();
+ }
+
+ /**
+ * Returns the total number of occurrences counted.
+ *
+ * @return number of counted occurrences
+ */
+ public synchronized long total() {
+ return total;
+ }
+
+ /**
+ * Returns the duration expressed in fractional number of seconds.
+ *
+ * @return fractional number of seconds since the last reset
+ */
+ public synchronized double duration() {
+ // Protect against 0 return by artificially setting duration to 1ms
+ long duration = (end == 0L ? System.currentTimeMillis() : end) - start;
+ return (duration == 0 ? 1 : duration) / 1000.0;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(total, start, end);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj instanceof Counter) {
+ final Counter other = (Counter) obj;
+ return Objects.equals(this.total, other.total) &&
+ Objects.equals(this.start, other.start) &&
+ Objects.equals(this.end, other.end);
+ }
+ return false;
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this)
+ .add("total", total)
+ .add("start", start)
+ .add("end", end)
+ .toString();
+ }
+}
diff --git a/utils/misc/src/test/java/org/onlab/util/CounterTest.java b/utils/misc/src/test/java/org/onlab/util/CounterTest.java
new file mode 100644
index 0000000..4b7c954
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/util/CounterTest.java
@@ -0,0 +1,71 @@
+package org.onlab.util;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.onlab.junit.TestTools.delay;
+
+/**
+ * Tests of the Counter utility.
+ */
+public class CounterTest {
+
+ @Test
+ public void basics() {
+ Counter tt = new Counter();
+ assertEquals("incorrect number of bytes", 0L, tt.total());
+ assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
+ tt.add(1234567890L);
+ assertEquals("incorrect number of bytes", 1234567890L, tt.total());
+ assertTrue("incorrect throughput", 1234567890.0 < tt.throughput());
+ delay(1500);
+ tt.add(1L);
+ assertEquals("incorrect number of bytes", 1234567891L, tt.total());
+ assertTrue("incorrect throughput", 1234567891.0 > tt.throughput());
+ tt.reset();
+ assertEquals("incorrect number of bytes", 0L, tt.total());
+ assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
+ }
+
+ @Test
+ public void freeze() {
+ Counter tt = new Counter();
+ tt.add(123L);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+ delay(1000);
+ tt.freeze();
+ tt.add(123L);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+
+ double d = tt.duration();
+ double t = tt.throughput();
+ assertEquals("incorrect duration", d, tt.duration(), 0.0001);
+ assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+ }
+
+ @Test
+ public void reset() {
+ Counter tt = new Counter();
+ tt.add(123L);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+
+ double d = tt.duration();
+ double t = tt.throughput();
+ assertEquals("incorrect duration", d, tt.duration(), 0.0001);
+ assertEquals("incorrect throughput", t, tt.throughput(), 0.0001);
+ assertEquals("incorrect number of bytes", 123L, tt.total());
+
+ tt.reset();
+ assertEquals("incorrect throughput", 0.0, tt.throughput(), 0.0001);
+ assertEquals("incorrect number of bytes", 0, tt.total());
+ }
+
+ @Test
+ public void syntheticTracker() {
+ Counter tt = new Counter(5000, 1000, 6000);
+ assertEquals("incorrect duration", 1, tt.duration(), 0.1);
+ assertEquals("incorrect throughput", 1000, tt.throughput(), 1.0);
+ }
+}