Permalink
Browse files

thread safe stats

  • Loading branch information...
1 parent 645ff57 commit ed7b9b5173260ced15a6d16c465f3ed9d8f91f88 Nick Kallen committed Apr 17, 2009
Showing with 18 additions and 11 deletions.
  1. +18 −11 util/statosaurus.rb
View
@@ -1,40 +1,47 @@
class Statosaurus
- attr_accessor :transaction_id
-
def initialize(fields, logger)
@fields = fields.sort
- @values = {}
+ values = {}
@logger = logger
@logger.info("# Fields: " + fields.join(" "))
end
+
+ def values
+ Thread.current[:values] ||= {}
+ end
+
+ def transaction_id
+ Thread.current[:transaction_id]
+ end
def measure(field, &block)
result = nil
measurement = Benchmark.measure do
result = yield
end
- @values["#{field}_real"] = min(measurement.real)
- @values["#{field}_sys"] = min(measurement.stime)
- @values["#{field}_user"] = min(measurement.utime)
+ values["#{field}_real"] = min(measurement.real)
+ values["#{field}_sys"] = min(measurement.stime)
+ values["#{field}_user"] = min(measurement.utime)
result
end
def set(key, value)
- @values[key] = value
+ values[key] = value
end
def transaction
- @transaction_id = "#{Process.pid}-#{Time.now.to_i}-#{rand(9999)}"
+ Thread.current[:transaction_id] = "#{Process.pid}-#{Time.now.to_i}-#{rand(9999)}"
result = yield
print
- @values, @transaction_id = {}, nil
+ values.clear
+ Thread.current[:transaction_id]= nil
result
end
private
def print
- prefix = [Time.now.iso8601, @transaction_id].join(" ")
- info = @fields.collect { |field| format(@values[field]) }.join(" ")
+ prefix = [Time.now.iso8601, transaction_id].join(" ")
+ info = @fields.collect { |field| format(values[field]) }.join(" ")
@logger.info(prefix + " " + info)
end

0 comments on commit ed7b9b5

Please sign in to comment.