#! /usr/bin/ruby
=begin
--------------------------------------------------------------------------------
Parse a file of JMeter test results (*./jtl), summarize the times for each test,
and make the summaries easily available.
--------------------------------------------------------------------------------
=end
# ------------------------------------------------------------------------------------
# TestResultMarshaller class
# ------------------------------------------------------------------------------------
class TestResultMarshaller
def marshall()
File.open(@output_filename, 'w') do | out |
write_html_header(out)
write_table_header(out)
write_table_lines(out)
write_table_footer(out)
write_html_footer(out)
end
end
def write_html_header(out)
out.puts <<"EOF"
Performance tests for #{@site_name}
EOF
end
def write_table_header(out)
top_cells = ['
 
']
@test_results.each do | test |
top_cells.push("
']
@test_results.each do | test |
bottom_cells.push('
Iterations
')
bottom_cells.push('
time (min/max)
')
bottom_cells.push("
compare
")
end
out.puts <<"EOF"
#{top_cells.join("\n ")}
#{bottom_cells.join("\n ")}
EOF
end
def write_table_lines(out)
all_test_names().each do | test_name |
out.puts <<"EOF"
#{test_name}
#{format_test_results(test_name)}
EOF
end
end
def all_test_names
names = []
@test_results.each do | test |
names.concat(test.summaries.keys)
end
names.uniq.sort
end
def format_test_results(test_name)
results = []
@test_results.each do | test |
results.push(format_test_result(test_name, test))
end
results.join("\n ")
end
def format_test_result(test_name, test)
s = test.summaries[test_name]
if s
<<"EOF"
#{s.how_many}
#{format_millis(s.avg_time)}
#{format_millis(s.min_time)}
#{format_millis(s.max_time)}
#{performance_ratio(test_name, s.avg_time)}
EOF
else
<<"EOF"
EOF
end
end
def format_millis(millis)
"%.3f" % [millis.to_f / 1000]
end
def performance_ratio(test_name, time)
return " " if @test_results.empty?
return " " unless @test_results[0].summaries.key?(test_name)
s = @test_results[0].summaries[test_name]
reference = s.avg_time
return " " if reference == 0
return "#{"%.0f" % [time * 100 / reference]}%"
end
def write_table_footer(out)
out.puts "
"
end
def write_html_footer(out)
out.puts <<"EOF"
EOF
end
def initialize(target_directory, site_name, test_results)
@target_directory = target_directory
@site_name = site_name
@test_results = test_results
filename = "#{site_name}-merged_#{Time.now.strftime('%Y-%m-%d_%H-%M-%S')}"
@output_filename = File.expand_path(filename, @target_directory)
end
end