📜  linux mesuare request time http - Shell-Bash (1)

📅  最后修改于: 2023-12-03 14:43:55.353000             🧑  作者: Mango

Linux Mesuare Request Time HTTP - Shell/Bash

If you're a developer, you've likely spent time optimizing the performance of your web application. One important metric is the amount of time it takes for a web request to be completed. In this tutorial, we will go over how to measure the request time of an HTTP request using a Shell/Bash script on Linux.

Step 1: Install cURL

To make HTTP requests with Shell/Bash, we will use the popular command-line tool cURL. If you don't already have cURL installed on your system, you can install it using the package manager of your Linux distribution. For example, on Ubuntu, you can install cURL with the following command:

sudo apt-get install curl
Step 2: Create the Script

Next, create a new Shell/Bash script, for example measure_request_time.sh, and open it in your preferred text editor.

Add the following code to the script:

#!/bin/bash
start_time=$(date +%s.%N)
response=$(curl -s -o /dev/null -w "%{http_code}" <url>)
end_time=$(date +%s.%N)
elapsed_time=$(echo "$end_time - $start_time" | bc)
echo "Response code: $response, Elapsed time: $elapsed_time seconds"

This script measures the time it takes to make an HTTP request to a given URL. The -s option makes cURL run in silent mode, the -o /dev/null option redirects the response to the null device so we don’t see any output, and the -w "%{http_code}" option specifies that we want to output only the HTTP status code.

Replace <url> with your desired URL.

Step 3: Run the Script

Save the script and make it executable with the following command:

chmod +x measure_request_time.sh

Then run the script with:

./measure_request_time.sh

The output should look something like:

Response code: 200, Elapsed time: 0.363210592 seconds
Conclusion

In this tutorial, we went over how to measure the request time of an HTTP request using a Shell/Bash script on Linux. This is a helpful tool for developers to optimize the performance of their web applications.