name: Post Code Review Checks (Micro Benchmarks)

on:
  pull_request:
    types: [opened, synchronize, reopened, labeled]
    branches:
      - master

jobs:
  openjdk-microbm:
    runs-on: [self-hosted, Linux, freq-scaling-off]
    if: contains(github.event.pull_request.labels.*.name, 'PR-benchmarking')
    steps:
      - name: Check Revisions
        uses: qinsoon/comment-env-vars@1.0.2
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          default_env: 'OPENJDK_BINDING_TRUNK_REF=master,MMTK_CORE_TRUNK_REF=master,OPENJDK_BINDING_BRANCH_REF=master,MMTK_CORE_BRANCH_REF=${{ github.event.pull_request.head.sha }}'
      # Trunk
      # - binding
      - name: Checkout OpenJDK Binding Trunk
        uses: actions/checkout@v2
        with:
          repository: mmtk/mmtk-openjdk
          token: ${{ secrets.CI_ACCESS_TOKEN }}
          path: mmtk-openjdk-trunk
          submodules: true
          ref: ${{ env.OPENJDK_BINDING_TRUNK_REF }}
      # -core
      - name: Checkout MMTk Core
        uses: actions/checkout@v2
        with:
          ref: ${{ env.MMTK_CORE_TRUNK_REF }}
          path: mmtk-core-trunk
      # Branch
      # - binding
      - name: Checkout OpenJDK Binding Branch
        uses: actions/checkout@v2
        with:
          repository: mmtk/mmtk-openjdk
          token: ${{ secrets.CI_ACCESS_TOKEN }}
          path: mmtk-openjdk-branch
          submodules: true
          ref: ${{ env.OPENJDK_BINDING_BRANCH_REF }}
      # - core
      - name: Checkout MMTk Core
        uses: actions/checkout@v2
        with:
          ref: ${{ env.MMTK_CORE_BRANCH_REF }}
          path: mmtk-core-branch
      # checkout perf-kit
      - name: Checkout Perf Kit
        uses: actions/checkout@v2
        with:
          repository: mmtk/ci-perf-kit
          token: ${{ secrets.CI_ACCESS_TOKEN }}
          ref: "0.4.3"
          path: ci-perf-kit
          submodules: true
      # run compare
      - name: Compare Performance
        id: run
        run: |
          RUSTUP_TOOLCHAIN=nightly-2020-07-08 ./ci-perf-kit/scripts/openjdk-micro-bm.sh mmtk-openjdk-trunk/ mmtk-core-trunk/ mmtk-openjdk-branch/ mmtk-core-branch/ openjdk-rebench-report.md
      # set report.md to output
      - uses: pCYSl5EDgo/cat@master
        id: cat
        with:
          path: openjdk-rebench-report.md
      # upload run results
      - uses: actions/upload-artifact@v2
        with:
          name: openjdk-rebench-data
          path: ci-perf-kit/microbm/ci.data
      - uses: actions/upload-artifact@v2
        with:
          name: openjdk-rebench-report.md
          path: openjdk-rebench-report.md
      # report
      - name: Result
        if: always()
        uses: thollander/actions-comment-pull-request@master
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          message: ${{ steps.cat.outputs.text }}
      # Cleanup logs (this is necessary for self-hosted runners)
      - name: Clean up logs and reports
        if: always()
        run: |
          rm -f ci-perf-kit/microbm/*.data
          rm openjdk-rebench-report.md