# coding: utf-8 class Siege class Results class << self def for(results) new results end end TRANSACTIONS = /Transactions:\s*([\d\.]*) hits/ AVAILABILITY = /Availability:\s*([\d\.]*) %/ DATA_TRANSFERRED = /Data transferred:\s*([\d\.]*) MB/ ELAPSED_TIME = /Elapsed time:\s*([\d\.]*) secs/ RESPONSE_TIME = /Response time:\s*([\d\.]*) secs/ TRANSACTION_RATE = /Transaction rate:\s*([\d\.]*) trans\/sec/ THROUGHPUT = /Throughput:\s*([\d\.]*) MB\/sec/ CONCURRENCY = /Concurrency:\s*([\d\.]*)/ FAILED_REQUESTS = /Failed transactions:\s*([\d\.]*)/ LONGEST_REQUEST = /Longest transaction:\s*([\d\.]*)/ SHORTEST_REQUEST = /Shortest transaction:\s*([\d\.]*)/ attr_reader :transactions, :availability, :elapsed_time, :data_transferred, :response_time, :transaction_rate, :throughput, :concurrency, :failed_requests, :longest_request, :shortest_request alias_method :failed, :failed_requests alias_method :shortest, :shortest_request alias_method :longest, :longest_request alias_method :response, :response_time def initialize(results) @transactions = Integer TRANSACTIONS.match(results)[1] @availability = Float AVAILABILITY.match(results)[1] @elapsed_time = Float ELAPSED_TIME.match(results)[1] @data_transferred = Float DATA_TRANSFERRED.match(results)[1] @response_time = Float RESPONSE_TIME.match(results)[1] @transaction_rate = Float TRANSACTION_RATE.match(results)[1] @throughput = Float THROUGHPUT.match(results)[1] @concurrency = Float CONCURRENCY.match(results)[1] @failed_requests = Integer FAILED_REQUESTS.match(results)[1] @longest_request = Float LONGEST_REQUEST.match(results)[1] @shortest_request = Float SHORTEST_REQUEST.match(results)[1] @results = results rescue ArgumentError => e (e.message.include?('invalid value')) ? abort("A Siege error cccurred:\n#{results}") : raise(e) end def[](key) to_h.fetch(key) end def to_s @results end def to_h { transactions: @transactions, availability: @availability, elapsed_time: @elapsed_time, data_transferred: @data_transferred, response_time: @response_time, transaction_rate: @transaction_rate, throughput: @throughput, concurrency: @concurrency, failed_requests: @failed_requests, longest_request: @longest_request, shortest_request: @shortest_request } end end end