
Profiling Optimization
- 410 installs
- 305 repo stars
- Updated March 4, 2026
- aj-geddes/useful-ai-prompts
profiling-optimization is a Claude Code skill that helps developers profile CPU, memory, and I/O bottlenecks, interpret flame graphs, and apply targeted optimizations before release when latency or infrastructure cost re
About
profiling-optimization is a prompt-driven skill from aj-geddes/useful-ai-prompts for systematic performance investigation before shipping. It guides developers through collecting CPU, memory, and I/O profiles, reading flame graphs, isolating hot paths, and prioritizing fixes that reduce latency or cloud spend. Teams invoke profiling-optimization when benchmarks slip, p99 latency spikes, or infra bills climb after recent changes. The skill emphasizes evidence-backed tuning—measure first, optimize second—so changes target real bottlenecks instead of speculative micro-optimizations across the codebase.
- CPU, memory, and I/O profiling workflow
- Flame graph and hotspot interpretation
- Before/after benchmark methodology
- Database and cache bottleneck triage
- Safe optimization prioritization by user impact
Profiling Optimization by the numbers
- 410 all-time installs (skills.sh)
- Ranked #106 of 596 Debugging skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill profiling-optimizationAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 410 |
|---|---|
| repo stars | ★ 305 |
| Last updated | March 4, 2026 |
| Repository | aj-geddes/useful-ai-prompts ↗ |
How do you profile CPU and memory bottlenecks?
Profile CPU, memory, and I/O bottlenecks, interpret flame graphs, and apply targeted optimizations before release when latency or cost regressions threaten user experience or infra spend.
Who is it for?
Backend or platform engineers investigating latency regressions, memory leaks, or I/O saturation before a production release.
Skip if: Greenfield projects with no performance baseline or teams needing automated load-test infrastructure setup instead of analysis guidance.
When should I use this skill?
A developer reports latency spikes, high CPU or memory usage, I/O saturation, or rising infra costs and needs a structured profiling workflow.
What you get
Profiling runbook, flame graph interpretation notes, ranked bottleneck list, and a targeted optimization plan with expected latency or cost impact.
- Profiling runbook
- Bottleneck analysis report
- Prioritized optimization plan
Files
Profiling & Optimization
Table of Contents
Overview
Profile code execution to identify performance bottlenecks and optimize critical paths using data-driven approaches.
When to Use
- Performance optimization
- Identifying CPU bottlenecks
- Optimizing hot paths
- Investigating slow requests
- Reducing latency
- Improving throughput
Quick Start
Minimal working example:
import { performance, PerformanceObserver } from "perf_hooks";
class Profiler {
private marks = new Map<string, number>();
mark(name: string): void {
this.marks.set(name, performance.now());
}
measure(name: string, startMark: string): number {
const start = this.marks.get(startMark);
if (!start) throw new Error(`Mark ${startMark} not found`);
const duration = performance.now() - start;
console.log(`${name}: ${duration.toFixed(2)}ms`);
return duration;
}
async profile<T>(name: string, fn: () => Promise<T>): Promise<T> {
const start = performance.now();
try {
return await fn();
} finally {
// ... (see reference guides for full implementation)Reference Guides
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| Node.js Profiling | Node.js Profiling |
| Chrome DevTools CPU Profile | Chrome DevTools CPU Profile |
| Python cProfile | Python cProfile |
| Benchmarking | Benchmarking |
| Database Query Profiling | Database Query Profiling |
| Flame Graph Generation | Flame Graph Generation |
Best Practices
✅ DO
- Profile before optimizing
- Focus on hot paths
- Measure impact of changes
- Use production-like data
- Consider memory vs speed tradeoffs
- Document optimization rationale
❌ DON'T
- Optimize without profiling
- Ignore readability for minor gains
- Skip benchmarking
- Optimize cold paths
- Make changes without measurement
Benchmarking
Benchmarking
class Benchmark {
async run(
name: string,
fn: () => Promise<any>,
iterations: number = 1000,
): Promise<void> {
console.log(`\nBenchmarking: ${name}`);
const times: number[] = [];
// Warmup
for (let i = 0; i < 10; i++) {
await fn();
}
// Actual benchmark
for (let i = 0; i < iterations; i++) {
const start = performance.now();
await fn();
times.push(performance.now() - start);
}
// Statistics
const sorted = times.sort((a, b) => a - b);
const min = sorted[0];
const max = sorted[sorted.length - 1];
const avg = times.reduce((a, b) => a + b, 0) / times.length;
const p50 = sorted[Math.floor(sorted.length * 0.5)];
const p95 = sorted[Math.floor(sorted.length * 0.95)];
const p99 = sorted[Math.floor(sorted.length * 0.99)];
console.log(` Iterations: ${iterations}`);
console.log(` Min: ${min.toFixed(2)}ms`);
console.log(` Max: ${max.toFixed(2)}ms`);
console.log(` Avg: ${avg.toFixed(2)}ms`);
console.log(` P50: ${p50.toFixed(2)}ms`);
console.log(` P95: ${p95.toFixed(2)}ms`);
console.log(` P99: ${p99.toFixed(2)}ms`);
}
async compare(
implementations: Array<{ name: string; fn: () => Promise<any> }>,
iterations: number = 1000,
): Promise<void> {
for (const impl of implementations) {
await this.run(impl.name, impl.fn, iterations);
}
}
}
// Usage
const bench = new Benchmark();
await bench.compare([
{
name: "Array.filter + map",
fn: async () => {
const arr = Array.from({ length: 1000 }, (_, i) => i);
return arr.filter((x) => x % 2 === 0).map((x) => x * 2);
},
},
{
name: "Single loop",
fn: async () => {
const arr = Array.from({ length: 1000 }, (_, i) => i);
const result = [];
for (const x of arr) {
if (x % 2 === 0) {
result.push(x * 2);
}
}
return result;
},
},
]);Chrome DevTools CPU Profile
Chrome DevTools CPU Profile
import inspector from "inspector";
import fs from "fs";
class CPUProfiler {
private session: inspector.Session | null = null;
start(): void {
this.session = new inspector.Session();
this.session.connect();
this.session.post("Profiler.enable");
this.session.post("Profiler.start");
console.log("CPU profiling started");
}
async stop(outputFile: string): Promise<void> {
if (!this.session) return;
this.session.post("Profiler.stop", (err, { profile }) => {
if (err) {
console.error("Profiling error:", err);
return;
}
fs.writeFileSync(outputFile, JSON.stringify(profile));
console.log(`Profile saved to ${outputFile}`);
this.session!.disconnect();
this.session = null;
});
}
}
// Usage
const cpuProfiler = new CPUProfiler();
// Start profiling
cpuProfiler.start();
// Run code to profile
await runExpensiveOperation();
// Stop and save
await cpuProfiler.stop("./profile.cpuprofile");Database Query Profiling
Database Query Profiling
import { Pool } from "pg";
class QueryProfiler {
constructor(private pool: Pool) {}
async profileQuery(
query: string,
params: any[] = [],
): Promise<{
result: any;
planningTime: number;
executionTime: number;
plan: any;
}> {
// Enable timing
await this.pool.query("SET track_io_timing = ON");
// Get query plan
const explainResult = await this.pool.query(
`EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) ${query}`,
params,
);
const plan = explainResult.rows[0]["QUERY PLAN"][0];
// Execute actual query
const start = performance.now();
const result = await this.pool.query(query, params);
const duration = performance.now() - start;
return {
result: result.rows,
planningTime: plan["Planning Time"],
executionTime: plan["Execution Time"],
plan,
};
}
formatPlan(plan: any): string {
let output = "Query Plan:\n";
output += `Planning Time: ${plan["Planning Time"]}ms\n`;
output += `Execution Time: ${plan["Execution Time"]}ms\n\n`;
const formatNode = (node: any, indent: number = 0) => {
const prefix = " ".repeat(indent);
output += `${prefix}${node["Node Type"]}\n`;
output += `${prefix} Cost: ${node["Total Cost"]}\n`;
output += `${prefix} Rows: ${node["Actual Rows"]}\n`;
output += `${prefix} Time: ${node["Actual Total Time"]}ms\n`;
if (node.Plans) {
node.Plans.forEach((child: any) => formatNode(child, indent + 1));
}
};
formatNode(plan.Plan);
return output;
}
}
// Usage
const profiler = new QueryProfiler(pool);
const { result, planningTime, executionTime, plan } =
await profiler.profileQuery("SELECT * FROM users WHERE age > $1", [25]);
console.log(profiler.formatPlan(plan));Flame Graph Generation
Flame Graph Generation
# Generate flame graph using 0x
npx 0x -o flamegraph.html node server.js
# Or using clinic.js
npx clinic doctor --on-port 'autocannon localhost:3000' -- node server.js
npx clinic flame --on-port 'autocannon localhost:3000' -- node server.jsNode.js Profiling
Node.js Profiling
import { performance, PerformanceObserver } from "perf_hooks";
class Profiler {
private marks = new Map<string, number>();
mark(name: string): void {
this.marks.set(name, performance.now());
}
measure(name: string, startMark: string): number {
const start = this.marks.get(startMark);
if (!start) throw new Error(`Mark ${startMark} not found`);
const duration = performance.now() - start;
console.log(`${name}: ${duration.toFixed(2)}ms`);
return duration;
}
async profile<T>(name: string, fn: () => Promise<T>): Promise<T> {
const start = performance.now();
try {
return await fn();
} finally {
const duration = performance.now() - start;
console.log(`${name}: ${duration.toFixed(2)}ms`);
}
}
}
// Usage
const profiler = new Profiler();
app.get("/api/users", async (req, res) => {
profiler.mark("request-start");
const users = await profiler.profile("fetch-users", async () => {
return await db.query("SELECT * FROM users");
});
profiler.measure("total-request-time", "request-start");
res.json(users);
});Python cProfile
Python cProfile
import cProfile
import pstats
from pstats import SortKey
import io
class Profiler:
def __init__(self):
self.profiler = cProfile.Profile()
def __enter__(self):
self.profiler.enable()
return self
def __exit__(self, *args):
self.profiler.disable()
def print_stats(self, sort_by: str = 'cumulative'):
"""Print profiling statistics."""
s = io.StringIO()
ps = pstats.Stats(self.profiler, stream=s)
if sort_by == 'time':
ps.sort_stats(SortKey.TIME)
elif sort_by == 'cumulative':
ps.sort_stats(SortKey.CUMULATIVE)
elif sort_by == 'calls':
ps.sort_stats(SortKey.CALLS)
ps.print_stats(20) # Top 20
print(s.getvalue())
def save_stats(self, filename: str):
"""Save profiling data."""
self.profiler.dump_stats(filename)
# Usage
with Profiler() as prof:
# Code to profile
result = expensive_function()
prof.print_stats('cumulative')
prof.save_stats('profile.prof')#!/bin/bash
# validate-api.sh - Validate API specification
# Usage: ./validate-api.sh <openapi_spec>
set -euo pipefail
SPEC_FILE="${{1:?Usage: $0 <openapi_spec>}}"
echo "Validating API spec: $SPEC_FILE"
# TODO: Add API validation
# - Validate OpenAPI/Swagger syntax
# - Check endpoint naming conventions
# - Verify response schemas
# - Check for required headers
# - Validate authentication definitions
echo "API validation complete."
# API Endpoint Scaffold
# TODO: Customize for your API framework
openapi: "3.0.3"
info:
title: "API Service"
version: "1.0.0"
paths:
/api/v1/resource:
get:
summary: "List resources"
# TODO: Define parameters and responses
responses:
"200":
description: "Success"
post:
summary: "Create resource"
# TODO: Define request body and responses
responses:
"201":
description: "Created"
Related skills
How it compares
Use profiling-optimization for guided bottleneck analysis; pair with load-testing skills when you need synthetic traffic generation rather than profile interpretation.
FAQ
What does profiling-optimization analyze?
profiling-optimization covers CPU, memory, and I/O profiling with flame graph interpretation. The skill helps developers locate hot paths and expensive operations, then rank fixes that reduce latency or infrastructure spend before release.
When should profiling-optimization run in a release cycle?
profiling-optimization belongs before release when benchmarks slip or costs rise after recent changes. It produces a measured bottleneck list and optimization plan rather than ad hoc tuning without profiling evidence.