Apache Ab Example

broken image


If you have access to a Mac or Linux server, chances are you may already have a really simple http load generating tool installed called Apache Bench, or ab. If you are on windows and have Apache installed, you may also have ab.exe in your apache/bin folder.

Suppose we want to see how fast Yahoo can handle 100 requests, with a maximum of 10 requests running concurrently:

This example shows how to receive an access token from Facebook. It should work similarly with other OAuth 2.0 compatible implementations. See OAuth Wiki for the list of current OAuth 2.0 server implementations. You can also run a sample client app available at: Client Tutorial or go through the following steps: Add Oltu client to your classpath. Apache Bench httpd and IHS ship with a cool little command line utility called Apache Bench (ab). At its simplest, you pass the number of requests you want to send (-n), at what concurrency (-c) and the URL to benchmark. Ab will return various statistics on the responses (mean, median, max, standard deviation, etc.).

It will then generate output as follows:

As you can see this is very useful information, it returned requests at a rate of 52.94 requests per second, the fastest request was 175ms, the slowest 224ms

So the next time you are tempted to whip out cfloop and GetTickCount to do some benchmarking on a piece of code, give ab a try, it's easy to use, and will yield much more realistic results.

Apache Ab Examples

Because ab supports concurrency, this has two big advantages over cfloop. The main one is that it allows you to test how your code runs concurrently, this can help you identify any possible race conditions, or locking issues. Concurrent requests are also a more natural simulation of load than loops.

Apache Ab Example

Suppose you wanted to test multiple url's concurrently as well? You can do this by creating a shell script, with multiple ab calls. At the end of each line place an & this makes the command run in the background, and lets the next command start execution. You will also want to redirect the output to a file for each url using > filename For example:

The usage info from the ab version installed on my Mac (v2.3) is listed below. As you can see there are many useful options for outputting results, and sending additional data in the request.

Is there a time when you are thinking how to do a server load testing to evaluate your PHP web application performance? This is where you will be using the Apachebench or 'ab' testing tool. This is a very useful open source tool not only to PHP developers but to the entire users of the Apache server. This tool is used to evaluate the execution times of your PHP web application or the response time of your website under 'stressing' conditions. This condition means how your PHP web application will perform under conditions similar to what exactly will be happening on a real web server not just on your local host environment (such as XAMPP). For example in actual website deployment in a production server; it will be subjected to two stressing conditions:

a.) Number of connections to that application – these are the number of visitors to your website which are connected to that application. The higher the number of visitors to that application, the higher also the number of connections. This is a stressing condition because sometimes if your PHP web application in your website is poorly written, it will result to a very poor performance in actual deployment which can slowed down if there are a lot of users in your website.

b.) Number of concurrent users – these are the actual number of users that are using the application at exactly the same time. Of course in a very low traffic website, the concurrent users is almost zero because the probability that two users will exactly hit the application and do the same request is very small. This can be an issue with high traffic websites; it is because the number of concurrent users can slowed down the application and put a signficant load on the web server. Some web host even limits the number of concurrent users (typically those that are originating from a free web hosting scheme).

The good thing with Apachebench is that you measure the performance of a certain web application or website given a stress conditions. The stressing conditions required by Apache bench are two above. What is expected to happen is that a poorly written PHP web application will perform poorly under stressing conditions. This is why performance benchmarks of CMS, PHP frameworks, etc are using ab testing to evaluate the quality of the application.

How to Get Started with AB testing tool in Linux/Ubuntu:

Below are the steps :

1.) In Ubuntu, Apachebench can be installed using the Apache 2 utils. Go to Applications – Accessories – Terminal, at the command prompt type:

2.) After installation, you are now ready to use Apachebench. Lets have an example. Supposing you will need to know how wordpress.org performs when there are 300 connections and 3 concurrent users. First, this is the syntax of ab:

3.) Using the syntax above for ab test, below is the final command to type in the shell prompt:

4.) Below are the results, you should see in your terminal:

Apache Ab Example

Very important interpretation of results:

1.) It means it takes 3722ms for wordpress.org to serve longest request under a stressing conditions of 300 connections and 3 concurrent users. This is the longest response time.

2.) But on the 'average' it will only take wordpress.org to respond within 728ms +- tolerance of 179 ms.

A more powerful and faster website can respond to faster times even with more stressing conditions. If you are using Windows, you can read this Apachebench tutorial using Windows OS.

Apache Ab Cookie Example

Warning: Do not use Apachebench excessively to make enormous request to a web server that you do not own. This is a form of denial of service attack and can put you to jail: https://www.pcworld.com/businesscenter/article/181127/ex_ceo_charged_in_denial_of_service_attack.html





broken image