The idea is to develop a simple project based on the book Python for DevOps by Alfredo Deza and Noah Gift. The project will consist of a simple CLI tool that gets the time for any given city. This should provide a simple enough platform to build around, using the tools presented in the book.
This project's dependencies are managed through poetry.
If you already have poetry up and running in your setup, you should be good to go by simply running
poetry install.
Otherwise, there is a great intro to setting up poetry here.
The whole series is worth a read, but the first chapter should be enough to run this project. I have found the
combination of pipx and pyenv to be the best, but feel free to experiment with other setups
Also, here you will find poetry's official documentation on
how to install with pipx
WorldTimer is a CLI tool that allows the user to get the time for any city in the world! Why google the local time of a city when you can use this tool? (yes... that is suposed to be read with a hint of irony)
- Install the project with
poetry(see Getting Started section above) - Define where you want to keep the logs by setting the environment variable
BASE_WT_LOGS_PATHlike so:
export BASE_WT_LOGS_PATH=<path to your logs directory>If you ommit this step, the current working directory will be used to store logs
3. run poetry shell to activate the virtual environment provided by poetry
(poetry shell actually spawns a new shell withinh the virtual environment. A subtle, but sometimes important, difference)
4. run the main.py script in the world_timer directory. Here's an example of how to run the script to
get to the tool's help section, from the project's root path:
python world_timer/main.py --help- [OPTIONAL]
poetryoffers another way of running this. Instead of activating the virtual environment throughpoetry shell, you could run it like this:
poetry run python world_timer/main.py --help- Follow the instructions in the
helpsection to use the tool! - [OPTIONAL] Feel free to play around with
wt_argparse.pyandwt_fire.py. These scripts implement the same tool by using other frameworks (argparseandfirerespectively)
In the utils directory, you will find two tools used for load testing: load_test_ab.py and load_test_molotov.py
WARNING! These tools might overload a server if not used correctly. Be careful when choosing the arguments.
The load_test_ab.py script allows you to run a simple load test by using the ab CLI tool. You can run it like this:
python load_test_ab.py --helpBy following the example in the tool's help menu, you should be able to run a simple load test.
The load_test_molotov.py script allows you to run a simple load test by using the molotov library. You can run it like this:
- Make sure you have the poetry virtual environment activated. You can do this by running
poetry shell - Run
molotovlike this (assuming you are in the project's root directory):
molotov -d 5 utils/load_test_molotov.pyThis will run the load tests specified in the utils/load_test_molotov.py script for 5 seconds. The current tests
are designed to fail about 20% of the time, so expect a few failures.
3. [OPTIONAL] I have found it difficult to capture the output of the tool. What worked best for me was to direct the
the output to a log file like this:
molotov -d 5 utils/load_test_molotov.py > <path_to_log_file>molotov has many CLI options you can play around.
Here's a link to the documentation describing what each of them does.
IN the utils directory, you will find a script called wt_time_benchmarking.py. This script allows you to run
a simple performance test. This tool is based on the Python timeit module.
How to run:
- Make sure you have the poetry virtual environment activated. You can do this by running
poetry shell(poetry shellactually spawns a new shell withinh the virtual environment. A subtle, but sometimes important, difference) - run
python utils/wt_time_benchmarking.py
The aim of this section is to customize the Python shell.
To make the functions and methods defined in utils/python_startup.py available in every Python REPL, you must first
set the PYTHONSTARTUP environment variable like this (assuming you are in the project's root directory):
export PYTHONSTARTUP=./utils/python_startup.pyIn the makefile there are a few useful recipes.
If you are running this on a MacOS system, I recommend you use gmake instead of make. (gmake can be installed with homebrew)
How to run:
gmake <target>See makefile for available targets.
if no target is specified, the default target is all.