class Ralphttp::Fixit
Public: Format data for a reasonable output
Attributes
bucket[RW]
csv[RW]
csvexport[RW]
detailed[RW]
error[RW]
req[RW]
status[RW]
total_time[RW]
Public Class Methods
new(options)
click to toggle source
Public: Start the class based on selected options
options - Hash containing settings
Returns nil
# File lib/ralphttp/fixit.rb, line 18 def initialize(options) @status = {} @error = [] @detailed = options[:detail] unless options[:csv].nil? @csv = options[:csv] assign_csv_report end end
Public Instance Methods
analyze()
click to toggle source
Public: Analyze collected data and show reasonable output
Return Text output
# File lib/ralphttp/fixit.rb, line 32 def analyze if @error.empty? display_results else puts 'Errors encountered when connecting:' @error.each do |e| puts e end end end
display_results()
click to toggle source
Public - Displays the results of the parsed data
Returns nil
# File lib/ralphttp/fixit.rb, line 46 def display_results print_header print_detailed_report write_csv_report display_status end
print_header()
click to toggle source
Public: Output the header information
Returns nil
# File lib/ralphttp/fixit.rb, line 56 def print_header unless @detailed.nil? puts sprintf('%-30s %-10s %-10s', 'Time', 'Req/s', 'Avg resp (ms)') end end
Private Instance Methods
assign_csv_report()
click to toggle source
Private - Assign Ralphttp::CsvExport
class if report is asked for
Returns nil
# File lib/ralphttp/fixit.rb, line 82 def assign_csv_report unless @csv.nil? csv_header = ['Time', 'Req/s', 'Avg. resp. (ms)'] @csvexport = Ralphttp::CsvExport.new(csv_header) end end
calc_response(resp)
click to toggle source
Private: Calculate average response time in ms
resp - Array
Returns Float of average ms
# File lib/ralphttp/fixit.rb, line 120 def calc_response(resp) r = [] resp.each do |re| r << re[1] end sprintf('%.2f', (r.inject(:+).to_f / r.length.to_f)) end
display_status()
click to toggle source
Private - Print the status banner
Returns nil
# File lib/ralphttp/fixit.rb, line 67 def display_status reqs_per_sec = sprintf('%.2f', (@req.inject(:+).to_f / @req.length.to_f)) puts "\nRequests per second: #{reqs_per_sec}" @status.map do |status_code, status_count| puts "HTTP Status #{status_code}: #{status_count}" end puts "Total time: #{@total_time} s" end
print_detailed_report()
click to toggle source
Private - Prints out a detailed report, if asked for
Returns Array containing summarized data
# File lib/ralphttp/fixit.rb, line 101 def print_detailed_report @req = [] @bucket.keys.sort.map do |time| @req << @bucket[time].length date = Time.at(time) ms = calc_response(@bucket[time]) @csvexport.add_row([date, @bucket[time].length, ms]) unless @csv.nil? puts sprintf('%-30s %-10s %-20s', date, @bucket[time].length, ms) unless @detailed.nil? end end
write_csv_report()
click to toggle source
Private - Write down the CSV report
Returns nil
# File lib/ralphttp/fixit.rb, line 92 def write_csv_report unless @csv.nil? @csvexport.write(@csv) end end