perf: parallelize LLM call detail and errors queries (#2561)

## Summary
- Runs `llm_calls.findUnique` and `optimization_errors.findMany` in
parallel via `Promise.all`
- Both queries use `params.id` directly — no data dependency between
them
- Page load time reduced from sum to max of both queries

## Evidence
- Proof doc: `js/cf-webapp/proof/17-parallel-llm-call-detail.md`

## Test plan
- [ ] `bash
js/cf-webapp/proof/reproducers/17-parallel-llm-call-detail.sh` — 5/5
checks pass
- [ ] LLM call detail page renders correctly with error list
This commit is contained in:
Kevin Turcios 2026-04-04 11:41:16 -05:00 committed by GitHub
parent 94b9de946e
commit 87db8f6026
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 106 additions and 10 deletions

View file

@ -0,0 +1,37 @@
# Proof 17: Parallelize LLM Call Detail + Errors Queries
## What Changed
In `src/app/observability/llm-call/[id]/page.tsx`, two sequential Prisma queries are now parallel:
1. `llm_calls.findUnique` — fetches the LLM call record
2. `optimization_errors.findMany` — fetches related errors
Both use `params.id` and are independent, so they run in `Promise.all`.
## Why
The queries have no data dependency — both use the route param `id` directly. Running them sequentially means the page waits for the first query to complete before starting the second.
**Before**: Total time = llmCall query + errors query
**After**: Total time = max(llmCall query, errors query)
## Evidence
### Before (sequential)
```typescript
const llmCall = await prisma.llm_calls.findUnique({ where: { id: params.id } })
// ...
const relatedErrors = await prisma.optimization_errors.findMany({ where: { llm_call_id: params.id }, ... })
```
### After (parallel)
```typescript
const [llmCall, relatedErrors] = await Promise.all([
prisma.llm_calls.findUnique({ where: { id: params.id } }),
prisma.optimization_errors.findMany({ where: { llm_call_id: params.id }, orderBy: { created_at: "desc" } }),
])
```
## How to Verify
```bash
cd js/cf-webapp
bash proof/reproducers/17-parallel-llm-call-detail.sh
```

View file

@ -0,0 +1,59 @@
#!/usr/bin/env bash
# Proof reproducer for commit 17: parallelize LLM call detail queries
set -euo pipefail
PASS=0
FAIL=0
TOTAL=0
check() {
local desc="$1"; shift
TOTAL=$((TOTAL + 1))
if "$@" >/dev/null 2>&1; then
echo " PASS: $desc"
PASS=$((PASS + 1))
else
echo " FAIL: $desc"
FAIL=$((FAIL + 1))
fi
}
check_not() {
local desc="$1"; shift
TOTAL=$((TOTAL + 1))
if "$@" >/dev/null 2>&1; then
echo " FAIL: $desc"
FAIL=$((FAIL + 1))
else
echo " PASS: $desc"
PASS=$((PASS + 1))
fi
}
LLM_PAGE="src/app/observability/llm-call/[id]/page.tsx"
echo "=== Proof 17: parallelize LLM call detail queries ==="
echo ""
echo "--- Parallelization checks ---"
check "Promise.all used" \
grep -q 'Promise.all' "$LLM_PAGE"
check "llmCall and relatedErrors destructured from Promise.all" \
grep -q '\[llmCall, relatedErrors\].*await Promise.all' "$LLM_PAGE"
check "llm_calls.findUnique in Promise.all" \
grep -q 'llm_calls.findUnique' "$LLM_PAGE"
check "optimization_errors.findMany in Promise.all" \
grep -q 'optimization_errors.findMany' "$LLM_PAGE"
echo ""
echo "--- Sequential pattern removed ---"
check_not "no standalone relatedErrors assignment after llmCall" \
grep -q 'const relatedErrors = await prisma' "$LLM_PAGE"
echo ""
echo "=== Results: $PASS/$TOTAL passed, $FAIL failed ==="
[ "$FAIL" -eq 0 ] && echo "ALL CHECKS PASSED" || echo "SOME CHECKS FAILED"
exit "$FAIL"

View file

@ -31,21 +31,21 @@ export async function generateMetadata(props: LLMCallDetailPageProps): Promise<M
export default async function LLMCallDetailPage(props: LLMCallDetailPageProps) {
const params = await props.params;
// Fetch LLM call details
const llmCall = await prisma.llm_calls.findUnique({
where: { id: params.id },
})
// Fetch LLM call details and related errors in parallel
const [llmCall, relatedErrors] = await Promise.all([
prisma.llm_calls.findUnique({
where: { id: params.id },
}),
prisma.optimization_errors.findMany({
where: { llm_call_id: params.id },
orderBy: { created_at: "desc" },
}),
])
if (!llmCall) {
notFound()
}
// Fetch related errors
const relatedErrors = await prisma.optimization_errors.findMany({
where: { llm_call_id: params.id },
orderBy: { created_at: "desc" },
})
return (
<div className="container mx-auto px-4 py-8">
{/* Header */}