profit 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,16 @@
1
1
  module Profit
2
+
3
+ def self.client
4
+ @client ||= Client.new
5
+ end
6
+
2
7
  class Client
3
8
 
4
- attr_accessor :ctx, :socket, :pending
9
+ attr_accessor :ctx, :pending
5
10
 
6
- def initialize(ctx = nil)
7
- @ctx = ctx || ZMQ::Context.new
8
- @socket = @ctx.connect(:PUSH, "tcp://127.0.0.1:5556")
11
+ def initialize(options = {})
12
+ @ctx = options[:ctx] || ZMQ::Context.new
13
+ @server_address = options[:server_address] || "tcp://127.0.0.1:5556"
9
14
  @pending = {}
10
15
  end
11
16
 
@@ -17,24 +22,34 @@ module Profit
17
22
  # TODO: wrap in a Mutex & make the key a combo of metric_type,
18
23
  # pid, and/or thread object_id to make this thread safe and
19
24
  # thread-robust.
20
- @pending[metric_type] = { start_file: start_file,
21
- start_line: start_line,
22
- start_time: now }
25
+ @pending[key_for(metric_type)] = { start_file: start_file,
26
+ start_line: start_line,
27
+ start_time: now }
23
28
  end
24
29
 
25
30
  def stop(metric_type)
26
31
  now = Time.now
27
- metric = @pending.delete(metric_type)
32
+ metric = @pending.delete key_for(metric_type)
28
33
  recorded_time = Time.now - metric[:start_time]
29
34
  stop_file = caller[0][/(.+):(.+):/,1]
30
35
  stop_line = caller[0][/(.+):(.+):/,2].to_i - 1
31
36
 
32
- @socket.send({ metric_type: metric_type,
33
- recorded_time: recorded_time,
34
- start_file: metric[:start_file],
35
- start_line: metric[:start_line],
36
- stop_file: stop_file,
37
- stop_line: stop_line }.to_json)
37
+ socket.send({ metric_type: metric_type,
38
+ recorded_time: recorded_time,
39
+ start_file: metric[:start_file],
40
+ start_line: metric[:start_line],
41
+ stop_file: stop_file,
42
+ stop_line: stop_line }.to_json)
43
+ end
44
+
45
+ def socket
46
+ Thread.current[:socket] ||= @ctx.connect(:PUSH, @server_address)
47
+ end
48
+
49
+ private
50
+
51
+ def key_for(metric_type)
52
+ "#{metric_type}:#{Thread.current.object_id}"
38
53
  end
39
54
  end
40
55
  end
@@ -1,3 +1,3 @@
1
1
  module Profit
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Profit::Client do
4
4
 
5
5
  let!(:redis) { Redis.new(host: "127.0.0.1", port: 6379) }
6
- let!(:client) { Profit::Client.new(server.ctx) }
6
+ let!(:client) { Profit::Client.new(ctx: server.ctx) }
7
7
  let!(:server) { TestServer.server }
8
8
  let!(:server_thread) { TestServer.server_thread }
9
9
 
@@ -60,7 +60,8 @@ describe Profit::Client do
60
60
  client.start("some_foo_measurement")
61
61
  now = Time.now
62
62
 
63
- pending_metric = client.pending["some_foo_measurement"]
63
+ key = client.pending.keys.grep(/some_foo_measurement/).first
64
+ pending_metric = client.pending[key]
64
65
  expect(pending_metric[:start_time].to_i).to be_within(1).of(now.to_i)
65
66
  end
66
67
 
@@ -71,7 +72,8 @@ describe Profit::Client do
71
72
  start_line = __LINE__
72
73
  start_file = __FILE__
73
74
 
74
- pending_metric = client.pending["some_foo_measurement"]
75
+ key = client.pending.keys.grep(/some_foo_measurement/).first
76
+ pending_metric = client.pending[key]
75
77
  expect(pending_metric[:start_file]).to eq start_file
76
78
  expect(pending_metric[:start_line]).to eq start_line
77
79
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: profit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
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: 2013-06-11 00:00:00.000000000 Z
12
+ date: 2013-06-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: eventmachine
@@ -155,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
155
155
  version: '0'
156
156
  segments:
157
157
  - 0
158
- hash: -957264636356464606
158
+ hash: 1282582636223262720
159
159
  required_rubygems_version: !ruby/object:Gem::Requirement
160
160
  none: false
161
161
  requirements:
@@ -164,7 +164,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
164
  version: '0'
165
165
  segments:
166
166
  - 0
167
- hash: -957264636356464606
167
+ hash: 1282582636223262720
168
168
  requirements: []
169
169
  rubyforge_project:
170
170
  rubygems_version: 1.8.23