DEV Community

shashank ms
shashank ms

Posted on

Monitoring Complex Coding Performance: Tools and Techniques

I recently shipped an internal tool that profiles Python functions and diagnoses performance bottlenecks with an LLM. In this tutorial we will build a lightweight agent that measures execution time, extracts static complexity metrics, and generates an optimization report via Oxlo.ai. It is useful for teams that want automated first-pass code reviews before expensive human review.

What you'll need

Step 1: Scaffold the profiler script

I start every project with a single file and a concrete target. Create perf_monitor.py and add a deliberately inefficient function so we have a real bottleneck to detect. I also add a simple timer that runs the function against a known input.

import time
import ast
import json
from openai import OpenAI

TARGET_CODE = '''
def find_duplicates(data):
    result = []
    for i in range(len(data)):
        for j in range(i + 1, len(data)):
            if data[i] == data[j] and data[i] not in result:
                result.append(data[i])
    return result
'''

def benchmark():
    namespace = {}
    exec(TARGET_CODE, namespace)
    fn = namespace["find_duplicates"]
    test_data = list(range(500)) + [250]
    start = time.perf_counter()
    result = fn(test_data)
    elapsed = time.perf_counter() - start
    return elapsed, result

Step 2: Extract static complexity metrics

Raw timing data is not enough. I walk the AST to count loops and branches so the model gets hard numbers instead of guessing structure from raw text.

def extract_metrics(source: str):
    tree = ast.parse(source)
    loops = sum(
        1 for node in ast.walk(tree)
        if isinstance(node, (ast.For, ast.While))
    )
    conditionals = sum(
        1 for node in ast.walk(tree)
        if isinstance(node, (ast.If, ast.IfExp))
    )
    return {
        "loops": loops,
        "conditionals": conditionals,
        "lines": len([l for l in source.splitlines() if l.strip()]),
    }

Step 3: Define the system prompt

The agentโ€™s personality lives in the system prompt. I keep it strict and structured so the output is predictable and easy to parse downstream.

SYSTEM_PROMPT = """You are a senior performance engineer.
Analyze the provided Python function using its source code, static metrics, and execution time.
Identify algorithmic complexity bottlenecks, redundant work, and memory inefficiencies.
Respond with valid JSON containing exactly these keys:
- summary: one sentence describing the core issue
- complexity_analysis: explanation of time and space complexity
- recommendations: list of specific, actionable fixes
- refactored_code: a corrected Python implementation
Be concise. Do not include markdown fencing around the JSON."""

Step 4: Wire up the Oxlo.ai client

I use Oxlo.ai because its request-based pricing keeps costs flat even when I pass large modules or long stack traces into the prompt. For coding analysis I pick deepseek-v3.2, which is available on the free tier and handles reasoning tasks well. The client is a drop-in replacement for the OpenAI SDK.

user_message = (
    f"Function source:\n{TARGET_CODE}\n\n"
    f"Static metrics: {json.dumps(metrics)}\n"
    f"Execution time (seconds): {elapsed:.6f}\n"
    f"Test output length: {len(output)}"
)

client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

response = client.chat.completions.create(
    model="deepseek-v3.2",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": user_message},
    ],
)

report = response.choices[0].message.content

Step 5: Assemble the CLI runner

Finally, I glue the pieces together in a main block so the script can be run directly.

if __name__ == "__main__":
    elapsed, output = benchmark()
    metrics = extract_metrics(TARGET_CODE)
    print(f"Elapsed: {elapsed:.6f}s | Metrics: {metrics}")

    user_message = (
        f"Function source:\n{TARGET_CODE}\n\n"
        f"Static metrics: {json.dumps(metrics)}\n"
        f"Execution time (seconds): {elapsed:.6f}\n"
        f"Test output length: {len(output)}"
    )

    client = OpenAI(base_url="https://api.oxlo.ai/v1", api_key="YOUR_OXLO_API_KEY")

    response = client.chat.completions.create(
        model="deepseek-v3.2",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": user_message},
        ],
    )

    print("\n--- Performance Report ---\n")
    print(response.choices[0].message.content)

Run it

Install the dependency, set your key, and execute the monitor.

pip install openai
export OXLO_API_KEY="sk-..."
python perf_monitor.py

On my machine the output looks like this:

Elapsed: 0.024531s | Metrics: {'loops': 2, 'conditionals': 2, 'lines': 7}

--- Performance Report ---

{
  "summary": "The function uses nested loops with an O(n^2) scan and a linear membership test inside the inner loop, creating O(n^3) behavior in the worst case.",
  "complexity_analysis": "The two nested for loops generate n*(n-1)/2 comparisons. The guard `data[i] not in result` scans the result list on every match, adding an extra O(n) factor and heavy constant-time overhead.",
  "recommendations": [
    "Replace nested iteration with a single pass using a set for O(1) lookups.",
    "Use two sets, `seen` and `duplicates`, to eliminate the linear `not in` check."
  ],
  "refactored_code": "def find_duplicates(data):\n    seen = set()\n    dups = set()\n    for item in data:\n        if item in seen:\n            dups.add(item)\n        else:\n            seen.add(item)\n    return list(dups)"
}

Wrap-up and next steps

This agent gives you an automated first pass on algorithmic bottlenecks. Because Oxlo.ai charges per request rather than per token, feeding entire modules or long stack traces into the context window does not inflate your bill the way token-based providers do. You can review details at https://oxlo.ai/pricing.

Two concrete next steps: integrate this into a pre-commit hook so developers get instant feedback before pushing, or extend the AST visitor to compute cyclomatic complexity with radon and add a latency regression check that fails CI when a refactor slows down by more than ten percent.

Top comments (0)