x_runtime 1.0.0 → 1.0.1

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.
data/README.markdown CHANGED
@@ -103,6 +103,28 @@ XRuntime.p.log("/index") do
103
103
  end
104
104
  ```
105
105
 
106
+ ### Probe探针
107
+
108
+ 探针的用途:
109
+ * 在服务端请求最开始的地方打点比如`before_filter require_user`
110
+ * 在服务端业务有性能陷阱的地方打点
111
+ * 在Nginx或者Apache日志中输出记录,在日志中可以看到每个请求的路径,及其对应的业务路径中每个探针点之间的请求时长
112
+
113
+ 这样做的好处是:
114
+ 探针会产生大量的数据,并且这些数据需要记录下来以供分析,如果需要Rails或者Sinatra来处理这些数据会降低自身业务的性能,
115
+ 不如将这些数据放在请求头中,当Nginx或者Apache这些前段服务器记录日志的时候将其写入日志中,在我们分析日志的时候就能发现某些API的某些加过探针的运算消耗。
116
+
117
+ 在Rails或者Sinatra中可以通过如下方法使用探针:`XRuntime::Utils.probe(headers)`
118
+
119
+ Nginx日志格式:
120
+ ```
121
+ log_format timing '$remote_addr - $remote_user [$time_local] "$status" $request "$http_user_agent" '
122
+ 'upstream_response_time $upstream_response_time '
123
+ 'msec $msec request_time $request_time probe $upstream_http_xx_runtime';
124
+ ```
125
+
126
+ 最主要的是这个`probe $upstream_http_xx_runtime`
127
+
106
128
  ### Test
107
129
 
108
130
  请先修改test/server.rb和test/client.rb中的Redis参数,我的地址是localhost:6380,这个请改为你的地址。
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.1
@@ -1,9 +1,9 @@
1
1
  local path = ARGV[1];
2
2
  local score = tonumber(ARGV[2])
3
3
  local key = KEYS[1]
4
- local key_counter = string.format("%s::Counter",key)
5
- local key_amount = string.format("%s::Amount",key)
6
- local key_average = string.format("%s::Average",key)
4
+ local key_counter = key.."::Counter"
5
+ local key_amount = key.."::Amount"
6
+ local key_average = key.."::Average"
7
7
 
8
8
  -- 记录最新一次请求的耗时
9
9
  redis.call('zadd',key,score,path)
@@ -1,8 +1,8 @@
1
1
  local path = ARGV[1];
2
2
  local key = KEYS[1]
3
- local key_counter = string.format("%s::Counter",key)
4
- local key_amount = string.format("%s::Amount",key)
5
- local key_average = string.format("%s::Average",key)
3
+ local key_counter = key.."::Counter"
4
+ local key_amount = key.."::Amount"
5
+ local key_average = key.."::Average"
6
6
 
7
7
  -- 删除最新一次请求的耗时
8
8
  redis.call('zrem',key,path)
@@ -17,6 +17,26 @@ module XRuntime
17
17
  hash
18
18
  }
19
19
  end
20
+
21
+ PROBELATESTFLAG = "XX-Runtime-Latest"
22
+ PROBEFLAG = "XX-Runtime"
23
+ # Rails : XRuntime::Utils.probe(headers)
24
+ # Sinatra : XRuntime::Utils.probe(headers)
25
+
26
+ # for Nginx log:
27
+ # log_format timing '$remote_addr - $remote_user [$time_local] "$status" $request "$http_user_agent" '
28
+ # 'upstream_response_time $upstream_response_time '
29
+ # 'msec $msec request_time $request_time probe $upstream_http_xx_runtime';
30
+ def probe(headers={})
31
+ headers[PROBEFLAG] = "0" unless headers[PROBEFLAG]
32
+
33
+ if headers[PROBELATESTFLAG]
34
+ last_probe = headers[PROBELATESTFLAG].to_f
35
+ headers[PROBEFLAG] += ",#{"%06f"%(Time.now.to_f - last_probe)}"
36
+ end
37
+
38
+ headers[PROBELATESTFLAG] = Time.now.to_f.to_s
39
+ end
20
40
  end
21
41
  end
22
42
  end
data/test/server.ru CHANGED
@@ -9,6 +9,15 @@ 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
11
  use Rack::XRuntime, Redis.connect(:url => "redis://localhost:6379/"), :threshold => 30.0, :cache => 50, :expire => 60
12
+
13
+ get '/probe' do
14
+ XRuntime::Utils.probe(headers)
15
+ sleep rand(0.9)
16
+ XRuntime::Utils.probe(headers)
17
+ sleep rand(0.9)
18
+ XRuntime::Utils.probe(headers)
19
+ headers.inspect
20
+ end
12
21
 
13
22
  get /.*/ do
14
23
  XRuntime.p.log("/index") do
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 = "1.0.0"
8
+ s.version = "1.0.1"
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-28"
12
+ s.date = "2012-08-31"
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: 1.0.0
4
+ version: 1.0.1
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-28 00:00:00.000000000 Z
12
+ date: 2012-08-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -151,7 +151,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
151
151
  version: '0'
152
152
  segments:
153
153
  - 0
154
- hash: 194114813501611628
154
+ hash: -3268609457104962014
155
155
  required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  none: false
157
157
  requirements: