How Do You Generate a Load for Your Nginx or Apache Web Server?

Problem scenario
You set up a load balancing mechanism for your Nginx or Apache web server.  You want to test it and set up an artificial load of traffic.  You want to generate a significant amount of traffic to test the HTTP load balancing mechanism.  How do you do this?

Solution
Here are three scripts that can, by themselves, download a web page 100 times when they execute.  The languages are Bash, Python (but the OS must be Linux), and PowerShell.  So if you have a Windows workstation and Linux computer, you should be able to get some traffic for your web page.  You may want to modify the web page to be larger (with more text).  You may want to modify the scripts below to change the "100" to "1000" for a longer-running script.  The load that these scripts will place will be a longer duration if you change the "100" in them to "1000".  A fourth suggestion would be to use Gatling.

Here is a bash script that will download a web page 100 times (just replace 10.10.0.1 with the URL you want to download):

#!/bin/bash 
function callit {
   curl 'http://10.10.0.1'
}
i="0" 
while [ $i -lt 100 ]
do
  callit
  i=$[$i+1]
done
echo "it is done"

Here is a Python script that will work on a Linux server; it will download a web page 100 times (just replace 10.10.0.1 with the URL you want to download):

import os
i=0
while (i < 100):
  os.system("curl http://10.10.0.1 >> delete")
  i = i + 1

Here is a PowerShell script to download the web page (you can replace this with the URL http://10.10.0.1 to your own website) 100 times:

function dwnld-webpage {
   $website = Invoke-WebRequest -URI "http://10.10.0.1"
   echo $website.content >> delete.txt
}
$i=0
while ($i -lt 100) {
  dwnld-webpage
  $i++
} 

You may want to try Gatling too.

Leave a comment

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