Hi, Gregorio!
First, I want to say that you are doing an amazing job analyzing the performance of themes in Moodle, and it would be very helpful if you could share it with the community.
A great solution is to use benchmark tools that are specifically designed to measure the performance of web pages. Tools like GTmetrix or PageSpeed Insights from Google can be very useful because they analyze the page load time and provide detailed reports on what can be optimized, but many of these are quite limited.
However, if you are looking for something more specific for Moodle, I'll suggest an approach using PHP.
First, log in as a student in Moodle, copy the generated cookie to have a logged-in user, and then I asked ChatGPT the following:
I have the list of themes: ```$themes = ["boost", "adaptable", "moove", "academi", "degrade",
"snap", "trema", "boost_union", "boost_magnific"];´´´
and I need a PHP script that executes a CURL in "shell_exec", passing -H
Cookie.
Calculate the execution time of the shell_exec and save it to a
.CSV
file.
I took the first list from: https://moodle.org/plugins/index.php/?q=type:theme%20moodle-version:4.5%20sort-by:fans
And it generated the following SCRIPT:
<?php
// List of themes
$themes = [
"boost",
"adaptable",
"moove",
"academi",
"degrade",
"snap",
"trema",
"boost_union",
"boost_magnific"
];
// URL for curl request (replace with your actual URL)
$url = "https://example.com"; // Modify this URL accordingly
// Initialize the CSV file for logging
$csvFile = 'execution_times.csv';
// Open the CSV file in append mode
$file = fopen($csvFile, 'a');
// Write header if the file is empty
if (filesize($csvFile) === 0) {
fputcsv($file, ['Theme', 'Execution Time (seconds)']);
}
// Loop through the themes
foreach ($themes as $theme) {
// Change moodle theme
set_config("theme", $theme);
// Build the curl command with cookie header (replace 'your_cookie_value' with the actual cookie)
$command = "curl -H 'Cookie: your_cookie_value' '$url'";
// Get the start time
$startTime = microtime(true);
// Execute the shell command and get the output
$output = shell_exec($command);
// Get the end time and calculate execution time
$endTime = microtime(true);
$executionTime = $endTime - $startTime;
// Write the theme and execution time to the CSV file
fputcsv($file, [$theme, $executionTime]);
}
// Close the file
fclose($file);
echo "Execution times have been saved to $csvFile.\n";
This script measures the time between when it starts execution and when it finishes.
Then I asked to add more things:
Now add require_once "config.php"; and list the courses from
mdl_course, then loop through the courses loading the times for the courses
in the URL: https://moodle/course/view.php?id={$course_id}.
And it generated the following code:
<?php
// Include Moodle configuration file
require_once "config.php"; // Make sure the path to config.php is correct
// List of themes (same as before)
$themes = [
"boost",
"adaptable",
"moove",
"academi",
"degrade",
"snap",
"trema",
"boost_union",
"boost_magnific"
];
// Initialize the CSV file for logging
$csvFile = 'execution_times.csv';
// Open the CSV file in append mode
$file = fopen($csvFile, 'a');
// Write header if the file is empty
if (filesize($csvFile) === 0) {
fputcsv($file, ['Course ID', 'Execution Time (seconds)']);
}
// Fetch all courses from the mdl_course table
$courses = $DB->get_records('mdl_course'); // Using Moodle's database API to fetch courses
// Loop through the themes
foreach ($themes as $theme) {
// Change moodle theme
set_config("theme", $theme);
// Loop through each course
foreach ($courses as $course) {
// Get the course ID
$course_id = $course->id;
// Build the URL for the course page with the theme applied (assuming theme parameter is valid)
$url = "https://moodle/course/view.php?id={$course_id}"; // Modify URL accordingly
// Build the curl command with the cookie header (replace 'your_cookie_value' with actual cookie)
$command = "curl -H 'Cookie: your_cookie_value' '$url'";
// Get the start time
$startTime = microtime(true);
// Execute the shell command and get the output
$output = shell_exec($command);
// Get the end time and calculate execution time
$endTime = microtime(true);
$executionTime = $endTime - $startTime;
// Write the course ID and execution time to the CSV file
fputcsv($file, [$course_id, $executionTime]);
}
}
// Close the file
fclose($file);
echo "Execution times for courses have been saved to $csvFile.\n";
And then I asked to create a bar chart:
Load the data from
execution_times.csv and create a chart.js with the data separated by
theme.
Now you can adapt it to load pages from different sections, such as the course, forum, or SCORM packages.
Additionally, you can include more details in the report, such as:
- How long Moodle takes to load the different components of a page.
- Which resources are taking longer to load.
- Optimization suggestions based on the measured times.
I hope this helps speed up your analysis process! Once you start collecting data automatically, it becomes much easier to generate complete and accurate reports without wasting time. And of course, if you need anything else, don't hesitate to reach out! We are always here to help! ✨
Eduardo Kraus
Innovation and new products