Class: Lavin::Statistics
- Inherits:
-
Object
- Object
- Lavin::Statistics
- Defined in:
- lib/lavin/statistics.rb
Class Method Summary collapse
- .duration ⇒ Object
-
.meassure ⇒ Object
FIXME: make thread safe.
- .register_request(method:, url:, status:, duration:) ⇒ Object
- .register_step(user:, step_name:, failure: nil) ⇒ Object
- .reset ⇒ Object
- .show ⇒ Object
- .stats ⇒ Object
- .stop ⇒ Object
- .total_requests ⇒ Object
- .total_steps ⇒ Object
Class Method Details
.duration ⇒ Object
35 36 37 38 39 |
# File 'lib/lavin/statistics.rb', line 35 def duration return data[:duration] if data[:duration] Time.now - data[:start] if data[:start] end |
.meassure ⇒ Object
FIXME: make thread safe
10 11 12 13 14 |
# File 'lib/lavin/statistics.rb', line 10 def meassure reset data[:start] = Time.now yield.tap { data[:duration] = Time.now - data[:start] } end |
.register_request(method:, url:, status:, duration:) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/lavin/statistics.rb', line 41 def register_request(method:, url:, status:, duration:) data[:total_requests] += 1 key = [method, url] result = {status:, duration:} result[:failure] = true if status > 499 data[:requests][key] << result end |
.register_step(user:, step_name:, failure: nil) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/lavin/statistics.rb', line 49 def register_step(user:, step_name:, failure: nil) data[:step_summary][:count] += 1 data[:step_summary][:success] += 1 unless failure data[:step_summary][:failure] += 1 if failure key = [user, step_name] data[:steps][key][:success] += 1 unless failure data[:steps][key][:failure] += 1 if failure data[:failures][failure.to_s] += 1 if failure end |
.reset ⇒ Object
16 17 18 |
# File 'lib/lavin/statistics.rb', line 16 def reset self.data = new_data end |
.show ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/lavin/statistics.rb', line 102 def show values = stats show_summary(values) show_steps(values) do |(user, step), hash| format( "%-48<user_step>s %8<success>d %8<failure>d", user_step: "#{user}.#{step}", **hash ) end show_table(values) do |request_values| format( "%-6<method>s %-100<url>s %6<requests>d %12<avg_duration>fs %12<min_duration>fs %12<max_duration>fs", **request_values ) end show_failures(values) do |, count| format("%-64<message>s %6<count>d", message:, count:) end end |
.stats ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/lavin/statistics.rb', line 59 def stats reset unless data time = Time.now requests = data[:requests].map do |(method, url), requests| durations = [] statuses = [] failed_requests = 0 requests.each do |request| durations << request[:duration] statuses << request[:status] failed_requests += 1 if request[:failure] end min_duration = durations.min max_duration = durations.max avg_duration = durations.empty? ? 0 : (durations.sum / durations.size) { method: method.to_s.upcase, url: url, requests: requests.size, statuses: statuses.tally, failed_requests:, avg_duration:, min_duration:, max_duration: } end Stats.new( duration: duration, total_requests: total_requests, rate: duration ? format("%.2f", total_requests / duration) : 0, step_summary: data[:step_summary], steps: data[:steps], requests: requests, failures: data[:failures] ).tap do |stats| # FIXME remove! puts "Calculated stats in #{Time.now - time}s" end end |
.stop ⇒ Object
20 21 22 23 24 25 |
# File 'lib/lavin/statistics.rb', line 20 def stop return if data.frozen? data[:duration] = (Time.now - data[:start]) data.freeze end |
.total_requests ⇒ Object
27 28 29 |
# File 'lib/lavin/statistics.rb', line 27 def total_requests data[:total_requests] end |
.total_steps ⇒ Object
31 32 33 |
# File 'lib/lavin/statistics.rb', line 31 def total_steps data[:step_summary][:count] end |