From d0ef83123e466365f5362b76d691bec489ffe94c Mon Sep 17 00:00:00 2001
From: Alp Mestanogullari <alpmestan@gmail.com>
Date: Fri, 1 Nov 2019 12:06:05 +0100
Subject: [PATCH] hadrian: fix support for the recording of perf test results

Before this patch, Hadrian didn't care about the TEST_ENV and
METRICS_FILE environment variables, that the performance testing
infrastructure uses to record perf tests results from CI jobs.

It now looks them up right before running the testsuite driver,
and passes suitable --test-env/--metrics-file arguments when
these environment variables are set.
---
 .gitlab-ci.yml                           | 14 +++++++++++---
 hadrian/src/Settings/Builders/RunTest.hs |  8 ++++++++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5b0d9b4e9d65..a328d5d1de17 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -202,7 +202,15 @@ lint-release-changelogs:
     - mv _build/bindist/ghc*.tar.xz ghc.tar.xz
     - export TOP=$(pwd)
     - cd _build/bindist/ghc-*/ && ./configure --prefix=$TOP/_build/install && make install && cd ../../../
-    - hadrian/build.cabal.sh --flavour=$FLAVOUR -j`mk/detect-cpu-count.sh` --docs=no-sphinx test --summary-junit=./junit.xml --test-compiler=$TOP/_build/install/bin/ghc
+    - |
+      # Prepare to push git notes.
+      METRICS_FILE=$CI_PROJECT_DIR/performance-metrics.tsv
+      git config user.email "ben+ghc-ci@smart-cactus.org"
+      git config user.name "GHC GitLab CI"
+    - hadrian/build.cabal.sh --flavour=$FLAVOUR -j`mk/detect-cpu-count.sh` --docs=no-sphinx test --summary-junit=./junit.xml --test-compiler=$TOP/_build/install/bin/ghc || (METRICS_FILE=$METRICS_FILE .gitlab/push-test-metrics.sh && false)
+    - |
+      # Push git notes.
+      METRICS_FILE=$METRICS_FILE .gitlab/push-test-metrics.sh
   cache:
     key: hadrian
     paths:
@@ -239,7 +247,7 @@ validate-x86_64-linux-deb9-unreg-hadrian:
   stage: full-build
   variables:
     CONFIGURE_ARGS: --enable-unregisterised
-    TEST_ENV: "x86_64-linux-deb9-unreg"
+    TEST_ENV: "x86_64-linux-deb9-unreg-hadrian"
 
 hadrian-ghc-in-ghci:
   <<: *only-default
@@ -790,7 +798,7 @@ nightly-i386-windows-hadrian:
   extends: .build-windows-hadrian
   variables:
     MSYSTEM: MINGW32
-    TEST_ENV: "x86_64-windows-hadrian"
+    TEST_ENV: "i386-windows-hadrian"
   only:
     variables:
       - $NIGHTLY
diff --git a/hadrian/src/Settings/Builders/RunTest.hs b/hadrian/src/Settings/Builders/RunTest.hs
index 6e07d1407afb..96df86606e13 100644
--- a/hadrian/src/Settings/Builders/RunTest.hs
+++ b/hadrian/src/Settings/Builders/RunTest.hs
@@ -75,6 +75,8 @@ runTestBuilderArgs = builder RunTest ? do
     (acceptPlatform, acceptOS) <- expr . liftIO $
         (,) <$> (maybe False (=="YES") <$> lookupEnv "PLATFORM")
             <*> (maybe False (=="YES") <$> lookupEnv "OS")
+    (testEnv, testMetricsFile) <- expr . liftIO $
+        (,) <$> lookupEnv "TEST_ENV" <*> lookupEnv "METRICS_FILE"
 
     threads     <- shakeThreads <$> expr getShakeOptions
     os          <- getTestSetting TestHostOS
@@ -138,9 +140,15 @@ runTestBuilderArgs = builder RunTest ? do
             , arg "--config", arg $ "timeout_prog=" ++ show (top -/- timeoutProg)
             , arg "--config", arg $ "stats_files_dir=" ++ statsFilesDir
             , arg $ "--threads=" ++ show threads
+            , emitWhenSet testEnv $ \env -> arg ("--test-env=" ++ show env)
+            , emitWhenSet testMetricsFile $ \file -> mconcat
+                [ arg "--metrics-file", arg (show file) ]
             , getTestArgs -- User-provided arguments from command line.
             ]
 
+      where emitWhenSet Nothing  _ = mempty
+            emitWhenSet (Just v) f = f v
+
 -- | Command line arguments for running GHC's test script.
 getTestArgs :: Args
 getTestArgs = do
-- 
GitLab