Skip to content
Browse files

Add tests for thread-safe meters, histograms, and counters.

  • Loading branch information...
1 parent 2c59e20 commit 9e68b99aca758d0e74788486579b38ad61746481 @aphyr aphyr committed
Showing with 138 additions and 4 deletions.
  1. +19 −1 test/counter_test.rb
  2. +83 −2 test/histogram_test.rb
  3. +11 −1 test/meter_test.rb
  4. +25 −0 test/test_helper.rb
View
20 test/counter_test.rb
@@ -3,6 +3,8 @@
require 'metriks/counter'
class CounterTest < Test::Unit::TestCase
+ include ThreadHelper
+
def setup
@counter = Metriks::Counter.new
end
@@ -13,9 +15,25 @@ def test_increment
assert_equal 1, @counter.count
end
+ def test_increment_threaded
+ thread 10, n: 100 do
+ @counter.increment
+ end
+
+ assert_equal 1000, @counter.count
+ end
+
def test_increment_by_more
@counter.increment 10
assert_equal 10, @counter.count
end
-end
+
+ def test_increment_by_more_threaded
+ thread 10, n: 100 do
+ @counter.increment 10
+ end
+
+ assert_equal 10000, @counter.count
+ end
+end
View
85 test/histogram_test.rb
@@ -3,6 +3,8 @@
require 'metriks/histogram'
class HistogramTest < Test::Unit::TestCase
+ include ThreadHelper
+
def setup
end
@@ -33,6 +35,17 @@ def test_uniform_sample_mean
assert_equal 7, @histogram.mean
end
+ def test_uniform_sample_mean_threaded
+ @histogram = Metriks::Histogram.new(Metriks::UniformSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE))
+
+ thread 10, n: 100 do
+ @histogram.update(5)
+ @histogram.update(10)
+ end
+
+ assert_equal 7, @histogram.mean
+ end
+
def test_uniform_sample_2000
@histogram = Metriks::Histogram.new(Metriks::UniformSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE))
@@ -42,6 +55,21 @@ def test_uniform_sample_2000
assert_equal 1999, @histogram.max
end
+
+ def test_uniform_sample_2000_threaded
+ @histogram = Metriks::Histogram.new(Metriks::UniformSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE))
+
+ t = 10
+ thread t do |i|
+ 2000.times do |x|
+ if (x % t) == i
+ @histogram.update x
+ end
+ end
+ end
+
+ assert_equal 1999, @histogram.max
+ end
def test_uniform_sample_snashot
@histogram = Metriks::Histogram.new(Metriks::UniformSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE))
@@ -55,6 +83,20 @@ def test_uniform_sample_snashot
assert_equal 49.5, snapshot.median
end
+ def test_uniform_sample_snapshot_threaded
+ @histogram = Metriks::Histogram.new(Metriks::UniformSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE))
+
+ thread 10 do
+ 100.times do |idx|
+ @histogram.update(idx)
+ end
+ end
+
+ snapshot = @histogram.snapshot
+
+ assert_equal 49.5, snapshot.median
+ end
+
def test_exponential_sample_min
@histogram = Metriks::Histogram.new(Metriks::ExponentiallyDecayingSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA))
@@ -63,7 +105,7 @@ def test_exponential_sample_min
assert_equal 5, @histogram.min
end
-
+
def test_exponential_sample_max
@histogram = Metriks::Histogram.new(Metriks::ExponentiallyDecayingSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA))
@@ -81,6 +123,17 @@ def test_exponential_sample_mean
assert_equal 7, @histogram.mean
end
+
+ def test_exponential_sample_mean_threaded
+ @histogram = Metriks::Histogram.new(Metriks::ExponentiallyDecayingSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA))
+
+ thread 10, n: 100 do
+ @histogram.update(5)
+ @histogram.update(10)
+ end
+
+ assert_equal 7, @histogram.mean
+ end
def test_exponential_sample_2000
@histogram = Metriks::Histogram.new(Metriks::ExponentiallyDecayingSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA))
@@ -91,6 +144,21 @@ def test_exponential_sample_2000
assert_equal 1999, @histogram.max
end
+
+ def test_exponential_sample_2000_threaded
+ @histogram = Metriks::Histogram.new(Metriks::ExponentiallyDecayingSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA))
+
+ t = 10
+ thread t do |i|
+ 2000.times do |idx|
+ if (idx % t) == i
+ @histogram.update(idx)
+ end
+ end
+ end
+
+ assert_equal 1999, @histogram.max
+ end
def test_exponential_sample_snashot
@histogram = Metriks::Histogram.new(Metriks::ExponentiallyDecayingSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA))
@@ -104,4 +172,17 @@ def test_exponential_sample_snashot
assert_equal 49.5, snapshot.median
end
-end
+ def test_exponential_sample_snapshot_threaded
+ @histogram = Metriks::Histogram.new(Metriks::ExponentiallyDecayingSample.new(Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA))
+
+ thread 10 do
+ 100.times do |idx|
+ @histogram.update(idx)
+ end
+ end
+
+ snapshot = @histogram.snapshot
+
+ assert_equal 49.5, snapshot.median
+ end
+end
View
12 test/meter_test.rb
@@ -3,6 +3,8 @@
require 'metriks/meter'
class MeterTest < Test::Unit::TestCase
+ include ThreadHelper
+
def setup
@meter = Metriks::Meter.new
end
@@ -17,6 +19,14 @@ def test_meter
assert_equal 1, @meter.count
end
+ def test_meter_threaded
+ thread 10, n: 100 do
+ @meter.mark
+ end
+
+ assert_equal 1000, @meter.count
+ end
+
def test_one_minute_rate
@meter.mark 1000
@@ -25,4 +35,4 @@ def test_one_minute_rate
assert_equal 200, @meter.one_minute_rate
end
-end
+end
View
25 test/test_helper.rb
@@ -7,3 +7,28 @@
require 'metriks'
Thread.abort_on_exception = true
+
+module ThreadHelper
+ require 'thread'
+
+ # Run the given block on n threads in parallel. Returns an array of the
+ # return values of each thread's last invocation of block. Options:
+
+ # :n: call block n times per thread. Default 1.
+ def thread(threads = 2, opts = {})
+ n = opts[:n] || 1
+ results = []
+
+ threads.times.map do |i|
+ Thread.new do
+ n.times do
+ results[i] = yield i
+ end
+ end
+ end.each do |thread|
+ thread.join
+ end
+
+ results
+ end
+end

0 comments on commit 9e68b99

Please sign in to comment.
Something went wrong with that request. Please try again.