Class: XRuntime::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/x_runtime/profiler.rb

Instance Method Summary collapse

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

#dsObject



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

Raises:

  • (ArgumentError)


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

#redis_keyObject



18
19
20
# File 'lib/x_runtime/profiler.rb', line 18

def redis_key
  @key ||= "#{XRuntime::NameSpace}::Profiler"
end

#scriptObject



14
15
16
# File 'lib/x_runtime/profiler.rb', line 14

def script
  @script ||= Script.new(@redis)
end