rack-multiplexer 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/rack/multiplexer.rb +4 -16
- data/lib/rack/multiplexer/version.rb +1 -1
- data/scripts/benchmark.rb +28 -0
- data/spec/rack/multiplexer_spec.rb +10 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1693a2a3b7c5e01ff4504b1050be22e70d56a476
|
4
|
+
data.tar.gz: be765bf4e45551f7888e8d445fe9046ea6209d94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e74c42adeebd12b164e473584ccf15b3745e4706ca21bf0c2bcbadf836474d76b7ebbaa1ba483d67ca6952a6cd15ece7d0da101e5d79f14d417baadd0485a68b
|
7
|
+
data.tar.gz: 0c0e978e9bb4aff842e0353e044eafae25df42328deacccace09534b7eb6d3d7131fbf83ce0e78dd98841cf95da6cc329a32f4033dc97637f0cce4c0606951f2
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Rack::Multiplexer
|
2
|
-
Provides a simple router & dispatcher for Rack applications as a Rack application.
|
3
|
-
The routing
|
2
|
+
Provides a simple router & dispatcher for Rack applications as a Rack application.
|
3
|
+
The routing algorithm has only O(1) time complexity because all routes are compiled into one Regexp.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
```
|
data/lib/rack/multiplexer.rb
CHANGED
@@ -2,8 +2,7 @@ require "rack/multiplexer/version"
|
|
2
2
|
require "rack/request"
|
3
3
|
|
4
4
|
# Provides a simple router & dispatcher for Rack applications as a Rack application.
|
5
|
-
#
|
6
|
-
# because all routes are compiled as one Regexp.
|
5
|
+
# The routing algorithm has only O(1) time complexity because all routes are compiled into one Regexp.
|
7
6
|
#
|
8
7
|
# Example:
|
9
8
|
#
|
@@ -17,18 +16,7 @@ require "rack/request"
|
|
17
16
|
#
|
18
17
|
module Rack
|
19
18
|
class Multiplexer
|
20
|
-
|
21
|
-
[
|
22
|
-
404,
|
23
|
-
{
|
24
|
-
"Content-Type" => "text/plain",
|
25
|
-
"Content-Length" => "0",
|
26
|
-
},
|
27
|
-
[""],
|
28
|
-
]
|
29
|
-
}
|
30
|
-
|
31
|
-
def initialize(not_found_application = DEFAULT_NOT_FOUND_APPLICATION, &block)
|
19
|
+
def initialize(not_found_application = default_not_found_application, &block)
|
32
20
|
@not_found_application = not_found_application
|
33
21
|
instance_eval(&block) if block
|
34
22
|
end
|
@@ -76,7 +64,7 @@ module Rack
|
|
76
64
|
404,
|
77
65
|
{
|
78
66
|
"Content-Type" => "text/plain",
|
79
|
-
"Content-Length" => 0,
|
67
|
+
"Content-Length" => "0",
|
80
68
|
},
|
81
69
|
[""],
|
82
70
|
]
|
@@ -135,7 +123,7 @@ module Rack
|
|
135
123
|
keys = []
|
136
124
|
segments = []
|
137
125
|
pattern.split("/").each do |segment|
|
138
|
-
segments << segment.gsub(PLACEHOLDER_REGEXP, "([^#?/]+)")
|
126
|
+
segments << Regexp.escape(segment).gsub(PLACEHOLDER_REGEXP, "([^#?/]+)")
|
139
127
|
if key = Regexp.last_match(1)
|
140
128
|
keys << key
|
141
129
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# Benchmark script for comparison of the routing algorithm between v0.0.2 and v0.0.3.
|
2
|
+
# In my laptop environment, v0.0.3 is 17x faster than 0.0.2 with 676 routes & 100,000 tries.
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
|
5
|
+
require "rack/multiplexer"
|
6
|
+
require "benchmark"
|
7
|
+
|
8
|
+
multiplexer = Rack::Multiplexer.new
|
9
|
+
(?a..?z).each do |head|
|
10
|
+
(?a..?z).each do |tail|
|
11
|
+
multiplexer.get("/#{head}{tail}") do
|
12
|
+
[200, {}, ["OK"]]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
env = {
|
18
|
+
"PATH_INFO" => "/mm",
|
19
|
+
"REQUEST_METHOD" => "GET",
|
20
|
+
}
|
21
|
+
|
22
|
+
n = 100_000
|
23
|
+
puts Benchmark::CAPTION
|
24
|
+
puts Benchmark.measure {
|
25
|
+
n.times do
|
26
|
+
multiplexer.call(env)
|
27
|
+
end
|
28
|
+
}
|
@@ -110,5 +110,15 @@ describe Rack::Multiplexer do
|
|
110
110
|
multiplexer.call(env.merge("REQUEST_METHOD" => "HEAD", "PATH_INFO" => "/a"))[0].should == 200
|
111
111
|
end
|
112
112
|
end
|
113
|
+
|
114
|
+
context "with path including '.'" do
|
115
|
+
it "matches exactly" do
|
116
|
+
multiplexer = described_class.new
|
117
|
+
multiplexer.get("/a.*") {|env| [200, {}, ["ok"]] }
|
118
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/a.*"))[0].should == 200
|
119
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/aaa"))[0].should == 404
|
120
|
+
multiplexer.call(env.merge("REQUEST_METHOD" => "GET", "PATH_INFO" => "/abc"))[0].should == 404
|
121
|
+
end
|
122
|
+
end
|
113
123
|
end
|
114
124
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-multiplexer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -110,6 +110,7 @@ files:
|
|
110
110
|
- lib/rack/multiplexer.rb
|
111
111
|
- lib/rack/multiplexer/version.rb
|
112
112
|
- rack-multiplexer.gemspec
|
113
|
+
- scripts/benchmark.rb
|
113
114
|
- spec/rack/multiplexer_spec.rb
|
114
115
|
- spec/spec_helper.rb
|
115
116
|
homepage: https://github.com/r7kamura/rack-multiplexer
|
@@ -139,4 +140,3 @@ summary: Provides a simple router & dispatcher for Rack.
|
|
139
140
|
test_files:
|
140
141
|
- spec/rack/multiplexer_spec.rb
|
141
142
|
- spec/spec_helper.rb
|
142
|
-
has_rdoc:
|