Skip to content

Commit

Permalink
add postgres setup
Browse files Browse the repository at this point in the history
  • Loading branch information
zzstoatzz committed Dec 26, 2024
1 parent d3c1bfa commit da5ba53
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 18 deletions.
44 changes: 27 additions & 17 deletions load_testing/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# investigating server performance

requirements:
Expand All @@ -14,6 +13,15 @@ uv pip install opentelemetry-api \
opentelemetry-instrumentation-fastapi
```

#### note

allow the following scripts to run via `chmod +x` (or similar)

```bash
./load_testing/local-telemetry/start
./load_testing/run-server.sh
./load_testing/populate-server.sh
```

### start the local telemetry stack

Expand All @@ -23,6 +31,24 @@ uv pip install opentelemetry-api \

### run the server with tracing

You can run the server with either SQLite (default) or PostgreSQL using the `run-server.sh` script:

```bash
# Run with SQLite (default)
./load_testing/run-server.sh

# Run with PostgreSQL 15
./load_testing/run-server.sh postgres:15
```

The script will:
- For SQLite: Use the default SQLite configuration
- For PostgreSQL:
- Start a Docker container with the specified version
- Configure the database connection
- Handle container lifecycle (reuse if possible, recreate if version changes)

If you need to run the server manually, here are the environment variables used:

```bash
prefect config set PREFECT_API_URL=http://localhost:4200/api
Expand All @@ -35,31 +61,15 @@ export OTEL_EXPORTER_OTLP_PROTOCOL=grpc
export OTEL_LOG_LEVEL=debug
export PREFECT__ENABLE_OSS_TELEMETRY=true
export PYTHONPATH=/Users/nate/github.com/prefecthq/prefect/src


opentelemetry-instrument \
uvicorn \
--app-dir /Users/nate/github.com/prefecthq/prefect/src \
--factory prefect.server.api.server:create_app \
--host 127.0.0.1 \
--port 4200 \
--timeout-keep-alive 5
```

### populate the server

if you haven't before, allow the script to run

```bash
chmod +x load_testing/populate-server.sh
```

create a work pool and some deployments
```bash
./load_testing/populate-server.sh
```


### start a worker

```bash
Expand Down
2 changes: 1 addition & 1 deletion load_testing/local-telemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ will be running:
* Jaeger is a frontend for viewing traces, and will be available at http://localhost:16686
* Prometheus captures metrics, and exposes a frontend at http://localhost:9090

Then, run your local server according to the instructions in the [load_testing/README.md](../load_testing/README.md) file.
Then, run your local server according to the instructions in the [load_testing/README.md](../README.md) file.

When making requests against your local server, you'll see trace appearing in the
Jaeger frontend at `http://localhost:16686`.
88 changes: 88 additions & 0 deletions load_testing/run-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env bash

DB_TYPE=${1:-sqlite} # Default to sqlite if no argument provided

# Function to start postgres container
start_postgres() {
local version=$1
local container_name="prefect-postgres"
local volume_name="prefectdb"

# Check if container exists
if docker ps -a --format '{{.Names}}' | grep -q "^${container_name}$"; then
echo "Found existing PostgreSQL container..."

# Get current version from running container
local current_version
current_version=$(docker exec ${container_name} postgres --version 2>/dev/null | grep -oE '[0-9]+' | head -1 || echo "0")

if [ "$current_version" != "${version%%.*}" ]; then
echo "Version mismatch: existing=${current_version}, requested=${version%%.*}"
echo "Removing container and volume for clean start..."
docker rm -f ${container_name} >/dev/null 2>&1
docker volume rm ${volume_name} >/dev/null 2>&1
else
echo "Version matches, reusing existing container..."
if ! docker start ${container_name} >/dev/null 2>&1; then
echo "Failed to start existing container, recreating..."
docker rm -f ${container_name} >/dev/null 2>&1
docker volume rm ${volume_name} >/dev/null 2>&1
fi
fi
fi

# Start container if it doesn't exist or was removed
if ! docker ps --format '{{.Names}}' | grep -q "^${container_name}$"; then
echo "Starting PostgreSQL ${version} container..."
docker run -d --name ${container_name} \
-v ${volume_name}:/var/lib/postgresql/data \
-p 5432:5432 \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=yourTopSecretPassword \
-e POSTGRES_DB=prefect \
postgres:${version}
fi

echo "Waiting for PostgreSQL to be ready..."
local retries=0
local max_retries=30
while ! docker exec ${container_name} pg_isready -U postgres > /dev/null 2>&1; do
((retries++))
if [ $retries -gt $max_retries ]; then
echo " Failed to start PostgreSQL after ${max_retries} seconds"
docker logs ${container_name}
exit 1
fi
echo -n "."
sleep 1
done
echo " PostgreSQL is ready!"
}

# Set database URL based on type
if [[ $DB_TYPE == sqlite ]]; then
: # Use default SQLite configuration
elif [[ $DB_TYPE == postgres:* ]]; then
PG_VERSION=${DB_TYPE#postgres:}
start_postgres $PG_VERSION
export PREFECT_API_DATABASE_CONNECTION_URL="postgresql+asyncpg://postgres:yourTopSecretPassword@localhost:5432/prefect"
else
echo "Invalid database type. Use 'sqlite' or 'postgres:<version>'"
exit 1
fi

PREFECT_API_URL=http://localhost:4200/api \
OTEL_SERVICE_NAME=prefect-server \
OTEL_TRACES_EXPORTER=otlp \
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317 \
OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
OTEL_LOG_LEVEL=debug \
PREFECT__ENABLE_OSS_TELEMETRY=true \
PYTHONPATH=/Users/nate/github.com/prefecthq/prefect/src \
opentelemetry-instrument \
uvicorn \
--app-dir /Users/nate/github.com/prefecthq/prefect/src \
--factory prefect.server.api.server:create_app \
--host 127.0.0.1 \
--port 4200 \
--timeout-keep-alive 5

0 comments on commit da5ba53

Please sign in to comment.