Experiments in Performance Optimization

Experiments in Performance Optimization

by Dakota Duff -
Number of replies: 4

I was wondering what methods are used to optimize the delivery of Moodle. Moodle runs fine for us on our shared server — we rarely have more than 20 users active at the same time — but as we work with Moodle I want to make sure it stays this snappy.

I've enabled CFG->dbpersist and internal caching, but they really don't seem to make a noticeable difference in our situation.

Two things that do make a very noticeable difference for us are zlib compression and a script I created that minifies all of Moodle's CSS and JS files before delivery. I had worried that these were increasing our server load (it hadn't been a problem, though) so I decided to test it today.

With "performance info" on, I loaded five pages three times each and recorded the Document Size from the FF Web Developer plug-in, the tick count, and the average load. I cleared the cache before each first load to make sure I was getting new data. Here are the results:


Avg Page Size % reduced Avg Ticks % reduced Avg Load % reduced
Raw (no zlib or minification) 358.4 KB - 80 - 1.67 -
zlib (only .php extention) 165.0 KB 54.00% 70 12.00% 1.41 16.00%
zlib + minification 66.2 KB 82.00% 74 7.00% 1.32 21.00%

I expected a big reduction in page size, but I was really surprised that zlib and zlib with minification both out-performed the default setup.

Another thing I have done it to create archive MySQL tables (of "archive" format instead of MyISAM). I have a weekly script that dumps all of the records from mdl_log and various grade history tables into the archive tables. Then I set Moodle to only keep records in its tables for six months — this way I keep the active table fairly clean, but still have the records if I need them. We've only used Moodle for just over six months, but just to give you an idea of the space savings, our mdl_log table has 387,820 rows which take up 35 MB of space and archive_log has 400,711 rows and takes up 7.8 MB (about 1/5 the original data size).

The same script that dumps all our log and grade histories into archive tables also checks all of our tables for excessive un-reclaimed space (Data_free) and runs OPTIMIZE TABLE if it's beyond a certain threshold.

I also tried something only as a test that didn't save nearly as much space as I thought it would. In my Moodle install nearly every ID field is a BIGINT(10). There are also a number of places where large VARCHAR fields are used for mostly replicated data. I made big changes to the Moodle install code so that the data type was more appropriately scaled for the purpose, used ENUMs in other areas were possible, and changed all VARCHAR fields to CHAR. This resulted in usually smaller table sizes, smaller indexes, and more fixed-length tables — I thought it would lead to a nice boost in speed. I didn't go all-out with the installation (perhaps it would have made a bigger difference at a certain threshold) but at least with a few dummy courses dumped in and and about 2000 user accounts the speed difference seemed non-existant.

I'm curious what other non-standard optimization methods users have employed and how they've worked.

Average of ratings: -
In reply to Dakota Duff

Re: Experiments in Performance Optimization

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The one thing that you have not mentioned that makes a huge difference is to use a PHP accelerator.

Interesting what you say about JS minification. We have been talking about making Moodle do that automatically, although there are some other things that have to be cleaned up before that is possible. It looks like it would be worth making the effort to do that.
In reply to Tim Hunt

Re: Experiments in Performance Optimization

by Dakota Duff -

Well, for what it's worth, here's the script that I use. I've cleaned it up quite a bit and done a little optimizing of code, so it's quite possible that it would have even less server drag than indicated above.

This does require PHP5 and might need some modifications to work on a non-Unix server (like directory separators). But it is already automatic and there's nothing that needs to be cleaned up in order for it to work.

To use it you just need to add the file as PHP's auto_prepend_file directive and make sure you have the cache_path and paths to jsmin and cssmin set correctly. Then you just modify your htaccess file so that .css and .js files are handled by PHP and you're off and running.

I'd love to use a PHP accelerator, but my host doesn't offer and of the three that Moodle has built in support for.

In reply to Dakota Duff

Re: Experiments in Performance Optimization

by Ron Meske -
Picture of Particularly helpful Moodlers
We tried using zlib compression and ran into many problems with IE 6, especially with SCORM courses. The issue seems to be entirely with IE 6 as other PHP applications that we run were also problematic. Turning off zlib compression solved the problem for all IE 6 users.

Did you try using minification without zlib compression? That would be interesting to see. We had tried it for just the SCORM api to solve the api not found issue and saw the potential for a large improvement there.

Personally, what I don't want to see happen to Moodle is what has happened with so many desktop applications. The faster the processor gets, the cheaper storage and RAM gets, the larger and more bloated the applications get. There is not much effort put towards optimizing applications, instead just buy more memory or storage.

From what I have seen so far it looks like Moodle is only including the files it needs for the current page, so minimizing the CSS and JavaScript would seem to be the next step for optimizing the page size.
In reply to Ron Meske

Re: Experiments in Performance Optimization

by Dakota Duff -

Just checked that…

Minification by itself cut 75KB off a course main page.

Not sure what it does to server load, but probably worth your troubles if you get a lot of traffic.