# 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 parse(:transactions) @availability = Float parse(:availability) @elapsed_time = Float parse(:elapsed_time) @data_transferred = Float parse(:data_transferred) @response_time = Float parse(:response_time) @transaction_rate = Float parse(:transaction_rate) @throughput = Float parse(:throughput) @concurrency = Float parse(:concurrency) @failed_requests = Integer parse(:failed_requests) @longest_request = Float parse(:longest_request) @shortest_request = Float parse(:shortest_request) @results = results rescue ArgumentError => e (e.message.include?('invalid value')) ? dump_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 def parse(const) dump_results unless match = Results.const_get(const).match(results) match[1] end def dump_results abort "A Siege error occurred:\n#@results" end end end