Class: SimplePerformer::Performr

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



126
127
128
# File 'lib/simple_performer.rb', line 126

def api_key
  @api_key
end

.base_uriObject

Returns the value of attribute base_uri.



126
127
128
# File 'lib/simple_performer.rb', line 126

def base_uri
  @base_uri
end

.dataObject

Returns the value of attribute data.



126
127
128
# File 'lib/simple_performer.rb', line 126

def data
  @data
end

.timerObject

Returns the value of attribute timer.



126
127
128
# File 'lib/simple_performer.rb', line 126

def timer
  @timer
end

Class Method Details

.benchmark(name, &block) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/simple_performer.rb', line 218

def benchmark name, &block
  opts = name
  stat=Benchmark::measure &block
  puts 'name2=' + name.inspect
  if opts.is_a? Hash
    name = opts[:name]
  end
  unless name && name.length > 0
    raise "Must provide a name for benchmark."
  end
  puts 'name =' + name
  pp stat.to_hash, stat.class
  collect_stats stat.to_hash.merge(:name => name)
end

.cancel_updateObject



207
208
209
# File 'lib/simple_performer.rb', line 207

def cancel_update
  timer.cancel if timer
end

.collect_stats(stat) ⇒ Object



233
234
235
# File 'lib/simple_performer.rb', line 233

def collect_stats stat
  self.data.push(stat)
end

.config(options = {}, &blk) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/simple_performer.rb', line 128

def config options={}, &blk
  SimplePerformer.configure do |config|
    config.access_key = options[:access_key]
    config.host = options[:host]
  end

  instance_eval &blk if block_given?
end

.full_url(path) ⇒ Object



186
187
188
# File 'lib/simple_performer.rb', line 186

def full_url(path)
  SimplePerformer.base_url + path
end

.periodic_updateObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/simple_performer.rb', line 190

def periodic_update

#                EventMachine.run do
#                    @timer = EventMachine::PeriodicTimer.new(60) do
##                        puts "the time is #{Time.now}"
#                        begin
#                            send_update
#                        rescue => ex
#                            puts 'Failed to send data to SimplePerformr!'
#                            puts ex.message
#                            puts ex.backtrace
#                        end
#
#                    end
#                end
end

.reset_queueObject



148
149
150
# File 'lib/simple_performer.rb', line 148

def reset_queue
  self.data = Queue.new
end

.return_dataObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/simple_performer.rb', line 165

def return_data
  avg_metrics = {}
  i=0
  data = self.data
  reset_queue
  # create a new one so the current queue doesn't have the opportunity to keep filling up and we try to keep popping too
  until data.empty?
    metric=data.pop
    name=metric[:name]
    avg_metrics[name] ||= {:count => 0, :user => 0, :system => 0, :total => 0, :real => 0}
    avg_metrics[name][:count] += 1
    avg_metrics[name][:user] += metric[:user]
    avg_metrics[name][:system] += metric[:system]
    avg_metrics[name][:total] += metric[:total]
    avg_metrics[name][:real] += metric[:real]
  end
  #            puts hash.inspect + " hash inspect"
  avg_metrics
end

.run_updateObject



212
213
214
215
216
# File 'lib/simple_performer.rb', line 212

def run_update
  Thread.new do
    periodic_update
  end
end

.send_updateObject



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/simple_performer.rb', line 152

def send_update
#                puts "send_update api_key=" + api_key
  url = "/update_metrics/"+api_key
  #consumer for data
  #delete all data from queue after update
  to_send = return_data
#                puts "sending json=" + to_send.inspect

  to_send = to_send.to_json
#                puts 'posting to ' + full_url(url)
  response = RestClient.post(full_url(url), to_send, :content_type => :json)
end

.startObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/simple_performer.rb', line 137

def start
  self.data = Queue.new

  puts "api_key=" + api_key.to_s
  if api_key
    # only start it if we passed in a key
    run_update
  end

end