Test your Code with Concurrent Requests.
./concurrent_curl.py "curl http://httpbin.org/status/200" -n 5 -c 2
./concurrent_curl.py "curl http://httpbin.org/status/404" -n 3 -c 1
./concurrent_curl.py "curl http://httpbin.org/delay/5" -n 2 -c 2
./concurrent_curl.py "curl -X POST -H 'Content-Type: application/json' -d '{\"foo\":\"bar\"}' http://httpbin.org/post" -n 1Per-command -n overrides global:
httpbin.org/status/200 runs 10 times.
httpbin.org/delay/1 runs 3 times (default 1 for the other two commands, as no -n is specified).
httpbin.org/get runs 1 time (global default).
httpbin.org/post runs 1 time (global default).
Total requests = 10+3+1+1=15.
./concurrent_load_tester.py "curl http://httpbin.org/status/200" "curl http://httpbin.org/delay/2" -n 5 -c 4
./concurrent_load_tester.py "curl http://httpbin.org/status/200 -n 10" "curl http://httpbin.org/delay/1 -n 3" "curl http://httpbin.org/get" "curl -X POST -d '{\"foo\":\"bar\"}' http://httpbin.org/post" -c 5
./concurrent_load_tester.py "curl http://httpbin.org/status/200" "curl http://httpbin.org/status/404"This is the most common and robust way to turn a script into a command.
Steps: Save your Python script: Let's say you save the updated Python script as concurrent_curl.py.
Make it executable:
chmod +x concurrent_curl.pyMove it to a directory in your $PATH: Your $PATH is an environment variable that lists directories where your shell looks for executable commands. Common places include:
Let's use ~/bin for a user-specific command:
mkdir -p ~/bin # Create the directory if it doesn't exist
mv concurrent_curl.py ~/bin/cconcur # Rename it to 'cconcur' for brevity
Add ~/bin to your $PATH (if not already there):
You'll need to edit your shell's configuration file.
For Bash: ~/.bashrc or ~/.bash_profile
For Zsh: ~/.zshrcAdd the following line to the end of the file:
export PATH="$HOME/bin:$PATH"
Note: If you already have a PATH export, just add $HOME/bin: to the beginning.Source your shell configuration file: After editing, apply the changes without restarting your terminal: For Bash: source ~/.bashrc (or source ~/.bash_profile) For Zsh: source ~/.zshrc
Now you can run your script as a command:
cconcur "curl http://httpbin.org/status/200" -n 5 -c 2
cconcur "curl -X POST -H 'Content-Type: application/json' -d '{\"foo\":\"bar\"}' http://httpbin.org/post" -n 1An alias is a shortcut for a command. It's good for short, frequently used commands.
Steps:
Add the alias to your shell configuration file:
For Bash: ~/.bashrc
For Zsh: ~/.zshrc
Add a line like this:
alias cconcur='python3 /path/to/your/concurrent_curl.py'
Replace /path/to/your/concurrent_curl.py with the actual path to where you saved your Python script.Source your shell configuration file: For Bash: source ~/.bashrc For Zsh: source ~/.zshrc
Now you can run it using the alias:
cconcur "curl http://httpbin.org/status/200" -n 5 -c 2