Class: XRuntime::Profiler
- Inherits:
-
Object
- Object
- XRuntime::Profiler
- Defined in:
- lib/x_runtime/profiler.rb
Instance Method Summary collapse
- #call(&blk) ⇒ Object
- #ds ⇒ Object
-
#initialize(redis, opts = {}) ⇒ Profiler
constructor
A new instance of Profiler.
- #logredis(key, &blk) ⇒ Object (also: #log)
- #redis_key ⇒ Object
- #script ⇒ Object
Constructor Details
#initialize(redis, opts = {}) ⇒ Profiler
Returns a new instance of Profiler.
3 4 5 6 7 8 |
# File 'lib/x_runtime/profiler.rb', line 3 def initialize(redis, opts = {}) @redis = redis opts = {:cache => 100, :expire => 120}.update opts @cache = opts[:cache].to_i @expire = opts[:expire].to_i end |
Instance Method Details
#call(&blk) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/x_runtime/profiler.rb', line 22 def call(&blk) start_time = Time.now result = yield cost = (Time.now - start_time).to_f*1000 return cost, result end |
#ds ⇒ Object
10 11 12 |
# File 'lib/x_runtime/profiler.rb', line 10 def ds @ds ||= DataSet.new(redis_key, script, @cache, @expire) end |
#logredis(key, &blk) ⇒ Object Also known as: log
30 31 32 33 34 35 |
# File 'lib/x_runtime/profiler.rb', line 30 def logredis(key, &blk) raise ArgumentError, "Need a block of code for profile." unless blk cost, result = call(&blk) ds.add(key, cost) rescue nil return result end |