All pastes #955398 Raw Edit

jtl2sdw.py

public python v1 · immutable
#955398 ·published 2008-03-24 16:28 UTC
rendered paste body
#!/usr/bin/python"""Description : Split JTL file into a Simple Data Writer Logby          : Oliver Erlewein (c)2008 - hacked by infernix to convert to SDW log instead of CSVDate        : 04.02.2008 - 24-3-2008Lang        : Python 2.4+JMeter JTL field contents:Attribute & Contentby      Bytesde      Data encodingdt      Data typeec      Error count (0 or 1, unless multiple samples are aggregated)hn      Hostname where the sample was generatedlb      Labellt      Latency = time to initial response (milliseconds) - not all samplers support thisna      Number of active threads for all thread groupsng      Number of active threads in this grouprc      Response Code (e.g. 200)rm      Response Message (e.g. OK)s       Success flag (true/false)sc      Sample count (1, unless multiple samples are aggregated)t       Elapsed time (milliseconds)tn      Thread Namets      timeStamp (milliseconds since midnight Jan 1, 1970 UTC)"""import sysimport stringimport reimport datetimeimport timestartTime = time.time()cnt                     = 0cnt2                    = 0failCnt                 = 0reCompile = re.compile("\s([^\s]*?)=\"(.*?)\"")delimiterCharacterOut = ","def writeCSVLine(line):    x = reCompile.findall(line)    a = dict((row[0], row[1]) for row in x)    try:        b = a['ts'] + "," + a['t'] + "," + a['lb'] + "," + a['rc'] + "," + a['rm'] + "," + a['tn'] + "," + a['dt'] + "," + a['s'] + "," + a['by'] + "," + a['by'] + "\n"    except:        return -1    o.write(b)    return 1print "Splitting JTL file"try:    runArgv             = sys.argv                      # Save the command line    jtlInfile           = str(sys.argv[1])              # Name of JTL input file    cvsOutfile          = str(sys.argv[2])              # Name of CVS output file    reFilter            = str(sys.argv[3])              # Filter the labels (lb) for the filterexcept:    print "Error: Input format: <input file> <output file> <Filter by regular expression>"    raisetry:    f = open(jtlInfile, "r")    o = open(cvsOutfile, "w")except:    raiseprint "Filtering on regular expression : " + reFilter cmpFilter = re.compile(reFilter)for line in f:    try:        if cmpFilter.search(line):           returnVal = writeCSVLine(line)           if returnVal < 0:              failCnt += 1           else:              cnt2 += 1    except:        print 'Error in line : ', cnt, line        raise    cnt += 1endTime = time.time()print "Time taken                    : ", str(endTime-startTime)print "Lines processed              : ", cntprint "Lines that passed the filter : ", cnt2print "Lines skipped (error?)       : ", failCntf.close()o.close()