#Assessing the Sustainability of Software Products - A Method Comparison - Replication package
#Copyright (C) 2018, 2019 Yannick Becker, Achim Guldner
#
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Inputs:
scenarioMarkersFilename <- "./GER_Measurements_01_actions.txt"
energyConsumptionScenarioFilename <- "./GER_Measurements_02_power.csv"
performanceScenarioFilename <- "./GER_Measurements_03_performance.csv"

baselineMarkersFilename <- "./GER_Measurements_04_baseline_actions.txt"
energyConsumptionBaselineFilename <- "./GER_Measurements_05_baseline_power.csv"
performanceBaselineFilename <- "./GER_Measurements_06_baseline_performance.csv"

markersTimestampFormat <- "%Y-%m-%d %H:%M:%OS"
energyConsumptionTimestampFormat <- "%d.%m.%y %H:%M:%OS"
performanceTimestampFormat <- "%m/%d/%Y %H:%M:%OS"

# Functions and Rmd:
rmdLocation <- "GER_analysis_script_Report.Rmd"
functionsLocation <- "GER_analysis_script_functions.R"

#Outputs:
dataSaveFileName <- "GER_analysis_script_Data.RData"
outputFileName <- "GER_Report.pdf"
graphicsFolder <- "GER_graphics"

#----------------------------
# Libraries
library(psych)
library(rmarkdown)
#----------------------------
# Options
op <- options(digits.secs = 3) # Make sure that fractions of seconds are included

# Load measurement data
# Energy consumption data
energyConsumptionData <- read.table(file = energyConsumptionScenarioFilename, header = T, sep = ";", skip = 1, dec = ",", stringsAsFactors = F)
# Performance data
# Microsoft-PerfMon data must be converted to UTF-8 first.
performanceData <- read.table(file = performanceScenarioFilename, header = T, sep = ",", quote = "\"", dec = ".", stringsAsFactors = F)
names(performanceData) <- c("timestamp", "ram", "networkReceived", "networkSent", "HDDWritten", "HDDRead", "processorTime")
# Markers
# WinAutomation log data must be converted to UTF-8 first.
markers <- read.table(file = scenarioMarkersFilename, header = F, sep = ";", fill = T, stringsAsFactors = F)
if(ncol(markers) == 3){
  names(markers) <- c("timestamp", "type", "text")
} else {
  names(markers) <- c("timestamp", "type")
}

# Load baseline
# Energy consumption data
energyConsumptionBaseline <- read.table(file = energyConsumptionBaselineFilename, header = T, sep = ";", skip = 1, dec = ",", stringsAsFactors = F)
# Performance data
# Microsoft-PerfMon data must be converted to UTF-8 first.
performanceBaselineData <- read.table(file = performanceBaselineFilename, header = T, sep = ",", quote = "\"", dec = ".", stringsAsFactors = F)
names(performanceBaselineData) <- c("timestamp", "ram", "networkReceived", "networkSent", "HDDWritten", "HDDRead", "processorTime")
# Markers
# WinAutomation log data must be converted to UTF-8 first.
baselineMarkers <- read.table(file = baselineMarkersFilename, header = F, sep = ";", fill = T, stringsAsFactors = F)
names(baselineMarkers) <- c("timestamp", "type")

# Convert timestamps to POSIXct
energyConsumptionData$Zeit <- as.POSIXct(strptime(energyConsumptionData$Zeit, energyConsumptionTimestampFormat))
energyConsumptionBaseline$Zeit <- as.POSIXct(strptime(energyConsumptionBaseline$Zeit, energyConsumptionTimestampFormat))
performanceData$timestamp <- as.POSIXct(strptime(performanceData$timestamp, performanceTimestampFormat))
performanceBaselineData$timestamp <- as.POSIXct(strptime(performanceBaselineData$timestamp, performanceTimestampFormat))
markers$timestamp <- as.POSIXct(strptime(markers$timestamp, markersTimestampFormat))
baselineMarkers$timestamp <- as.POSIXct(strptime(baselineMarkers$timestamp, markersTimestampFormat))

# Convert Bytes to kB
performanceData$networkReceived <- performanceData$networkReceived / 1024
performanceData$networkSent <- performanceData$networkSent / 1024
performanceData$HDDRead <- performanceData$HDDRead / 1024
performanceData$HDDWritten <- performanceData$HDDWritten / 1024

performanceBaselineData$networkReceived <- performanceBaselineData$networkReceived / 1024
performanceBaselineData$networkSent <- performanceBaselineData$networkSent / 1024
performanceBaselineData$HDDRead <- performanceBaselineData$HDDRead / 1024
performanceBaselineData$HDDWritten <- performanceBaselineData$HDDWritten / 1024

# Calculate network traffic and hdd activity
performanceData$networkTraffic <- performanceData$networkReceived + performanceData$networkSent
performanceData$hddActivity <- performanceData$HDDRead + performanceData$HDDWritten

performanceBaselineData$networkTraffic <- performanceBaselineData$networkReceived + performanceBaselineData$networkSent
performanceBaselineData$hddActivity <- performanceBaselineData$HDDRead + performanceBaselineData$HDDWritten

#remove wrong measurements
#markers <- markers[-(67:88),]

# Select timestamps of the startTestrun markers
startmarkers <- markers[which(markers$type == "startTestrun"),]
endmarkers <- markers[which(markers$type == "endTestrun"),]
startActions <- markers[which(markers$type == "startAction"),]
stopActions <- markers[which(markers$type == "stopAction"),]
baselineStartmarkers <- baselineMarkers[which(baselineMarkers$type == "startTestrun"),]
baselineEndmarkers <- baselineMarkers[which(baselineMarkers$type == "endTestrun"),]
## Sort markers by timestamp
#startmarkers <- startmarkers[with(startmarkers, order(timestamp)),]
#baselineStartmarkers <- baselineStartmarkers[with(baselineStartmarkers, order(timestamp)),]


# get Action Names
actionNames <- unique(startActions$text)

# Select the power measurements according to the markers
powerMeasurement <- list()
for(i in 1:nrow(startmarkers)){
  element <- length(powerMeasurement)+1
  #DEBUG:
  #cat("Pass ", i, ": ", length(which((energyConsumptionData$Zeit >= startmarkers$timestamp[i])&(energyConsumptionData$Zeit <= endmarkers$timestamp[i]))), "\n", sep="")
  powerMeasurement[[element]] <- energyConsumptionData[which((energyConsumptionData$Zeit >= startmarkers$timestamp[i])&(energyConsumptionData$Zeit <= endmarkers$timestamp[i])),]
  # Convert timestamp into a time differences in seconds
  powerMeasurement[[element]]$second <- powerMeasurement[[element]]$Zeit-powerMeasurement[[element]]$Zeit[1]
}

# Select the power measurements according to the actions
powerMeasurementActions <- list()
for(i in 1:nrow(startActions)){
  element <- length(powerMeasurementActions)+1
  #DEBUG:
  #cat("Pass ", i, ": ", length(which((energyConsumptionData$Zeit >= startActions$timestamp[i])&(energyConsumptionData$Zeit <= stopActions$timestamp[i]))), "\n", sep="")
  powerMeasurementActions[[element]] <- energyConsumptionData[which((energyConsumptionData$Zeit >= startActions$timestamp[i])&(energyConsumptionData$Zeit <= stopActions$timestamp[i])),]
  # Convert timestamp into a time differences in seconds
  powerMeasurementActions[[element]]$second <- powerMeasurementActions[[element]]$Zeit-powerMeasurementActions[[element]]$Zeit[1]
}

# And the same for the baselines
powerBaseline <- list()
for(i in 1:nrow(baselineStartmarkers)){
  element <- length(powerBaseline)+1
  powerBaseline[[element]] <- energyConsumptionBaseline[which((energyConsumptionBaseline$Zeit >= baselineStartmarkers$timestamp[i])&(energyConsumptionBaseline$Zeit <= baselineEndmarkers$timestamp[i])),]
  # Convert timestamp into a time differences in seconds
  powerBaseline[[element]]$second <- powerBaseline[[element]]$Zeit-powerBaseline[[element]]$Zeit[1]
}

# Select the performance measurements according to the markers
performanceMeasurement <- list()
for(i in 1:nrow(startmarkers)){
  element <- length(performanceMeasurement)+1
  #DEBUG:
  #cat("Pass ", i, ": ", length(which((performanceData$timestamp >= startmarkers$timestamp[i])&(performanceData$timestamp <= endmarkers$timestamp[i]))), "\n", sep="")
  performanceMeasurement[[element]] <- performanceData[which((performanceData$timestamp >= startmarkers$timestamp[i])&(performanceData$timestamp <= endmarkers$timestamp[i])),]
  # Convert timestamp into a time differences in seconds
  performanceMeasurement[[element]]$second <- round(performanceMeasurement[[element]]$timestamp-performanceMeasurement[[element]]$timestamp[1])
}

# Select the performance measurements according to the actions
performanceMeasurementActions <- list()
for(i in 1:nrow(startActions)){
  element <- length(performanceMeasurementActions)+1
  #DEBUG:
  #cat("Pass ", i, ": ", length(which((performanceData$timestamp >= startActions$timestamp[i])&(performanceData$timestamp <= stopActions$timestamp[i]))), "\n", sep="")
  performanceMeasurementActions[[element]] <- performanceData[which((performanceData$timestamp >= startActions$timestamp[i])&(performanceData$timestamp <= stopActions$timestamp[i])),]
  # Convert timestamp into a time differences in seconds
  performanceMeasurementActions[[element]]$second <- round(performanceMeasurementActions[[element]]$timestamp-performanceMeasurementActions[[element]]$timestamp[1])
}

# And the same for the baselines
performanceBaseline <- list()
for(i in 1:nrow(baselineStartmarkers)){
  element <- length(performanceBaseline)+1
  performanceBaseline[[element]] <- performanceBaselineData[which((performanceBaselineData$timestamp >= baselineStartmarkers$timestamp[i])&(performanceBaselineData$timestamp <= baselineEndmarkers$timestamp[i])),]
  # Convert timestamp into a time differences in seconds
  performanceBaseline[[element]]$second <- performanceBaseline[[element]]$timestamp-performanceBaseline[[element]]$timestamp[1]
}

source(functionsLocation)

# Generate summary statistics for the powerMeasurement
allPowerMeasurements <- generateMeasurementTable(powerMeasurement)
allPerformanceMeasurements <- generateMeasurementTable(performanceMeasurement)
allPowerBaselines <- generateMeasurementTable(powerBaseline)
allPerformanceBaselines <- generateMeasurementTable(performanceBaseline)

# DEBUG
#printSummary(allPowerMeasurements, "Wert.1.avg.W.")

#calculate measurement duration
shortestTestrun <- 1
shortestTestrunCount <- nrow(powerMeasurement[[1]])
longestTestrun <- 1
longestTestrunCount <- nrow(powerMeasurement[[1]])
for(i in 2:length(powerMeasurement)){
  if(shortestTestrunCount > nrow(powerMeasurement[[i]])){
    shortestTestrunCount <- nrow(powerMeasurement[[i]])
    shortestTestrun <- i
  }
  if(longestTestrunCount < nrow(powerMeasurement[[i]])){
    longestTestrunCount <- nrow(powerMeasurement[[i]])
    longestTestrun <- i
  }
}


# generate results and plots for the testruns
dir.create(graphicsFolder)
saveWd <- getwd()
setwd(graphicsFolder)

plotAllMeasurementsAndMean(measurements = powerMeasurement, "Wert.1.avg.W.", main = "Plot of power measurement", xlab = "Time [s]", ylab="Power [W]\n", plotFilename = "power.png", markers = markers)

plotAllMeasurementsAndMean(measurements = performanceMeasurement, "ram", main = "Plot of RAM usage", xlab = "Time [s]", ylab="RAM usage [%]\n", plotFilename = "ram_usage.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceMeasurement, "networkTraffic", main = "Plot of network traffic (sending + receiving)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceMeasurement, "hddActivity", main = "Plot of HDD activity (readig + writing)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceMeasurement, "processorTime", main = "Plot of CPU usage", xlab = "Time [s]", ylab="CPU usage [%]\n", plotFilename = "cpu_usage.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceMeasurement, "networkSent", main = "Plot of network traffic (sending)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_sent.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceMeasurement, "networkReceived", main = "Plot of network traffic (receiving)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_received.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceMeasurement, "HDDRead", main = "Plot of HDD activity (readig)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_read.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceMeasurement, "HDDWritten", main = "Plot of HDD activity (writing)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_written.png", markers = markers)

mean_power <- plotAllMeasurementsAndMean(measurements = powerMeasurement, "Wert.1.avg.W.", main = "Plot of power measurement", xlab = "Time [s]", ylab="Power [W]\n", plotFilename = "power_mean.png", markers = markers, meansOnly = T)

mean_ram <- plotAllMeasurementsAndMean(measurements = performanceMeasurement, "ram", main = "Plot of RAM usage", xlab = "Time [s]", ylab="RAM usage [%]\n", plotFilename = "ram_usage_mean.png", markers = markers, meansOnly = T)
mean_networkTraffic <- plotAllMeasurementsAndMean(measurements = performanceMeasurement, "networkTraffic", main = "Plot of network traffic (sending + receiving)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_mean.png", markers = markers, meansOnly = T)
mean_hddActivity <- plotAllMeasurementsAndMean(measurements = performanceMeasurement, "hddActivity", main = "Plot of HDD activity (readig + writing)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_mean.png", markers = markers, meansOnly = T)
mean_processorTime <- plotAllMeasurementsAndMean(measurements = performanceMeasurement, "processorTime", main = "Plot of CPU usage", xlab = "Time [s]", ylab="CPU usage [%]\n", plotFilename = "cpu_usage_mean.png", markers = markers, meansOnly = T)
mean_networkSent <- plotAllMeasurementsAndMean(measurements = performanceMeasurement, "networkSent", main = "Plot of network traffic (sending)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_sent_mean.png", markers = markers, meansOnly = T)
mean_networkReceived <- plotAllMeasurementsAndMean(measurements = performanceMeasurement, "networkReceived", main = "Plot of network traffic (receiving)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_received_mean.png", markers = markers, meansOnly = T)
mean_HDDRead <- plotAllMeasurementsAndMean(measurements = performanceMeasurement, "HDDRead", main = "Plot of HDD activity (readig)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_read_mean.png", markers = markers, meansOnly = T)
mean_HDDWritten <- plotAllMeasurementsAndMean(measurements = performanceMeasurement, "HDDWritten", main = "Plot of HDD activity (writing)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_written_mean.png", markers = markers, meansOnly = T)

plotAllMeasurementsAndMean(measurements = powerBaseline, "Wert.1.avg.W.", main = "Plot of power baseline", xlab = "Time [s]", ylab="Power [W]\n", plotFilename = "power_baseline.png", markers = markers)

plotAllMeasurementsAndMean(measurements = performanceBaseline, "ram", main = "Plot of RAM usage baseline", xlab = "Time [s]", ylab="RAM usage [%]\n", plotFilename = "ram_usage_baseline.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceBaseline, "networkTraffic", main = "Plot of network traffic baseline (sending + receiving)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_baseline.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceBaseline, "hddActivity", main = "Plot of HDD activity baseline (readig + writing)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_baseline.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceBaseline, "processorTime", main = "Plot of CPU usage baseline", xlab = "Time [s]", ylab="CPU usage [%]\n", plotFilename = "cpu_usage_baseline.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceBaseline, "networkSent", main = "Plot of network traffic baseline (sending)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_sent_baseline.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceBaseline, "networkReceived", main = "Plot of network traffic baseline (receiving)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_received_baseline.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceBaseline, "HDDRead", main = "Plot of HDD activity baseline (readig)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_read_baseline.png", markers = markers)
plotAllMeasurementsAndMean(measurements = performanceBaseline, "HDDWritten", main = "Plot of HDD activity baseline (writing)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_written_baseline.png", markers = markers)

mean_power_baseline <- plotAllMeasurementsAndMean(measurements = powerBaseline, "Wert.1.avg.W.", main = "Plot of power baseline", xlab = "Time [s]", ylab="Power [W]\n", plotFilename = "power_baseline_mean.png", markers = markers, meansOnly = T)

mean_ram_baseline <- plotAllMeasurementsAndMean(measurements = performanceBaseline, "ram", main = "Plot of RAM usage baseline", xlab = "Time [s]", ylab="RAM usage [%]\n", plotFilename = "ram_usage_baseline_mean.png", markers = markers, meansOnly = T)
mean_networkTraffic_baseline <- plotAllMeasurementsAndMean(measurements = performanceBaseline, "networkTraffic", main = "Plot of network traffic baseline (sending + receiving)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_baseline_mean.png", markers = markers, meansOnly = T)
mean_hddActivity_baseline <- plotAllMeasurementsAndMean(measurements = performanceBaseline, "hddActivity", main = "Plot of HDD activity baseline (readig + writing)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_baseline_mean.png", markers = markers, meansOnly = T)
mean_processorTime_baseline <- plotAllMeasurementsAndMean(measurements = performanceBaseline, "processorTime", main = "Plot of CPU usage baseline", xlab = "Time [s]", ylab="CPU usage [%]\n", plotFilename = "cpu_usage_baseline_mean.png", markers = markers, meansOnly = T)
mean_networkSent_baseline <- plotAllMeasurementsAndMean(measurements = performanceBaseline, "networkSent", main = "Plot of network traffic baseline (sending)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_sent_baseline_mean.png", markers = markers, meansOnly = T)
mean_networkReceived_baseline <- plotAllMeasurementsAndMean(measurements = performanceBaseline, "networkReceived", main = "Plot of network traffic baseline (receiving)", xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = "network_traffic_received_baseline_mean.png", markers = markers, meansOnly = T)
mean_HDDRead_baseline <- plotAllMeasurementsAndMean(measurements = performanceBaseline, "HDDRead", main = "Plot of HDD activity baseline (readig)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_read_baseline_mean.png", markers = markers, meansOnly = T)
mean_HDDWritten_baseline <- plotAllMeasurementsAndMean(measurements = performanceBaseline, "HDDWritten", main = "Plot of HDD activity baseline (writing)", xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = "hdd_activity_written_baseline_mean.png", markers = markers, meansOnly = T)

# generate results and plots for the actions
for(action in actionNames){
  plotAllMeasurementsAndMean(measurements = powerMeasurementActions[which(startActions$text == action)], "Wert.1.avg.W.", main = paste("Plot of power measurement for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="Power [W]\n", plotFilename = paste("power_", action, ".png", sep = ""))
  plotAllMeasurementsAndMean(measurements = powerMeasurementActions[which(startActions$text == action)], "Wert.1.avg.W.", main = paste("Plot of power measurement for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="Power [W]\n", plotFilename = paste("power_mean_", action, ".png", sep = ""), meansOnly = T)
  
  plotAllMeasurementsAndMean(measurements = performanceMeasurementActions[which(startActions$text == action)], "ram", main = paste("Plot of RAM usage for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="RAM usage [%]\n", plotFilename = paste("ram_usage.png_", action, ".png", sep = ""))
  plotAllMeasurementsAndMean(measurements = performanceMeasurementActions[which(startActions$text == action)], "networkTraffic", main = paste("Plot of network traffic (sending + receiving) for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = paste("network_traffic.png_", action, ".png", sep = ""))
  plotAllMeasurementsAndMean(measurements = performanceMeasurementActions[which(startActions$text == action)], "hddActivity", main = paste("Plot of HDD activity (readig + writing) for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = paste("hdd_activity.png_", action, ".png", sep = ""))
  plotAllMeasurementsAndMean(measurements = performanceMeasurementActions[which(startActions$text == action)], "processorTime", main = paste("Plot of CPU usage for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="CPU usage [%]\n", plotFilename = paste("cpu_usage.png_", action, ".png", sep = ""))
  plotAllMeasurementsAndMean(measurements = performanceMeasurementActions[which(startActions$text == action)], "networkSent", main = paste("Plot of network traffic (sending) for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = paste("network_traffic_sent.png_", action, ".png", sep = ""))
  plotAllMeasurementsAndMean(measurements = performanceMeasurementActions[which(startActions$text == action)], "networkReceived", main = paste("Plot of network traffic (receiving) for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="Network traffic [KB]\n", plotFilename = paste("network_traffic_received.png_", action, ".png", sep = ""))
  plotAllMeasurementsAndMean(measurements = performanceMeasurementActions[which(startActions$text == action)], "HDDRead", main = paste("Plot of HDD activity (readig) for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = paste("hdd_activity_read.png_", action, ".png", sep = ""))
  plotAllMeasurementsAndMean(measurements = performanceMeasurementActions[which(startActions$text == action)], "HDDWritten", main = paste("Plot of HDD activity (writing) for \"", action, "\"", sep=""), xlab = "Time [s]", ylab="HDD activity [KB]\n", plotFilename = paste("hdd_activity_written.png_", action, ".png", sep = ""))
}

setwd(saveWd)

#Generate the pdf using RMarkdown
render(input = rmdLocation, output_file = outputFileName, params = list(
  title = "Measurement analysis GER",
  subtitle = "Measured Software: Sorting algorithms",
  measurementName = "Sorting algorithms",
  startmarkers = startmarkers,
  endmarkers = endmarkers,
  baselineStartmarkers = baselineStartmarkers,
  baselineEndmarkers = baselineEndmarkers,
  startActions = startActions,
  stopActions = stopActions,
  measurementCount = nrow(startmarkers),
  shortestTestrun = shortestTestrun,
  longestTestrun = longestTestrun,
  actionNames = actionNames,
  baselineCount = nrow(baselineStartmarkers),
  performanceBaseline = performanceBaseline,
  performanceMeasurement = performanceMeasurement,
  performanceMeasurementActions = performanceMeasurementActions,
  allPerformanceMeasurements = allPerformanceMeasurements,
  allPerformanceBaselines = allPerformanceBaselines,
  powerBaseline = powerBaseline,
  powerMeasurement = powerMeasurement,
  powerMeasurementActions = powerMeasurementActions,
  allPowerMeasurements = allPowerMeasurements,
  allPowerBaselines = allPowerBaselines,
  graphicsFolder = graphicsFolder
))

#restore options
options(op)

#save data
save.image(dataSaveFileName)
