How I Load Tested Moodle

I got a task: check how many concurrent users a moodle-based site can handle.

Or more precisely: Can Moodle handle 1000 concurrent user sessions

Load testing is a pretty popular topic these days, since every site owner wants to know the limits of their setup, and ideally push those limits further.

The scenario was:

  1. Go to the site.
  2. Log in.
  3. Go to the quiz page
  4. Click the Attempt quiz button
  5. Fill out the form and submit it.
  6. Confirm your answers.

I used LoadStorm as the traffic generator.

A couple of things to keep in mind while running the scenario:

  1. A user/student can’t fill out the same quiz multiple times at once. So you need to generate the right number of users and import them into moodle. You also need to add the same users to LoadStorm, just the files will be different.
  2. The button in step four can’t be triggered by a click on the page. It’s a form submit with the value Attempt quiz now. If the user/student already attempted the quiz, that value changes to re-attempt quiz.

First, let’s generate a csv file to import users into moodle’s database. You can do this with a spreadsheet tool like Excel, Libre Office Calc, or a GoogleDrive table. The problem is I couldn’t get it to generate unique email addresses like [email protected], [email protected][email protected].

So I used a small bash loop instead.

First, create the file and add the required fields:

echo `username,password,firstname,lastname,email` >> mdl_users.csv

Then fill in this file with the needed data:

i=0 && while [ $i -le 1000 ]; do echo `user$i,password$i,user$i,user$i,user$i@tempmail.com` >> mdl_users.csv; let `i = $i + 1`; done

Don’t forget to back up the moodle database

Now let’s import the users into moodle. Log in to the site with an admin account. Then, as shown in the screenshot, expand Site administration -> Users -> Accounts
Screenshot from 2014-08-12 13:58:01

Select Upload Users and you’ll get a form like this. You can just drag the csv file into the right area:
Screenshot from 2014-08-12 14:00:00

The rest is self-explanatory, I won’t dwell on it.

It’s also obvious why the users don’t have access to the quiz - they’re simply not in the list of allowed users.

To fix that, in the administration section go to Cource administration -> Users and click Enrollment methods:
Screenshot from 2014-08-12 14:04:07

Unfortunately I couldn’t find a way to do this with a mysql query. So I added all 1000 users to the course by hand. In the column under Edit there’s a small person icon with a + sign. Click it:
Screenshot from 2014-08-12 14:05:39

In the new window you’ll see two fields. The right one will say it can’t display all users and suggest using search. I searched for users using this pattern:
0. user100
0.1. user10
0.2. user1
1.0. user20
1.1. user2
2.0. user30
2.1. user3
and so on.

After that, let’s move on to LoadStorm. First, let’s generate a file with logins and passwords:

echo `username,password` >> mdl_load_storm_users.csv

Then fill in this file with the needed data:

i=0 && while [ $i -le 1000 ]; do echo `user$i,password$i` >> mdl_load_storm_users.csv; let `i = $i + 1`; done

Here too you can use the spreadsheet tools I mentioned earlier.

Next, create an account in LoadStorm. Log in. Add your server. Verify it. I won’t include screenshots for every step.

In the Form Data Sets section click Upload data and upload the mdl_load_storm_users.csv file, giving it a name in the corresponding field.

Next, create a test plan. In it, create a scenario. On the scenario creation page, select the name of the mdl_load_storm_users.csv file we uploaded from the Form Data set dropdown.

Check the boxes for Download and execute javascript and Download images. Click Save.

The system should open the page with your scenario. At the bottom there will be an empty table, which will fill up with the test steps you create.

To create a step, click Add step. On the new page, select your server from the dropdown and check the Open page option, enter the login page address in the text field, and click Save:
/login/index.php

As a result you’ll see a page showing how LoadStorm sees your site:
Screenshot from 2014-08-12 14:31:30

Click New step. In the new window select Submit form and set it up like this:
Screenshot from 2014-08-12 14:32:53

Click New step. Select Open page and enter the address of the quiz you need:
/mod/quiz/view.php?id=1

Click New step. Select Submit form and set it up like this:
Screenshot from 2014-08-12 14:35:30

At this point I ran a script on the server that clears the table with quiz attempts, because the form submit value changes after the first attempt and LoadStorm can no longer handle this form. The script looks like this:

while true; do echo `delete from **moodle**.**mdl_**quiz_attempts where quiz = **1**;` |mysql; echo `Flushed`; sleep 5; done

where:
moodle - the database name
mdl_ - the table prefix
1 - the id of the quiz you need.

you can get the list of quizzes with this sql query:

select * from moodle1.mdl_quiz \mG;

or

select id,name,intro from moodle1.mdl_quiz;

Click New step. You’ll get the quiz form with a bunch of fields. If you don’t - select Submit form. I tried checking all the boxes, but for some reason the form gets submitted empty. Don’t bother with that, just move on to the next step. Scroll down the page, find and check the following item:
Screenshot from 2014-08-12 14:42:17

Click New step. Select Submit form and set it up like this:
Screenshot from 2014-08-12 14:43:09

When you’re done, click Save. The scenario is ready. Time to start testing.

On the scenarios page you can click Run steps to make sure all the steps work. You’ll get this output:
Screenshot from 2014-08-12 14:45:44

If something’s off - make sure the cleanup script for the quiz attempts table is running on the server.

That covers my approach to load testing Moodle. Figuring out how to actually run the test is easy enough, since the interface is pretty friendly.