@@ -64,6 +64,7 @@ function performance_modules() {
6464 local modules=(
6565 env
6666 summary
67+ moodle-branch
6768 docker
6869 docker-logs
6970 git
@@ -94,8 +95,11 @@ function performance_config() {
9495 EXITCODE=0
9596
9697 export MOODLE_WWWROOT=" http://${WEBSERVER} "
97- export SITESIZE=" ${SITESIZE:- S } "
98+ export SITESIZE=" ${SITESIZE:- XS } "
9899 export COURSENAME=" performance_course"
100+
101+ # Default target file (relative to WORKSPACE) where rundata.json will be stored.
102+ export TARGET_FILE=" ${TARGET_FILE:- storage/ performance/ ${MOODLE_BRANCH} / rundata.json} "
99103}
100104
101105# Performance job type setup.
@@ -116,15 +120,43 @@ function performance_setup_normal() {
116120 local initcmd
117121 performance_initcmd initcmd # By nameref.
118122 echo " Running: ${initcmd[*]} "
123+
124+ plugin_repo=" https://github.com/moodlehq/moodle-local_performancetool"
125+ dest=" /var/www/html/local/performancetool"
126+
127+ echo " Installing moodle-local_performancetool plugin into ${dest} "
128+
129+ # Ensure host shared directories exist and are writable so plugin can save files.
130+ mkdir -p " ${SHAREDDIR} /planfiles" " ${SHAREDDIR} /output/logs" " ${SHAREDDIR} /output/runs"
131+ chmod -R 2777 " ${SHAREDDIR} " || true
132+
133+ # Clone the performance data generator plugin inside the container.
134+ docker exec " ${WEBSERVER} " sh -c " git clone --depth 1 ${plugin_repo} ${dest} "
135+
119136 docker exec -t -u www-data " ${WEBSERVER} " " ${initcmd[@]} "
120137
121- # Copy the site normalisation script and execute it.
122- echo " Copying and executing the site normalisation script"
123- docker cp " ${BASEDIR} /jobtypes/performance/normalise_site.php" " ${WEBSERVER} :/var/www/html/normalise_site.php"
124- docker exec -t -u www-data " ${WEBSERVER} " php /var/www/html/normalise_site.php
138+ # Execute the script inside the container as www-data
139+ docker exec -t -u www-data " ${WEBSERVER} " php " ${dest} "
140+ exec_status=$?
125141
126- echo " Creating test data"
127- performance_generate_test_data
142+ if [[ $exec_status -ne 0 ]]; then
143+ echo " Error: php returned exit ${exec_status} when executing ${dest} "
144+ exit $exec_status
145+ fi
146+ performance_perftoolcmd perftoolcmd
147+ docker exec -t -u www-data " ${WEBSERVER} " " ${perftoolcmd[@]} "
148+
149+ # Copy generated plan files (jmx, csv) from container to host-shared dir.
150+ echo " Copying generated plan files from container to ${SHAREDDIR} /planfiles"
151+
152+ docker exec -u root " ${WEBSERVER} " bash -lc " \
153+ mkdir -p /shared/planfiles && \
154+ cp -a /var/www/html/local/performancetool/planfiles/. /shared/planfiles/ || true && \
155+ chown -R www-data:www-data /shared/planfiles || true"
156+
157+ chmod -R 2777 " ${SHAREDDIR} /planfiles" || true
158+ echo " Files in ${SHAREDDIR} /planfiles:"
159+ ls -la " ${SHAREDDIR} /planfiles" || true
128160
129161 echo " ============================================================================"
130162 echo " >>> stopsection <<<"
@@ -148,71 +180,49 @@ function performance_initcmd() {
148180 )
149181}
150182
151- function performance_generate_test_data() {
152- local phpcmd=" php"
153-
154- # Generate Test Site.
155- local testsitecmd
156- perfomance_testsite_generator_command testsitecmd # By nameref.
157- echo " Running: ${testsitecmd[*]} "
158- docker exec -t -u www-data " ${WEBSERVER} " " ${testsitecmd[@]} "
159-
160- # Generate the test plan files and capture the output
161- local testplancmd
162- performance_testplan_generator_command testplancmd # By nameref.
163- echo " Running: docker exec -t -u www-data " ${WEBSERVER} " " ${testplancmd[@]} " "
164- testplanfiles=$( docker exec -t -u www-data " ${WEBSERVER} " " ${testplancmd[@]} " )
165-
166- # Display the captured output
167- echo " Captured Output:"
168- echo " ${testplanfiles} "
169- echo " ${SHAREDDIR} "
170-
171- # Ensure the directory exists and is writable
172- mkdir -p " ${SHAREDDIR} /planfiles"
173- mkdir -p " ${SHAREDDIR} /output/logs"
174- mkdir -p " ${SHAREDDIR} /output/runs"
175-
176- chmod -R 2777 " ${SHAREDDIR} "
177-
178- # Extract URLs and download files to ${SHAREDDIR}
179- urls=$( echo " ${testplanfiles} " | grep -oP ' http://[^ ]+' )
180- for url in ${urls} ; do
181- # Trim any whitespace or newline characters from the URL
182- url=$( echo " ${url} " | tr -d ' \r\n' )
183- # Extract the filename from the URL
184- filename=$( basename " ${url} " )
185- echo " Downloading: ${url} to ${SHAREDDIR} /${filename} "
186- docker exec -t -u www-data " ${WEBSERVER} " curl -o " /shared/planfiles/${filename} " " ${url} "
187- done
183+ # Returns (by nameref) an array with the command needed to init the Performance site.
184+ function performance_perftoolcmd() {
185+ local -n cmd=$1
186+ # We need to determine the init suite to use.
187+ local initsuite=" "
188+
189+ # Build the complete init command.
190+ cmd=(
191+ php local/performancetool/generate_test_data.php \
192+ --size=" ${SITESIZE} " \
193+ --planfilespath=" /shared" \
194+ --quiet=" false"
195+ )
188196}
189197
190198# Performance job type run.
191199function performance_run() {
192- echo
193- if [[ RUNCOUNT -gt 1 ]]; then
194- echo " >>> startsection Starting ${RUNCOUNT} Performance main runs at $( date) <<<"
195- else
196- echo " >>> startsection Starting Performance main run at $( date) <<<"
197- fi
200+
201+ echo " >>> startsection Starting Performance main run at $( date) <<<"
198202 echo " ============================================================================"
199203
200204 datestring=` date ' +%Y%m%d%H%M' `
201205 # Get the plan file name.
202- testplanfile=` ls " ${SHAREDDIR} " /planfiles/* .jmx | head -1 | sed " s@${SHAREDDIR} @/shared@" `
203- testusersfile=` ls " ${SHAREDDIR} " /planfiles/* .csv | head -1 | sed " s@${SHAREDDIR} @/shared@" `
206+ testplanfile=` ls " ${SHAREDDIR} " /* .jmx | head -1 | sed " s@${SHAREDDIR} @/shared@" `
207+ echo " Using test plan file: ${testplanfile} "
208+ testusersfile=` ls " ${SHAREDDIR} " /* .csv | head -1 | sed " s@${SHAREDDIR} @/shared@" `
209+ echo " Using test users file: ${testusersfile} "
204210 group=" ${MOODLE_BRANCH} "
205211 description=" ${GIT_COMMIT} "
206212 siteversion=" "
207213 sitebranch=" ${MOODLE_BRANCH} "
208214 sitecommit=" ${GIT_COMMIT} "
209215 runoutput=" ${SHAREDDIR} /output/logs/run.log"
210216
217+ # Ensure run log directory exists and is writable so 'tee' can create the file.
218+ mkdir -p " $( dirname " ${runoutput} " ) "
219+ chmod -R 2777 " ${SHAREDDIR} /output/logs" || true
220+
211221 # Calculate the command to run. The function will return the command in the passed array.
212222 local jmeterruncmd=
213223 performance_main_command jmeterruncmd # By nameref.
214224
215- echo " Running: ${jmeterruncmd[*]} "
225+ echo " Running performance command : ${jmeterruncmd[*]} "
216226 echo " >>> Performance run at $( date) <<<"
217227 local dockerrunargs=
218228 docker-jmeter_run_args dockerrunargs # By nameref
@@ -241,21 +251,39 @@ function performance_run() {
241251# Performance job type teardown.
242252function performance_teardown() {
243253 DATADIR=" ${SHAREDDIR} /output/runs"
254+
255+ # Ensure DATADIR exists before copying format_rundata.php.
256+ mkdir -p " ${DATADIR} "
257+
244258 cp " ${BASEDIR} /jobtypes/performance/format_rundata.php" " ${DATADIR} /format_rundata.php"
259+
260+ # Check if rundata.php exists (generated by JMeter run).
261+ if [[ ! -f " ${DATADIR} /rundata.php" ]]; then
262+ echo " Error: rundata.php not found in ${DATADIR} "
263+ return 1
264+ fi
265+
245266 docker run \
246267 -v " ${DATADIR} :/shared" \
247268 -w /shared \
248269 php:8.3-cli \
249- php /shared/format_rundata.php rundata.php
270+ php " /shared/format_rundata.php" " rundata.php"
250271
251272 echo " Storing data with a git commit of '${GIT_COMMIT} '"
252273
253- # We use the storage directory to store data for long term comparison.
254- TARGETDIR=` dirname " ${TARGET_FILE} " `
255- mkdir -p " ${WORKSPACE} /${TARGETDIR} "
256- cp -rf " ${DATADIR} /rundata.json" " ${TARGET_FILE} "
274+ # Resolve absolute target path (use WORKSPACE for relative TARGET_FILE)
275+ if [[ " ${TARGET_FILE} " = /* ]]; then
276+ targetpath=" ${TARGET_FILE} "
277+ else
278+ targetpath=" ${WORKSPACE} /${TARGET_FILE} "
279+ fi
280+
281+ targetdir=" $( dirname " ${targetpath} " ) "
282+ mkdir -p " ${targetdir} "
283+ cp -f " ${DATADIR} /rundata.json" " ${targetpath} "
257284}
258285
286+
259287# Calculate the command to run for Performance main execution,
260288# returning it in the passed array parameter.
261289# Parameters:
@@ -299,38 +327,3 @@ function perfomance_testsite_generator_command() {
299327 --filesizelimit=" 1000"
300328 )
301329}
302-
303- function performance_testplan_generator_command() {
304- local -n _cmd=$1 # Return by nameref.
305-
306- case " ${SITESIZE} " in
307- ' XS' )
308- targetcourse=' testcourse_3'
309- ;;
310- ' S' )
311- targetcourse=' testcourse_12'
312- ;;
313- ' M' )
314- targetcourse=' testcourse_73'
315- ;;
316- ' L' )
317- targetcourse=' testcourse_277'
318- ;;
319- ' XL' )
320- targetcourse=' testcourse_1065'
321- ;;
322- ' XXL' )
323- targetcourse=' testcourse_4177'
324- ;;
325- * )
326- ;;
327- esac
328-
329- # Build the complete perf command for the run.
330- _cmd=(
331- php admin/tool/generator/cli/maketestplan.php \
332- --size=" ${SITESIZE} " \
333- --shortname=" ${targetcourse} " \
334- --bypasscheck
335- )
336- }
0 commit comments