x_runtime 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -33,7 +33,9 @@ XRuntime是一个Rack的middleware,配合Redis用来分析Http Server每个URI
33
33
  可以指定XRuntime使用的Redis的key前缀或者叫命名空间:
34
34
  `XRuntime::NameSpace = "XRuntime::Threshold"`
35
35
 
36
- ### Server
36
+ ### Middleware
37
+
38
+ #### Server
37
39
 
38
40
  可以通过自带的Http页面查看请求数据,这些页面可以设置Http basic auth验证以保护起来
39
41
 
@@ -45,7 +47,7 @@ end
45
47
 
46
48
  增加一个子页面`/incache`,可以查看缓存中没有插入Redis的数据,但要记得这些缓存数据是在每个Server各自的进程中保存,每次访问这个页面,得到的缓存数据只代表该进程正在缓存的数据。
47
49
 
48
- ### Sinatra
50
+ #### Sinatra
49
51
 
50
52
  收集数据 `config.ru`:
51
53
 
@@ -61,7 +63,7 @@ run Rack::URLMap.new \
61
63
  "/xruntime" => XRuntime::Server.new
62
64
  ```
63
65
 
64
- ### Rails3
66
+ #### Rails3
65
67
 
66
68
  收集数据 `config/environment.rb`:
67
69
 
@@ -75,6 +77,32 @@ config.middleware.use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6
75
77
  mount XRuntime::Server, :at => "/xruntime"
76
78
  ```
77
79
 
80
+ ### Profiler
81
+
82
+ 这个功能用来将web server中一段代码的执行时间记录下来以供分析
83
+
84
+ 需要传递两个参数
85
+
86
+ * 一个作为这块代码的标示:key
87
+ * 另外一个是proc代码片段
88
+
89
+ 调用时使用:`XRuntime.profiler.log(key){...}`或者简写`XRuntime.p.log(key){...}`
90
+
91
+ 返回值是代码块的返回值,不影响原有逻辑。
92
+
93
+ #### Server
94
+
95
+ 通过这个地址可以查看运行结果:`\profiler`
96
+
97
+ #### Rails3 && Sinatra
98
+
99
+ ``` ruby
100
+ XRuntime.p.log("/index") do
101
+ sleep(0.01*rand(10))
102
+ "Hello, I'am x_runtime"
103
+ end
104
+ ```
105
+
78
106
  ### Test
79
107
 
80
108
  请先修改test/server.rb和test/client.rb中的Redis参数,我的地址是localhost:6380,这个请改为你的地址。
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.0
1
+ 0.9.0
@@ -1,6 +1,6 @@
1
1
  module XRuntime
2
2
  class Middleware
3
- attr_accessor :auth
3
+ attr_accessor :auth, :redis
4
4
  # threshold => ms
5
5
  def initialize(app, redis, opts = {})
6
6
  @app = app
@@ -0,0 +1,40 @@
1
+ module XRuntime
2
+ class Profiler
3
+ def initialize(redis, opts = {})
4
+ @redis = redis
5
+ opts = {:cache => 100, :expire => 120}.update opts
6
+ @cache = opts[:cache].to_i
7
+ @expire = opts[:expire].to_i
8
+ end
9
+
10
+ def ds
11
+ @ds ||= DataSet.new(redis_key, script, @cache, @expire)
12
+ end
13
+
14
+ def script
15
+ @script ||= Script.new(@redis)
16
+ end
17
+
18
+ def redis_key
19
+ @key ||= "#{XRuntime::NameSpace}::Profiler"
20
+ end
21
+
22
+ def call(&blk)
23
+ start_time = Time.now
24
+ result = yield
25
+ cost = (Time.now - start_time).to_f*1000
26
+
27
+ return cost, result
28
+ end
29
+
30
+ def logredis(key, &blk)
31
+ raise ArgumentError, "Need a block of code for profile." unless blk
32
+ cost, result = call(&blk)
33
+ ds.add(key, cost)
34
+ return result
35
+ end
36
+
37
+ alias :log :logredis
38
+
39
+ end
40
+ end
@@ -13,7 +13,7 @@ module XRuntime
13
13
 
14
14
  get '/' do
15
15
  @req = Rack::Request.new(env)
16
- Template.new(XRuntime.middleware.ds, :limit => (@req.params["limit"] ? @req.params["limit"].to_i : 20), :offset => @req.params["offset"].to_i).render
16
+ Template.new(XRuntime.m.ds, :limit => (@req.params["limit"] ? @req.params["limit"].to_i : 20), :offset => @req.params["offset"].to_i).render
17
17
  end
18
18
 
19
19
  get '/incache' do
@@ -26,5 +26,11 @@ module XRuntime
26
26
  }
27
27
  }.to_json
28
28
  end
29
- end
29
+
30
+ get '/profiler' do
31
+ @req = Rack::Request.new(env)
32
+ Template.new(XRuntime.p.ds, :limit => (@req.params["limit"] ? @req.params["limit"].to_i : 20), :offset => @req.params["offset"].to_i).render
33
+ end
34
+
35
+ end#end of server
30
36
  end
data/lib/x_runtime.rb CHANGED
@@ -9,7 +9,21 @@ $:.unshift(File.dirname(__FILE__))
9
9
  module XRuntime
10
10
  NameSpace = "XRuntime::Threshold"
11
11
  extend self
12
- attr_accessor :middleware
12
+
13
+ def middleware
14
+ @middleware || raise(RuntimeError, "XRuntime::Middleware haven't been used as a middleware.")
15
+ end
16
+
17
+ def middleware=(m)
18
+ @middleware = m
19
+ end
20
+
21
+ def profiler
22
+ @profiler ||= Profiler.new(middleware.redis, :cache => 200, :expire => 120)
23
+ end
24
+
25
+ alias :p :profiler
26
+ alias :m :middleware
13
27
 
14
28
  autoload :Middleware, "x_runtime/middleware"
15
29
  autoload :DataSet, "x_runtime/data_set"
@@ -17,6 +31,7 @@ module XRuntime
17
31
  autoload :Server, "x_runtime/server"
18
32
  autoload :Template, "x_runtime/template"
19
33
  autoload :Utils, "x_runtime/utils"
34
+ autoload :Profiler, "x_runtime/profiler"
20
35
  end
21
36
 
22
37
  module Rack
data/test/server.ru CHANGED
@@ -8,12 +8,13 @@ require "#{File.dirname(__FILE__)}/../lib/x_runtime"
8
8
  class Server < Sinatra::Base
9
9
  # use XRuntime::Middleware, 10, Redis.connect(:url => "redis://localhost:6380/")
10
10
  # XRuntime::Middleware is same as Rack::XRuntime
11
- use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6379/"), :threshold => 50.0, :cache => 50, :expire => 60
11
+ use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6379/"), :threshold => 30.0, :cache => 50, :expire => 60
12
12
 
13
13
  get /.*/ do
14
- # sleep(1)
15
- sleep(0.01*rand(10))
16
- "Hello, I'am x_runtime"
14
+ XRuntime.p.log("/index") do
15
+ sleep(0.01*rand(10))
16
+ "Hello, I'am x_runtime"
17
+ end
17
18
  end
18
19
  end
19
20
 
data/x_runtime.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "x_runtime"
8
- s.version = "0.8.0"
8
+ s.version = "0.9.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}"]
12
- s.date = "2012-08-25"
12
+ s.date = "2012-08-27"
13
13
  s.description = "\u{7531}\u{4e8e}\u{4f7f}\u{7528}\u{5230}Redis\u{7684}lua script,\u{6240}\u{4ee5}\u{9700}\u{8981}\u{4f60}\u{7684}Redis\u{670d}\u{52a1}\u{652f}\u{6301},redis-server\u{7248}\u{672c}>2.6.x,redis(ruby gem)>3.0.1."
14
14
  s.email = "zheng.cuizh@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,7 @@ 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/profiler.rb",
29
30
  "lib/x_runtime/redis.lua",
30
31
  "lib/x_runtime/script.rb",
31
32
  "lib/x_runtime/server.rb",
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.8.0
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-25 00:00:00.000000000 Z
12
+ date: 2012-08-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -109,6 +109,7 @@ files:
109
109
  - lib/x_runtime.rb
110
110
  - lib/x_runtime/data_set.rb
111
111
  - lib/x_runtime/middleware.rb
112
+ - lib/x_runtime/profiler.rb
112
113
  - lib/x_runtime/redis.lua
113
114
  - lib/x_runtime/script.rb
114
115
  - lib/x_runtime/server.rb
@@ -135,7 +136,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
136
  version: '0'
136
137
  segments:
137
138
  - 0
138
- hash: -3201334121123236605
139
+ hash: -3365753816145701982
139
140
  required_rubygems_version: !ruby/object:Gem::Requirement
140
141
  none: false
141
142
  requirements: