Skip to content
This repository
Browse code

Move from using rbtree gem to avl_tree gem

  • Loading branch information...
commit cb43adf8af8199da03233d6a1cfdff92336f4806 1 parent 6c38466
Eric Lindvall authored
2  Gemfile
@@ -2,6 +2,8 @@ source :rubygems
2 2
 
3 3
 gemspec
4 4
 
  5
+gem 'rbtree'
  6
+
5 7
 group :test do
6 8
   gem 'turn', '0.8.2', :require => false
7 9
   gem 'rake', '0.8.7'
106  benchmark/samplers.rb
@@ -2,57 +2,91 @@
2 2
 
3 3
 require 'benchmark'
4 4
 require 'metriks'
5  
-
6  
-
7  
-def fib(n)
8  
-  n < 2 ? n : fib(n-1) + fib(n-2)
9  
-end
10  
-
11  
-uniform_timer     = Metriks::Timer.new(Metriks::Histogram.new_uniform)
12  
-exponential_timer = Metriks::Timer.new(Metriks::Histogram.new_exponentially_decaying)
  5
+require 'rbtree'
  6
+require 'avl_tree'
  7
+require 'red_black_tree'
13 8
 
14 9
 fib_times = ARGV[0] ? ARGV[0].to_i : 10
15 10
 iter      = ARGV[1] ? ARGV[1].to_i : 100000
16 11
 
17  
-puts "fib(#{fib_times}): #{iter} iterations"
18  
-puts "-" * 50
19 12
 
20  
-plain = Benchmark.realtime do
21  
-  for i in 1..iter
22  
-    fib(fib_times)
  13
+class TimerBenchmarker
  14
+  attr_reader :iter, :fib_times
  15
+
  16
+  def initialize(fib_times, iter)
  17
+    @fib_times = fib_times
  18
+    @iter      = iter
  19
+    @mapping   = { :plain => nil }
23 20
   end
24  
-end
25 21
 
26  
-puts "%15s: %f secs %f secs/call" % [ 'plain', plain, plain / iter ]
  22
+  def measure(key, value)
  23
+    @mapping[key] = value
  24
+  end
27 25
 
28  
-uniform = Benchmark.realtime do
29  
-  for i in 1..iter
30  
-    uniform_timer.time do
31  
-      fib(fib_times)
  26
+  def run
  27
+    @results = {}
  28
+    @mapping.each do |key, timer|
  29
+      @results[key] = Benchmark.realtime do
  30
+        if timer
  31
+          for i in 1..iter
  32
+            timer.time do
  33
+              fib(fib_times)
  34
+            end
  35
+          end
  36
+        else
  37
+          for i in 1..iter
  38
+            fib(fib_times)
  39
+          end
  40
+        end
  41
+      end
32 42
     end
  43
+    report
33 44
   end
34  
-end
35 45
 
36  
-puts "%15s: %f secs %f secs/call -- %.1f%% slower than plain (%f secs/call)" % [ 
37  
-  'uniform', uniform, uniform / iter, 
38  
-  (uniform - plain) / plain * 100 ,
39  
-  (uniform - plain) / iter,
40  
-]
  46
+  def report
  47
+    results = @results.sort_by { |k,v| v }
  48
+    results.each_with_index do |(name, time), idx|
  49
+      puts "%23s: %f secs %f secs/call" % [
  50
+        name, time, time / iter
  51
+      ]
41 52
 
42  
-exponential = Benchmark.realtime do
43  
-  for i in 1..iter
44  
-    exponential_timer.time do
45  
-      fib(fib_times)
  53
+      if idx > 0
  54
+        prev_name, prev_time = results[idx - 1]
  55
+        puts "#{' ' * 25} - %.1f%% slower than %s (%f secs/call)" % [
  56
+          (time - prev_time) / prev_time * 100, prev_name,
  57
+          (time - prev_time) / iter
  58
+        ]
  59
+      end
  60
+
  61
+      if idx > 1
  62
+        plain_name, plain_time = results[0]
  63
+        puts "#{' ' * 25} - %.1f%% slower than %s (%f secs/call)" % [
  64
+          (time - plain_time) / plain_time * 100, plain_name,
  65
+          (time - plain_time) / iter
  66
+        ]
  67
+      end
46 68
     end
47 69
   end
  70
+
  71
+  def fib(n)
  72
+    n < 2 ? n : fib(n-1) + fib(n-2)
  73
+  end
48 74
 end
49 75
 
50  
-puts "%15s: %f secs %f secs/call -- %.1f%% slower than plain (%f secs/call) -- %.1f%% slower than uniform (%f secs/call)" % [ 
51  
-  'exponential', exponential, exponential / iter, 
52  
-  (exponential - plain) / plain * 100 ,
53  
-  (exponential - plain) / iter,
54  
-  (exponential - uniform) / uniform * 100 ,
55  
-  (exponential - uniform) / iter
56  
-]
  76
+reporter = TimerBenchmarker.new(fib_times, iter)
  77
+
  78
+reporter.measure :uniform, Metriks::Timer.new(Metriks::Histogram.new_uniform)
  79
+
  80
+reporter.measure :exponential, Metriks::Timer.new(Metriks::ExponentiallyDecayingSample.new(
  81
+  Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA, RBTree.new))
57 82
 
  83
+reporter.measure :exponential_avl, Metriks::Timer.new(Metriks::ExponentiallyDecayingSample.new(
  84
+  Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA, AVLTree.new))
  85
+
  86
+reporter.measure :exponential_red_black, Metriks::Timer.new(Metriks::ExponentiallyDecayingSample.new(
  87
+  Metriks::Histogram::DEFAULT_SAMPLE_SIZE, Metriks::Histogram::DEFAULT_ALPHA, RedBlackTree.new))
  88
+
  89
+puts "fib(#{fib_times}): #{iter} iterations"
  90
+puts "-" * 50
58 91
 
  92
+reporter.run
6  lib/metriks/exponentially_decaying_sample.rb
... ...
@@ -1,13 +1,13 @@
1 1
 require 'atomic'
2  
-require 'rbtree'
  2
+require 'red_black_tree'
3 3
 require 'metriks/snapshot'
4 4
 
5 5
 module Metriks
6 6
   class ExponentiallyDecayingSample
7 7
     RESCALE_THRESHOLD = 60 * 60 # 1 hour
8 8
 
9  
-    def initialize(reservoir_size, alpha)
10  
-      @values = RBTree.new
  9
+    def initialize(reservoir_size, alpha, values = nil)
  10
+      @values = values || RedBlackTree.new
11 11
       @count = Atomic.new(0)
12 12
       @next_scale_time = Atomic.new(0)
13 13
       @alpha = alpha
4  metriks.gemspec
@@ -41,11 +41,11 @@ Gem::Specification.new do |s|
41 41
   ## that are needed for an end user to actually USE your code.
42 42
   s.add_dependency('atomic', ["~> 1.0"])
43 43
   s.add_dependency('hitimes', [ "~> 1.1"])
44  
-  s.add_dependency('rbtree', [ "~> 0.3" ])
  44
+  s.add_dependency('avl_tree', [ "~> 1.1.1" ])
45 45
 
46 46
   ## List your development dependencies here. Development dependencies are
47 47
   ## those that are only needed during development
48  
-  s.add_development_dependency('tomdoc', ["~> 0.2"])
  48
+  # s.add_development_dependency('tomdoc', ["~> 0.2"])
49 49
   s.add_development_dependency('mocha', ['~> 0.10'])
50 50
 
51 51
   ## Leave this section as-is. It will be automatically generated from the

0 notes on commit cb43adf

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