DEV Community

shashank ms
shashank ms

Posted on

Monitoring Complex Coding Performance: Best Practices and Tools

We are building a lightweight coding performance monitor that ingests Python functions and returns structured complexity scores, bottleneck warnings, and optimization hints. It runs entirely through Oxlo.ai's API, so there is no local GPU setup or model management to worry about. The final tool is a single Python file you can drop into any repo and run against new commits.

What you will need

Step 1: Set up the Oxlo.ai client and input parser

I start by initializing the OpenAI-compatible client pointing at Oxlo.ai and writing a small helper to read a target file. This keeps the API logic separate from the analysis logic.

import sys
from openai import OpenAI

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

def load_function(path):
    with open(path, "r") as f:
        return f.read()

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("usage: python monitor.py ")
        sys.exit(1)
    source = load_function(sys.argv[1])
    print(f"loaded {len(source)} characters")

Step 2: Design the system prompt for structured analysis

The system prompt is the contract. I tell the model to act as a static performance analyzer and to respond with strict JSON containing complexity, bottleneck list, and recommendations. I keep the categories fixed so downstream tools can rely on the schema.

import sys
from openai import OpenAI

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

SYSTEM_PROMPT = """You are a static code performance analyzer. Analyze the provided Python function for algorithmic complexity, structural bottlenecks, and resource usage patterns.

Respond ONLY with a JSON object matching this schema:
{
  "time_complexity": "string, e.g. O(n log n)",
  "space_complexity": "string, e.g. O(n)",
  "bottlenecks": [
    {
      "line": "integer or null",
      "description": "string explaining the issue",
      "severity": "low|medium|high"
    }
  ],
  "optimizations": [
    {
      "description": "string explaining the improvement",
      "effort": "quick|moderate|large"
    }
  ],
  "overall_score": "integer from 1 to 10"
}

Be concise. Do not include markdown formatting outside the JSON."""

def load_function(path):
    with open(path, "r") as f:
        return f.read()

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("usage: python monitor.py ")
        sys.exit(1)
    source = load_function(sys.argv[1])
    print(f"loaded {len(source)} characters")

Step 3: Call the model with JSON mode

Now I wire the prompt into a completion request. I use Oxlo.ai's JSON mode to enforce valid output, which removes the need for fragile regex parsing. I also set a generous max_tokens because long functions can produce lengthy bottleneck lists.

import json
import sys
from openai import OpenAI

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

SYSTEM_PROMPT = """You are a static code performance analyzer. Analyze the provided Python function for algorithmic complexity, structural bottlenecks, and resource usage patterns.

Respond ONLY with a JSON object matching this schema:
{
  "time_complexity": "string, e.g. O(n log n)",
  "space_complexity": "string, e.g. O(n)",
  "bottlenecks": [
    {
      "line": "integer or null",
      "description": "string explaining the issue",
      "severity": "low|medium|high"
    }
  ],
  "optimizations": [
    {
      "description": "string explaining the improvement",
      "effort": "quick|moderate|large"
    }
  ],
  "overall_score": "integer from 1 to 10"
}

Be concise. Do not include markdown formatting outside the JSON."""

def load_function(path):
    with open(path, "r") as f:
        return f.read()

def analyze_code(source_code):
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": f"Analyze this Python function:\n\n

```python\n{source_code}\n```

"},
        ],
        response_format={"type": "json_object"},
        max_tokens=2048,
        temperature=0.2,
    )
    raw = response.choices[0].message.content
    return json.loads(raw)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("usage: python monitor.py ")
        sys.exit(1)
    source = load_function(sys.argv[1])
    result = analyze_code(source)
    print(json.dumps(result, indent=2))

Step 4: Add a local cache to avoid re-analyzing unchanged files

During iterative development I do not want to burn requests on the same file. I add a simple SHA-256 cache so repeated runs are free and instant.

import hashlib
import json
import os
import sys
from openai import OpenAI

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

CACHE_DIR = ".oxlo_cache"
os.makedirs(CACHE_DIR, exist_ok=True)

SYSTEM_PROMPT = """You are a static code performance analyzer. Analyze the provided Python function for algorithmic complexity, structural bottlenecks, and resource usage patterns.

Respond ONLY with a JSON object matching this schema:
{
  "time_complexity": "string, e.g. O(n log n)",
  "space_complexity": "string, e.g. O(n)",
  "bottlenecks": [
    {
      "line": "integer or null",
      "description": "string explaining the issue",
      "severity": "low|medium|high"
    }
  ],
  "optimizations": [
    {
      "description": "string explaining the improvement",
      "effort": "quick|moderate|large"
    }
  ],
  "overall_score": "integer from 1 to 10"
}

Be concise. Do not include markdown formatting outside the JSON."""

def load_function(path):
    with open(path, "r") as f:
        return f.read()

def cache_key(source):
    return hashlib.sha256(source.encode()).hexdigest() + ".json"

def analyze_code(source_code):
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": f"Analyze this Python function:\n\n

```python\n{source_code}\n```

"},
        ],
        response_format={"type": "json_object"},
        max_tokens=2048,
        temperature=0.2,
    )
    raw = response.choices[0].message.content
    return json.loads(raw)

def cached_analyze(source_code):
    key = cache_key(source_code)
    path = os.path.join(CACHE_DIR, key)
    if os.path.exists(path):
        with open(path, "r") as f:
            return json.load(f)
    result = analyze_code(source_code)
    with open(path, "w") as f:
        json.dump(result, f)
    return result

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("usage: python monitor.py ")
        sys.exit(1)
    source = load_function(sys.argv[1])
    result = cached_analyze(source)
    print(json.dumps(result, indent=2))

Step 5: Build a summary reporter

Raw JSON is useful for pipelines, but humans need a quick summary. I add a reporter that prints a formatted table and highlights high-severity bottlenecks.

import hashlib
import json
import os
import sys
from openai import OpenAI

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

CACHE_DIR = ".oxlo_cache"
os.makedirs(CACHE_DIR, exist_ok=True)

SYSTEM_PROMPT = """You are a static code performance analyzer. Analyze the provided Python function for algorithmic complexity, structural bottlenecks, and resource usage patterns.

Respond ONLY with a JSON object matching this schema:
{
  "time_complexity": "string, e.g. O(n log n)",
  "space_complexity": "string, e.g. O(n)",
  "bottlenecks": [
    {
      "line": "integer or null",
      "description": "string explaining the issue",
      "severity": "low|medium|high"
    }
  ],
  "optimizations": [
    {
      "description": "string explaining the improvement",
      "effort": "quick|moderate|large"
    }
  ],
  "overall_score": "integer from 1 to 10"
}

Be concise. Do not include markdown formatting outside the JSON."""

def load_function(path):
    with open(path, "r") as f:
        return f.read()

def cache_key(source):
    return hashlib.sha256(source.encode()).hexdigest() + ".json"

def analyze_code(source_code):
    response = client.chat.completions.create(
        model="llama-3.3-70b",
        messages=[
            {"role": "system", "content": SYSTEM_PROMPT},
            {"role": "user", "content": f"Analyze this Python function:\n\n

```python\n{source_code}\n```

"},
        ],
        response_format={"type": "json_object"},
        max_tokens=2048,
        temperature=0.2,
    )
    raw = response.choices[0].message.content
    return json.loads(raw)

def cached_analyze(source_code):
    key = cache_key(source_code)
    path = os.path.join(CACHE_DIR, key)
    if os.path.exists(path):
        with open(path, "r") as f:
            return json.load(f)
    result = analyze_code(source_code)
    with open(path, "w") as f:
        json.dump(result, f)
    return result

def print_report(result):
    print(f"\nOverall Performance Score: {result['overall_score']}/10")
    print(f"Time Complexity:   {result['time_complexity']}")
    print(f"Space Complexity:  {result['space_complexity']}\n")
    print("Bottlenecks:")
    for b in result.get("bottlenecks", []):
        marker = "!!!" if b["severity"] == "high" else "   "
        line_info = f"line {b['line']}" if b.get("line") else "general"
        print(f"  {marker} {line_info}: {b['description']} ({b['severity']})")
    print("\nSuggested Optimizations:")
    for opt in result.get("optimizations", []):
        print(f"  - [{opt['effort']}] {opt['description']}")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("usage: python monitor.py ")
        sys.exit(1)
    source = load_function(sys.argv[1])
    result = cached_analyze(source)
    print_report(result)

Run it

Create a file named slow_sort.py with a deliberately inefficient implementation.

def bubble_sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

Run the monitor:

$ python monitor.py slow_sort.py

Overall Performance Score: 3/10
Time Complexity:   O(n^2)
Space Complexity:  O(1)

Bottlenecks:
  !!! line 3: Nested loops over entire array cause quadratic scaling (high)
      line 4: Repeated element swaps without early exit check (medium)

Suggested Optimizations:
  - [quick] Add a swapped flag to detect already sorted arrays.
  - [moderate] Replace with Timsort via built-in sorted() for average O(n log n).

Wrap-up

From here you can extend the monitor to diff two versions of a file and flag score regressions, or wire it into a pre-commit hook so every push is reviewed for complexity creep. If you are analyzing large modules instead of single functions, swap the model to kimi-k2.6 or deepseek-v3.2 in the client call. Both handle longer context windows well, and Oxlo.ai's per-request pricing means you are not penalized for sending full files with imports and docstrings. See https://oxlo.ai/pricing for plan details.

Top comments (0)