Skip to content

Instantly share code, notes, and snippets.

@MartinGross
Created April 30, 2010 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MartinGross/385502 to your computer and use it in GitHub Desktop.
Save MartinGross/385502 to your computer and use it in GitHub Desktop.
JMeter log file extractor for GnuPlot
class LogfileFilter
def initialize regex1, regex2, input_file_name, output_file_name
@read_file = File.new input_file_name, "r"
puts "Reading file #{input_file_name}"
@write_file = File.new output_file_name, "w"
puts "Writing to file #{output_file_name}"
@reg_ex_1 = Regexp.new regex1
@reg_ex_2 = Regexp.new regex2
@reg_ex_ts = /ts="(\d+)"/
@reg_ex_t = /t="(\d+)"/
puts "First condition for filtering: #{@reg_ex_1.inspect}"
puts "Second condition for filtering: #{@reg_ex_2.inspect}"
puts "---"
end
def parse_file
@write_file.puts "#Time ElapsedTime"
matches = 0
# ... process all lines in the file
@read_file.each_line do |line|
if (line.match @reg_ex_1) && (line.match @reg_ex_2)
matches+=1
# print line
# puts line
m = line.match(@reg_ex_ts)
epoch_ms = m[1]
epoch_s = epoch_ms[0...-3] # cut-off milliseconds
time = Time.at (epoch_s.to_i)
m = line.match(@reg_ex_t)
elapsed = m[1]
@write_file.puts "#{time.utc.strftime("%H:%M:%S")} #{elapsed}"
end
end
puts "---"
puts "#{@read_file.lineno} lines read"
puts "#{matches} matches found"
@read_file.close
end
end
# commandline
# get file name from command line
if ARGV.size < 4
puts "Error: Arguments missing"
puts "Usage: logfile_filter.rb regex filename"
puts "regex: Regular expression which is used for filtering lines"
puts "filename: Name of the file to be filtered"
exit(1)
end
write_file = ARGV.pop
file_name = ARGV.pop
regex2 = ARGV.pop
regex1 = ARGV.pop
analyzer = LogfileFilter.new regex1, regex2, file_name, write_file
analyzer.parse_file
@MartinGross
Copy link
Author

This file belongs to JMeter log file extractor for GnuPlot: http://www.datazoo.de/articles/158/performance-testing-analyzing-jmeter-results

Example reading the log file called jmeter_results.log , writing to file the_start.dat , using two regular expressions lb=\"the start\" and s=\"true\" as conditions to extract elapsed time and timestamp from the line of the logfile:
logfile_filter.rb "lb="the start"" s="true" jmeter_results.log the_start.dat

Regular expressions with spaces inside should be surrounded by double-quotes, see "lb=\"the start\""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment