summaryrefslogtreecommitdiff
path: root/.gitlab/test-metrics.sh
blob: 8d67ee7ceedd65a75795483479d36097967fe03c (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
# vim: sw=2 et
set -euo pipefail

NOTES_ORIGIN="https://gitlab.haskell.org/ghc/ghc-performance-notes.git"
REF="perf"

run() {
  echo "$@"
  $@
}

fail() {
  echo "ERROR: $*" >&2
  exit 1
}

function pull() {
  local ref="refs/notes/$REF"
  run git fetch -f "$NOTES_ORIGIN" "$ref:$ref"
  echo "perf notes ref $ref is $(git rev-parse $ref)"
}

# Reset the git notes and append the metrics file to the notes, then push and return the result.
# This is favoured over a git notes merge as it avoids potential data loss/duplication from the merge strategy.
function reset_append_note_push {
  pull || true
  run git notes --ref="$REF" append -F "$METRICS_FILE" HEAD
  run git push "$PERF_NOTES_PUSH_REPO" "refs/notes/$REF"
}

function push() {
  # PERF_NOTES_PUSH_CREDENTIALS is a CI variable set on all GitLab protected branches.
  if [ -z "${PERF_NOTES_PUSH_REPO:-}" ]; then
    if [ -n "${PERF_NOTES_PUSH_CREDENTIALS:-}" ]; then
      PERF_NOTES_PUSH_REPO="https://$PERF_NOTES_PUSH_CREDENTIALS@gitlab.haskell.org/ghc/ghc-performance-notes.git"
    else
      echo "Not pushing performance git notes: PERF_NOTES_PUSH_REPO or PERF_NOTES_PUSH_CREDENTIALS not set."
      exit 0
    fi
  fi

  # TEST_ENV must be set.
  if [ -z "${TEST_ENV:-}" ]
  then
    fail "Not pushing performance git notes: TEST_ENV must be set."
  fi

  # Assert that the METRICS_FILE exists and can be read.
  if [ -z "${METRICS_FILE:-}" ]
  then
    fail "\$METRICS_FILE not set."
  fi
  if ! [ -r "$METRICS_FILE" ]
  then
    fail "Metrics file not found: $METRICS_FILE"
  fi

  # Push the metrics file as a git note. This may fail if another task pushes a note first. In that case
  # the latest note is fetched and appended.
  MAX_RETRY=20
  until reset_append_note_push || [ $MAX_RETRY -le 0 ]
  do
    ((MAX_RETRY--))
    echo ""
    echo "Failed to push git notes. Fetching, appending, and retrying... $MAX_RETRY retries left."
  done
}

case $1 in
  push) push ;;
  pull) pull ;;
  *) fail "Invalid mode $1" ;;
esac