about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/mongrel.rb30
-rw-r--r--test/test_uriclassifier.rb49
2 files changed, 55 insertions, 24 deletions
diff --git a/lib/mongrel.rb b/lib/mongrel.rb
index d8bf3d8..edee39e 100644
--- a/lib/mongrel.rb
+++ b/lib/mongrel.rb
@@ -59,19 +59,17 @@ module Mongrel
       @matcher = nil
     end
     
-    def register(path, handler)
-      if @routes[path]
-        raise RegistrationError, "#{path.inspect} is already registered"
-      elsif !path or path.empty?
-        raise RegistrationError, "Path is empty"
-      end      
-      @routes[path.dup] = handler
+    def register(uri, handler)
+      raise RegistrationError, "#{uri.inspect} is already registered" if @routes[uri]
+      raise RegistrationError, "URI is empty" if !uri or uri.empty?
+      raise RegistrationError, "URI must begin with a \"#{Const::SLASH}\"" unless uri[0..0] == Const::SLASH
+      @routes[uri.dup] = handler
       rebuild
     end
     
-    def unregister(path)
-      handler = @routes.delete(path)
-      raise RegistrationError, "#{path.inspect} was not registered" unless handler
+    def unregister(uri)
+      handler = @routes.delete(uri)
+      raise RegistrationError, "#{uri.inspect} was not registered" unless handler
       rebuild
       handler
     end
@@ -94,15 +92,15 @@ module Mongrel
     private
     
     def rebuild
-      routes = @routes.sort_by do |path, handler|        
+      routes = @routes.sort_by do |uri, handler|        
         # Sort by name
-        path
-      end.sort_by do |path, handler|          
+        uri
+      end.sort_by do |uri, handler|          
         # Then by descending length
-        -path.length
+        -uri.length
       end
-      @matcher = Regexp.new(routes.map do |path, handler|
-        Regexp.new('^' + Regexp.escape(path))
+      @matcher = Regexp.new(routes.map do |uri, handler|
+        Regexp.new('^' + Regexp.escape(uri))
       end.join('|'))
     end    
     
diff --git a/test/test_uriclassifier.rb b/test/test_uriclassifier.rb
index 2698034..99bbda2 100644
--- a/test/test_uriclassifier.rb
+++ b/test/test_uriclassifier.rb
@@ -52,11 +52,11 @@ class URIClassifierTest < Test::Unit::TestCase
   def test_exceptions
     uri_classifier = URIClassifier.new
 
-    uri_classifier.register("test", 1)
+    uri_classifier.register("/test", 1)
     
     failed = false
     begin
-      uri_classifier.register("test", 1)
+      uri_classifier.register("/test", 1)
     rescue => e
       failed = true
     end
@@ -78,17 +78,17 @@ class URIClassifierTest < Test::Unit::TestCase
     uri_classifier = URIClassifier.new
     
     100.times do
-      uri_classifier.register("stuff", 1)
-      value = uri_classifier.unregister("stuff")
+      uri_classifier.register("/stuff", 1)
+      value = uri_classifier.unregister("/stuff")
       assert_equal 1, value
     end
 
-    uri_classifier.register("things",1)
-    script_name, path_info, value = uri_classifier.resolve("things")
+    uri_classifier.register("/things",1)
+    script_name, path_info, value = uri_classifier.resolve("/things")
     assert_equal 1, value
 
-    uri_classifier.unregister("things")
-    script_name, path_info, value = uri_classifier.resolve("things")
+    uri_classifier.unregister("/things")
+    script_name, path_info, value = uri_classifier.resolve("/things")
     assert_nil value
 
   end
@@ -184,6 +184,39 @@ class URIClassifierTest < Test::Unit::TestCase
       assert_equal 2, handler
     end
   end
+  
+  def test_benchmark    
+    require 'facets/core/array/combos'
+  
+    @fragments = %w(the benchmark module provides methods to measure and report the time used to execute ruby code)
+    @classifier = URIClassifier.new
 
+    @fragments.size.times do |n|
+      @classifier.register("/" + @fragments[0..n].join("/"), 1)
+    end
+    
+    flip = false
+    @requests = @fragments.combos.map do |combo|
+      request = "/" + combo.join("/")
+      request = request[0..-4] if flip and request.size > 4
+      flip = !flip
+      request
+    end
+    
+    p @requests
+    
+    puts "#{@fragments.size} paths registered"
+    puts "#{@requests.size} requests queued"
+    
+    Benchmark.bm do |x|
+      x.report do
+        @requests.each do |request|
+          @classifier.resolve(request)
+        end
+      end
+    end
+    
+  end
+  
 end