发布新日志

  • 从 Jmeter输出数据导出excel表格

    2008-11-17 15:12:13

    时间匆忙,脚本未仔细测试

    require 'rexml/document'
    require 'win32ole'
    include REXML

    $input_file_name = ""
    $output_file_name = ""
    $column_names = ["Sample number", "Response Time", "Latency Time", "Time Start", "Status", "URL", "Return Code", "Return Msg", "Thread Number", "Date Type", "Bytes"]

    def print_usage
      puts "Usage: JmeterExtractor [JMeterResultFile] [ExcelFile]"
    end

    def check_input_file
      if File.exist? $input_file_name
        puts "input file "+ $input_file_name+" found"
      else
        puts "input file"+ $input_file_name+ " not found"
        exit
      end
    end

    def check_output_file
      if File.exist? $output_file_name
        puts "input file "+ $output_file_name+" found"
      else
        puts "input file"+ $output_file_name+ " not found"
        exit
      end
    end

    def init_excel
      # -4100 is the value for the Excel constant xl3DColumn.
      $ChartTypeVal = -4100;
     
      # Creates OLE object to Excel
      $excel = WIN32OLE.new("excel.application")
      $excel.Visible = true
      $excel.WorkBooks.Open($output_file_name)
      worksheet = $excel.WorkSheets.Add();
      worksheet.Activate
      worksheet.name = Time.new.to_i.to_s
      #  $excel.WorkSheets("testResults").Activate
    end

    def close_excel
      $excel.ActiveWorkbook.Close(1)
      $excel.Quit()
     
    end

    def get_start_time time
      usec = time % 1000
      sec = time/1000
      Time.at(sec).to_s + " " + usec.to_s + "ms"
    end

    def get_status s
      if (s.eql? "true")
        "success"
      else
        "fail"
      end
    end

    def print_excel_title
      $column_names.each_index do |i|
        $excel.Cells(1, i+1).value = $column_names[i]
      end
    end

    arg_len = ARGV.length
    #puts arg_len
    case arg_len
      when 0
      $input_file_name = "C:/tool/jakarta-jmeter-2.3.1/workspace/1"
      $output_file_name = "C:/tool/jakarta-jmeter-2.3.1/workspace/result.xls"
      when 1
      $input_file_name = ARGV[0];
      $output_file_name = "C:/tool/jakarta-jmeter-2.3.1/workspace/result.xls"
      when 2
      $input_file_name = ARGV[0];
      $output_file_name = ARGV[1];
    else
    end
    #puts $input_file_name
    #puts $output_file_name
    check_input_file
    check_output_file
    init_excel
    input_file = File.new($input_file_name);
    doc = Document.new(input_file)
    $element_index = 0
    doc.elements.each("//httpSample") do |e|
      $element_index += 1
      print e.attributes["t"]
      print " "
      print e.attributes["lt"]
      print " "
      print e.attributes["ts"]
      print " "
      print e.attributes["s"]
      print " "
      print e.attributes["lb"]
      print " "
      print e.attributes["rc"]
      print " "
      print e.attributes["rm"]
      print " "
      print e.attributes["tn"]
      print " "
      print e.attributes["dt"]
      print " "
      print e.attributes["by"]
      puts
      STDOUT.flush
      print_excel_title
      $excel.Cells($element_index+1, 1).value = $element_index
      $excel.Cells($element_index+1, 2).value = e.attributes["t"]
      $excel.Cells($element_index+1, 3).value = e.attributes["lt"]
      $excel.Cells($element_index+1, 4).value = get_start_time(e.attributes["ts"].to_i)
      $excel.Cells($element_index+1, 5).value = get_status(e.attributes["s"])
      $excel.Cells($element_index+1, 6).value = e.attributes["lb"]
      $excel.Cells($element_index+1, 7).value = e.attributes["rc"]
      $excel.Cells($element_index+1, 8).value = e.attributes["rm"]
      $excel.Cells($element_index+1, 9).value = e.attributes["tn"]
      $excel.Cells($element_index+1, 10).value = e.attributes["dt"]
      $excel.Cells($element_index+1, 11).value = e.attributes["by"]
    end

    close_excel

Open Toolbar