benchmarkRunModel.R 1.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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"
28
dir.create("tests/tmp", showWarnings = FALSE)
29
30
write.table(dfBM, file = file,
            row.names = FALSE, col.names = !file.exists(file), quote = FALSE,
31
            sep = "\t", append = file.exists(file))
32
33
34
35
36
37
38
39
40
41
42

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")
43
  res <- testthat::test_file("tests/testthat/benchmarkRunModel.R")
44
45
46
47
48
49
50
  dRes <- as.data.frame(res)
  if (any(dRes[, "failed"] > 0) | any(dRes[, "error"])) {
    quit(status = 1)
  }
}