version: "3.8"

services:
  # Next.orly.dev relay with Badger (this repository)
  next-orly-badger:
    build:
      context: ../..
      dockerfile: cmd/benchmark/Dockerfile.next-orly
    container_name: benchmark-next-orly-badger
    environment:
      - ORLY_DATA_DIR=/data
      - ORLY_LISTEN=0.0.0.0
      - ORLY_PORT=8080
      - ORLY_LOG_LEVEL=off
      - ORLY_DB_TYPE=badger
    volumes:
      - ./data/next-orly-badger:/data
    ports:
      - "8001:8080"
    networks:
      - benchmark-net
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s

  # Next.orly.dev relay with DGraph (this repository)
  next-orly-dgraph:
    build:
      context: ../..
      dockerfile: cmd/benchmark/Dockerfile.next-orly
    container_name: benchmark-next-orly-dgraph
    environment:
      - ORLY_DATA_DIR=/data
      - ORLY_LISTEN=0.0.0.0
      - ORLY_PORT=8080
      - ORLY_LOG_LEVEL=off
      - ORLY_DB_TYPE=dgraph
      - ORLY_DGRAPH_URL=dgraph-alpha:9080
    volumes:
      - ./data/next-orly-dgraph:/data
    ports:
      - "8007:8080"
    networks:
      - benchmark-net
    depends_on:
      dgraph-alpha:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # DGraph Zero - cluster coordinator
  dgraph-zero:
    image: dgraph/dgraph:v23.1.0
    container_name: benchmark-dgraph-zero
    working_dir: /data/zero
    ports:
      - "5080:5080"
      - "6080:6080"
    volumes:
      - ./data/dgraph-zero:/data
    command: dgraph zero --my=dgraph-zero:5080
    networks:
      - benchmark-net
    healthcheck:
      test: ["CMD", "sh", "-c", "dgraph version || exit 1"]
      interval: 5s
      timeout: 3s
      retries: 3
      start_period: 5s

  # DGraph Alpha - data node
  dgraph-alpha:
    image: dgraph/dgraph:v23.1.0
    container_name: benchmark-dgraph-alpha
    working_dir: /data/alpha
    ports:
      - "8088:8080"
      - "9080:9080"
    volumes:
      - ./data/dgraph-alpha:/data
    command: dgraph alpha --my=dgraph-alpha:7080 --zero=dgraph-zero:5080 --security whitelist=0.0.0.0/0
    networks:
      - benchmark-net
    depends_on:
      dgraph-zero:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "sh", "-c", "dgraph version || exit 1"]
      interval: 5s
      timeout: 3s
      retries: 6
      start_period: 10s

  # Next.orly.dev relay with Neo4j (this repository)
  next-orly-neo4j:
    build:
      context: ../..
      dockerfile: cmd/benchmark/Dockerfile.next-orly
    container_name: benchmark-next-orly-neo4j
    environment:
      - ORLY_DATA_DIR=/data
      - ORLY_LISTEN=0.0.0.0
      - ORLY_PORT=8080
      - ORLY_LOG_LEVEL=off
      - ORLY_DB_TYPE=neo4j
      - ORLY_NEO4J_URI=bolt://neo4j:7687
      - ORLY_NEO4J_USER=neo4j
      - ORLY_NEO4J_PASSWORD=benchmark123
    volumes:
      - ./data/next-orly-neo4j:/data
    ports:
      - "8008:8080"
    networks:
      - benchmark-net
    depends_on:
      neo4j:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 60s

  # Neo4j database
  neo4j:
    image: neo4j:5.15-community
    container_name: benchmark-neo4j
    ports:
      - "7474:7474"  # HTTP
      - "7687:7687"  # Bolt
    environment:
      - NEO4J_AUTH=neo4j/benchmark123
      - NEO4J_server_memory_heap_initial__size=2G
      - NEO4J_server_memory_heap_max__size=4G
      - NEO4J_server_memory_pagecache_size=2G
      - NEO4J_dbms_security_procedures_unrestricted=apoc.*
      - NEO4J_dbms_security_procedures_allowlist=apoc.*
      - NEO4JLABS_PLUGINS=["apoc"]
    volumes:
      - ./data/neo4j:/data
      - ./data/neo4j-logs:/logs
    networks:
      - benchmark-net
    healthcheck:
      test: ["CMD-SHELL", "cypher-shell -u neo4j -p benchmark123 'RETURN 1;' || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 10
      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 -q -O- http://localhost:3334 || exit 0"]
      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 -q -O- http://localhost:3334 || exit 0"]
      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 -q -O- http://localhost:7447 || exit 0"]
      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-db
      - STRFRY_RELAY_PORT=8080
    volumes:
      - ./data/strfry:/data
      - ./configs/strfry.conf:/etc/strfry.conf
    ports:
      - "8005:8080"
    networks:
      - benchmark-net
    entrypoint: /bin/sh
    command: -c "mkdir -p /data/strfry-db && exec /app/strfry relay"
    healthcheck:
      test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://127.0.0.1:8080"]
      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

  # Rely-SQLite relay
  rely-sqlite:
    build:
      context: .
      dockerfile: Dockerfile.rely-sqlite
    container_name: benchmark-rely-sqlite
    environment:
      - DATABASE_PATH=/data/relay.db
      - RELAY_LISTEN=0.0.0.0:3334
    volumes:
      - ./data/rely-sqlite:/data
    ports:
      - "8009:3334"
    networks:
      - benchmark-net
    healthcheck:
      test: ["CMD-SHELL", "curl -s --max-time 2 http://localhost:3334 2>&1 | head -1 | grep -q ."]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 30s

  # Benchmark runner
  benchmark-runner:
    build:
      context: ../..
      dockerfile: cmd/benchmark/Dockerfile.benchmark
    container_name: benchmark-runner
    depends_on:
      next-orly-badger:
        condition: service_healthy
      next-orly-dgraph:
        condition: service_healthy
      next-orly-neo4j:
        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
      rely-sqlite:
        condition: service_healthy
    environment:
      - BENCHMARK_TARGETS=rely-sqlite:3334,next-orly-badger:8080,next-orly-dgraph:8080,next-orly-neo4j:8080,khatru-sqlite:3334,khatru-badger:3334,relayer-basic:7447,strfry:8080,nostr-rs-relay:8080
      - BENCHMARK_EVENTS=50000
      - BENCHMARK_WORKERS=24
      - 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 &&
        echo 'Benchmark complete - triggering shutdown...' &&
        exit 0
      "

  # 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
