Class: Profit::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/profit/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx = nil) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
# File 'lib/profit/client.rb', line 6

def initialize(ctx = nil)
  @ctx = ctx || ZMQ::Context.new
  @socket = @ctx.connect(:PUSH, "tcp://127.0.0.1:5556")
  @pending = {}
end

Instance Attribute Details

#ctxObject

Returns the value of attribute ctx.



4
5
6
# File 'lib/profit/client.rb', line 4

def ctx
  @ctx
end

#pendingObject

Returns the value of attribute pending.



4
5
6
# File 'lib/profit/client.rb', line 4

def pending
  @pending
end

#socketObject

Returns the value of attribute socket.



4
5
6
# File 'lib/profit/client.rb', line 4

def socket
  @socket
end

Instance Method Details

#start(metric_type) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/profit/client.rb', line 12

def start(metric_type)
  now = Time.now
  start_file = caller[0][/(.+):(.+):/,1]
  start_line = caller[0][/(.+):(.+):/,2].to_i + 1

  # TODO: wrap in a Mutex & make the key a combo of metric_type,
  #       pid, and/or thread object_id to make this thread safe and
  #       thread-robust.
  @pending[metric_type] = { start_file: start_file,
                            start_line: start_line,
                            start_time: now }
end

#stop(metric_type) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/profit/client.rb', line 25

def stop(metric_type)
  now = Time.now
  metric = @pending.delete(metric_type)
  recorded_time = Time.now - metric[:start_time]
  stop_file = caller[0][/(.+):(.+):/,1]
  stop_line = caller[0][/(.+):(.+):/,2].to_i - 1

  @socket.send({ metric_type: metric_type,
                 recorded_time: recorded_time,
                 start_file: metric[:start_file],
                 start_line: metric[:start_line],
                 stop_file: stop_file,
                 stop_line: stop_line }.to_json)
end