x_runtime 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source "http://ruby.taobao.org"
2
+ source "http://gems.github.com/"
2
3
  gem 'rack'
3
4
  gem 'rake'
4
5
  gem 'jeweler'
data/Gemfile.lock CHANGED
@@ -1,5 +1,6 @@
1
1
  GEM
2
2
  remote: http://ruby.taobao.org/
3
+ remote: http://gems.github.com/
3
4
  specs:
4
5
  git (1.2.5)
5
6
  jeweler (1.8.4)
@@ -7,7 +8,7 @@ GEM
7
8
  git (>= 1.2.5)
8
9
  rake
9
10
  rdoc
10
- json (1.7.5)
11
+ json (1.7.4)
11
12
  rack (1.4.1)
12
13
  rack-protection (1.2.0)
13
14
  rack
@@ -15,7 +16,7 @@ GEM
15
16
  rdoc (3.12)
16
17
  json (~> 1.4)
17
18
  redis (3.0.1)
18
- sinatra (1.3.3)
19
+ sinatra (1.3.2)
19
20
  rack (~> 1.3, >= 1.3.6)
20
21
  rack-protection (~> 1.2)
21
22
  tilt (~> 1.3, >= 1.3.3)
data/README.markdown CHANGED
@@ -24,8 +24,9 @@ XRuntime是一个Rack的middleware,配合Redis用来分析Http Server每个URI
24
24
 
25
25
  引入这个middleware需要两个参数:
26
26
 
27
- 1. threshold,表示处理时间超过多少毫秒的请求才会被记录
28
- 2. redis对象
27
+ 1. redis对象
28
+ 2. :threshold,表示处理时间超过多少毫秒的请求才会被记录
29
+ 3. :cache,表示请求积累到多少条的时候才通过redis的pipeline机制插入到redis数据库中
29
30
 
30
31
  可以指定XRuntime使用的Redis的key前缀或者叫命名空间:
31
32
  `XRuntime::NameSpace = "RuntimeEx::Threshold"`
@@ -33,17 +34,19 @@ XRuntime是一个Rack的middleware,配合Redis用来分析Http Server每个URI
33
34
  ### Server
34
35
 
35
36
  可以通过自带的Http页面查看请求数据,这些页面可以设置Http basic auth验证以保护起来
36
- 只要在加载XRuntime::Server的时候,传递一个proc作为认证条件即可
37
- 可以在配置url路由的时候指定XRuntime::Server的挂载路径,然后就可以通过该路径访问页面
37
+
38
+ ``` ruby
39
+ XRuntime::Server.use(Rack::Auth::Basic) do |user, password|
40
+ user == 'cui' && password == "hello"
41
+ end
42
+ ```
38
43
 
39
44
  ### Sinatra
40
45
 
41
46
  收集数据 `config.ru`:
42
47
 
43
48
  ``` ruby
44
- use Rack::XRuntime, 10, Redis.connect(:url => "redis://localhost:6379/") do |name, password|
45
- name == "cui" and password == "hello"
46
- end
49
+ use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6379/"), :threshold => 100.0, :cache => 50
47
50
  ```
48
51
 
49
52
  查看数据 `config.ru`:
@@ -59,15 +62,13 @@ run Rack::URLMap.new \
59
62
  收集数据 `config/environment.rb`:
60
63
 
61
64
  ``` ruby
62
- config.middleware.insert_after Rack::Runtime, Rack::XRuntime, 100, Redis.connect(:url => "redis://localhost:6380/") do |name, password|
63
- name == "cui" and password == "hello"
64
- end
65
+ config.middleware.use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6380/"), :threshold => 100.0, :cache => 50
65
66
  ```
66
67
 
67
68
  查看数据 `config/routes.rb`:
68
69
 
69
70
  ``` ruby
70
- mount Rack::XRuntime, :at => "/xruntime"
71
+ mount XRuntime::Server, :at => "/xruntime"
71
72
  ```
72
73
 
73
74
  ### Test
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
1
+ 0.5.0
@@ -1,13 +1,14 @@
1
1
  module XRuntime
2
2
  class DataSet
3
3
 
4
- def initialize(key, script)
4
+ def initialize(key, script, count)
5
5
  raise ArgumentError, "Script must not nil and be valid!" unless script
6
6
  @key = key
7
7
  @key_counter = "#{@key}::Counter"
8
8
  @key_amount = "#{@key}::Amount"
9
9
  @key_average = "#{@key}::Average"
10
10
  @script = script
11
+ @count = count
11
12
  # 预先加载Lua脚本
12
13
  @script.sha
13
14
  @data = []
@@ -45,7 +46,7 @@ module XRuntime
45
46
  def add(member, score)
46
47
  @data.push([member, score])
47
48
  # 如果@data数据达到一定数量,则一起插入redis
48
- if @data.size >= 50
49
+ if @data.size >= @count
49
50
  @script.redis.multi do
50
51
  while (data = @data.pop) do
51
52
  @script.evalsha([@key], [data[0], data[1]])
@@ -2,16 +2,17 @@ module XRuntime
2
2
  class Middleware
3
3
  attr_accessor :auth
4
4
  # threshold => ms
5
- def initialize(app, threshold, redis, &auth)
5
+ def initialize(app, redis, opts = {})
6
6
  @app = app
7
- @threshold = threshold.to_f
8
7
  @redis = redis
9
- @auth = auth
8
+ opts = {:threshold => 100.0, :cache => 50}.update opts
9
+ @cache = opts[:cache].to_i
10
+ @threshold = opts[:threshold].to_f
10
11
  XRuntime.middleware = self
11
12
  end
12
13
 
13
14
  def ds
14
- @ds ||= DataSet.new(redis_key, script)
15
+ @ds ||= DataSet.new(redis_key, script, @cache)
15
16
  end
16
17
 
17
18
  def script
@@ -26,7 +27,6 @@ module XRuntime
26
27
  start_time = Time.now
27
28
  status, headers, body = @app.call(env)
28
29
  request_time = (Time.now - start_time).to_f*1000
29
-
30
30
  if request_time >= @threshold
31
31
  logredis(request_time, env['REQUEST_URI']) rescue nil
32
32
  end
@@ -1,15 +1,14 @@
1
+ require "sinatra/base"
2
+
3
+ if defined? Encoding
4
+ Encoding.default_external = Encoding::UTF_8
5
+ end
6
+
1
7
  module XRuntime
2
- class Server
3
- def initialize(app=nil)
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, &XRuntime.middleware.auth) if XRuntime.middleware.auth
9
- end
10
-
11
- def call(env)
12
- @server.call(env)
8
+ class Server < Sinatra::Base
9
+ get '/' do
10
+ @req = Rack::Request.new(env)
11
+ Template.new(XRuntime.middleware.ds, :limit => (@req.params["limit"] ? @req.params["limit"].to_i : 20), :offset => @req.params["offset"].to_i).render
13
12
  end
14
13
  end
15
- end
14
+ end
@@ -1,3 +1,5 @@
1
+ require "erb"
2
+
1
3
  module XRuntime
2
4
  class Template
3
5
  def initialize(ds, opts={})
data/test/server.ru CHANGED
@@ -4,13 +4,11 @@ require "sinatra/base"
4
4
  require "#{File.dirname(__FILE__)}/../lib/x_runtime"
5
5
 
6
6
  # redis-server version must > 2.6.0 for lua script.
7
-
7
+
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, 10, Redis.connect(:url => "redis://localhost:6379/") do |name, password|
12
- name == "cui" and password == "hello"
13
- end
11
+ use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6379/"), :threshold => 50.0, :cache => 50
14
12
 
15
13
  get /.*/ do
16
14
  # sleep(1)
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.4.1"
8
+ s.version = "0.5.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-21"
12
+ s.date = "2012-08-22"
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 = [
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.4.1
4
+ version: 0.5.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-21 00:00:00.000000000 Z
12
+ date: 2012-08-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -135,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
135
  version: '0'
136
136
  segments:
137
137
  - 0
138
- hash: -2051941527554950884
138
+ hash: -1119447801023194543
139
139
  required_rubygems_version: !ruby/object:Gem::Requirement
140
140
  none: false
141
141
  requirements: