Class: Profit::Client
- Inherits:
-
Object
- Object
- Profit::Client
- Defined in:
- lib/profit/client.rb
Instance Attribute Summary collapse
-
#ctx ⇒ Object
Returns the value of attribute ctx.
-
#pending ⇒ Object
Returns the value of attribute pending.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
-
#initialize(ctx = nil) ⇒ Client
constructor
A new instance of Client.
- #start(metric_type) ⇒ Object
- #stop(metric_type) ⇒ Object
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
#ctx ⇒ Object
Returns the value of attribute ctx.
4 5 6 |
# File 'lib/profit/client.rb', line 4 def ctx @ctx end |
#pending ⇒ Object
Returns the value of attribute pending.
4 5 6 |
# File 'lib/profit/client.rb', line 4 def pending @pending end |
#socket ⇒ Object
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 |