x_runtime 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,4 +2,5 @@ source "http://ruby.taobao.org"
2
2
  gem 'rack'
3
3
  gem 'rake'
4
4
  gem 'jeweler'
5
- gem 'redis', '~>3.0.1'
5
+ gem 'redis', '~>3.0.1'
6
+ gem 'sinatra'
data/Gemfile.lock CHANGED
@@ -9,10 +9,17 @@ GEM
9
9
  rdoc
10
10
  json (1.7.5)
11
11
  rack (1.4.1)
12
+ rack-protection (1.2.0)
13
+ rack
12
14
  rake (0.9.2.2)
13
15
  rdoc (3.12)
14
16
  json (~> 1.4)
15
17
  redis (3.0.1)
18
+ sinatra (1.3.3)
19
+ rack (~> 1.3, >= 1.3.6)
20
+ rack-protection (~> 1.2)
21
+ tilt (~> 1.3, >= 1.3.3)
22
+ tilt (1.3.3)
16
23
 
17
24
  PLATFORMS
18
25
  ruby
@@ -22,3 +29,4 @@ DEPENDENCIES
22
29
  rack
23
30
  rake
24
31
  redis (~> 3.0.1)
32
+ sinatra
data/README.markdown CHANGED
@@ -30,26 +30,45 @@ XRuntime是一个Rack的middleware,配合Redis用来分析Http Server每个URI
30
30
  可以指定XRuntime使用的Redis的key前缀或者叫命名空间:
31
31
  `XRuntime::NameSpace = "RuntimeEx::Threshold"`
32
32
 
33
- [http://localhost:4567/xruntime](/xruntime)支持Http basic auth验证:
33
+ ### Server
34
34
 
35
- use Rack::XRuntime, 100, Redis.connect(:url => "redis://localhost:6380/") do |name, password|
36
- name == "cui" and password == "hello"
37
- end
35
+ 可以通过自带的Http页面查看请求数据,这些页面可以设置Http basic auth验证以保护起来
36
+ 只要在加载XRuntime::Server的时候,传递一个proc作为认证条件即可
37
+ 可以在配置url路由的时候指定XRuntime::Server的挂载路径,然后就可以通过该路径访问页面
38
38
 
39
39
  ### Sinatra
40
40
 
41
+ 收集数据 `config.ru`:
42
+
41
43
  use Rack::XRuntime, 100, Redis.connect(:url => "redis://localhost:6380/")
42
44
 
45
+ 查看数据 `config.ru`:
46
+
47
+ ``` ruby
48
+ run Rack::URLMap.new \
49
+ "/" => Server.new,
50
+ "/xruntime" => XRuntime::Server.new{|name, password|name == "cui" and password == "hello"}
51
+ ```
52
+
43
53
  ### Rails3
44
54
 
45
- # config/environment.rb
46
- config.middleware.insert_after Rack::Runtime, Rack::XRuntime, 100, Redis.connect(:url => "redis://localhost:6380/")
55
+ 收集数据 `config/environment.rb`:
56
+
57
+ ``` ruby
58
+ config.middleware.insert_after Rack::Runtime, Rack::XRuntime, 100, Redis.connect(:url => "redis://localhost:6380/")
59
+ ```
60
+
61
+ 查看数据 `config/route.rb`:
62
+
63
+ ``` ruby
64
+ mount Rack::XRuntime.new{|name, password|name == "cui" and password == "hello"}, :at => "/xruntime"
65
+ ```
47
66
 
48
67
  ### Test
49
68
 
50
69
  请先修改test/server.rb和test/client.rb中的Redis参数,我的地址是localhost:6380,这个请改为你的地址。
51
70
 
52
- * 先启动服务 `ruby test/server.rb`
71
+ * 先启动服务 `rackup test/server.ru`
53
72
  * 再产生测试数据 `ruby test/client.rb`
54
73
 
55
74
  执行完毕后可以打开浏览器访问[/xruntime](http://localhost:4567/xruntime),看是否已经准确的记录了一些数据
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.4.0
@@ -1,12 +1,12 @@
1
1
  module XRuntime
2
2
  class Middleware
3
+
3
4
  # threshold => ms
4
- def initialize(app, threshold, redis, &auth)
5
+ def initialize(app, threshold, redis)
5
6
  @app = app
6
7
  @threshold = threshold.to_f
7
8
  @redis = redis
8
- @portal = Portal.new(ds)
9
- @portal = Rack::Auth::Basic.new(@portal, &auth) if block_given?
9
+ XRuntime.middleware = self
10
10
  end
11
11
 
12
12
  def ds
@@ -16,16 +16,12 @@ module XRuntime
16
16
  def script
17
17
  @script ||= Script.new(@redis)
18
18
  end
19
-
20
- def call(env)
21
- if env['REQUEST_PATH'] == "/xruntime"
22
- call_portal(env)
23
- else
24
- call_app(env)
25
- end
19
+
20
+ def redis_key
21
+ @key ||= "#{XRuntime::NameSpace}::#{@threshold}"
26
22
  end
27
23
 
28
- def call_app(env)
24
+ def call(env)
29
25
  start_time = Time.now
30
26
  status, headers, body = @app.call(env)
31
27
  request_time = (Time.now - start_time).to_f*1000
@@ -36,18 +32,10 @@ module XRuntime
36
32
 
37
33
  [status, headers, body]
38
34
  end
39
-
40
- def call_portal(env)
41
- @portal.call(env)
42
- end
43
35
 
44
36
  def logredis(cost,uri)
45
37
  ds.add(uri, cost)
46
38
  end
47
39
 
48
- def redis_key
49
- @key ||= "#{XRuntime::NameSpace}::#{@threshold}"
50
- end
51
-
52
40
  end#end of Middleware
53
41
  end
@@ -0,0 +1,15 @@
1
+ module XRuntime
2
+ class Server
3
+ def initialize(app=nil, &auth)
4
+ @server = lambda {|env|
5
+ @req = Rack::Request.new(env)
6
+ [200, {'Content-Type' => 'text/html'}, [Template.new(XRuntime.middleware.ds, :limit => (@req.params["limit"] ? @req.params["limit"].to_i : 20), :offset => @req.params["offset"].to_i).render]]
7
+ }
8
+ @server = Rack::Auth::Basic.new(@server, &auth) if auth
9
+ end
10
+
11
+ def call(env)
12
+ @server.call(env)
13
+ end
14
+ end
15
+ end
data/lib/x_runtime.rb CHANGED
@@ -7,12 +7,14 @@ require "fileutils"
7
7
  $:.unshift(File.dirname(__FILE__))
8
8
 
9
9
  module XRuntime
10
- NameSpace = "RuntimeEx::Threshold"
10
+ NameSpace = "RuntimeEx::Threshold"
11
+ extend self
12
+ attr_accessor :middleware
11
13
 
12
14
  autoload :Middleware, "x_runtime/middleware"
13
15
  autoload :DataSet, "x_runtime/data_set"
14
16
  autoload :Script, "x_runtime/script"
15
- autoload :Portal, "x_runtime/portal"
17
+ autoload :Server, "x_runtime/server"
16
18
  autoload :Template, "x_runtime/template"
17
19
  autoload :Utils, "x_runtime/utils"
18
20
  end
data/test/client.rb CHANGED
@@ -4,7 +4,7 @@ def alpha
4
4
  end
5
5
 
6
6
  def rand_url(size=1)
7
- "http://localhost:4567/#{(1..size).to_a.inject(''){|s,t|s+=alpha[rand(alpha.size-1)].to_s}}"
7
+ "http://localhost:9292/#{(1..size).to_a.inject(''){|s,t|s+=alpha[rand(alpha.size-1)].to_s}}"
8
8
  end
9
9
 
10
10
  100.times do
data/test/server.ru ADDED
@@ -0,0 +1,23 @@
1
+ # run with : rackup server.ru
2
+
3
+ require "sinatra/base"
4
+ require "#{File.dirname(__FILE__)}/../lib/x_runtime"
5
+
6
+ # redis-server version must > 2.6.0 for lua script.
7
+
8
+ class Server < Sinatra::Base
9
+ # use XRuntime::Middleware, 10, Redis.connect(:url => "redis://localhost:6380/")
10
+ # XRuntime::Middleware is same as Rack::XRuntime
11
+ use Rack::XRuntime, 10, Redis.connect(:url => "redis://localhost:6379/")
12
+
13
+ get /.*/ do
14
+ # sleep(1)
15
+ sleep(0.01*rand(10))
16
+ "Hello, I'am x_runtime"
17
+ end
18
+ end
19
+
20
+ # Use with basic auth
21
+ run Rack::URLMap.new \
22
+ "/" => Server.new,
23
+ "/xruntime" => XRuntime::Server.new{|name, password|name == "cui" and password == "hello"}
data/x_runtime.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "x_runtime"
8
- s.version = "0.3.2"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["\u{5d14}\u{5ce5}"]
@@ -26,15 +26,15 @@ Gem::Specification.new do |s|
26
26
  "lib/x_runtime.rb",
27
27
  "lib/x_runtime/data_set.rb",
28
28
  "lib/x_runtime/middleware.rb",
29
- "lib/x_runtime/portal.rb",
30
29
  "lib/x_runtime/redis.lua",
31
30
  "lib/x_runtime/script.rb",
31
+ "lib/x_runtime/server.rb",
32
32
  "lib/x_runtime/template.erb",
33
33
  "lib/x_runtime/template.rb",
34
34
  "lib/x_runtime/utils.rb",
35
35
  "test/client.rb",
36
36
  "test/helper.rb",
37
- "test/server.rb",
37
+ "test/server.ru",
38
38
  "test/test_x_runtime.rb",
39
39
  "x_runtime.gemspec"
40
40
  ]
@@ -52,17 +52,20 @@ Gem::Specification.new do |s|
52
52
  s.add_runtime_dependency(%q<rake>, [">= 0"])
53
53
  s.add_runtime_dependency(%q<jeweler>, [">= 0"])
54
54
  s.add_runtime_dependency(%q<redis>, ["~> 3.0.1"])
55
+ s.add_runtime_dependency(%q<sinatra>, [">= 0"])
55
56
  else
56
57
  s.add_dependency(%q<rack>, [">= 0"])
57
58
  s.add_dependency(%q<rake>, [">= 0"])
58
59
  s.add_dependency(%q<jeweler>, [">= 0"])
59
60
  s.add_dependency(%q<redis>, ["~> 3.0.1"])
61
+ s.add_dependency(%q<sinatra>, [">= 0"])
60
62
  end
61
63
  else
62
64
  s.add_dependency(%q<rack>, [">= 0"])
63
65
  s.add_dependency(%q<rake>, [">= 0"])
64
66
  s.add_dependency(%q<jeweler>, [">= 0"])
65
67
  s.add_dependency(%q<redis>, ["~> 3.0.1"])
68
+ s.add_dependency(%q<sinatra>, [">= 0"])
66
69
  end
67
70
  end
68
71
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: x_runtime
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -75,6 +75,22 @@ dependencies:
75
75
  - - ~>
76
76
  - !ruby/object:Gem::Version
77
77
  version: 3.0.1
78
+ - !ruby/object:Gem::Dependency
79
+ name: sinatra
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
78
94
  description: 由于使用到Redis的lua script,所以需要你的Redis服务支持,redis-server版本>2.6.x,redis(ruby
79
95
  gem)>3.0.1.
80
96
  email: zheng.cuizh@gmail.com
@@ -93,15 +109,15 @@ files:
93
109
  - lib/x_runtime.rb
94
110
  - lib/x_runtime/data_set.rb
95
111
  - lib/x_runtime/middleware.rb
96
- - lib/x_runtime/portal.rb
97
112
  - lib/x_runtime/redis.lua
98
113
  - lib/x_runtime/script.rb
114
+ - lib/x_runtime/server.rb
99
115
  - lib/x_runtime/template.erb
100
116
  - lib/x_runtime/template.rb
101
117
  - lib/x_runtime/utils.rb
102
118
  - test/client.rb
103
119
  - test/helper.rb
104
- - test/server.rb
120
+ - test/server.ru
105
121
  - test/test_x_runtime.rb
106
122
  - x_runtime.gemspec
107
123
  homepage: http://github.com/charlescui/x_runtime
@@ -119,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
119
135
  version: '0'
120
136
  segments:
121
137
  - 0
122
- hash: -4238907334465737071
138
+ hash: 336817792487171531
123
139
  required_rubygems_version: !ruby/object:Gem::Requirement
124
140
  none: false
125
141
  requirements:
@@ -1,12 +0,0 @@
1
- module XRuntime
2
- class Portal
3
- def initialize(ds)
4
- @ds = ds
5
- end
6
-
7
- def call(env)
8
- @req = Rack::Request.new(env)
9
- [200, {}, [Template.new(@ds, :limit => (@req.params["limit"] ? @req.params["limit"].to_i : 20), :offset => @req.params["offset"].to_i).render]]
10
- end
11
- end
12
- end
data/test/server.rb DELETED
@@ -1,21 +0,0 @@
1
- require "sinatra"
2
- require_relative "../lib/x_runtime"
3
-
4
- # redis-server version must > 2.6.0 for lua script.
5
-
6
- # use XRuntime::Middleware, 10, Redis.connect(:url => "redis://localhost:6380/")
7
-
8
- # XRuntime::Middleware is same as Rack::XRuntime
9
-
10
- # use Rack::XRuntime, 10, Redis.connect(:url => "redis://localhost:6380/")
11
-
12
- # Use with basic auth
13
- use Rack::XRuntime, 10, Redis.connect(:url => "redis://localhost:6380/") do |name, password|
14
- name == "cui" and password == "hello"
15
- end
16
-
17
- get /.*/ do
18
- # sleep(1)
19
- sleep(0.01*rand(10))
20
- "Hello, I'am x_runtime"
21
- end