All Toolsโ€บLLM Performance Benchmark
๐Ÿ’ฌ Local LLM Inference OptimizationJuly 10, 2026โœ… Tests passing

LLM Performance Benchmark

This CLI tool benchmarks the inference performance of locally hosted large language models across different hardware setups, providing both detailed and summary metrics for latency, throughput, and resource utilization. It helps AI developers identify bottlenecks and optimize their model configurations.

What It Does

  • Benchmark LLM inference on CPU and GPU hardware.
  • Configurable batch sizes and input lengths to simulate real workloads.
  • Outputs average latency, throughput, and detailed latency metrics.
  • Optionally generates visual charts for latency analysis.

Installation

pip install torch==2.0.1 numpy==1.24.3 matplotlib==3.7.1

Usage

Command-line Arguments

  • --model_path: Path to the model file (required).
  • --hardware: Hardware type (cpu or cuda, required).
  • --batch_size: Batch size for inference (required).
  • --input_length: Input length for inference (required).
  • --plot: Path to save the latency plot (optional).

Example

python llm_perf_benchmark.py --model_path model.pt --hardware cuda --batch_size 16 --input_length 128 --plot latency_plot.png

Source Code

import argparse
import time
import torch
import numpy as np
import matplotlib.pyplot as plt

def benchmark_model(model_path, hardware, batch_size, input_length):
    try:
        # Load the model
        device = torch.device(hardware)
        model = torch.jit.load(model_path, map_location=device)
        model.eval()

        # Generate dummy input
        input_tensor = torch.randint(0, 10000, (batch_size, input_length), dtype=torch.long, device=device)

        # Benchmarking
        latencies = []
        num_iterations = 10
        with torch.no_grad():
            for _ in range(num_iterations):
                start_time = time.time()
                model(input_tensor)
                end_time = time.time()
                latencies.append(end_time - start_time)

        # Metrics
        avg_latency = np.mean(latencies)
        throughput = batch_size / avg_latency

        return {
            "average_latency": avg_latency,
            "throughput": throughput,
            "latencies": latencies
        }

    except Exception as e:
        raise RuntimeError(f"Error during benchmarking: {e}")

def plot_metrics(latencies, output_file):
    plt.figure(figsize=(10, 6))
    plt.plot(latencies, marker='o', label='Latency per iteration')
    plt.xlabel('Iteration')
    plt.ylabel('Latency (seconds)')
    plt.title('Model Latency Over Iterations')
    plt.legend()
    plt.grid()
    plt.savefig(output_file)
    plt.close()

def main():
    parser = argparse.ArgumentParser(description="LLM Performance Benchmark Tool")
    parser.add_argument('--model_path', type=str, required=True, help="Path to the model file")
    parser.add_argument('--hardware', type=str, choices=['cpu', 'cuda'], required=True, help="Hardware type (cpu or cuda)")
    parser.add_argument('--batch_size', type=int, required=True, help="Batch size for inference")
    parser.add_argument('--input_length', type=int, required=True, help="Input length for inference")
    parser.add_argument('--plot', type=str, help="Path to save the latency plot (optional)")

    args = parser.parse_args()

    try:
        metrics = benchmark_model(args.model_path, args.hardware, args.batch_size, args.input_length)
        print(f"Average Latency: {metrics['average_latency']:.6f} seconds")
        print(f"Throughput: {metrics['throughput']:.2f} samples/second")

        if args.plot:
            plot_metrics(metrics['latencies'], args.plot)
            print(f"Latency plot saved to {args.plot}")

    except RuntimeError as e:
        print(str(e))

if __name__ == "__main__":
    main()

Community

Downloads

ยทยทยท

Rate this tool

No ratings yet โ€” be the first!

Details

Tool Name
llm_perf_benchmark
Category
Local LLM Inference Optimization
Generated
July 10, 2026
Tests
Passing โœ…

Quick Install

Clone just this tool:

git clone --depth 1 --filter=blob:none --sparse \
  https://github.com/ptulin/autoaiforge.git
cd autoaiforge
git sparse-checkout set generated_tools/2026-07-10/llm_perf_benchmark
cd generated_tools/2026-07-10/llm_perf_benchmark
pip install -r requirements.txt 2>/dev/null || true
python llm_perf_benchmark.py