From 303ff41a6ebbb23384650e86ebee30bbba216c21 Mon Sep 17 00:00:00 2001
From: Dorchies David <david.dorchies@inrae.fr>
Date: Fri, 17 Sep 2021 20:27:51 +0200
Subject: [PATCH] test: adjust benchmark test

Refs #136
---
 .gitlab-ci.yml                            | 8 ++++++++
 tests/scheduled_tests/benchmarkRunModel.R | 4 ++--
 tests/scheduled_tests/regression.R        | 2 +-
 tests/testthat/benchmarkRunModel.R        | 7 +++++--
 tests/testthat/regression.R               | 4 +++-
 5 files changed, 19 insertions(+), 6 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d657787a..a95b86c2 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -18,6 +18,10 @@ default:
     - R CMD INSTALL .
     - Rscript tests/scheduled_tests/regression.R dev
     - Rscript tests/scheduled_tests/regression.R compare
+  artifacts:
+    paths:
+      - tests/tmp/stable/timing.tsv
+      - tests/tmp/dev/timing.tsv
 
 .check:
   stage: check
@@ -27,6 +31,10 @@ default:
   - Rscript tests/scheduled_tests/benchmarkRunModel.R
   - R CMD INSTALL .
   - Rscript tests/scheduled_tests/benchmarkRunModel.R
+  artifacts:
+    paths:
+      - tests/tmp/benchmark.tsv
+      - tests/tmp/mean_execution_time.tsv
 
 .check_not_cran:
   variables:
diff --git a/tests/scheduled_tests/benchmarkRunModel.R b/tests/scheduled_tests/benchmarkRunModel.R
index 922fa5a8..a983b2f6 100644
--- a/tests/scheduled_tests/benchmarkRunModel.R
+++ b/tests/scheduled_tests/benchmarkRunModel.R
@@ -24,7 +24,7 @@ colnames(dfBM) <- sModelNames
 dfBM <- cbind(version = as.character(packageVersion('airGR')), dfBM)
 
 
-file <- "tests/tmp/benchmark.txt"
+file <- "tests/tmp/benchmark.tsv"
 dir.create("tests/tmp", showWarnings = FALSE)
 write.table(dfBM, file = file,
             row.names = FALSE, col.names = !file.exists(file), quote = FALSE,
@@ -39,7 +39,7 @@ if(length(unique(df$version)) > 1) {
   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")
+  write.table(dfMean, "tests/tmp/mean_execution_time.tsv", 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"])) {
diff --git a/tests/scheduled_tests/regression.R b/tests/scheduled_tests/regression.R
index 069b1438..317fae8b 100644
--- a/tests/scheduled_tests/regression.R
+++ b/tests/scheduled_tests/regression.R
@@ -67,7 +67,7 @@ StoreTopicResults <- function(topic, package, path, run.dontrun = TRUE, run.dont
   dev.off()
 
   write.table(data.frame(topic = topic, time = end_time - start_time),
-            file.path(path, "timing.csv"),
+            file.path(path, "timing.tsv"),
             row.names = FALSE, col.names = FALSE, quote = FALSE,
             sep = "\t", append = TRUE)
 
diff --git a/tests/testthat/benchmarkRunModel.R b/tests/testthat/benchmarkRunModel.R
index aff3a838..67997d97 100644
--- a/tests/testthat/benchmarkRunModel.R
+++ b/tests/testthat/benchmarkRunModel.R
@@ -1,7 +1,10 @@
-df <- read.table("../tmp/mean_execution_time.txt", sep = "\t", header = T)
+df <- read.table("../tmp/mean_execution_time.tsv", sep = "\t", header = T)
 
 lapply(df$model, function(model) {
   test_that(paste(model, ": RunModel should be as fast as CRAN version"), {
-    expect_lt(df$evolution[df$model == model], 1.1)
+    sel <- df$model == model
+    threshold <- max(0.2, -0.15 * df[sel, 2] + 1) # decrease from 1 at 0.1ms to 0.2 at 5.5ms
+    expect_lt(df$evolution[sel], threshold)
   })
 })
+
diff --git a/tests/testthat/regression.R b/tests/testthat/regression.R
index 147b73c5..13e774ab 100644
--- a/tests/testthat/regression.R
+++ b/tests/testthat/regression.R
@@ -33,7 +33,9 @@ CompareWithStable <- function(refVarFile, testDir, regIgnore) {
 tmp_path <- file.path("../tmp", Sys.getenv("R_VERSION"));
 
 if (dir.exists(file.path(tmp_path, "stable")) & dir.exists(file.path(tmp_path, "dev"))) {
-  refVarFiles <- list.files(file.path(tmp_path, "stable"), recursive = TRUE, full.names = TRUE)
+  refVarFiles <- list.files(path = file.path(tmp_path, "stable"),
+                            pattern = "\\.rds$",
+                            recursive = TRUE, full.names = TRUE)
   regIgnoreFile <- "../../.regressionignore"
   if (file.exists(regIgnoreFile)) {
     message("Using .regressionignore file. The following variables are going to be skipped:")
-- 
GitLab