services:
  couchdb:
    image: couchdb:3.5.0
    environment:
      COUCHDB_USER: ${BENCH_COUCHDB_USER:-admin}
      COUCHDB_PASSWORD: ${BENCH_COUCHDB_PASSWORD:-testpassword}
      COUCHDB_SINGLE_NODE: "true"
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "curl -fsS -u ${BENCH_COUCHDB_USER:-admin}:${BENCH_COUCHDB_PASSWORD:-testpassword} http://127.0.0.1:5984/_up >/dev/null",
        ]
      interval: 2s
      timeout: 5s
      retries: 30

  nostr-relay:
    image: ghcr.io/hoytech/strfry:latest
    entrypoint: sh
    command:
      - -lc
      - |
        cat > /tmp/strfry.conf <<'EOF'
        db = "./strfry-db/"

        relay {
          bind = "0.0.0.0"
          port = 7777
          nofiles = 65536

          info {
            name = "livesync bench relay"
            description = "local relay for livesync compose benchmarks"
          }

          maxWebsocketPayloadSize = 131072
          autoPingSeconds = 55

          writePolicy {
            plugin = ""
          }
        }
        EOF
        exec /app/strfry --config /tmp/strfry.conf relay
    tmpfs:
      - /app/strfry-db:rw,size=256m,mode=1777
    healthcheck:
      test: ["CMD-SHELL", "nc -z 127.0.0.1 7777"]
      interval: 2s
      timeout: 5s
      retries: 30

  coturn:
    image: coturn/coturn:latest
    command:
      - --log-file=stdout
      - --listening-port=3478
      - --user=${BENCH_TURN_USERNAME:-testuser}:${BENCH_TURN_CREDENTIAL:-testpass}
      - --realm=${BENCH_TURN_REALM:-livesync.test}
    profiles:
      - turn

  bench-runner:
    build:
      context: ../..
      dockerfile: test/bench-network/Dockerfile.runner
    depends_on:
      couchdb:
        condition: service_healthy
      nostr-relay:
        condition: service_healthy
    environment:
      BENCH_COMMAND: ${BENCH_COMMAND:-cases}
      BENCH_CASES: ${BENCH_CASES:-couchdb-baseline,p2p-direct-local}
      BENCH_REPEAT_COUNT: ${BENCH_REPEAT_COUNT:-1}
      BENCH_CASES_ROOT: /workspace/src/apps/cli/testdeno/bench-results
      BENCH_COMPRESSION_RESULT_ROOT: /workspace/src/apps/cli/testdeno/bench-results
      BENCH_COMPRESSION_REPEAT_COUNT: ${BENCH_COMPRESSION_REPEAT_COUNT:-1}
      BENCH_SWEEP_ROOT: /workspace/src/apps/cli/testdeno/bench-results
      BENCH_SWEEP_RTT_MS: ${BENCH_SWEEP_RTT_MS:-20,50,100,150,300}
      BENCH_SWEEP_INCLUDE_P2P: ${BENCH_SWEEP_INCLUDE_P2P:-true}
      BENCH_COUCHDB_MANAGED: "false"
      BENCH_COUCHDB_BACKEND_URI: http://couchdb:5984
      BENCH_COUCHDB_URI: http://127.0.0.1:15989
      BENCH_COUCHDB_USER: ${BENCH_COUCHDB_USER:-admin}
      BENCH_COUCHDB_PASSWORD: ${BENCH_COUCHDB_PASSWORD:-testpassword}
      BENCH_RELAY: ws://nostr-relay:7777/
      BENCH_LOCAL_TURN_SERVERS: turn:coturn:3478
      BENCH_MD_FILE_COUNT: ${BENCH_MD_FILE_COUNT:-20}
      BENCH_MD_MIN_SIZE_BYTES: ${BENCH_MD_MIN_SIZE_BYTES:-512}
      BENCH_MD_MAX_SIZE_BYTES: ${BENCH_MD_MAX_SIZE_BYTES:-2048}
      BENCH_BIN_FILE_COUNT: ${BENCH_BIN_FILE_COUNT:-5}
      BENCH_BIN_SIZE_BYTES: ${BENCH_BIN_SIZE_BYTES:-8192}
      BENCH_VERIFY_MODE: ${BENCH_VERIFY_MODE:-all}
      BENCH_COUCHDB_RTT_MS: ${BENCH_COUCHDB_RTT_MS:-20}
      BENCH_TETHERING_VPN_RTT_MS: ${BENCH_TETHERING_VPN_RTT_MS:-120}
      BENCH_SYNC_TIMEOUT: ${BENCH_SYNC_TIMEOUT:-300}
      BENCH_PEERS_TIMEOUT: ${BENCH_PEERS_TIMEOUT:-60}
      LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS: ${LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS:-60000}
      BENCH_LIVESYNC_TEST_TEE: ${BENCH_LIVESYNC_TEST_TEE:-0}
      LIVESYNC_TEST_TEE: ${BENCH_LIVESYNC_TEST_TEE:-0}
      LIVESYNC_CLI_DEBUG: ${LIVESYNC_CLI_DEBUG:-0}
      LIVESYNC_CLI_VERBOSE: ${LIVESYNC_CLI_VERBOSE:-0}
      CLI_E2E_TASK: ${CLI_E2E_TASK:-test:p2p:ci}
      RELAY: ${RELAY:-ws://nostr-relay:7777/}
      PEERS_TIMEOUT: ${PEERS_TIMEOUT:-20}
      SYNC_TIMEOUT: ${SYNC_TIMEOUT:-60}
      LIVESYNC_USE_COTURN: ${LIVESYNC_USE_COTURN:-0}
      TURN_SERVERS: ${TURN_SERVERS:-none}
      LIVESYNC_P2P_PEERS_RETRY: ${LIVESYNC_P2P_PEERS_RETRY:-1}
    volumes:
      - ./bench-results:/workspace/src/apps/cli/testdeno/bench-results

  couchdb-shim:
    build:
      context: ../..
      dockerfile: test/bench-network/Dockerfile.shim
    profiles:
      - shim
    depends_on:
      couchdb:
        condition: service_healthy
    cap_add:
      - NET_ADMIN
    environment:
      NETEM_PROFILE: ${NETEM_PROFILE:-home-wifi}
      NETEM_INTERFACE: ${NETEM_INTERFACE:-eth0}
      NETEM_DELAY_MS: ${NETEM_DELAY_MS:-20}
      NETEM_JITTER_MS: ${NETEM_JITTER_MS:-5}
      NETEM_LOSS_PERCENT: ${NETEM_LOSS_PERCENT:-0.1}
      NETEM_BANDWIDTH_MBIT: ${NETEM_BANDWIDTH_MBIT:-100}
      NETEM_MTU: ${NETEM_MTU:-1500}
      NETEM_RESULT_ROOT: /bench-results
      SHIM_LISTEN_PORT: 5984
      SHIM_TARGET_HOST: couchdb
      SHIM_TARGET_PORT: 5984
    volumes:
      - ./bench-results:/bench-results
    healthcheck:
      test: ["CMD-SHELL", "nc -z 127.0.0.1 5984"]
      interval: 2s
      timeout: 5s
      retries: 30

  bench-runner-shim:
    build:
      context: ../..
      dockerfile: test/bench-network/Dockerfile.runner
    profiles:
      - shim
    depends_on:
      couchdb-shim:
        condition: service_healthy
    environment:
      BENCH_COMMAND: ${BENCH_COMMAND:-cases}
      BENCH_CASES: ${BENCH_CASES:-couchdb-netem-home-wifi}
      BENCH_REPEAT_COUNT: ${BENCH_REPEAT_COUNT:-1}
      BENCH_CASES_ROOT: /workspace/src/apps/cli/testdeno/bench-results
      BENCH_SWEEP_ROOT: /workspace/src/apps/cli/testdeno/bench-results
      BENCH_COUCHDB_MANAGED: "false"
      BENCH_COUCHDB_BACKEND_URI: http://couchdb-shim:5984
      BENCH_SHIM_COUCHDB_URI: http://couchdb-shim:5984
      BENCH_COUCHDB_URI: http://127.0.0.1:15989
      BENCH_COUCHDB_USER: ${BENCH_COUCHDB_USER:-admin}
      BENCH_COUCHDB_PASSWORD: ${BENCH_COUCHDB_PASSWORD:-testpassword}
      BENCH_MD_FILE_COUNT: ${BENCH_MD_FILE_COUNT:-20}
      BENCH_MD_MIN_SIZE_BYTES: ${BENCH_MD_MIN_SIZE_BYTES:-512}
      BENCH_MD_MAX_SIZE_BYTES: ${BENCH_MD_MAX_SIZE_BYTES:-2048}
      BENCH_BIN_FILE_COUNT: ${BENCH_BIN_FILE_COUNT:-5}
      BENCH_BIN_SIZE_BYTES: ${BENCH_BIN_SIZE_BYTES:-8192}
      BENCH_COUCHDB_RTT_MS: ${BENCH_COUCHDB_RTT_MS:-1}
      BENCH_SYNC_TIMEOUT: ${BENCH_SYNC_TIMEOUT:-300}
      BENCH_LIVESYNC_TEST_TEE: ${BENCH_LIVESYNC_TEST_TEE:-0}
    volumes:
      - ./bench-results:/workspace/src/apps/cli/testdeno/bench-results

  p2p-signalling-shim:
    build:
      context: ../..
      dockerfile: test/bench-network/Dockerfile.shim
    profiles:
      - signalling-shim
    depends_on:
      nostr-relay:
        condition: service_healthy
    cap_add:
      - NET_ADMIN
    environment:
      NETEM_PROFILE: ${NETEM_PROFILE:-home-wifi}
      NETEM_INTERFACE: ${NETEM_INTERFACE:-eth0}
      NETEM_DELAY_MS: ${NETEM_DELAY_MS:-20}
      NETEM_JITTER_MS: ${NETEM_JITTER_MS:-5}
      NETEM_LOSS_PERCENT: ${NETEM_LOSS_PERCENT:-0.1}
      NETEM_BANDWIDTH_MBIT: ${NETEM_BANDWIDTH_MBIT:-100}
      NETEM_MTU: ${NETEM_MTU:-1500}
      NETEM_RESULT_ROOT: /bench-results
      SHIM_LISTEN_PORT: 7777
      SHIM_TARGET_HOST: nostr-relay
      SHIM_TARGET_PORT: 7777
    volumes:
      - ./bench-results:/bench-results
    healthcheck:
      test: ["CMD-SHELL", "nc -z 127.0.0.1 7777"]
      interval: 2s
      timeout: 5s
      retries: 30

  bench-runner-signalling-shim:
    build:
      context: ../..
      dockerfile: test/bench-network/Dockerfile.runner
    profiles:
      - signalling-shim
    depends_on:
      p2p-signalling-shim:
        condition: service_healthy
    environment:
      BENCH_COMMAND: ${BENCH_COMMAND:-cases}
      BENCH_CASES: ${BENCH_CASES:-p2p-signalling-netem-home-wifi}
      BENCH_REPEAT_COUNT: ${BENCH_REPEAT_COUNT:-1}
      BENCH_CASES_ROOT: /workspace/src/apps/cli/testdeno/bench-results
      BENCH_SIGNAL_SHIM_RELAY: ws://p2p-signalling-shim:7777/
      BENCH_MD_FILE_COUNT: ${BENCH_MD_FILE_COUNT:-20}
      BENCH_MD_MIN_SIZE_BYTES: ${BENCH_MD_MIN_SIZE_BYTES:-512}
      BENCH_MD_MAX_SIZE_BYTES: ${BENCH_MD_MAX_SIZE_BYTES:-2048}
      BENCH_BIN_FILE_COUNT: ${BENCH_BIN_FILE_COUNT:-5}
      BENCH_BIN_SIZE_BYTES: ${BENCH_BIN_SIZE_BYTES:-8192}
      BENCH_VERIFY_MODE: ${BENCH_VERIFY_MODE:-all}
      BENCH_SYNC_TIMEOUT: ${BENCH_SYNC_TIMEOUT:-300}
      BENCH_PEERS_TIMEOUT: ${BENCH_PEERS_TIMEOUT:-60}
      LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS: ${LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS:-60000}
      BENCH_LIVESYNC_TEST_TEE: ${BENCH_LIVESYNC_TEST_TEE:-0}
    volumes:
      - ./bench-results:/workspace/src/apps/cli/testdeno/bench-results

  p2p-split-host:
    build:
      context: ../..
      dockerfile: test/bench-network/Dockerfile.runner
    profiles:
      - p2p-split
    depends_on:
      nostr-relay:
        condition: service_healthy
    cap_add:
      - NET_ADMIN
    environment:
      BENCH_COMMAND: p2p-split-node
      BENCH_P2P_SPLIT_ROLE: host
      BENCH_SPLIT_RUN_ID: ${BENCH_SPLIT_RUN_ID:-bench-split-run}
      BENCH_SPLIT_WORK_ROOT: /p2p-work
      BENCH_SPLIT_RESULT_ROOT: /workspace/src/apps/cli/testdeno/bench-results
      BENCH_RELAY: ws://nostr-relay:7777/
      BENCH_APP_ID: ${BENCH_APP_ID:-self-hosted-livesync-cli-benchmark}
      BENCH_ROOM_ID: ${BENCH_ROOM_ID:-bench-split-room}
      BENCH_PASSPHRASE: ${BENCH_PASSPHRASE:-bench-split-passphrase}
      BENCH_TURN_SERVERS: ${BENCH_TURN_SERVERS:-}
      BENCH_MD_FILE_COUNT: ${BENCH_MD_FILE_COUNT:-20}
      BENCH_MD_MIN_SIZE_BYTES: ${BENCH_MD_MIN_SIZE_BYTES:-512}
      BENCH_MD_MAX_SIZE_BYTES: ${BENCH_MD_MAX_SIZE_BYTES:-2048}
      BENCH_BIN_FILE_COUNT: ${BENCH_BIN_FILE_COUNT:-5}
      BENCH_BIN_SIZE_BYTES: ${BENCH_BIN_SIZE_BYTES:-8192}
      BENCH_SYNC_TIMEOUT: ${BENCH_SYNC_TIMEOUT:-300}
      BENCH_PEERS_TIMEOUT: ${BENCH_PEERS_TIMEOUT:-60}
      BENCH_NETEM_ENABLED: ${BENCH_NETEM_ENABLED:-1}
      BENCH_NETWORK_PROFILE: ${BENCH_NETWORK_PROFILE:-home-wifi}
      NETEM_PROFILE: ${NETEM_PROFILE:-home-wifi}
      NETEM_INTERFACE: ${NETEM_INTERFACE:-eth0}
      NETEM_DELAY_MS: ${NETEM_DELAY_MS:-20}
      NETEM_JITTER_MS: ${NETEM_JITTER_MS:-5}
      NETEM_LOSS_PERCENT: ${NETEM_LOSS_PERCENT:-0.1}
      NETEM_BANDWIDTH_MBIT: ${NETEM_BANDWIDTH_MBIT:-100}
      NETEM_MTU: ${NETEM_MTU:-1500}
      LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS: ${LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS:-60000}
      LIVESYNC_TEST_TEE: ${BENCH_LIVESYNC_TEST_TEE:-0}
    volumes:
      - p2p-split-work:/p2p-work
      - ./bench-results:/workspace/src/apps/cli/testdeno/bench-results

  p2p-split-client:
    build:
      context: ../..
      dockerfile: test/bench-network/Dockerfile.runner
    profiles:
      - p2p-split
    depends_on:
      nostr-relay:
        condition: service_healthy
      p2p-split-host:
        condition: service_started
    cap_add:
      - NET_ADMIN
    environment:
      BENCH_COMMAND: p2p-split-node
      BENCH_P2P_SPLIT_ROLE: client
      BENCH_SPLIT_RUN_ID: ${BENCH_SPLIT_RUN_ID:-bench-split-run}
      BENCH_SPLIT_WORK_ROOT: /p2p-work
      BENCH_SPLIT_RESULT_ROOT: /workspace/src/apps/cli/testdeno/bench-results
      BENCH_RELAY: ws://nostr-relay:7777/
      BENCH_APP_ID: ${BENCH_APP_ID:-self-hosted-livesync-cli-benchmark}
      BENCH_ROOM_ID: ${BENCH_ROOM_ID:-bench-split-room}
      BENCH_PASSPHRASE: ${BENCH_PASSPHRASE:-bench-split-passphrase}
      BENCH_TURN_SERVERS: ${BENCH_TURN_SERVERS:-}
      BENCH_SYNC_TIMEOUT: ${BENCH_SYNC_TIMEOUT:-300}
      BENCH_PEERS_TIMEOUT: ${BENCH_PEERS_TIMEOUT:-60}
      BENCH_NETEM_ENABLED: ${BENCH_NETEM_ENABLED:-1}
      BENCH_NETWORK_PROFILE: ${BENCH_NETWORK_PROFILE:-home-wifi}
      NETEM_PROFILE: ${NETEM_PROFILE:-home-wifi}
      NETEM_INTERFACE: ${NETEM_INTERFACE:-eth0}
      NETEM_DELAY_MS: ${NETEM_DELAY_MS:-20}
      NETEM_JITTER_MS: ${NETEM_JITTER_MS:-5}
      NETEM_LOSS_PERCENT: ${NETEM_LOSS_PERCENT:-0.1}
      NETEM_BANDWIDTH_MBIT: ${NETEM_BANDWIDTH_MBIT:-100}
      NETEM_MTU: ${NETEM_MTU:-1500}
      LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS: ${LIVESYNC_P2P_RELAY_READY_TIMEOUT_MS:-60000}
      LIVESYNC_TEST_TEE: ${BENCH_LIVESYNC_TEST_TEE:-0}
    volumes:
      - p2p-split-work:/p2p-work
      - ./bench-results:/workspace/src/apps/cli/testdeno/bench-results

  netem-smoke:
    build:
      context: ../..
      dockerfile: test/bench-network/Dockerfile.netem
    profiles:
      - netem
    cap_add:
      - NET_ADMIN
    environment:
      NETEM_PROFILE: ${NETEM_PROFILE:-home-wifi}
      NETEM_INTERFACE: ${NETEM_INTERFACE:-eth0}
      NETEM_DELAY_MS: ${NETEM_DELAY_MS:-20}
      NETEM_JITTER_MS: ${NETEM_JITTER_MS:-5}
      NETEM_LOSS_PERCENT: ${NETEM_LOSS_PERCENT:-0.1}
      NETEM_BANDWIDTH_MBIT: ${NETEM_BANDWIDTH_MBIT:-100}
      NETEM_MTU: ${NETEM_MTU:-1500}
      NETEM_RESULT_ROOT: /bench-results
    volumes:
      - ./bench-results:/bench-results

volumes:
  p2p-split-work:
