Skip to main content
DARKPC.NET

Measure Prompt and Generation Speed With Ollama

Calculate prompt processing and generated-token rates from Ollama's response metrics and compare warm and cold runs fairly.

Difficulty Intermediate

Prerequisites

Use a running local Ollama service, a small installed model and Python 3. Copy the exact installed tag from ollama list. A CPU works for the exercise, with lower throughput expected on some systems.

Request one controlled response

Save this as rate.py and run python3 rate.py YOUR_MODEL_TAG. Use the same tag, prompt and options for each repeat.

import json
import sys
from urllib.request import Request, urlopen

data = {"model": sys.argv[1], "prompt": "List five uses of a text editor.",
        "stream": False, "options": {"num_predict": 120}}
req = Request("http://127.0.0.1:11434/api/generate",
              json.dumps(data).encode(),
              {"Content-Type": "application/json"})
with urlopen(req, timeout=180) as reply:
    result = json.load(reply)
for count, duration in [("prompt_eval_count", "prompt_eval_duration"),
                        ("eval_count", "eval_duration")]:
    elapsed = result.get(duration, 0) / 1e9
    if elapsed and count in result:
        print(count, round(result[count] / elapsed, 2), "tokens/s")
    else:
        print(count, "unavailable for this request")
print("load ms", result.get("load_duration", 0) / 1e6)

Read the measurements

Ollama reports durations in nanoseconds. Dividing by one billion converts them to seconds. Ask the same question three times and record model name, generated token count and load duration. A warm model may show lower loading time. A repeated prompt can benefit from caching, so note whether prompt processing work changed.

Troubleshooting and practice

If a run times out, review memory pressure and pick a smaller model before changing the timeout. If the endpoint rejects the tag, compare it with the locally installed name. Compare a short prompt with a longer synthetic prompt, then record prompt-token rate and end-to-end wait separately. Keep the service on loopback and avoid putting private prompt text in shared benchmark logs.

References and related reading