From 54da0d6da6a9fbda5780c3bd3cd42838708731d4 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Mon, 18 Nov 2024 15:05:06 +0800 Subject: [PATCH] Performance runner --- exports | 47 ++++ .../main/jobtypes/performance/performance.sh | 218 ++++++++++++++++++ .../modules/docker-php/config.template.php | 2 +- 3 files changed, 266 insertions(+), 1 deletion(-) create mode 100755 exports create mode 100644 runner/main/jobtypes/performance/performance.sh diff --git a/exports b/exports new file mode 100755 index 0000000..6531a7b --- /dev/null +++ b/exports @@ -0,0 +1,47 @@ +# The location of the workspace. +export WORKSPACE=/home/simey/moodlehq/moodle-ci-runner/workspace +export CODEDIR=/home/simey/www/docker_integration_main + +# The test to run +#export TESTTORUN=phpunit +#export TESTTORUN=install +export JOBTYPE=performance + +# The browser to use for Behat tests +#export BROWSER=firefox +#export BROWSER=chrome +#export BROWSER_HEADLESS=0 +#export BEHAT_TOTAL_RUNS=3 + +# The build id - this is used to generated the chash +export BUILD_ID="1" + +# The Database to use +#export DBTYPE=mysqli +#export DBTYPE=mariadb +#export DBTYPE=oci +export DBTYPE=pgsql +#export DBTYPE=sqlsrv + +# Database slaces +export DBREPLICAS=0 +#export DBTAG=latest + +# Any Behat Tags to run +#export TAGS='@javascript&&@mod_workshop&&@arn' +#export TAGS=core_notes +#export TAGS=arn +#export TAGS=filter +#export TAGS="@install" +#export BEHAT_NAME="We can use calculated grade totals" + +# PHP Version +export PHP_VERSION=8.1 + +# Install the moodle mobile app plugin. +#export PLUGINSTOINSTALL="https://github.com/moodlehq/moodle-local_moodlemobileapp.git|local/moodlemobileapp|" +#export MOBILE_VERSION="latest" + + +#export GOOD_COMMIT="7f450dcea36d752e62ab169cbeaf830ecb54c70f" +#export BAD_COMMIT="29ab56c4d28ccdc82722595fac1a1d739e6f2a0d" diff --git a/runner/main/jobtypes/performance/performance.sh b/runner/main/jobtypes/performance/performance.sh new file mode 100644 index 0000000..d0cbc5d --- /dev/null +++ b/runner/main/jobtypes/performance/performance.sh @@ -0,0 +1,218 @@ +#!/usr/bin/env bash +# +# This file is part of the Moodle Continuous Integration Project. +# +# Moodle is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Moodle is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Moodle. If not, see . + +# Performance job type functions. + +# Performance needed variables to go to the env file. +function performance_to_env_file() { + local env=( + DBTYPE + DBTAG + DBHOST + DBNAME + DBUSER + DBPASS + DBCOLLATION + DBREPLICAS + DBHOST_DBREPLICA + WEBSERVER + MOODLE_CONFIG + ) + echo "${env[@]}" +} + +# Performance information to be added to the summary. +function performance_to_summary() { + echo "== Moodle branch (version.php): ${MOODLE_BRANCH}" + echo "== PHP version: ${PHP_VERSION}" + echo "== DBTYPE: ${DBTYPE}" + echo "== DBTAG: ${DBTAG}" + echo "== DBREPLICAS: ${DBREPLICAS}" + echo "== MOODLE_CONFIG: ${MOODLE_CONFIG}" + echo "== PLUGINSTOINSTALL: ${PLUGINSTOINSTALL}" +} + +# This job type defines the following env variables +function performance_env() { + env=( + RUNCOUNT + EXITCODE + ) + echo "${env[@]}" +} + +# Performance needed modules. Note that the order is important. +function performance_modules() { + local modules=( + env + summary + docker + docker-logs + git + browser + plugins + docker-database + docker-php + moodle-config + moodle-core-copy + docker-healthy + docker-summary + ) + echo "${modules[@]}" +} + +# Performance job type checks. +function performance_check() { + # Check all module dependencies. + verify_modules $(performance_modules) + + # These env variables must be set for the job to work. + verify_env UUID WORKSPACE SHAREDDIR ENVIROPATH WEBSERVER GOOD_COMMIT BAD_COMMIT +} + +# Performance job type init. +function performance_config() { + + EXITCODE=0 + +} + +# Performance job type setup. +function performance_setup() { + # If both GOOD_COMMIT and BAD_COMMIT are not set, we are going to run a normal session. + # (for bisect sessions we don't have to setup the environment). + if [[ -z "${GOOD_COMMIT}" ]] && [[ -z "${BAD_COMMIT}" ]]; then + performance_setup_normal + fi +} + +# Performance job type setup for normal mode. +function performance_setup_normal() { + + # Create an empty timing file. + touch "${SHAREDDIR}"/timing.json + + # Clone the moodle-performance-comparison repository. + git clone https://github.com/moodlehq/moodle-performance-comparison.git + + # Set up the environment. + cd moodle-performance-comparison + composer install + + # Pull the JMeter Docker image. + docker pull justb4/jmeter + + # Run the JMeter container. + docker run -d --name jmeter-container justb4/jmeter + + # Init the Performance site. + echo + echo ">>> startsection Initialising Performance environment at $(date)<<<" + echo "============================================================================" + local initcmd + performance_initcmd initcmd # By nameref. + + echo "Running: ${initcmd[*]}" + + docker exec -t -u www-data "${WEBSERVER}" "${initcmd[@]}" + echo "============================================================================" + echo ">>> stopsection <<<" +} + +# Returns (by nameref) an array with the command needed to init the Performance site. +function performance_initcmd() { + local -n cmd=$1 + # We need to determine the init suite to use. + local initsuite="" + + + # Build the complete init command. + cmd=( + php admin/cli/install_database.php --agree-license --fullname="Moodle Performance Test" --shortname="moodle" --adminuser=admin --adminpass=adminpass -- + ) +} + +#function performance_datacmd() { +# +#} + +# Performance job type run. +function performance_run() { + performance_run_normal +} + +# PHPUnit job tye run for normal mode. +function performance_run_normal() { # Run the job type. + echo + if [[ RUNCOUNT -gt 1 ]]; then + echo ">>> startsection Starting ${RUNCOUNT} Performance main runs at $(date) <<<" + else + echo ">>> startsection Starting Performance main run at $(date) <<<" + fi + echo "============================================================================" + + # Calculate the command to run. The function will return the command in the passed array. + local cmd= + performance_main_command cmd + + echo "Running: ${cmd[*]}" + + # Run the command "RUNCOUNT" times. + local iter=1 + while [[ ${iter} -le ${RUNCOUNT} ]]; do + echo + echo ">>> Performance run ${iter} at $(date) <<<" + docker exec -t -u www-data "${WEBSERVER}" "${cmd[@]}" + EXITCODE=$((EXITCODE + $?)) + iter=$((iter+1)) + done + + echo "============================================================================" + echo "== Date: $(date)" + echo "== Main run exit code: ${EXITCODE}" + echo "============================================================================" + echo ">>> stopsection <<<" + + # If the main run passed, we are done. + if [[ "${EXITCODE}" -eq 0 ]]; then + return + fi +} + +# Performance job type teardown. +function performance_teardown() { + # Need to copy the updated timing file back to the workspace. + cp "${SHAREDDIR}"/timing.json "${timingpath}" +} + +# Calculate the command to run for Performance main execution, +# returning it in the passed array parameter. +# Parameters: +# $1: The array to store the command. +function performance_main_command() { + local -n _cmd=$1 # Return by nameref. + + # Let's build the complete perf command for the 1st (parallel) run. + _cmd=( + php admin/tool/perf/cli/run.php + ) + + # Add the options and profile. + _cmd+=("${options[@]}") + _cmd+=("${profile[@]}") +} + diff --git a/runner/main/modules/docker-php/config.template.php b/runner/main/modules/docker-php/config.template.php index b1fb4f2..0bd7fd0 100644 --- a/runner/main/modules/docker-php/config.template.php +++ b/runner/main/modules/docker-php/config.template.php @@ -58,7 +58,7 @@ define('TOOL_LANGIMPORT_REMOTE_TESTS', true); } -$CFG->wwwroot = 'http://host.name'; +$CFG->wwwroot = getenv('MOODLE_WWWROOT') ?: 'http://host.name'; $CFG->dataroot = '/var/www/moodledata'; $CFG->admin = 'admin'; $CFG->directorypermissions = 0777;