cfw-api-cpu-memory-optimization

Measures cfw-api per-request CPU, memory, and latency using api-perf Cloudflare Worker Previews and Datadog metrics.

7|12|Updated Mar 26, 2026
One-click install
npx skills add https://github.com/OpenRouterTeam/docs --skill cfw-api-cpu-memory-optimization-openrouterteam
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: cfw-api-cpu-memory-optimization
Source: https://github.com/OpenRouterTeam/docs/tree/main/.agents/skills/cfw-api-cpu-memory-optimization
Command: npx skills add https://github.com/OpenRouterTeam/docs --skill cfw-api-cpu-memory-optimization-openrouterteam

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Measuring request-path CPU, memory, and latency of the cfw-api Cloudflare Worker under real load is error-prone: client-side timing conflates network and cold starts, local CPU profiles inflate wall time, and single-hostname baselines are unusable. This runbook provides a rigorous differential measurement methodology using disposable api-perf Worker Previews. ## Core Features & Use Cases - Preview Deployment & Load Generation: Deploy branch-scoped api-perf Previews via repository_dispatch, authenticate through Cloudflare Access service tokens plus OpenRouter API keys, and generate load with FakeProvider endpoints at no cost. - Differential CPU Measurement: Compare a branch Preview against a main Preview using worker-reported cpuTime from Datadog log events, grouped per request with statistically sound percentile sampling. - Memory & Leak Analysis: Track per-version memoryUsageBytes quantiles to distinguish footprint changes from leaks, and capture pre-traffic heap snapshots via workerd serve for retainer attribution. - Use Case: Before merging a streaming-pipeline change, deploy Previews from main and your branch, run matched load at production-like chunk pacing, and compare p50/p90/p99 cpuTime per request to prove the optimization is real. ## Quick Start Deploy an api-perf Preview from my branch and measure its per-request CPU against a main-branch baseline using the perf preview query script and Datadog worker events.

Frequently Asked Questions about cfw-api-cpu-memory-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I measure Cloudflare Worker CPU time per request?▼

Read cpuTime from the worker's own request events in Datadog, tagged by script_name and URL, rather than client-side latency. Group log lines by request id, take the max per request, then compute percentiles over those per-request values.

How do I deploy a Cloudflare Worker Preview for load testing?▼

Trigger the deploy workflow with a repository_dispatch call specifying the branch ref and action. The workflow deploys wrangler.perf.toml as an api-perf Preview whose hostname follows the branch, with an immutable hostname pinned to the commit.

Why does my Preview request return a 401 error?▼

Preview requests pass two independent checks: Cloudflare Access at the edge needs CF-Access-Client-Id and CF-Access-Client-Secret headers, and cfw-api needs an Authorization Bearer header with a valid OpenRouter API key. A bare key without the Bearer prefix fails as a missing authentication header.

Can I use wall time as a proxy for CPU in worker profiles?▼

No. wallTime measures elapsed invocation time and diverges from CPU by orders of magnitude when a request waits on I/O. Local .cpuprofile self-ms values have the same inflation because V8 charges each sample the whole gap since the previous sample.

How do I detect a memory leak in a Cloudflare Worker Preview?▼

Query the workersInvocationsAdaptive memoryUsageBytes P99 and max series filtered by script_name and version over a sustained load window. Monotonic growth within one arm indicates a leak, while a level shift between arms indicates a footprint change.

What are the limitations of the api-perf Preview environment?▼

The Preview cannot write to Postgres, has no queue or usage-record bindings, and omits CF_AI, presidio, sandbox, files-api, fusion, and image-api. It defaults to FakeProvider so it cannot spend credits, and its rate limiters fail open.