version: "3.8"

services:
  # Next.orly.dev relay (this repository)
  next-orly:
    build:
      context: ../..
      dockerfile: cmd/benchmark/Dockerfile.next-orly
    container_name: benchmark-next-orly
    environment:
      - ORLY_DATA_DIR=/data
      - ORLY_LISTEN=0.0.0.0
      - ORLY_PORT=8080
      - ORLY_LOG_LEVEL=off
    volumes:
      - ./data/next-orly:/data
    ports:
      - "8001:8080"
    networks:
      - benchmark-net
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "code=$(curl -s -o /dev/null -w '%{http_code}' http://localhost:8080 || echo 000); echo $$code | grep -E '^(101|200|400|404|426)$' >/dev/null",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Khatru with SQLite
  khatru-sqlite:
    build:
      context: ./external/khatru
      dockerfile: ../../Dockerfile.khatru-sqlite
    container_name: benchmark-khatru-sqlite
    environment:
      - DATABASE_TYPE=sqlite
      - DATABASE_PATH=/data/khatru.db
    volumes:
      - ./data/khatru-sqlite:/data
    ports:
      - "8002:3334"
    networks:
      - benchmark-net
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "wget --quiet --server-response --tries=1 http://localhost:3334 2>&1 | grep -E 'HTTP/[0-9.]+ (101|200|400|404)' >/dev/null",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Khatru with Badger
  khatru-badger:
    build:
      context: ./external/khatru
      dockerfile: ../../Dockerfile.khatru-badger
    container_name: benchmark-khatru-badger
    environment:
      - DATABASE_TYPE=badger
      - DATABASE_PATH=/data/badger
    volumes:
      - ./data/khatru-badger:/data
    ports:
      - "8003:3334"
    networks:
      - benchmark-net
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "wget --quiet --server-response --tries=1 http://localhost:3334 2>&1 | grep -E 'HTTP/[0-9.]+ (101|200|400|404)' >/dev/null",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Relayer basic example
  relayer-basic:
    build:
      context: ./external/relayer
      dockerfile: ../../Dockerfile.relayer-basic
    container_name: benchmark-relayer-basic
    environment:
      - POSTGRESQL_DATABASE=postgres://relayer:relayerpass@postgres:5432/relayerdb?sslmode=disable
    volumes:
      - ./data/relayer-basic:/data
    ports:
      - "8004:7447"
    networks:
      - benchmark-net
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "wget --quiet --server-response --tries=1 http://localhost:7447 2>&1 | grep -E 'HTTP/[0-9.]+ (101|200|400|404)' >/dev/null",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Strfry
  strfry:
    image: ghcr.io/hoytech/strfry:latest
    container_name: benchmark-strfry
    environment:
      - STRFRY_DB_PATH=/data/strfry.lmdb
      - STRFRY_RELAY_PORT=8080
    volumes:
      - ./data/strfry:/data
      - ./configs/strfry.conf:/etc/strfry.conf
    ports:
      - "8005:8080"
    networks:
      - benchmark-net
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "wget --quiet --server-response --tries=1 http://127.0.0.1:8080 2>&1 | grep -E 'HTTP/[0-9.]+ (101|200|400|404|426)' >/dev/null",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Nostr-rs-relay
  nostr-rs-relay:
    build:
      context: ./external/nostr-rs-relay
      dockerfile: ../../Dockerfile.nostr-rs-relay
    container_name: benchmark-nostr-rs-relay
    environment:
      - RUST_LOG=info
    volumes:
      - ./data/nostr-rs-relay:/data
      - ./configs/config.toml:/app/config.toml
    ports:
      - "8006:8080"
    networks:
      - benchmark-net
    healthcheck:
      test:
        [
          "CMD",
          "wget",
          "--quiet",
          "--tries=1",
          "--spider",
          "http://localhost:8080",
        ]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Benchmark runner
  benchmark-runner:
    build:
      context: ../..
      dockerfile: cmd/benchmark/Dockerfile.benchmark
    container_name: benchmark-runner
    depends_on:
      next-orly:
        condition: service_healthy
      khatru-sqlite:
        condition: service_healthy
      khatru-badger:
        condition: service_healthy
      relayer-basic:
        condition: service_healthy
      strfry:
        condition: service_healthy
      nostr-rs-relay:
        condition: service_healthy
    environment:
      - BENCHMARK_TARGETS=next-orly:8080,khatru-sqlite:3334,khatru-badger:3334,relayer-basic:7447,strfry:8080,nostr-rs-relay:8080
      - BENCHMARK_EVENTS=10000
      - BENCHMARK_WORKERS=8
      - BENCHMARK_DURATION=60s
    volumes:
      - ./reports:/reports
    networks:
      - benchmark-net
    command: >
      sh -c "
        echo 'Waiting for all relays to be ready...' &&
        sleep 30 &&
        echo 'Starting benchmark tests...' &&
        /app/benchmark-runner --output-dir=/reports
      "

  # PostgreSQL for relayer-basic
  postgres:
    image: postgres:16-alpine
    container_name: benchmark-postgres
    environment:
      - POSTGRES_DB=relayerdb
      - POSTGRES_USER=relayer
      - POSTGRES_PASSWORD=relayerpass
    volumes:
      - ./data/postgres:/var/lib/postgresql/data
    networks:
      - benchmark-net
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U relayer -d relayerdb"]
      interval: 10s
      timeout: 5s
      retries: 5
      start_period: 20s

networks:
  benchmark-net:
    driver: bridge

volumes:
  benchmark-data:
    driver: local
