How to test website speed with curl

It is really worthwhile to read developer reports.

Have you observed a minor slowness in your websites recently, but measuring it with a stopwatch won’t help? If you discover this, you know you need to report it to management, but you don’t want to become involved unless you have actionable information.

After all, a small decrease in speed today might result in a far bigger decrease later. And, with the competition always developing and evolving, your company cannot risk a minor hiccup becoming a major one.

CHECK OUT: Hire Kit: Backend Developer (TechRepublic Premium)

To that end, you should monitor your website speeds so you have the data you need to approach people in authority and say, “We have an issue.”

You may create a simple bash script that gathers these speeds and saves them in a date-stamped file using the curl command. I’ll teach you how to do it exactly.

What you’ll need to test your website’s speed

The only requirements are a working Linux system and a user with sudo access. Linux versions can be desktop or server, and any distribution can be used. I’ll show you how with Ubuntu Server 22.04. If you use a different distribution, you may need to modify the curl install command.

That’s all. Let’s get started on the scripting.

How to Setup Curl

Curl should already be installed on your computer. If not, it’s simple. Log in to your Linux distribution, launch a terminal window, and type the following command:

curl -y sudo apt-get install

Keep in mind that the installation command will differ depending on the distribution, for example, B. sudo dnf install curl -y for RHEL-based distributions.

Now that we have curl installed, we can write our script.

How to Write a Bash Script to Test Web Speed

The script we’re using is actually rather simple to make using the following command:

nano ~/webtest

Paste the following into it:

!/bin/bash

curl -s -w ‘Test website response time for: %{url_effective}\n\nLookup time:\t\t%{time_namelookup}\nConnection time:\t\t%{time_connect}\nTime before transfer:\t % {time_pretransfer}\nStart transfer time:\t%{time_starttransfer}\n\nTotal time:\t\t%{time_total}\n’ -o /dev/null URL > webspeedtest_”$(date)”

Where URL denotes the URL of the site to be examined.

The script is broken down as follows:

  • The search time indicates how long it takes for a request to receive a response.
  • The time it takes to establish a TCP connection to a distant server is recorded as connection time.
  • AppCon time keeps track of how long it takes to handle an SSL communication.
  • The entire time spent on redirect requests is tracked by detour time.
  • The whole time before the file transfer begins is recorded as time before the transfer.
  • The amount of time before the first byte is transferred to a distant server is recorded as the start transmission time.
  • Total time is the amount of time it takes to finish a response.

We pipe the output to the webspeedtest file at the conclusion of the script, which appends the date and time to the end of the filename. This manner, we have not only one file containing the test results for that instance, but also one file for each instance where the test is performed, allowing you to compare the findings.

Save and exit the file.

Use the following command to make the script’s output executable:

chmod u+x ~/webtest

What is the procedure for running the test script?

Enter the following command to perform the test:

./webtest

The script should execute rapidly and generate a file called webspeedtest DATE, where DATE is the date and time of execution. The script’s output looks somewhat like this:

URL is being tested for website response speed.

Search time: 0.120128
Connection time: 0.177519
Time before transmission: 0.177644
Start transmission time: 0.240367

Total time: 0.240540

Where URL is the address that you specified in the script.

How can the test be automated?

We use cron to automate the test so that we don’t have to remember to run it every day. Use the following command to modify your crontab file:

crontab -e

To run the test automatically every morning at 8 a.m., add the following to the end of your crontab file:

0 8 * * * /home/USER/webtest

Where USER represents your Linux username. Make careful to adjust the path if you saved the script in a different directory.

Save and close the crontab file with Ctrl + X.

You may also use the line: to schedule this test to run every hour if necessary.

0 * * * * /home/USER/webtest

Remember to purge these test result files as they accumulate.

A straightforward test for an essential metric

That’s it – an automated speed test script for checking a critical parameter for your websites. Create a script for each site and add a crontab item for each test if you need to test more than one site.

While this will not provide you with all of the metrics you want, it will keep you up to date on a single test that can help you identify if something is wrong with your websites. If they continue to slow down, it’s time to start troubleshooting.

Leave a Reply

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