Forked from HYCAR-Hydro / airGR
Source project has a limited visibility.
benchmarkRunModel.R 1.70 KiB
library(airGR)
source("tests/testthat/helper_scheduled_Calibration.R")
BenchmarkRunModel <- function(model) {
  e <- PrepareCalibration(model)
  for(n in ls(e, all.names=TRUE)) assign(n, get(n, e))
  # RunOptions calibration configuration
  RunOptions$Outputs_Sim <- RunOptions$Outputs_Cal
  mbm <- microbenchmark::microbenchmark(RunModel =
    RunModel(InputsModel = InputsModel, RunOptions = RunOptions,
             Param = as.numeric(strsplit(model$ParamFinalR, ";")[[1]]), FUN_MOD = sModel)
  mbm$time
Args <- commandArgs(trailingOnly = TRUE)
sModelNames <- paste0(dfModels$name,
                     ifelse(as.logical(dfModels$IsHyst), "_Hyst", ""))
dfBM <- as.data.frame(apply(dfModels, 1, BenchmarkRunModel))
colnames(dfBM) <- sModelNames
dfBM <- cbind(version = as.character(packageVersion('airGR')), dfBM)
file <- "tests/tmp/benchmark.txt"
dir.create("tests/tmp", showWarnings = FALSE)
write.table(dfBM, file = file,
            row.names = FALSE, col.names = !file.exists(file), quote = FALSE,
            sep = "\t", append = file.exists(file))
df <- read.table(file = file, sep = "\t", header = TRUE)
if(length(unique(df$version)) > 1) {
  lV <- lapply(unique(df$version), function(version) {
    apply(df[df$version == version, -1] / 1E6, 2, mean)
  names(lV) <- unique(df$version)
  dfMean <- cbind(model = sModelNames, as.data.frame(t(do.call(rbind, lV))))
  dfMean$evolution <- (dfMean[,3] - dfMean[,2]) / dfMean[,2]
  write.table(dfMean, "tests/tmp/mean_execution_time.txt", row.names = FALSE, quote = F, sep = "\t")
  res <- testthat::test_file("tests/testthat/benchmarkRunModel.R")
  dRes <- as.data.frame(res)
  if (any(dRes[, "failed"] > 0) | any(dRes[, "error"])) {
    quit(status = 1)