Class: SDBTools::Transaction
- Inherits:
-
Object
- Object
- SDBTools::Transaction
- Defined in:
- lib/sdbtools/transaction.rb
Overview
SimpleDB is not remotely transactional. A Transaction in this context is just a way to group together a series of SimpleDB requests for the purpose of benchmarking.
Instance Attribute Summary collapse
-
#box_usage ⇒ Object
readonly
Returns the value of attribute box_usage.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#item_count ⇒ Object
readonly
Returns the value of attribute item_count.
-
#nesting_level ⇒ Object
readonly
Returns the value of attribute nesting_level.
-
#on_close ⇒ Object
readonly
Returns the value of attribute on_close.
-
#request_count ⇒ Object
readonly
Returns the value of attribute request_count.
-
#times ⇒ Object
readonly
Returns the value of attribute times.
Class Method Summary collapse
- .add_stats(box_usage, request_count, item_count, times) ⇒ Object
- .current ⇒ Object
-
.log_transaction_close(logger, cutoff_level = :none) ⇒ Object
Usage: Transaction.on_close = Transaction.log_transaction_close(logger).
-
.on_close ⇒ Object
Get the default on_close action.
-
.on_close=(action) ⇒ Object
Set the default on_close action.
- .open(description, on_close = self.on_close) ⇒ Object
- .transaction_stack ⇒ Object
Instance Method Summary collapse
- #add_stats(box_usage, request_count, item_count, times = Benchmark::Tms.new) ⇒ Object
- #add_stats_from_aws_response(response) ⇒ Object
- #close ⇒ Object
-
#initialize(description, on_close_action = self.class.on_close) ⇒ Transaction
constructor
A new instance of Transaction.
- #measure_aws_call ⇒ Object
-
#time ⇒ Object
Benchmark the block and add its times to the transaction.
Constructor Details
#initialize(description, on_close_action = self.class.on_close) ⇒ Transaction
Returns a new instance of Transaction.
73 74 75 76 77 78 79 80 81 |
# File 'lib/sdbtools/transaction.rb', line 73 def initialize(description, on_close_action=self.class.on_close) @description = description @box_usage = 0.0 @request_count = 0 @item_count = 0 @on_close = on_close_action @times = Benchmark::Tms.new @nesting_level = self.class.transaction_stack.size end |
Instance Attribute Details
#box_usage ⇒ Object (readonly)
Returns the value of attribute box_usage.
66 67 68 |
# File 'lib/sdbtools/transaction.rb', line 66 def box_usage @box_usage end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
65 66 67 |
# File 'lib/sdbtools/transaction.rb', line 65 def description @description end |
#item_count ⇒ Object (readonly)
Returns the value of attribute item_count.
68 69 70 |
# File 'lib/sdbtools/transaction.rb', line 68 def item_count @item_count end |
#nesting_level ⇒ Object (readonly)
Returns the value of attribute nesting_level.
71 72 73 |
# File 'lib/sdbtools/transaction.rb', line 71 def nesting_level @nesting_level end |
#on_close ⇒ Object (readonly)
Returns the value of attribute on_close.
70 71 72 |
# File 'lib/sdbtools/transaction.rb', line 70 def on_close @on_close end |
#request_count ⇒ Object (readonly)
Returns the value of attribute request_count.
67 68 69 |
# File 'lib/sdbtools/transaction.rb', line 67 def request_count @request_count end |
#times ⇒ Object (readonly)
Returns the value of attribute times.
69 70 71 |
# File 'lib/sdbtools/transaction.rb', line 69 def times @times end |
Class Method Details
.add_stats(box_usage, request_count, item_count, times) ⇒ Object
53 54 55 |
# File 'lib/sdbtools/transaction.rb', line 53 def self.add_stats(box_usage, request_count, item_count, times) current and current.add_stats(box_usage, request_count, item_count, times) end |
.current ⇒ Object
57 58 59 |
# File 'lib/sdbtools/transaction.rb', line 57 def self.current transaction_stack.last end |
.log_transaction_close(logger, cutoff_level = :none) ⇒ Object
Usage:
Transaction.on_close = Transaction.log_transaction_close(logger)
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/sdbtools/transaction.rb', line 36 def self.log_transaction_close(logger, cutoff_level=:none) pattern = "%s \"%s\" (User %0.6u; System %0.6y; CPU %0.6t; Clock %0.6r; Box %0.6f; Reqs %d; Items %d)" lambda do |t| if cutoff_level == :none || cutoff_level <= t.nesting_level prefix = "*" * (t.nesting_level + 1) logger.info( t.times.format( pattern, prefix, t.description, t.box_usage, t.request_count, t.item_count)) end end end |
.on_close ⇒ Object
Get the default on_close action
26 27 28 29 30 31 32 |
# File 'lib/sdbtools/transaction.rb', line 26 def self.on_close if current current.on_close else @on_close_action ||= lambda{|t|} end end |
.on_close=(action) ⇒ Object
Set the default on_close action. An on_close action receives a Transaction object which is being closed
21 22 23 |
# File 'lib/sdbtools/transaction.rb', line 21 def self.on_close=(action) @on_close_action = action end |
.open(description, on_close = self.on_close) ⇒ Object
10 11 12 13 14 15 16 17 |
# File 'lib/sdbtools/transaction.rb', line 10 def self.open(description, on_close=self.on_close) transaction = self.new(description, on_close) transaction_stack.push(transaction) yield(transaction) ensure transaction = transaction_stack.pop transaction.close end |
.transaction_stack ⇒ Object
61 62 63 |
# File 'lib/sdbtools/transaction.rb', line 61 def self.transaction_stack Thread.current[:sdbtools_transaction_stack] ||= [] end |
Instance Method Details
#add_stats(box_usage, request_count, item_count, times = Benchmark::Tms.new) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/sdbtools/transaction.rb', line 89 def add_stats(box_usage, request_count, item_count, times=Benchmark::Tms.new) @request_count += request_count.to_i @box_usage += box_usage.to_f @item_count += item_count @times += times end |
#add_stats_from_aws_response(response) ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/sdbtools/transaction.rb', line 96 def add_stats_from_aws_response(response) response = response.nil? ? {} : response box_usage = response[:box_usage].to_f item_count = Array(response[:items]).size + Array(response[:domains]).size item_count += 1 if response.key?(:attributes) add_stats(box_usage, 1, item_count) response end |
#close ⇒ Object
83 84 85 86 87 |
# File 'lib/sdbtools/transaction.rb', line 83 def close self.class.add_stats(@box_usage, @request_count, @item_count, @times) @on_close.call(self) self end |
#measure_aws_call ⇒ Object
105 106 107 |
# File 'lib/sdbtools/transaction.rb', line 105 def measure_aws_call add_stats_from_aws_response(time{yield}) end |
#time ⇒ Object
Benchmark the block and add its times to the transaction
110 111 112 113 114 115 116 |
# File 'lib/sdbtools/transaction.rb', line 110 def time result = nil self.times.add! do result = yield end result end |