Wednesday, November 24, 2010

Aggregate Jmeter Raw data into CSV file

I am doing daily automated Jmeter load test to track performance trend using command line, due to only raw data available , so i need to write some code to parser and Aggregate it.
PS: I am using Ruby1.9 to handle this, Ruby1.8.7 and below do not implement the append row to CSV file yet.

You need to Modify the settings for Results file configuration in jmeter.properties in advance:

 jmeter.save.saveservice.output_format=csv  
 jmeter.save.saveservice.assertion_results=none  
 jmeter.save.saveservice.data_type=false  
 jmeter.save.saveservice.label=true  
 jmeter.save.saveservice.response_code=true  
 jmeter.save.saveservice.response_data=false   
 jmeter.save.saveservice.response_data.on_error=false  
 jmeter.save.saveservice.response_message=false  
 jmeter.save.saveservice.successful=false  
 jmeter.save.saveservice.thread_name=true  
 jmeter.save.saveservice.time=true  
 jmeter.save.saveservice.subresults=false  
 jmeter.save.saveservice.assertions=false  
 jmeter.save.saveservice.latency=true  
 jmeter.save.saveservice.samplerData=false  
 jmeter.save.saveservice.responseHeaders=false  
 jmeter.save.saveservice.requestHeaders=false  
 jmeter.save.saveservice.encoding=false  
 jmeter.save.saveservice.bytes=true  
 jmeter.save.saveservice.url=false  
 jmeter.save.saveservice.filename=false  
 jmeter.save.saveservice.hostname=false  
 jmeter.save.saveservice.thread_counts=false  
 jmeter.save.saveservice.sample_count=false  
 jmeter.save.saveservice.idle_time=false
 jmeter.save.saveservice.print_field_names=false

Ruby Source code:

 require 'csv'
 require 'time'
 #Generate agg result from Jmeter raw data
 def parserCSVperfdata(filename)  
  h = {};  
  h_count = {}  
  result = [];  
  i =0;  
  CSV.foreach(filename) do |rec|  
   if (h.has_key?(rec[2]))  
    h[rec[2]] = h[rec[2]].to_i + rec[1].to_i  
    h_count[rec[2]] = h_count[rec[2]].to_i + 1  
    next;  
   else  
    h[rec[2]] = rec[1]  
    h_count[rec[2]] = h_count[rec[2]].to_i + 1  
   end  
  end  
  h.each_key { |key|  
   h[key] = h[key].to_i/h_count[key].to_i  
   result[i] = key;  
   result[i+1] = ",";  
   result[i+2] = h[key];  
   result[i+3] = "\n";  
   i=i+4  
  }  
  return h.sort;  
 end 

 #insert agg result into target CSV file
 def insertlog(src,dest)  
  # get current date and collect the data today  
  t = Time.now  
  cdate = t.strftime("%Y-%m-%d")  
  #append row to CSV file, using Ruby1.9 here for mode = 'a'  
  csvwriter = CSV.open(dest,'a')  
  #append array of string, need to convert date string to array  
  csvwriter << cdate.split  
  src.each do |row|  
   csvwriter << row  
  end  
 end  

Testing code:

 t = Time.now  
 mdate = t.strftime("%Y%m%d")  
 aggresultfile = "D://Perf//loadtest.csv";  
 singlethreadresultfile = "D://Perf//singleuser.csv"  
 # Generate JMeter agg result on daily bases  
 arraydir = Dir.entries("D://tools//jakarta-jmeter-2.4//bin");  
 arraydir.each { |item|  
       item.scan(/#{mdate}_[0-9]+_rawdata\.csv/) { |match|  
           aggdata = parserCSVperfdata("D://tools//jakarta-jmeter-2.4//bin//#{match}");  
           insertlog(aggdata,aggresultfile);  
      }  
 }  

Enjoy!

No comments:

Post a Comment