Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@marvin9000
Last active December 19, 2015 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvin9000/6006367 to your computer and use it in GitHub Desktop.
Save marvin9000/6006367 to your computer and use it in GitHub Desktop.
Groovy script to condense JMeter CSV logfiles (reduce number of lines, calculate some averages).
@Grab(group='net.sf.opencsv', module='opencsv', version='2.3')
import au.com.bytecode.opencsv.CSVReader
import static Constants.*
class Constants {
static final TIMESTAMP_IDX = 0
static final ELAPSED_IDX = 1
static final LABEL_IDX = 2
static final CODE_IDX = 3
}
precision = args[0].toInteger()
inputFile = args[1]
outputFile = args[2]
def timeSlices = [:]
def reader = new CSVReader(new FileReader(inputFile), '|' as char)
reader.readNext() // skip headers
String[] row
while ((row = reader.readNext()) != null) {
def choppedTimestamp = row[TIMESTAMP_IDX].substring(0, precision)
if (timeSlices[choppedTimestamp] != null) {
if (timeSlices[choppedTimestamp][row[LABEL_IDX]] != null) {
((Slice) timeSlices[choppedTimestamp][row[LABEL_IDX]]).add(row)
} else {
slice = new Slice(row[LABEL_IDX])
slice.add(row)
timeSlices[choppedTimestamp][row[LABEL_IDX]] = slice
}
} else {
def timeSlice = [:]
timeSlices[choppedTimestamp] = timeSlice
slice = new Slice(row[LABEL_IDX])
slice.add(row)
timeSlice[row[LABEL_IDX]] = slice
}
}
new File(outputFile).withWriter { out ->
out.println "time|elapsed|count|count500|req_per_min|label|node"
timeSlices.each { timeStamp, map ->
map.each { name, slice ->
out.println timeStamp + "|" + slice.sumUp() + "|" + slice.count + "|" + slice.count500 + "|" + slice.requestRate() + "|" + slice.name + "|" + slice.node
}
}
}
class Slice {
private String name, node
private int count, sum, count500
public Slice(String name) {
if (containsNode(name)) {
this.name = name.substring(0, name.lastIndexOf("_"))
this.node = name.substring(name.lastIndexOf("_") + 1)
} else {
this.name = name
this.node = "-"
}
}
private static containsNode(String name) {
if (name.lastIndexOf("_") > 0) {
String nodeCandidate = name.substring(name.lastIndexOf("_") + 1)
if (nodeCandidate.size() == 3) {
return true
}
}
return false
}
public add(String[] row) {
count++
sum += row[ELAPSED_IDX].toInteger()
if (row[CODE_IDX].startsWith("5")) {
count500++
}
}
public String sumUp() {
return Math.round(sum / count)
}
public double requestRate() {
return count / 10
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment