name: Performance

on: [ workflow_dispatch ]

jobs:
  performance:
    runs-on: ubuntu-latest
    env:
      MOODLE_DOCKER_DB: pgsql
      MOODLE_DOCKER_BROWSER: chrome
      MOODLE_DOCKER_PHP_VERSION: '8.0'
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-node@v3
      with:
        node-version-file: '.nvmrc'
    - name: Additional checkouts
      run: |
        git clone --branch master --depth 1 https://github.com/moodle/moodle $GITHUB_WORKSPACE/moodle
        git clone --branch master --depth 1 https://github.com/moodlehq/moodle-docker $GITHUB_WORKSPACE/moodle-docker
    - name: Install npm packages
      run: npm ci --no-audit
    - name: Generate Behat tests plugin
      run: |
        export MOODLE_DOCKER_WWWROOT=$GITHUB_WORKSPACE/moodle
        npx gulp behat
    - name: Configure & launch Moodle with Docker
      run: |
        export MOODLE_DOCKER_WWWROOT=$GITHUB_WORKSPACE/moodle
        cp $GITHUB_WORKSPACE/moodle-docker/config.docker-template.php $GITHUB_WORKSPACE/moodle/config.php
        sed -i "67i\        'capabilities' => [" $GITHUB_WORKSPACE/moodle/config.php
        sed -i "68i\            'extra_capabilities' => [" $GITHUB_WORKSPACE/moodle/config.php
        sed -i "69i\                'goog:loggingPrefs' => ['performance' => 'ALL']," $GITHUB_WORKSPACE/moodle/config.php
        sed -i "70i\                'chromeOptions' => ['perfLoggingPrefs' => ['traceCategories' => 'devtools.timeline']]," $GITHUB_WORKSPACE/moodle/config.php
        sed -i "71i\            ]," $GITHUB_WORKSPACE/moodle/config.php
        sed -i "72i\        ]," $GITHUB_WORKSPACE/moodle/config.php
        sed -i "84i\$CFG->behat_ionic_wwwroot = 'http://moodleapp';" $GITHUB_WORKSPACE/moodle/config.php
        $GITHUB_WORKSPACE/moodle-docker/bin/moodle-docker-compose pull
        $GITHUB_WORKSPACE/moodle-docker/bin/moodle-docker-compose up -d
        $GITHUB_WORKSPACE/moodle-docker/bin/moodle-docker-wait-for-db
    - name: Compile & launch app with Docker
      run: |
        docker build --build-arg build_command="npm run build:test" -t moodlehq/moodleapp:performance .
        docker run -d --rm --name moodleapp moodlehq/moodleapp:performance
        docker network connect moodle-docker_default moodleapp --alias moodleapp
    - name: Init Behat
      run: |
        export MOODLE_DOCKER_WWWROOT=$GITHUB_WORKSPACE/moodle
        $GITHUB_WORKSPACE/moodle-docker/bin/moodle-docker-compose exec -T webserver sh -c "php admin/tool/behat/cli/init.php"
    - name: Run performance tests
      run: |
        export MOODLE_DOCKER_WWWROOT=$GITHUB_WORKSPACE/moodle
        for i in {0..2}
        do
          $GITHUB_WORKSPACE/moodle-docker/bin/moodle-docker-compose exec -T webserver sh -c "php admin/tool/behat/cli/run.php --tags="@performance" --auto-rerun"
        done
    - name: Show performance results
      run: node ./scripts/print-performance-measures.js $GITHUB_WORKSPACE/moodle/behatperformancemeasures/