simple_performer 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/simple_performer.rb +180 -158
  2. metadata +4 -16
@@ -12,160 +12,182 @@ require_relative 'sp_rack'
12
12
  #for Now data array would simply be a queue
13
13
  module SimplePerformer
14
14
 
15
- @@metrics_path = '/api/metrics'
16
- @@base_url = 'http://incoming.simpleperformr.com' + @@metrics_path
15
+ @@metrics_path = '/api/metrics'
16
+ @@base_url = 'http://incoming.simpleperformr.com' + @@metrics_path
17
17
 
18
- class << self
19
- attr_accessor :config,
20
- :service
18
+ class << self
19
+ attr_accessor :config,
20
+ :service
21
21
 
22
- def configure()
23
- SimplePerformer.config ||= SimplePerformer::Config.new
24
- yield(config)
22
+ def configure()
23
+ SimplePerformer.config ||= SimplePerformer::Config.new
24
+ yield(config)
25
25
  # SimplePerformer.service = Performr.new(config.access_key, config.secret_key, :config=>config)
26
- # todo: should delay start until used?
27
- Performr.start
28
- end
26
+ # todo: should delay start until used?
27
+ Performr.start
29
28
  end
29
+ end
30
30
 
31
- # name is what this chunk of code will be referred to in the UI.
32
- def self.benchmark(name, &block)
33
- Performr.benchmark(name, &block)
34
- end
31
+ # name is what this chunk of code will be referred to in the UI.
32
+ def self.benchmark(name, &block)
33
+ Performr.benchmark(name, &block)
34
+ end
35
35
 
36
- def self.shutdown
36
+ def self.shutdown
37
37
  # EventMachine.stop
38
+ end
39
+
40
+ def self.base_url=(url)
41
+ @@base_url = url + @@metrics_path
42
+ end
43
+
44
+ def self.base_url
45
+ @@base_url
46
+ end
47
+
48
+ # Simple function that simply spits out the duration of the block
49
+ # - name is for reference.
50
+ def self.puts_duration(name, &block)
51
+ start_time = Time.now
52
+ yield
53
+ end_time = Time.now
54
+ duration = end_time-start_time
55
+ puts "#{name} duration: #{duration} seconds."
56
+ duration
57
+ end
58
+
59
+ class Aggregator
60
+ def initialize
61
+ @aggs = {}
38
62
  end
39
63
 
40
- def self.base_url=(url)
41
- @@base_url = url + @@metrics_path
42
- end
43
64
 
44
- def self.base_url
45
- @@base_url
46
- end
65
+ def time(name, &block)
66
+ start_time = Time.now
67
+ yield
68
+ end_time = Time.now
69
+ agg = @aggs[name]
70
+ agg = Agger.new(name) if agg.nil?
71
+ agg.add(end_time-start_time)
72
+ @aggs[name] = agg
47
73
 
48
- # Simple function that simply spits out the duration of the block
49
- # - name is for reference.
50
- def self.puts_duration(name, &block)
51
- start_time = Time.now
52
- yield
53
- end_time = Time.now
54
- puts "#{name} duration: #{(end_time-start_time)} seconds."
55
74
  end
56
75
 
57
- class Aggregator
58
- def initialize
59
- @aggs = {}
60
- end
76
+ def to_s
77
+ s = ''
78
+ @aggs.each_pair do |k, v|
79
+ s << v.to_s + "\n"
80
+ end
81
+ s
82
+ end
61
83
 
62
- def benchmark(name, &block)
63
- start_time = Time.now
64
- yield
65
- end_time = Time.now
66
- agg = @aggs[name]
67
- agg = Agger.new(name) if agg.nil?
68
- agg.add(end_time-start_time)
69
- @aggs[name] = agg
84
+ # Prints current info
85
+ # def puts(name)
86
+ # agg = @aggs[name]
87
+ # agg.puts
88
+ # end
89
+ end
70
90
 
71
- end
91
+ class Agger
92
+ attr_accessor :name, :sum, :count
72
93
 
73
- # Prints current info
74
- def puts(name)
75
- agg = @aggs[name]
76
- agg.puts
77
- end
94
+ def initialize(name)
95
+ @name = name
96
+ @sum = 0.0
97
+ @count = 0
78
98
  end
79
99
 
80
- class Agger
81
- attr_accessor :name, :sum, :count
82
-
83
- def initialize(name)
84
- @name = name
85
- @sum = 0.0
86
- @count = 0
87
- end
100
+ def add(duration)
101
+ @sum += duration
102
+ @count += 1
103
+ end
88
104
 
89
- def add(duration)
90
- @sum += duration
91
- @count += 1
92
- end
105
+ def avg
106
+ if @count && @count > 0
107
+ return 1.0 * @sum / @count
108
+ end
109
+ return 0.0
110
+ end
93
111
 
94
- def puts
95
- Kernel.puts "Aggregator #{name}: count=#{count} avg=#{avg}"
96
- end
112
+ def to_s
113
+ "Aggregator #{name}: count=#{count} avg=#{avg}"
97
114
  end
98
115
 
99
- class Performr
100
- #< ApiAuth
116
+ # def puts
117
+ # Kernel.puts "Aggregator #{name}: count=#{count} avg=#{avg}"
118
+ # end
119
+ end
101
120
 
102
- class <<self
121
+ class Performr
122
+ #< ApiAuth
103
123
 
104
- attr_accessor :data, :api_key, :base_uri, :timer
124
+ class <<self
105
125
 
106
- def config options={}, &blk
107
- SimplePerformer.configure do |config|
108
- config.access_key = options[:access_key]
109
- config.host = options[:host]
110
- end
126
+ attr_accessor :data, :api_key, :base_uri, :timer
111
127
 
112
- instance_eval &blk if block_given?
113
- end
128
+ def config options={}, &blk
129
+ SimplePerformer.configure do |config|
130
+ config.access_key = options[:access_key]
131
+ config.host = options[:host]
132
+ end
133
+
134
+ instance_eval &blk if block_given?
135
+ end
114
136
 
115
- def start
116
- self.data = Queue.new
137
+ def start
138
+ self.data = Queue.new
117
139
 
118
- puts "api_key=" + api_key.to_s
119
- if api_key
120
- # only start it if we passed in a key
121
- run_update
122
- end
140
+ puts "api_key=" + api_key.to_s
141
+ if api_key
142
+ # only start it if we passed in a key
143
+ run_update
144
+ end
123
145
 
124
- end
146
+ end
125
147
 
126
- def reset_queue
127
- self.data = Queue.new
128
- end
148
+ def reset_queue
149
+ self.data = Queue.new
150
+ end
129
151
 
130
- def send_update
152
+ def send_update
131
153
  # puts "send_update api_key=" + api_key
132
- url = "/update_metrics/"+api_key
133
- #consumer for data
134
- #delete all data from queue after update
135
- to_send = return_data
154
+ url = "/update_metrics/"+api_key
155
+ #consumer for data
156
+ #delete all data from queue after update
157
+ to_send = return_data
136
158
  # puts "sending json=" + to_send.inspect
137
159
 
138
- to_send = to_send.to_json
160
+ to_send = to_send.to_json
139
161
  # puts 'posting to ' + full_url(url)
140
- response = RestClient.post(full_url(url), to_send, :content_type => :json)
141
- end
142
-
143
- def return_data
144
- avg_metrics = {}
145
- i=0
146
- data = self.data
147
- reset_queue
148
- # create a new one so the current queue doesn't have the opportunity to keep filling up and we try to keep popping too
149
- until data.empty?
150
- metric=data.pop
151
- name=metric[:name]
152
- avg_metrics[name] ||= {:count => 0, :user => 0, :system => 0, :total => 0, :real => 0}
153
- avg_metrics[name][:count] += 1
154
- avg_metrics[name][:user] += metric[:user]
155
- avg_metrics[name][:system] += metric[:system]
156
- avg_metrics[name][:total] += metric[:total]
157
- avg_metrics[name][:real] += metric[:real]
158
- end
159
- # puts hash.inspect + " hash inspect"
160
- avg_metrics
161
- end
162
-
163
-
164
- def full_url(path)
165
- SimplePerformer.base_url + path
166
- end
167
-
168
- def periodic_update
162
+ response = RestClient.post(full_url(url), to_send, :content_type => :json)
163
+ end
164
+
165
+ def return_data
166
+ avg_metrics = {}
167
+ i=0
168
+ data = self.data
169
+ reset_queue
170
+ # create a new one so the current queue doesn't have the opportunity to keep filling up and we try to keep popping too
171
+ until data.empty?
172
+ metric=data.pop
173
+ name=metric[:name]
174
+ avg_metrics[name] ||= {:count => 0, :user => 0, :system => 0, :total => 0, :real => 0}
175
+ avg_metrics[name][:count] += 1
176
+ avg_metrics[name][:user] += metric[:user]
177
+ avg_metrics[name][:system] += metric[:system]
178
+ avg_metrics[name][:total] += metric[:total]
179
+ avg_metrics[name][:real] += metric[:real]
180
+ end
181
+ # puts hash.inspect + " hash inspect"
182
+ avg_metrics
183
+ end
184
+
185
+
186
+ def full_url(path)
187
+ SimplePerformer.base_url + path
188
+ end
189
+
190
+ def periodic_update
169
191
 
170
192
  # EventMachine.run do
171
193
  # @timer = EventMachine::PeriodicTimer.new(60) do
@@ -180,50 +202,50 @@ module SimplePerformer
180
202
  #
181
203
  # end
182
204
  # end
183
- end
184
-
185
- def cancel_update
186
- timer.cancel if timer
187
- end
188
-
189
-
190
- def run_update
191
- Thread.new do
192
- periodic_update
193
- end
194
- end
195
-
196
- def benchmark name, &block
197
- opts = name
198
- stat=Benchmark::measure &block
199
- puts 'name2=' + name.inspect
200
- if opts.is_a? Hash
201
- name = opts[:name]
202
- end
203
- unless name && name.length > 0
204
- raise "Must provide a name for benchmark."
205
- end
206
- puts 'name =' + name
207
- pp stat.to_hash, stat.class
208
- collect_stats stat.to_hash.merge(:name => name)
209
- end
210
-
211
- def collect_stats stat
212
- self.data.push(stat)
213
- end
205
+ end
214
206
 
207
+ def cancel_update
208
+ timer.cancel if timer
209
+ end
210
+
211
+
212
+ def run_update
213
+ Thread.new do
214
+ periodic_update
215
+ end
216
+ end
217
+
218
+ def benchmark name, &block
219
+ opts = name
220
+ stat=Benchmark::measure &block
221
+ puts 'name2=' + name.inspect
222
+ if opts.is_a? Hash
223
+ name = opts[:name]
224
+ end
225
+ unless name && name.length > 0
226
+ raise "Must provide a name for benchmark."
215
227
  end
228
+ puts 'name =' + name
229
+ pp stat.to_hash, stat.class
230
+ collect_stats stat.to_hash.merge(:name => name)
231
+ end
232
+
233
+ def collect_stats stat
234
+ self.data.push(stat)
235
+ end
236
+
216
237
  end
238
+ end
217
239
  end
218
240
 
219
241
  class Benchmark::Tms
220
- def to_hash
221
- {
222
- :user => @utime,
223
- :real => @real,
224
- :total => @total,
225
- :system =>@stime
226
- }
227
- end
242
+ def to_hash
243
+ {
244
+ :user => @utime,
245
+ :real => @real,
246
+ :total => @total,
247
+ :system =>@stime
248
+ }
249
+ end
228
250
  end
229
251
 
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_performer
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 11
9
- version: 0.0.11
4
+ prerelease:
5
+ version: 0.0.12
10
6
  platform: ruby
11
7
  authors:
12
8
  - Travis Reeder
@@ -14,8 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-01-02 00:00:00 -08:00
18
- default_executable:
13
+ date: 2011-05-02 00:00:00 Z
19
14
  dependencies:
20
15
  - !ruby/object:Gem::Dependency
21
16
  name: rest-client
@@ -25,8 +20,6 @@ dependencies:
25
20
  requirements:
26
21
  - - ">="
27
22
  - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
23
  version: "0"
31
24
  type: :runtime
32
25
  version_requirements: *id001
@@ -47,7 +40,6 @@ files:
47
40
  - lib/simple_performr_rufus.rb
48
41
  - lib/sp_rack.rb
49
42
  - README.markdown
50
- has_rdoc: true
51
43
  homepage: http://www.appoxy.com
52
44
  licenses: []
53
45
 
@@ -61,21 +53,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
61
53
  requirements:
62
54
  - - ">="
63
55
  - !ruby/object:Gem::Version
64
- segments:
65
- - 0
66
56
  version: "0"
67
57
  required_rubygems_version: !ruby/object:Gem::Requirement
68
58
  none: false
69
59
  requirements:
70
60
  - - ">="
71
61
  - !ruby/object:Gem::Version
72
- segments:
73
- - 0
74
62
  version: "0"
75
63
  requirements: []
76
64
 
77
65
  rubyforge_project:
78
- rubygems_version: 1.3.7
66
+ rubygems_version: 1.7.2
79
67
  signing_key:
80
68
  specification_version: 3
81
69
  summary: Appoxy SimplePerformer Client Gem