|
| 1 | +# IncreaseServerSpeedWithRedis |
| 2 | + |
| 3 | +The purpose of this repository is to show the performance advantages that can be gained by implementing redis even in simple rest api. |
| 4 | + |
| 5 | +## Files |
| 6 | + |
| 7 | +The file **with_redis.py** contains the code of a server made in **fastapi** that implements redis. |
| 8 | + |
| 9 | +The file **without_redis.py** contains the code of a server made in **fastapi** that does not implement redis. |
| 10 | + |
| 11 | +The **run_test.py** file is a utility to measure the seconds it takes for a server to respond to a request. |
| 12 | + |
| 13 | +The **example.sh** file is an example script that shows how a set of tests can be automated using the **run_test.py** utility. |
| 14 | + |
| 15 | +## Requirements |
| 16 | + |
| 17 | +- git |
| 18 | +- python3 |
| 19 | +- docker or redis |
| 20 | +- internet connection |
| 21 | + |
| 22 | +## Install dependencies |
| 23 | + |
| 24 | +### Get code |
| 25 | + |
| 26 | +```sh |
| 27 | +git clone https://github.com/luovkle/IncreaseServerSpeedWithRedis.git |
| 28 | +cd IncreaseServerSpeedWithRedis |
| 29 | +``` |
| 30 | + |
| 31 | +### Create a virtual environment |
| 32 | + |
| 33 | +```sh |
| 34 | +python -m venv .venv |
| 35 | +source ./venv/bin/activate |
| 36 | +``` |
| 37 | + |
| 38 | +### Install packages |
| 39 | + |
| 40 | +```sh |
| 41 | +pip install -r requirements.txt |
| 42 | +``` |
| 43 | + |
| 44 | +## Running redis server |
| 45 | + |
| 46 | +The following example shows how to run the server from docker, however you can run the server installed on the local machine directly. |
| 47 | + |
| 48 | +```sh |
| 49 | +docker run --rm --name redis -p 6379:6379 -d redis:alpine |
| 50 | +``` |
| 51 | + |
| 52 | +## How to run servers |
| 53 | + |
| 54 | +### Granting execution permits |
| 55 | + |
| 56 | +```sh |
| 57 | +chmod +x without_redis.py |
| 58 | +chmod +x with_redis.py |
| 59 | +``` |
| 60 | + |
| 61 | +### Running servers |
| 62 | + |
| 63 | +Running the server that does not use redis on port 8080. |
| 64 | + |
| 65 | +```sh |
| 66 | +./without_redis.py 8080 |
| 67 | +``` |
| 68 | +Running the server using redis on port 8081. |
| 69 | + |
| 70 | +```sh |
| 71 | +./with_redis.py 8081 |
| 72 | +``` |
| 73 | + |
| 74 | +### Without execution permissions |
| 75 | + |
| 76 | +Alternatively, the servers can be run without the need to secure execution permissions. |
| 77 | + |
| 78 | +```sh |
| 79 | +python without_redis.py 8080 |
| 80 | +``` |
| 81 | + |
| 82 | +```sh |
| 83 | +python without_redis.py 8081 |
| 84 | +``` |
| 85 | + |
| 86 | +### Things to consider |
| 87 | + |
| 88 | +The servers have two endpoints: |
| 89 | +- /photos |
| 90 | +- /ptohos/{photo_id} |
| 91 | + |
| 92 | +Both endpoints use http **get** method. |
| 93 | + |
| 94 | +## How to run tests |
| 95 | + |
| 96 | +### Granting execution permits |
| 97 | + |
| 98 | +```sh |
| 99 | +chmod +x run_test.py |
| 100 | +``` |
| 101 | + |
| 102 | +### Running tests |
| 103 | + |
| 104 | +If you have followed the examples above, the server with port 8080 corresponds to the server that does not use redis and the server with port 8081 corresponds to the server that uses redis. |
| 105 | + |
| 106 | +Note that the first request made to an endpoint on the server using redis will not make any real difference to the server not using redis. The real difference will be reflected in a second request to the same endpoint. |
| 107 | + |
| 108 | +#### Test on port 8080 |
| 109 | + |
| 110 | +The following command will run a test on the server **127.0.0.1:8080/photos**. |
| 111 | + |
| 112 | +```sh |
| 113 | +./run_test.py 8080 |
| 114 | +``` |
| 115 | + |
| 116 | +The following command will run a test on the server **127.0.0.1:8080/photos/1**. |
| 117 | + |
| 118 | +```sh |
| 119 | +./run_test.py 8080 -p 1 |
| 120 | +``` |
| 121 | + |
| 122 | +#### Test on port 8081 |
| 123 | + |
| 124 | +The following command will run a test on the server **127.0.0.1:8081/photos**. |
| 125 | + |
| 126 | +```sh |
| 127 | +./run_test.py 8081 |
| 128 | +``` |
| 129 | + |
| 130 | +The following command will run a test on the server **127.0.0.1:8081/photos/1**. |
| 131 | + |
| 132 | +```sh |
| 133 | +./run_test.py 8081 -p 1 |
| 134 | +``` |
| 135 | + |
| 136 | +### Example automated test |
| 137 | + |
| 138 | +Inside the files of this repository you can find the **example.sh** script. This script is intended to test on 4 different endpoints on both servers. |
| 139 | + |
| 140 | +**example.sh** expects that the servers are already running and that the server that does not use redis runs on port 8080 while the server that does use redis runs on port 8081. |
| 141 | + |
| 142 | +#### Running example |
| 143 | + |
| 144 | +```sh |
| 145 | +chmod +x example.sh |
| 146 | +./example.sh |
| 147 | +``` |
| 148 | +Expected output for first run. |
| 149 | + |
| 150 | +```txt |
| 151 | +>>> Without redis |
| 152 | +127.0.0.1:8080/photos |
| 153 | +0.432171 |
| 154 | +127.0.0.1:8080/photos/1 |
| 155 | +0.330197 |
| 156 | +127.0.0.1:8080/photos/2 |
| 157 | +0.238029 |
| 158 | +127.0.0.1:8080/photos/3 |
| 159 | +0.229818 |
| 160 | +----------------------- |
| 161 | +>>> With redis |
| 162 | +127.0.0.1:8081/photos |
| 163 | +0.435041 |
| 164 | +127.0.0.1:8081/photos/1 |
| 165 | +0.232723 |
| 166 | +127.0.0.1:8081/photos/2 |
| 167 | +0.239102 |
| 168 | +127.0.0.1:8081/photos/3 |
| 169 | +0.225132 |
| 170 | +``` |
| 171 | + |
| 172 | +Expected output for a second run. |
| 173 | + |
| 174 | +```txt |
| 175 | +>>> Without redis |
| 176 | +127.0.0.1:8080/photos |
| 177 | +0.615531 |
| 178 | +127.0.0.1:8080/photos/1 |
| 179 | +0.389103 |
| 180 | +127.0.0.1:8080/photos/2 |
| 181 | +0.461283 |
| 182 | +127.0.0.1:8080/photos/3 |
| 183 | +0.339153 |
| 184 | +----------------------- |
| 185 | +>>> With redis |
| 186 | +127.0.0.1:8081/photos |
| 187 | +0.096654 |
| 188 | +127.0.0.1:8081/photos/1 |
| 189 | +0.002529 |
| 190 | +127.0.0.1:8081/photos/2 |
| 191 | +0.00229 |
| 192 | +127.0.0.1:8081/photos/3 |
| 193 | +0.002277 |
| 194 | +``` |
0 commit comments