Permalink
Browse files

windowed statistics.

  • Loading branch information...
1 parent 82ae578 commit 265f2ee75118d565b43f58b9688593b0d41a8bae @mariusae mariusae committed Nov 2, 2010
@@ -14,7 +14,7 @@ trait Statistic {
def average: Int = if (count != 0) sum / count else 0
- def add(value: Int): Unit = add(1, value)
+ def add(value: Int): Unit = add(value, 1)
def add(value: Int, count: Int)
}
@@ -26,7 +26,7 @@ class ScalarStatistic extends Statistic {
def sum = accumulator
def count = counter
- def add(count: Int, value: Int) = serializer {
+ def add(value: Int, count: Int) = serializer {
counter += count
accumulator += value
}
@@ -46,8 +46,7 @@ class TimeWindowedStatistic[S <: Statistic](bucketCount: Int, bucketDuration: Du
def rateInHz = {
val (begin, end) = collection.timeSpan
val timeDiff = end - begin
- println("rateInHz: (%s - %s ) = %s / %s".format(end, begin, count, timeDiff.inSeconds))
- count / timeDiff.inSeconds.toDouble
+ count / timeDiff.inSeconds
}
override def toString = collection.toString
@@ -15,7 +15,18 @@ object StatisticSpec extends Specification {
c.sum must be_==(2)
c.add(1, 1000)
- c.sum must be_==(1002)
+ c.sum must be_==(3)
+ c.count must be_==(1002)
+ }
+
+ "compute averages" in {
+ val c = new ScalarStatistic
+ for (i <- 1 to 100)
+ c.add(i)
+
+ c.count must be_==(100)
+ c.sum must be_==(50 * (1+100))
+ c.average must be_==(50)
}
}
@@ -25,28 +36,33 @@ object StatisticSpec extends Specification {
"keep a total sum over its window" in {
val c = new TimeWindowedStatistic[ScalarStatistic](10, 10.seconds)
c.sum must be_==(0)
- c.add(1, 1000)
- c.sum must be_==(1000)
+ c.add(1, 1)
+ c.sum must be_==(1)
+ c.count must be_==(1)
Time.advance(11.seconds)
c.add(1)
- c.sum must be_==(1001)
+ c.sum must be_==(2)
+ c.count must be_==(2)
Time.advance(80.seconds)
c.add(1)
- c.sum must be_==(1002)
+ c.sum must be_==(3)
+ c.count must be_==(3)
Time.advance(10.seconds)
c.sum must be_==(2)
+ c.count must be_==(2)
c.add(1)
c.sum must be_==(3)
+ c.count must be_==(3)
}
"keep a total sum over its window (2)" in {
val c = new TimeWindowedStatistic[ScalarStatistic](10, 10.seconds)
for (i <- 1 to 100) {
- c.add(1)
+ c.add(1, 1)
c.sum must be_==(i)
Time.advance(1.seconds)
}
@@ -62,20 +78,13 @@ object StatisticSpec extends Specification {
"compute rate" in {
val c = new TimeWindowedStatistic(10, 10.seconds)
- println(1, c)
- c.add(1)
- println(2, c)
- c.rateInHz() must be_==(1/10.0)
- println(3, c)
- Time.advance(50.seconds)
c.add(1)
- println(4, c)
- c.rateInHz() must be_==(1)
+ c.rateInHz() must be_==(0)
Time.advance(50.seconds)
for (i <- 0 until 100) {
c.rateInHz() must be_==(i)
- c.add(60)
+ c.add(60, 60)
}
}
}

0 comments on commit 265f2ee

Please sign in to comment.