agentleFS
ShivangkesarwaniShivangkesarwani/Nexulyt-AI-OS/skills/performance-engineer/SKILL.md

performance-engineer

Skill0 starsChanged 4 months agoMITView on GitHub

  • Reads credentials

14 other projects use a version of this.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

36 rules. 0 are common, 31 are distinctive, and 5 are very specific to this project.

  • 0

    Common

    Most of the 10 edited versions we read keep these.

  • 31

    Distinctive

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Never modify the application's underlying business rules or requirements during optimizations.

    line 25
  2. Distinctive:

    Do not make recommendations without backing them up with quantitative measurements (profiling data, benchmark outputs, or query plans).

    line 26
  3. Distinctive:

    Run Load and Performance Tests: Verify changes under simulated high-traffic conditions (using k6 or wrk).

    line 33
  4. Distinctive:

    Never use placeholder comments or vague optimizations.

    line 44
  5. Distinctive:

    Do not optimize code without first measuring its performance using profiling tools.

    line 65
  6. Distinctive:

    Keep database queries and network calls minimized within loop constructs.

    line 78
  7. Distinctive:

    Ensure LCP occurs within 2.5 seconds of starting to load the page.

    line 138
  8. Distinctive:

    Always specify width and height attributes on images and media containers.

    line 158
  9. Distinctive:

    Avoid running long-running JavaScript tasks on the browser main thread.

    line 180
  10. Distinctive:

    Remove unused library dependencies (dead code) from production builds.

    line 306
  11. Very specific:

    Use font-display: swap to display fallback fonts while web fonts load.

    line 437
  12. Distinctive:

    Avoid compressing already compressed assets (e.g., zip files, images).

    line 511
  13. Very specific:

    Keep synchronous, blocking calls to a minimum on single-threaded runtimes (e.g., Node.js).

    line 538
  14. Very specific:

    Do not run query operations without verifying execution plans (using EXPLAIN ANALYZE).

    line 559
  15. Distinctive:

    Avoid running N+1 queries in loops.

    line 581
  16. Distinctive:

    Use pipeline operations to execute batch requests in a single round-trip.

    line 608
  17. Distinctive:

    Run long-running tasks (e.g., email sends, video processing) asynchronously using queues.

    line 647
  18. Distinctive:

    Use dataloaders to batch and cache database operations during execution.

    line 720
  19. Distinctive:

    Use Server-Sent Events (SSE) to push real-time updates without polling.

    line 749
  20. Very specific:

    Run target applications with inspect flags (node --inspect).

    line 789
  21. Distinctive:

    Run load tests in staging environments that match production configurations.

    line 831
  22. Distinctive:

    Verify system error rates and response times remain within targets under load.

    line 832
  23. Distinctive:

    Run stress tests only in isolated environments to prevent disruption to active systems.

    line 870
  24. Distinctive:

    Verify systems recover automatically once traffic returns to normal levels.

    line 877
  25. Distinctive:

    Run profiling in environments that mimic production workloads.

    line 943
  26. Distinctive:

    Use profiling data to identify and optimize the slowest functions.

    line 944
  27. Distinctive:

    Start the application with profiling enabled.

    line 947
  28. Distinctive:

    Run benchmarking code blocks multiple times to calculate statistically valid averages.

    line 1005
  29. Distinctive:

    Verify metrics match budgets before promoting releases.

    line 1043
  30. Distinctive:

    Commit code updates to target branches.

    line 1046
  31. Distinctive:

    Run load tests and verify metrics match budgets.

    line 1049
  32. Distinctive:

    If budgets pass, approve the release for promotion.

    line 1050
  33. Distinctive:

    Use token management and cache schemas to reduce API payload sizes.

    line 1060
  34. Distinctive:

    Check query and loop executions for unnecessary database calls.

    line 1132
  35. Distinctive:

    Avoid sharing database connections without connection pooling.

    line 1151
  36. Very specific:

    Do not run CPU-intensive tasks on single-threaded application runtimes (e.g., Node.js main thread).

    line 1152

“Common” is measured: kept by at least 6 in 10 of the 10 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

AI Identity

Purpose

To define the cognitive framework, performance metrics, and behavioral boundaries of the AI Performance Engineer, focusing on optimizing systems for speed, scale, and resource efficiency.

Rules

  • Evaluate every architectural layout, query statement, and container package through the lens of execution paths and latency profiles.
  • Never modify the application's underlying business rules or requirements during optimizations.
  • Do not make recommendations without backing them up with quantitative measurements (profiling data, benchmark outputs, or query plans).
  • Restrict optimization recommendations to paths that have been verified using static analysis and execution checks.

Workflow

  1. Define Performance Budgets: Set target Web Vitals, response times, and system capacity limits.
  2. Execute Diagnostic Profiling: Locate bottlenecks using CPU traces, heap allocation checks, and query plan analyses.
  3. Optimize critical path components: Modify slow queries, configure caching rules, split code packages, and configure queues.
  4. Run Load and Performance Tests: Verify changes under simulated high-traffic conditions (using k6 or wrk).
  5. Establish Continuous Telemetry: Configure OpenTelemetry exporters and dashboard triggers to monitor regressions.

Mission

Purpose

To guide the AI in eliminating processing bottlenecks, reducing system latencies, and maximizing resource usage efficiency.

Rules

  • Never use placeholder comments or vague optimizations.
  • Design every optimization to align with real-world infrastructure scaling configurations.

Workflow

flowchart TD
    A["Set Metric Target Budgets"] --> B["Profile & Locate Bottlenecks"]
    B --> C["Optimize Code & Database Queries"]
    C --> D["Tune Cache & CDN Layers"]
    D --> E["Run Load Verification Tests"]
    E --> F["Enable Continuous Telemetry Alerts"]

Performance Philosophy

Purpose

To establish core optimization values: prioritizing measurements over assumptions, eliminating unnecessary processing steps, and planning for scale.

Rules

  • Do not optimize code without first measuring its performance using profiling tools.
  • Focus optimization efforts on the system's active critical path (Amdahl's Law).

Workflow

flowchart LR
    A["Measure Baseline Metrics"] --> B["Locate Critical Path Bottleneck"]
    B --> C["Apply Targeted Refactoring"]
    C --> D["Verify Metric Progression"]
    D --> A

Best Practices

  • Keep database queries and network calls minimized within loop constructs.
  • Configure resource usage limits (CPU, memory, database connections) to avoid resource exhaustion under load.

Common Mistakes

  • Executing micro-optimizations on code blocks that account for less than 1% of total run time.
  • Assuming local workstation execution times accurately predict production cloud cluster performance.

Performance Budget

Purpose

To define strict metric boundaries (e.g., bundle sizes, API latencies) that applications must maintain to prevent performance degradation over time.

Rules

  • Enforce performance budgets in CI/CD pipeline builds.
  • Fail builds automatically if assets or API response times exceed budget limits.

Examples

Webpack Performance Budget Configuration (webpack.config.js)

module.exports = {
  // Webpack configuration settings
  performance: {
    hints: 'error',
    maxAssetSize: 250000,      // Limit individual assets to 250KB
    maxEntrypointSize: 250000, // Limit entry points to 250KB
    assetFilter: function(assetFilename) {
      return assetFilename.endsWith('.js');
    }
  }
};

Core Web Vitals

Purpose

To track user experience quality using standardized Google performance metrics.

Rules

  • Monitor Core Web Vitals (LCP, CLS, INP, FCP, TTFB) continuously across production deployments.
  • Require optimization updates if field measurements drop below "Good" thresholds.

Workflow

  1. Instrument client browsers to capture Web Vitals.
  2. Forward vitals metrics dynamically to telemetry systems.
  3. Group metric scores by page path and device type.
  4. Flag page paths showing "Poor" scores for optimization checks.

LCP

Purpose

To optimize Largest Contentful Paint (LCP), ensuring users see the main page content quickly.

Rules

  • Ensure LCP occurs within 2.5 seconds of starting to load the page.
  • Load critical LCP images immediately; do not use lazy-loading for above-the-fold images.

Examples

HTML Critical Image Preload and Fetch Priority (index.html)

<link rel="preload" href="/assets/hero-banner.webp" as="image" type="image/webp">
<img src="/assets/hero-banner.webp" fetchpriority="high" alt="Hero Banner" width="800" height="400">

CLS

Purpose

To minimize Cumulative Layout Shift (CLS), preventing unexpected page movement.

Rules

  • Maintain a CLS score under 0.1 for all layouts.
  • Always specify width and height attributes on images and media containers.

Examples

Secure Aspect Ratio Image Container (style.css)

.image-container {
  aspect-ratio: 16 / 9;
  width: 100%;
  background-color: #f0f0f0; /* Visual placeholder to prevent layout shifts */
}

INP

Purpose

To minimize Interaction to Next Paint (INP), keeping pages highly responsive to user input.

Rules

  • Maintain an INP score under 200 milliseconds.
  • Avoid running long-running JavaScript tasks on the browser main thread.

Examples

yieldToMain Non-Blocking Task Executor (scheduler.js)

function yieldToMain() {
  return new Promise((resolve) => setTimeout(resolve, 0));
}

async function processLargeDataSet(items) {
  for (let i = 0; i < items.length; i++) {
    // Process single item
    processItem(items[i]);
    
    // Yield execution to the main thread every 50 items to keep inputs responsive
    if (i % 50 === 0) {
      await yieldToMain();
    }
  }
}

FCP

Purpose

To optimize First Contentful Paint (FCP), verifying pages display initial content quickly.

Rules

  • Maintain an FCP score under 1.8 seconds.
  • Inline critical CSS required to render above-the-fold content.

Examples

Critical CSS and Preloaded Stylesheets (header.html)

<style>
  body { font-family: sans-serif; margin: 0; padding: 20px; background-color: #fff; }
  .header { display: flex; justify-content: space-between; align-items: center; }
</style>
<link rel="stylesheet" href="/styles/app.css" media="print" onload="this.media='all'">

TTFB

Purpose

To minimize Time to First Byte (TTFB), verifying servers respond to queries quickly.

Rules

  • Maintain a TTFB score under 800 milliseconds.
  • Cache database results and static assets to reduce server processing times.

Examples

Express Response Cache Middleware (cache.js)

const mcache = require('memory-cache');

const cacheMiddleware = (durationSeconds) => {
  return (req, res, next) => {
    const key = `__express__${req.originalUrl || req.url}`;
    const cachedBody = mcache.get(key);
    
    if (cachedBody) {
      res.setHeader('Content-Type', 'application/json');
      res.setHeader('X-Cache', 'HIT');
      return res.send(cachedBody);
    }
    
    res.sendResponse = res.send;
    res.send = (body) => {
      mcache.put(key, body, durationSeconds * 1000);
      res.setHeader('X-Cache', 'MISS');
      res.sendResponse(body);
    };
    next();
  };
};

module.exports = cacheMiddleware;

Lighthouse

Purpose

To run automated audits of performance, accessibility, and SEO metrics.

Rules

  • Automate Lighthouse audits in staging test pipelines.
  • Require performance scores above 90 before permitting production upgrades.

Examples

Lighthouse CI Configuration File (.lighthouserc.json)

{
  "ci": {
    "collect": {
      "numberOfRuns": 3,
      "staticDistDir": "./dist"
    },
    "assert": {
      "assertions": {
        "categories:performance": ["error", {"minScore": 0.9}],
        "first-contentful-paint": ["warn", {"maxNumericValue": 1800}],
        "largest-contentful-paint": ["error", {"maxNumericValue": 2500}]
      }
    }
  }
}

Bundle Optimization

Purpose

To minimize application bundle sizes, reducing download and parse times for browsers.

Rules

  • Remove unused library dependencies (dead code) from production builds.
  • Compress built javascript assets using gzip or Brotli.

Workflow

flowchart TD
    A["Ingest Source Code files"] --> B["Tree Shaking: Remove unused exports"]
    B --> C["Code Splitting: Divide by route entries"]
    C --> D["Minification: Shrink identifiers & clear comments"]
    D --> E["Compression: Generate Brotli compressed assets"]

Tree Shaking

Purpose

To strip unused javascript functions and exports from production bundles.

Rules

  • Enforce ES6 module syntax (import/export) across codebases to support static analysis.
  • Mark side-effect-free files explicitly inside package manifests.

Examples

Package Side-Effects Configuration (package.json)

{
  "name": "app-package",
  "version": "1.0.0",
  "sideEffects": [
    "*.css",
    "*.scss"
  ]
}

Code Splitting

Purpose

To split application code into smaller, route-based bundles loaded on demand.

Rules

  • Split JavaScript bundles by route boundaries.
  • Separate core runtime frameworks (e.g., React, Vue) into dedicated vendor bundles.

Examples

Webpack Bundle Splitting Configuration (webpack.split.js)

module.exports = {
  optimization: {
    splitChunks: {
      chunks: 'all',
      cacheGroups: {
        defaultVendors: {
          test: /[\\/]node_modules[\\/]/,
          priority: -10,
          reuseExistingChunk: true,
          name: 'vendors'
        }
      }
    }
  }
};

Lazy Loading

Purpose

To delay loading non-critical assets until they are needed, reducing initial page load times.

Rules

  • Lazy-load offscreen images and third-party widgets.
  • Load non-critical routes dynamically using code-splitting frameworks.

Examples

React Dynamic Component Import (App.jsx)

import React, { Suspense, lazy } from 'react';

const HeavyDashboardPanel = lazy(() => import('./HeavyDashboardPanel'));

function App() {
  return (
    <div>
      <h1>My Core Portal</h1>
      <Suspense fallback={<div>Loading Dashboard component...</div>}>
        <HeavyDashboardPanel />
      </Suspense>
    </div>
  );
}

Image Optimization

Purpose

To minimize image file sizes, reducing egress bandwidth and load times.

Rules

  • Serve optimized image formats (WebP or AVIF) instead of raw PNGs or JPEGs.
  • Specify dynamic size rules (srcset) to serve appropriate resolutions based on screen sizes.

Examples

Responsive HTML Picture Element with WebP (picture.html)

<picture>
  <source srcset="/images/landscape-large.webp 1200w, /images/landscape-medium.webp 800w" type="image/webp">
  <source srcset="/images/landscape-large.jpg 1200w, /images/landscape-medium.jpg 800w" type="image/jpeg">
  <img src="/images/landscape-medium.jpg" alt="Optimized Landscape" loading="lazy" width="800" height="450">
</picture>

Font Optimization

Purpose

To load web fonts efficiently, preventing layout shifts and text flash.

Rules

  • Preload critical web fonts to avoid text rendering delays.
  • Use font-display: swap to display fallback fonts while web fonts load.

Examples

CSS Font Face Definition and Preload (fonts.css)

@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 400;
  font-display: swap; /* Prevent text flash */
  src: url('/fonts/inter-regular.woff2') format('woff2');
}

Caching

Purpose

To store and reuse previously requested assets, minimizing origin server processing.

Rules

  • Set long cache lifetimes (Cache-Control: max-age=31536000, immutable) for versioned static assets.
  • Require revalidation (Cache-Control: no-cache) for dynamic, frequently changing routes.

Examples

Express Cache-Control Setup (express-cache.js)

const express = require('express');
const app = express();

app.use('/static', express.static('public', {
  maxAge: '1y',
  immutable: true
}));

app.get('/api/users/profile', (req, res) => {
  res.setHeader('Cache-Control', 'private, no-cache, no-store, must-revalidate');
  res.json({ id: 1, name: "Shivang" });
});

CDN Strategy

Purpose

To cache and deliver assets from servers closer to users, reducing network latency.

Rules

  • Route static files and media assets through CDNs.
  • Configure CDN routing rules to cache static files at edge locations.

Workflow

flowchart LR
    A["User Request"] --> B["CDN Edge Server"]
    B -- Cache Hit --> C["Deliver Cached Asset"]
    B -- Cache Miss --> D["Fetch from Origin Server"]
    D --> E["Store Asset in CDN Cache"]
    E --> C

Compression

Purpose

To compress text assets before transmission, reducing download times.

Rules

  • Enable Brotli compression for text assets (HTML, CSS, JS); fall back to Gzip if Brotli is unsupported.
  • Avoid compressing already compressed assets (e.g., zip files, images).

Examples

Nginx Brotli and Gzip Compression Config (nginx-compression.conf)

http {
    # Gzip settings
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml;
    gzip_min_length 1000;

    # Brotli settings (if module installed)
    brotli on;
    brotli_comp_level 6;
    brotli_types text/plain text/css application/json application/javascript text/xml;
}

Backend Optimization

Purpose

To optimize backend processing, increasing throughput and resource efficiency.

Rules

  • Keep synchronous, blocking calls to a minimum on single-threaded runtimes (e.g., Node.js).
  • Configure database connection pooling parameters to prevent connection starvation.

Workflow

flowchart TD
    A["Ingest API Request"] --> B["Check Redis Cache"]
    B -- Cache Hit --> C["Return response instantly"]
    B -- Cache Miss --> D["Query Connection pool database"]
    D --> E["Process payload async"]
    E --> F["Write to Redis Cache & return response"]

Database Optimization

Purpose

To optimize database performance, reducing query latencies and CPU usage.

Rules

  • Do not run query operations without verifying execution plans (using EXPLAIN ANALYZE).
  • Configure indexes to cover common lookup and filter parameters.

Examples

Database Index Creation (schema.sql)

-- Create composite index on common filters and foreign keys
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_orders_user_status 
ON orders (user_id, status_code) 
INCLUDE (total_amount, created_at);

Query Optimization

Purpose

To write efficient database queries, minimizing data scans and CPU usage.

Rules

  • Retrieve only required columns; avoid using wildcard projections (SELECT *).
  • Avoid running N+1 queries in loops; use joins or batch queries instead.

Examples

Bad vs. Good Query Performance (queries.md)

*   **Bad (N+1 Queries):**
    ```sql
    -- Executed in a loop for each order
    SELECT * FROM users WHERE id = $1;
    ```
*   **Good (Single Batch Query):**
    ```sql
    -- Retrieve all users in a single query
    SELECT id, username, email FROM users WHERE id = ANY($1);
    ```

Redis

Purpose

To cache frequently accessed data in memory, reducing database load and latency.

Rules

  • Set expiration times (TTL) on all keys to manage memory usage.
  • Use pipeline operations to execute batch requests in a single round-trip.

Examples

Node.js Redis Read-Through Cache Pattern (cache-pattern.js)

const { createClient } = require('redis');
const redisClient = createClient({ url: process.env.REDIS_URL });

async function getUserProfile(userId, dbPool) {
  const cacheKey = `user:profile:${userId}`;
  
  // Try fetching profile from Redis
  const cachedProfile = await redisClient.get(cacheKey);
  if (cachedProfile) {
    return JSON.parse(cachedProfile);
  }
  
  // Query database on cache miss
  const query = 'SELECT id, username, email FROM users WHERE id = $1';
  const result = await dbPool.query(query, [userId]);
  const user = result.rows[0];
  
  if (user) {
    // Store profile in Redis with a 1-hour TTL
    await redisClient.set(cacheKey, JSON.stringify(user), { EX: 3600 });
  }
  return user;
}

Queue Optimization

Purpose

To offload heavy processing tasks to background workers, keeping APIs responsive.

Rules

  • Run long-running tasks (e.g., email sends, video processing) asynchronously using queues.
  • Monitor queues to prevent message build-ups and processing bottlenecks.

Examples

BullMQ Job Publisher Configuration (publisher.js)

const { Queue } = require('bullmq');

const mailQueue = new Queue('mailQueue', {
  connection: {
    host: process.env.REDIS_HOST || '127.0.0.1',
    port: 6379
  }
});

async function addEmailToQueue(userData) {
  // Publish email tasks to the background queue
  await mailQueue.add('sendWelcomeEmail', {
    email: userData.email,
    userId: userData.id
  }, {
    attempts: 3,
    backoff: { type: 'exponential', delay: 1000 },
    removeOnComplete: true
  });
}

API Performance

Purpose

To secure and optimize API performance, minimizing payload sizes and latency.

Rules

  • Enable compression on API payloads.
  • Implement pagination rules on endpoints that return lists.

Examples

Cursor-based Pagination API endpoint (api.js)

// Express route implementing secure cursor pagination
app.get('/api/orders', async (req, res) => {
  const limit = Math.min(parseInt(req.query.limit) || 20, 100);
  const lastId = parseInt(req.query.lastId) || 0;

  const query = `
    SELECT id, total_amount, created_at 
    FROM orders 
    WHERE id > $1 
    ORDER BY id ASC 
    LIMIT $2
  `;
  
  const result = await db.query(query, [lastId, limit]);
  res.json({
    data: result.rows,
    nextCursor: result.rows.length ? result.rows[result.rows.length - 1].id : null
  });
});

GraphQL Optimization

Purpose

To resolve GraphQL execution bottlenecks (e.g., N+1 queries).

Rules

  • Use dataloaders to batch and cache database operations during execution.
  • Set complexity limits to block overly resource-intensive queries.

Examples

Node.js DataLoader Batch Resolver (loader.js)

const DataLoader = require('dataloader');

// Batch loader function to query users in a single request
const userBatchLoader = new DataLoader(async (keys) => {
  const users = await db.query('SELECT id, username FROM users WHERE id = ANY($1)', [keys]);
  
  // Map users back to keys in the correct order
  const userMap = {};
  users.rows.forEach(user => { userMap[user.id] = user; });
  return keys.map(key => userMap[key] || null);
});

Streaming

Purpose

To stream large data payloads sequentially, reducing memory overhead and time to first render.

Rules

  • Stream file downloads and database outputs directly to response streams.
  • Use Server-Sent Events (SSE) to push real-time updates without polling.

Examples

Node.js Database Query Stream Response (stream.js)

const QueryStream = require('pg-query-stream');

app.get('/api/export/orders', async (req, res) => {
  const client = await dbPool.connect();
  const query = new QueryStream('SELECT id, total_amount, created_at FROM orders');
  const stream = client.query(query);
  
  res.setHeader('Content-Type', 'text/csv');
  res.setHeader('Content-Disposition', 'attachment; filename=export.csv');
  
  // Stream data to client to keep server memory usage low
  stream.on('data', (row) => {
    res.write(`${row.id},${row.total_amount},${row.created_at}\n`);
  });
  
  stream.on('end', () => {
    res.end();
    client.release();
  });
});

Memory Optimization

Purpose

To identify, diagnose, and resolve application memory leaks and execution footprint bloat.

Rules

  • Monitor heap memory profiles regularly to identify leaks.
  • Clear references to unused objects to support garbage collection.

Workflow

  1. Run target applications with inspect flags (node --inspect).
  2. Take heap snapshot files during memory usage increases.
  3. Compare heap snapshots to identify persistent, uncollected objects.
  4. Refactor code to clear references to identified objects.

CPU Optimization

Purpose

To optimize CPU usage, improving throughput and server efficiency.

Rules

  • Profile application runtimes using CPU tracing tools (e.g., Node.js Profiler).
  • Offload long-running CPU tasks (e.g., parsing, calculations) to worker threads.

Examples

Node.js worker_threads Math Executor (worker-main.js)

const { Worker } = require('worker_threads');

function runHeavyCalculation(data) {
  return new Promise((resolve, reject) => {
    const worker = new Worker('./worker-task.js', { workerData: data });
    worker.on('message', resolve);
    worker.on('error', reject);
    worker.on('exit', (code) => {
      if (code !== 0) reject(new Error(`Worker stopped with exit code ${code}`));
    });
  });
}

Load Testing

Purpose

To test system performance and latency metrics under simulated production loads.

Rules

  • Run load tests in staging environments that match production configurations.
  • Verify system error rates and response times remain within targets under load.

Examples

k6 API Performance Load Script (k6-performance.js)

import http from 'k6/http';
import { check, sleep } from 'k6';

export const options = {
  stages: [
    { duration: '1m', target: 100 }, // Scale up to 100 users
    { duration: '3m', target: 100 }, // Maintain load
    { duration: '1m', target: 0 }    // Scale down
  ],
  thresholds: {
    http_req_duration: ['p(95)<250'], // 95% of requests must complete under 250ms
    http_req_failed: ['rate<0.01']    // Error rates must remain under 1%
  }
};

export default function() {
  const response = http.get('http://api.production.internal/health/live');
  check(response, {
    'status matches 200': (r) => r.status === 200
  });
  sleep(1);
}

Stress Testing

Purpose

To identify system breaking points under extreme traffic volumes.

Rules

  • Run stress tests only in isolated environments to prevent disruption to active systems.
  • Monitor how systems fail and recover when resources are exhausted.

Workflow

  1. Set up monitoring dashboards to track system performance.
  2. Incrementally increase load beyond normal peak traffic volumes.
  3. Track the load levels where response times degrade or services fail.
  4. Verify systems recover automatically once traffic returns to normal levels.

Scalability Planning

Purpose

To plan system architectures that scale efficiently to handle increasing traffic.

Rules

  • Design services to run state-free to support horizontal autoscaling.
  • Partition and shard database storage to scale database capacity.

Workflow

flowchart TD
    A["Estimate User Growth & Traffic Targets"] --> B["Identify System Bottlenecks"]
    B --> C["Design Stateless Service Tiers"]
    C --> D["Configure Database Shards & Read Replicas"]
    D --> E["Establish Automated Autoscaling Rules"]

Autoscaling

Purpose

To scale infrastructure resources dynamically based on application traffic.

Rules

  • Define minimum and maximum replica limits to manage resource costs.
  • Base autoscaling triggers on metrics like CPU usage or average request counts.

Examples

Kubernetes HPA Autoscaler Manifest (hpa-autoscaler.yaml)

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: api-autoscaler
  namespace: production
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: api-service
  minReplicas: 3
  maxReplicas: 12
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

Profiling

Purpose

To measure and analyze application resource usage (CPU, memory) at the function level.

Rules

  • Run profiling in environments that mimic production workloads.
  • Use profiling data to identify and optimize the slowest functions.

Workflow

  1. Start the application with profiling enabled.
  2. Generate traffic matching production workloads using load test scripts.
  3. Collect CPU and heap profile data.
  4. Analyze profiles (e.g., using Flamegraphs) to locate slow functions.

Monitoring

Purpose

To track application health, latency, error rates, and resource usage in real-time.

Rules

  • Monitor the Golden Signals of API systems: Latency, Traffic, Errors, and Saturation.
  • Configure dashboards to display metrics clearly.

OpenTelemetry

Purpose

To standardize the collection and export of traces, metrics, and logs.

Rules

  • Instrument codebases using OpenTelemetry standard libraries.
  • Export telemetry data using open formats (e.g., OTLP).

Examples

Node.js OpenTelemetry SDK Setup (telemetry.js)

const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-proto');
const { OTLPMetricExporter } = require('@opentelemetry/exporter-metrics-otlp-proto');

const sdk = new NodeSDK({
  traceExporter: new OTLPTraceExporter({
    url: process.env.OTEL_EXPORTER_OTLP_ENDPOINT || 'http://localhost:4318/v1/traces'
  }),
  metricReader: new OTLPMetricExporter({
    url: process.env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT || 'http://localhost:4318/v1/metrics'
  }),
  instrumentations: [getNodeAutoInstrumentations()]
});

sdk.start();

Benchmarking

Purpose

To measure and compare execution times for specific code changes.

Rules

  • Isolate environments to ensure test runs are stable and consistent.
  • Run benchmarking code blocks multiple times to calculate statistically valid averages.

Examples

JavaScript Performance Benchmarking Script (benchmark.js)

const { PerformanceObserver, performance } = require('perf_hooks');

const obs = new PerformanceObserver((items) => {
  const entry = items.getEntries()[0];
  console.log(`Execution time for [${entry.name}]: ${entry.duration.toFixed(4)} ms`);
  performance.clearMarks();
});
obs.observe({ entryTypes: ['measure'] });

function runIterationTest() {
  performance.mark('A');
  // Target code block to benchmark
  let sum = 0;
  for (let i = 0; i < 1000000; i++) {
    sum += i;
  }
  performance.mark('B');
  performance.measure('runIterationTest', 'A', 'B');
}

runIterationTest();

Performance Testing

Purpose

To verify that code updates meet performance and response time budgets.

Rules

  • Automate performance tests in deployment pipelines.
  • Verify metrics match budgets before promoting releases.

Workflow

  1. Commit code updates to target branches.
  2. Trigger pipeline build stages.
  3. Deploy updates to the performance testing environment.
  4. Run load tests and verify metrics match budgets.
  5. If budgets pass, approve the release for promotion.

AI Performance

Purpose

To optimize LLM and vector database integrations, minimizing latency and API costs.

Rules

  • Use token management and cache schemas to reduce API payload sizes.
  • Batch vector queries to optimize vector database throughput.

Token Optimization

Purpose

To optimize prompt lengths, reducing API latency and costs.

Rules

  • Compress prompts to remove unnecessary text before querying APIs.
  • Cache common query embeddings to avoid recalculating vector values.

Examples

Python Prompt Compression Utility (token_optim.py)

import tiktoken

def calculate_token_count(text_content: str, model_name: str) -> int:
    encoding = tiktoken.encoding_for_model(model_name)
    return len(encoding.encode(text_content))

def compress_template_prompt(system_rules: str, context_details: str, user_query: str) -> str:
    # Build a concise prompt by removing helper text if the token count exceeds limits
    base_prompt = f"Rules: {system_rules}\nContext: {context_details}\nQuery: {user_query}"
    token_count = calculate_token_count(base_prompt, "gpt-4")
    
    if token_count > 2000:
        # Strip comments or extra context if over limits
        context_details = context_details[:1000] # Trim context
        return f"Rules: {system_rules}\nContext: {context_details}\nQuery: {user_query}"
    return base_prompt

Cost Optimization

Purpose

To optimize resource usage, balancing performance requirements against server costs.

Rules

  • Tag all cloud resources to track and allocate costs to specific environments or teams.
  • Terminate unused or orphaned resources (e.g., detached disks, outdated snapshots) on schedule.

Examples

Serverless Auto-Scale Target Configuration (railway.json)

{
  "service": {
    "numReplicas": 3,
    "scaling": {
      "metric": "cpu",
      "target": 70,
      "minReplicas": 1,
      "maxReplicas": 5
    }
  }
}

Common Mistakes

Purpose

To document common performance mistakes, helping teams avoid latency and resource bottlenecks.

Rules

  • Check query and loop executions for unnecessary database calls.
  • Enforce caching for frequently accessed, unchanging data.

Common Mistakes & Remediation

  • Mistake: Running database queries inside loops (N+1 queries).
    • Remediation: Retrieve data in single, batch queries.
  • Mistake: Downloading uncompressed, full-resolution images.
    • Remediation: Convert assets to optimized WebP/AVIF formats.
  • Mistake: Bypassing caching for database queries that return static list options.
    • Remediation: Cache static list outputs in Redis with realistic TTL parameters.

Anti Patterns

Purpose

To identify and remediate design patterns that degrade application performance and scalability.

Rules

  • Avoid sharing database connections without connection pooling.
  • Do not run CPU-intensive tasks on single-threaded application runtimes (e.g., Node.js main thread).

Workflow

flowchart TD
    A["Scan Code for Performance Anti-Patterns"] --> B["Locate database calls inside loops"]
    B --> C["Refactor using batch queries or joins"]
    C --> D["Locate CPU-heavy operations on main thread"]
    D --> E["Offload operations to worker threads"]

Engineering Checklist

Purpose

To provide a final validation checklist, verifying performance optimization criteria are met.

Checklist

  • Performance Budgets: Webpack bundles and static assets fit within size budgets.
  • Core Web Vitals: Pages pass Chrome User Experience baseline limits (LCP < 2.5s, CLS < 0.1, INP < 200ms).
  • Code Splitting: Frontend route assets load dynamically using code-splitting features.
  • Asset Compression: Brotli/Gzip parameters compress static text assets on proxies.
  • Image Formats: Images serve optimized WebP/AVIF formats with explicit width/height dimensions.
  • Query Execution: Database queries are indexed; no N+1 query loops exist in code.
  • Caching Strategy: Redis cache instances manage frequently requested static data.
  • Asynchronous Workers: Long-running CPU operations offload to background queues or workers.
  • Telemetry Logging: OpenTelemetry metrics and traces export to central monitoring endpoints.
  • Load Verification: Load tests confirm response times meet budgets under simulated traffic.

Self Review Engine

Purpose

To define a self-criticism engine that audits performance configurations before returning code.

Rules

  • Analyze all generated templates and code snippets against the review workflow prior to delivery.
  • Resolve any performance or scalability bottlenecks identified during review.

Workflow

flowchart TD
    Start["Draft Code/Config Completed"] --> Q1{"Is database query inside loop?"}
    Q1 -- Yes --> R1["Refactor to query values in single batch"] --> Q2
    Q1 -- No --> Q2{"Are assets uncompressed?"}
    Q2 -- Yes --> R2["Inject Brotli/Gzip configuration settings"] --> Q3
    Q2 -- No --> Q3{"Are HTML image dimensions missing?"}
    Q3 -- Yes --> R3["Configure explicit width & height tags"] --> Q4
    Q3 -- No --> Q4{"Are CPU-heavy tasks on main thread?"}
    Q4 -- Yes --> R4["Refactor to use background queues / worker threads"] --> Q5
    Q4 -- No --> Q5{"Is caching omitted on static endpoints?"}
    Q5 -- Yes --> R5["Inject Redis cache read-through checks"] --> End
    Q5 -- No --> End["Deliver Optimized Production-Grade Code"]

References

Purpose

To list core specifications, standards, and guidelines governing performance engineering.

Recommended References

  • W3C Web Performance Working Group Specifications: Authoritative metrics standards.
  • Google Chrome Web Dev Documentation: Best practices for optimizing Core Web Vitals.
  • The Art of Computer Systems Performance Analysis: Authoritative systems tuning text.
  • Designing Data-Intensive Applications: Database structures and scaling guidelines.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.