In last post, I have mentioned that I migrate my website from sharing hosting to Amazon EC2 cloud. I spent some time on learning Linux, Apache2 and MySQL configurations. After the website was up, I thought everything was perfect, until I got a warning email from Amazon CloudWatch. The warning email notified me that CPU usage was greater than 75%. And my Amazon EC2 server was getting slower and slower. The worse case was that it hanged for several times.

Check log files to see what happened on my webserver

To solve the problem, I start to check my apache2 log file first. Usually, the apache2 log files are located in:

/var/log/apache2/access.log
/var/log/apache2/error.log

In the log file, I find there are numbers of suspicious POST direct request from one IP address. That IP address continues sending POST requests to my wordpress wp-login.php file every 30 seconds. I guess it is an attack, which try to login my wordpress with different username and password. After some research online, I solve this attack problems and you can check my next post about how to avoid wp-login.php brute force attack.

Performance Issues of WordPress in Amazon EC2

The continuous POST requests make my Amazon EC2 server getting slower and slower. It also exposes the performance issues of WordPress at the same time. Therefore, I decide to do some performance testing on my Amazon EC2 server with WordPress.

My testing case is sending a number of requests to my website simultaneously, and checking how many concurrent request my Amazon EC2 server can handle. In my testing suite, there are 3 script files. Please make sure that putting all the files in the same folder when you are running the test.

First, I create a php script, overloading.php, which will send request to my website continuously. In this script, it will send a request to my website address. After getting the response, it will send a new request. To avoid the cache which can affect the test result, I append a timer parameter in the end of request address. Here is the code:

<?php
	$searchULR = "http://www.yahoo.com?t=".time();
	echo $searchULR."\n";
	$reponseInfo = null;
	do {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $searchULR);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, false);
		$forumStr = curl_exec($ch);
		
		$reponseInfo = curl_getinfo($ch);

		echo $reponseInfo&#91;'http_code'&#93;.":".$reponseInfo&#91;'total_time'&#93;."\n";
		sleep(1); //wait for 1 sec
	} while(!empty($reponseInfo) && $reponseInfo&#91;'http_code'&#93; == 200)
?>

I create a windows batch file, start.bat, which will start running a number of this PHP script processes according to my specified number. To run 1 testing process, you can open a windows command line and run the following command:

start.bat 1

Running Testing
Running Testing

To run 10 testing process, use the following command:

start.bat 10

Running 10 Testing
Running 10 Testing

The above example starts 10 php process to send request to your server continuously. In my example, it will send the request to http://www.yahoo.com/. You can find out that you will get the response in 2 seconds. To run testing on your server, you can replace request address with your website address in PHP script.

To stop the testing easily, I create another windows batch file, which will close all testing process. Running following command to stop the testing process.

stop.bat

How can this testing suite help?

Now let’s talk about the performance issues in WordPress with Amazon EC2. My Amazon EC2 is a m1.small instance, which provides 1 CPU and 1.7G memory. I install all service in one instance, including ssh, a proxy server (which is not on when I am running the test), apache2 server and mysql server. Let’s run 1 testing process and check my server performance in ssh terminal.

Running 1 Testing on My Server
Running 1 Testing on My Server

Running 1 Testing on My Server Performance
Running 1 Testing on My Server Performance

Let’s run 10 testing process and check my server performance in ssh terminal.

Running 10 Testing on My Server Performance
Running 10 Testing on My Server Performance

The total CUP usage is 38.6%. There are 59M memory left (out of 1.7G). Obviously, my server is almost down because lacking of memory. I guess it will hang over after I add another 10 testing processes. But the performance is good enough, because I can just switch my Amazon EC2 server from m1.small to m1.medium. The memory problem will be solved. In m1.medium server, there are 3.75G memory available. But in my second server, the situation is quite worse.

The second server is serving one big site of mine. It is a m1.medium server, bigger memory than m1.small, but the same CPU. Let’s see the testing result:

CPU Usage Problem in WordPress with Amazon EC2
CPU Usage Problem in WordPress with Amazon EC2

CPU usage is around 95%. This is the real problem. The question is here, why cpu usage is 38.6% in m1.small server, but it is 95% in m1.medium. It is definitely not the configuration problem, because I am using the same configuration in both Amazon EC2 server. The only different part is the wordpress website. In the second website, it has many wordpress plugins installed, twice more than my first website. The problem is WordPress performance.

Using Xdebug Profiler to Check WordPress Performance

I am using Xdebug extension for PHP, to generate profiling data for the wordpress script. After getting the data and opening in the WinCacheGrind, I find most of loading time is spent in “require_once::wp-load.php” and “require_once::template-loader.php“.

WordPress Performance Issue
WordPress Performance Issue

wp-load.php file will require wp-config.php, and wp-config.php will require wp-settings.php. In wp-settings.php, the one of the most time consuming function is wp_get_active_and_valid_plugins, which will call php function file_exists more than 30 times. Another time consuming function is do_action. I guess it will call plugin functions one by one.

WordPress Performance Issue in plugin
WordPress Performance Issue in plugin

WordPress Performance Issue in plugin 2
WordPress Performance Issue in plugin 2

In template-loader.php, most of time is spent in my WordPress template index.php, which will call dynamic_sidebar function and get_header function.
WordPress Performance Issue in Template
WordPress Performance Issue in Template

WordPress Performance Issue in Template 2
WordPress Performance Issue in Template 2

For different WordPress templates, the performance may be quite different. But for plugins, I think they will be main performance issue in all common WordPress website.

Get Full Amazon EC2 WordPress Website Testing Suite under $5.99


You will get whole my test suite to test your website. It is suitable to testing website performance such as website responding time. It is a light tool to add overload on your website server and observer your server performance such as CPU usage/CPU utilization, memory usage/memory utilization, especially for beginners to test Amazon EC2 server with WordPress website. In the package:

  • overloading.php: PHP script to send request to website continuously
  • start.bat: Windows bat file to start testing with given number of processes
  • stop.bat: Windows bat file to stop testing
  • sendfile.bat: Windows bat file to send file to your linux server with pscp.exe; replace the password, username and ip address with your own linux server information
  • pscp.exe
  • readme.txt
Previous PostNext Post

Leave a Reply

Your email address will not be published. Required fields are marked *