Files
karaoke-gen/infrastructure/compute/encoding_worker_vm.py
T
Andrew Beveridge ☄️andClaude Opus 4.5 2e71dc3158 feat(infra): Upgrade encoding worker to c4d-highcpu-32 (4.92x faster) (#244)
## Summary

Upgrades the GCE encoding worker from c4-standard-8 (Intel Xeon) to
c4d-highcpu-32 (AMD EPYC 9B45 Turin), achieving **4.92x faster encoding
performance** based on rigorous benchmarking.

## Changes

- **Infrastructure**: Updated `MachineTypes.ENCODING_WORKER` to
`c4d-highcpu-32` in `config.py`
- **Documentation**: Comprehensive benchmark results in
`docs/archive/2026-01-08-performance-investigation.md`
- **Lessons Learned**: Added "GCE Instance Type Selection for FFmpeg
Encoding" section
- **Benchmark Scripts**: Added reusable scripts for future
infrastructure testing:
- `scripts/benchmark_encoding.py` - Local Python benchmark using actual
code paths
  - `scripts/benchmark_encoding_gce.sh` - Shell benchmark for GCE VMs
- `scripts/benchmark_candidates.sh` - Multi-VM orchestration for
comparative testing

## Benchmark Results

| Instance | CPU | Total Time | vs Baseline |
|----------|-----|------------|-------------|
| c4-standard-8 (baseline) | Intel Xeon | 666s | 1.00x |
| c4-highcpu-16 | Intel Xeon | 309s | 2.16x |
| c4a-highcpu-16 | Google Axion ARM | 248s | 2.69x |
| c4d-highcpu-16 | AMD EPYC 9B45 | 220s | 3.03x |
| **c4d-highcpu-32** | **AMD EPYC 9B45** | **135s** | **4.92x** |

Key finding: AMD EPYC Turin significantly outperforms Intel Granite
Rapids for CPU-bound FFmpeg/libass workloads.

## Testing

- [x] Benchmarked on actual GCE instances with production data
- [x] Verified c4d-highcpu-32 uses hyperdisk-balanced (already
configured)
- [ ] Infrastructure deploys via CI after merge

## Deployment Impact

⚠️ Changing machine type will cause Pulumi to **replace** the VM (~1-2
min downtime). Jobs queued during restart will retry automatically.

@coderabbitai ignore

---
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-08 14:17:02 -05:00

111 lines
3.5 KiB
Python

"""
Encoding Worker VM resources.
Manages the high-performance VM instance for video encoding,
including static IP and firewall rules.
"""
import pulumi
from pulumi_gcp import compute, serviceaccount
from config import REGION, ZONE, PROJECT_ID, MachineTypes, DiskSizes
from .startup_scripts import read_script
def create_encoding_worker_ip() -> compute.Address:
"""
Create static IP for the encoding worker.
Returns:
compute.Address: The static IP resource.
"""
return compute.Address(
"encoding-worker-ip",
name="encoding-worker-static-ip",
region=REGION,
address_type="EXTERNAL",
description="Static external IP for encoding worker service",
)
def create_encoding_worker_vm(
ip: compute.Address,
service_account: serviceaccount.Account,
) -> compute.Instance:
"""
Create the encoding worker VM instance.
This VM runs video encoding jobs with high CPU/memory resources.
Uses c4d-highcpu-32 (AMD EPYC 9B45 Turin, 32 vCPU) for 4.92x faster
encoding vs c4-standard-8. Uses hyperdisk-balanced for fast I/O.
Uses a custom Packer-built image with Python 3.13, FFmpeg, and fonts
pre-installed to reduce startup time from ~10 minutes to ~30 seconds.
See infrastructure/packer/README.md for image build instructions.
Args:
ip: The static IP address for the VM.
service_account: The encoding worker service account.
Returns:
compute.Instance: The VM instance resource.
"""
startup_script = read_script("encoding_worker.sh")
# Use custom Packer image with dependencies pre-installed
# Image family returns latest image in the family
# Fallback: "debian-cloud/debian-12" if image doesn't exist yet
custom_image = f"projects/{PROJECT_ID}/global/images/family/encoding-worker"
return compute.Instance(
"encoding-worker",
name="encoding-worker",
machine_type=MachineTypes.ENCODING_WORKER,
zone=ZONE,
boot_disk=compute.InstanceBootDiskArgs(
initialize_params=compute.InstanceBootDiskInitializeParamsArgs(
image=custom_image,
size=DiskSizes.ENCODING_WORKER,
type="hyperdisk-balanced",
),
),
network_interfaces=[compute.InstanceNetworkInterfaceArgs(
network="default",
access_configs=[compute.InstanceNetworkInterfaceAccessConfigArgs(
nat_ip=ip.address,
)],
)],
service_account=compute.InstanceServiceAccountArgs(
email=service_account.email,
scopes=["cloud-platform"],
),
metadata_startup_script=startup_script,
tags=["encoding-worker"],
allow_stopping_for_update=True,
advanced_machine_features=compute.InstanceAdvancedMachineFeaturesArgs(
threads_per_core=2,
),
)
def create_encoding_worker_firewall() -> compute.Firewall:
"""
Create firewall rules for the encoding worker.
Opens port 8080 for the HTTP API (authentication required).
Returns:
compute.Firewall: The firewall resource.
"""
return compute.Firewall(
"encoding-worker-firewall",
name="encoding-worker-allow-http",
network="default",
allows=[
compute.FirewallAllowArgs(protocol="tcp", ports=["8080"]),
],
source_ranges=["0.0.0.0/0"],
target_tags=["encoding-worker"],
description="Allow HTTP access to encoding worker (auth required)",
)