-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add FastAPI Example to Python Examples Folder (#443)
* Update to version 0.4.0 * Add fastapi to subdirectory
- Loading branch information
1 parent
9f7e83f
commit 1292277
Showing
31 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM python:3.9 | ||
|
||
# RUN pip3 install pipenv | ||
# COPY Pipfile ./Pipfile | ||
# COPY Pipfile.lock ./Pipfile.lock | ||
# RUN pipenv install | ||
|
||
RUN pip3 install fastapi pyroscope-io==0.4.0 uvicorn[standard] | ||
|
||
ENV FLASK_ENV=development | ||
ENV PYTHONUNBUFFERED=1 | ||
|
||
COPY lib ./lib | ||
CMD [ "uvicorn", "lib.server:app", "--host", "0.0.0.0", "--port", "5000"] | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## Fastapi Example | ||
|
||
To run the example run the following commands: | ||
``` | ||
# Pull latest pyroscope image: | ||
docker pull pyroscope/pyroscope:latest | ||
# Run the example project: | ||
docker-compose up --build | ||
# Reset the database (if needed): | ||
# docker-compose down | ||
``` |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from lib.utility.utility import find_nearest_vehicle | ||
|
||
def order_bike(search_radius): | ||
find_nearest_vehicle(search_radius, "bike") |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from lib.utility.utility import find_nearest_vehicle | ||
|
||
def order_car(search_radius): | ||
find_nearest_vehicle(search_radius, "car") |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from lib.utility.utility import find_nearest_vehicle | ||
|
||
def order_scooter(search_radius): | ||
find_nearest_vehicle(search_radius, "scooter") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import os | ||
import time | ||
import pyroscope | ||
# from flask import Flask | ||
from fastapi import FastAPI | ||
from lib.bike.bike import order_bike | ||
from lib.car.car import order_car | ||
from lib.scooter.scooter import order_scooter | ||
|
||
pyroscope.configure( | ||
app_name = "ride-sharing-app", | ||
server_address = "http://pyroscope:4040", | ||
tags = { | ||
"region": f'{os.getenv("REGION")}', | ||
} | ||
) | ||
|
||
|
||
app = FastAPI() | ||
|
||
@app.get("/") | ||
def read_root(): | ||
return {"Hello": "World"} | ||
|
||
@app.get("/bike") | ||
def bike(): | ||
order_bike(0.2) | ||
return "<p>Bike ordered</p>" | ||
|
||
|
||
@app.get("/scooter") | ||
def scooter(): | ||
order_scooter(0.3) | ||
return "<p>Scooter ordered</p>" | ||
|
||
|
||
@app.get("/car") | ||
def car(): | ||
order_car(0.4) | ||
return "<p>Car ordered</p>" | ||
|
||
|
||
@app.get("/") | ||
def environment(): | ||
result = "<h1>environment vars:</h1>" | ||
for key, value in os.environ.items(): | ||
result +=f"<p>{key}={value}</p>" | ||
return result |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import time | ||
import pyroscope | ||
import os | ||
from datetime import datetime | ||
|
||
# How much time mutex_lock() takes relative to search_radius() | ||
MUTEX_LOCK_MULTIPLIER = 2 | ||
|
||
# How much time check_driver_availability() takes relative to search_radius() | ||
DRIVER_AVAILABILITY_MULTIPLIER = 0.5 | ||
|
||
def mutex_lock(n): | ||
i = 0 | ||
start_time = time.time() | ||
while time.time() - start_time < n * MUTEX_LOCK_MULTIPLIER: | ||
i += 1 | ||
|
||
def check_driver_availability(n): | ||
i = 0 | ||
start_time = time.time() | ||
while time.time() - start_time < n * DRIVER_AVAILABILITY_MULTIPLIER: | ||
i += 1 | ||
|
||
# Every 4 minutes this will artificially create make requests in us-west-1 region slow | ||
# this is just for demonstration purposes to show how performance impacts show up in the | ||
# flamegraph | ||
|
||
force_mutex_lock = datetime.today().minute * 4 % 8 == 0 | ||
if os.getenv("REGION") == "us-west-1" and force_mutex_lock: | ||
mutex_lock(n) | ||
|
||
|
||
def find_nearest_vehicle(n, vehicle): | ||
with pyroscope.tag_wrapper({ "vehicle": vehicle}): | ||
i = 0 | ||
start_time = time.time() | ||
while time.time() - start_time < n: | ||
i += 1 | ||
if vehicle == "car": | ||
check_driver_availability(n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import random | ||
import requests | ||
import time | ||
|
||
HOSTS = [ | ||
'us-east-1', | ||
'us-west-1', | ||
'eu-west-1', | ||
] | ||
|
||
VEHICLES = [ | ||
'bike', | ||
'scooter', | ||
'car', | ||
] | ||
|
||
if __name__ == "__main__": | ||
print(f"starting load generator") | ||
time.sleep(3) | ||
while True: | ||
host = HOSTS[random.randint(0, len(HOSTS) - 1)] | ||
vehicle = VEHICLES[random.randint(0, len(VEHICLES) - 1)] | ||
print(f"requesting {vehicle} from {host}") | ||
resp = requests.get(f'http://{host}:5000/{vehicle}') | ||
print(f"received {resp}") | ||
time.sleep(random.uniform(0.1, 0.2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
FROM python:3.9 | ||
|
||
RUN pip3 install requests | ||
|
||
COPY load-generator.py ./load-generator.py | ||
|
||
ENV PYTHONUNBUFFERED=1 | ||
|
||
CMD [ "python", "load-generator.py" ] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## Flask Example | ||
|
||
To run the example run the following commands: | ||
``` | ||
# Pull latest pyroscope image: | ||
docker pull pyroscope/pyroscope:latest | ||
# Run the example project: | ||
docker-compose up --build | ||
# Reset the database (if needed): | ||
# docker-compose down | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
version: '3.9' | ||
services: | ||
us-east-1: | ||
environment: | ||
- REGION=us-east-1 | ||
build: | ||
context: . | ||
|
||
us-west-1: | ||
environment: | ||
- REGION=us-west-1 | ||
build: | ||
context: . | ||
|
||
eu-west-1: | ||
environment: | ||
- REGION=eu-west-1 | ||
build: | ||
context: . | ||
|
||
pyroscope: | ||
image: pyroscope/pyroscope | ||
environment: | ||
- PYROSCOPE_LOG_LEVEL=debug | ||
ports: | ||
- '4040:4040' | ||
command: | ||
- 'server' | ||
|
||
load-generator: | ||
build: | ||
context: . | ||
dockerfile: Dockerfile.load-generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
File renamed without changes.
File renamed without changes.