Skip to main content

A tool to generate CPU load on a system with runtime configuration through a WebUI and REST API

Project description

CPU Loader

CI Build Wheels Python Version License

A tool to generate CPU load on a system with runtime configuration through a WebUI and REST API.

✨ Key Features

  • 🎯 Precise Per-Thread Control: Set individual CPU load (0-100%) for each core independently
  • ⚡ High Performance: Native C implementation with pthreads ensures accurate load generation
  • 📊 Real-Time Monitoring: Live WebSocket updates showing actual CPU usage via psutil
  • 🎛️ Interactive WebUI: Beautiful gradient interface with sliders and visual feedback
  • 🚀 REST API: Complete programmatic control for automation and testing
  • 📱 Responsive Design: Works seamlessly on desktop and mobile devices
  • 🔄 Instant Updates: Changes take effect immediately with sub-second response time
  • 📡 MQTT Integration: Publish metrics and settings to MQTT broker for IoT/monitoring systems

🖼️ Screenshots

Desktop Interface

Initial view with zero load

Clean interface showing all CPU threads at idle

Mixed load across threads

Different load levels: 50%, 75%, and 25% on individual threads

Live CPU metrics updating

Real-time metrics showing actual CPU usage with color-coded bars

Mobile responsive view

Fully responsive mobile interface

Features Shown

  • Visual Load Bars: Green progress bars show target load, blue bars display actual CPU usage
  • Real-Time Updates: WebSocket connection provides live metrics every second
  • Preset Buttons: Quick-set options (0%, 25%, 50%, 75%, 100%) for all threads
  • Smooth Gradients: Modern UI with purple-to-blue gradient design

Installation

From Pre-built Wheels (Recommended)

Pre-compiled wheels are available for Linux (x86_64, ARM64) and macOS (x86_64, ARM64):

pip install cpu-loader

From Source

  1. Clone the repository:
git clone https://github.com/the78mole/cpu-loader.git
cd cpu-loader
  1. Install dependencies and build C extension:
uv pip install -e .

This will compile the high-performance C extension for efficient CPU load generation.

  1. (Optional) Set up pre-commit hooks for development:
pre-commit install
  1. (Optional) Set up commit message template for semantic versioning:
git config commit.template .gitmessage

See COMMIT_MESSAGE_FORMAT.md for commit message guidelines.

Usage

Starting the Server

uv run src/main.py

The server will start on http://localhost:8000

Command-Line Options

uv run src/main.py --help

Available Options:

  • --host HOST: Host to bind the server to (default: 0.0.0.0)
  • --port PORT: Port to bind the server to (default: 8000)
  • --mqtt-broker-host HOST: MQTT broker hostname
  • --mqtt-broker-port PORT: MQTT broker port (default: 1883)
  • --mqtt-username USER: MQTT username
  • --mqtt-password PASS: MQTT password
  • --mqtt-topic-prefix PREFIX: MQTT topic prefix (default: cpu-loader)
  • --mqtt-client-id ID: MQTT client ID (default: cpu-loader)

WebUI

Open your browser and navigate to http://localhost:8000

Features:

  • Individual Thread Control: Use sliders to set load for each thread (0-100%)
  • Preset Buttons: Quick-set all threads to 0%, 10%, 25%, 50%, 80%, 90%, or 100%
  • Live Stats: View active thread count and average load
  • Real-time Updates: Changes are applied instantly with visual feedback

REST API

Get Thread Status

curl http://localhost:8000/api/threads

Response:

{
  "num_threads": 4,
  "loads": {
    "0": 0.0,
    "1": 0.0,
    "2": 0.0,
    "3": 0.0
  }
}

Set Load for Specific Thread

curl -X PUT http://localhost:8000/api/threads/0/load \
  -H "Content-Type: application/json" \
  -d '{"load_percent": 50.0}'

Set Load for All Threads

curl -X POST http://localhost:8000/api/threads/load/all \
  -H "Content-Type: application/json" \
  -d '{"load_percent": 75.0}'

Change Number of Threads

curl -X POST http://localhost:8000/api/threads \
  -H "Content-Type: application/json" \
  -d '{"num_threads": 8}'

API Documentation

Once the server is running, visit http://localhost:8000/docs for interactive API documentation powered by Swagger UI.

MQTT Publishing

CPU Loader can publish real-time CPU metrics and load control settings to an MQTT broker for integration with home automation systems, monitoring tools, or custom applications.

MQTT Topics

The application publishes to two topics:

  1. {prefix}/cpu_metrics: Published every second with current CPU utilization

    {
      "total_cpu_percent": 25.5,
      "per_cpu_percent": [25.0, 26.0, 25.3, 25.7]
    }
    
  2. {prefix}/load_settings: Published when load settings change (retained message)

    {
      "num_threads": 4,
      "loads": {"0": 25.0, "1": 25.0, "2": 25.0, "3": 25.0},
      "average_load": 25.0
    }
    

Configuration

MQTT can be configured using environment variables or command-line arguments. Command-line arguments take precedence over environment variables.

Using Environment Variables

export MQTT_BROKER_HOST=mqtt.example.com
export MQTT_BROKER_PORT=1883
export MQTT_USERNAME=myuser
export MQTT_PASSWORD=mypassword
export MQTT_TOPIC_PREFIX=cpu-loader
export MQTT_CLIENT_ID=cpu-loader-001

uv run src/main.py

Using Command-Line Arguments

uv run src/main.py \
  --mqtt-broker-host mqtt.example.com \
  --mqtt-broker-port 1883 \
  --mqtt-username myuser \
  --mqtt-password mypassword \
  --mqtt-topic-prefix cpu-loader \
  --mqtt-client-id cpu-loader-001

Testing with Mosquitto

To test MQTT publishing locally:

# Install mosquitto
sudo apt-get install mosquitto mosquitto-clients

# Start CPU Loader with MQTT
uv run src/main.py --mqtt-broker-host localhost

# Subscribe to all topics (in another terminal)
mosquitto_sub -h localhost -t "cpu-loader/#" -v

MQTT Settings

Setting Environment Variable CLI Argument Default Description
Broker Host MQTT_BROKER_HOST --mqtt-broker-host None MQTT broker hostname or IP
Broker Port MQTT_BROKER_PORT --mqtt-broker-port 1883 MQTT broker port
Username MQTT_USERNAME --mqtt-username None MQTT authentication username
Password MQTT_PASSWORD --mqtt-password None MQTT authentication password
Topic Prefix MQTT_TOPIC_PREFIX --mqtt-topic-prefix cpu-loader Prefix for all MQTT topics
Client ID MQTT_CLIENT_ID --mqtt-client-id cpu-loader MQTT client identifier

Note: If no MQTT broker host is configured, MQTT publishing will be disabled and the application will function normally without it.

Example Script

An example script (src/example.py) is provided to demonstrate programmatic control:

# Start the server first
uv run src/main.py

# In another terminal, run the example
uv run src/example.py

The example demonstrates:

  • Getting current status
  • Setting all threads to a specific load
  • Gradually increasing load
  • Individual thread control
  • Resetting to idle

Architecture

  • src/cpu_loader_core.c: High-performance C implementation using pthreads for CPU load generation
  • src/cpu_loader.py: Python wrapper providing a clean API to the C extension
  • src/main.py: FastAPI application with REST API and embedded WebUI
  • src/mqtt_publisher.py: MQTT client for publishing metrics and settings
  • Threading Model: Native pthreads for maximum efficiency and precise timing
  • Load Algorithm: High-resolution busy-wait loops with nanosecond precision

Requirements

  • Python 3.8+
  • FastAPI 0.115.0+
  • Uvicorn 0.32.0+
  • Pydantic 2.10.0+
  • paho-mqtt 2.1.0+ (for MQTT publishing)
  • C compiler (gcc or clang) for building the extension

Use Cases

  • Performance Testing: Test application behavior under various CPU loads
  • Stress Testing: Validate system stability under high CPU utilization
  • Thermal Testing: Check cooling system effectiveness
  • Power Consumption Analysis: Measure power usage at different load levels
  • Benchmarking: Create reproducible load scenarios
  • IoT Integration: Integrate with Home Assistant, Node-RED, or other MQTT-based systems
  • Monitoring: Feed CPU metrics into monitoring dashboards via MQTT

Development

Pre-commit Hooks

This project uses pre-commit hooks to ensure code quality. The hooks include:

  • Code Formatting: Black and isort for consistent Python formatting
  • Linting: Flake8 for code quality checks
  • Type Checking: Mypy for static type analysis
  • General Checks: Trailing whitespace, end-of-file fixes, YAML/JSON/TOML validation

To run pre-commit manually on all files:

pre-commit run --all-files

The hooks will run automatically on git commit after installation.

Building and Publishing Releases

The project uses automatic semantic versioning with GitHub Actions:

Versioning Rules

Versions are automatically determined based on commit messages:

  • Patch bump (0.0.X): Every commit to main
  • Minor bump (0.X.0): Commits prefixed with feat:
    git commit -m "feat: add new CPU monitoring feature"
    
  • Major bump (X.0.0): Commits with major:, breaking:, or BREAKING CHANGE:
    git commit -m "major: redesign API interface"
    git commit -m "breaking: remove deprecated endpoints"
    

Release Process

  1. Commit and push to main:

    git add .
    git commit -m "feat: add WebSocket support"
    git push origin main
    
  2. Automated workflow:

    • Version is automatically calculated using semantic versioning
    • Wheels are built for Linux (x86_64, ARM64) and macOS (x86_64, ARM64)
    • Python versions: 3.8, 3.9, 3.10, 3.11, 3.12
    • Source distribution (sdist) is created
    • All artifacts are published to PyPI
    • A GitHub Release is created with version tag and artifacts

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cpu_loader-0.1.2.tar.gz (18.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

cpu_loader-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.9 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

cpu_loader-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (32.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

cpu_loader-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (32.0 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

cpu_loader-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.2 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ ARM64

cpu_loader-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.7 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (32.2 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ ARM64

File details

Details for the file cpu_loader-0.1.2.tar.gz.

File metadata

  • Download URL: cpu_loader-0.1.2.tar.gz
  • Upload date:
  • Size: 18.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cpu_loader-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5abd56b7963ebe92c15df2e0dd08eee97ba73ad52287fc5838f77489cac4234e
MD5 9ccf93e3f2d1c083df2e62063b0ea750
BLAKE2b-256 f0b65cb19b8f37fd84a668fd4146a323a1bfcd62a0a598f9c3cf487351e9900c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2.tar.gz:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1c662fcfbcff0a0b3076a11330680d78486a57ac349e20378f973cbbf21a0e17
MD5 c1cbf5907be05bc697d4d356fd8d8711
BLAKE2b-256 6eaf6fcebf1ae2a017ca64ab71d80d9a7be43c7035d7df797c4e02ca41f0f901

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 4ab4fc81f8bc6b21bad3db49ddeb1f37882a9530dab72f08ef4ddcd653dbca75
MD5 d98f49cf310fb9c21508554cc1b4298a
BLAKE2b-256 b90bf07ba480287ec54a5eba58afe3a349f64ad63db4903869517c67be4b9a25

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7275422318728a09ed61698640feb33b2e8b01d9e9854382d011ee50b0820632
MD5 5f6c6724f0edd7245771086ab8eb0499
BLAKE2b-256 a433a42305c51dc5ed3f48ed47730fc2c209506d63b9a080d301b0b0f551e87d

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6f8b60e3fad1b37b27217eac7e2f275a62d20a36da138bc5fba18da4f87105e7
MD5 b6a15f8401d1d41533382dbab8cc78de
BLAKE2b-256 386b3856b91a4696fea25e7cb0165a5e3d0323215127a9af42e47a7055c3b429

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9f13f8274d711adb44800ff65ad36433537ca66848b9fef05f068175ee476249
MD5 feca09b8a2ba292e850706a5aa8bd05b
BLAKE2b-256 35ccaa5b677aa55344d01700fb02272bff248389b43a5336b3a169c51d0fe168

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 45e8773b03351f9008e211ef1e0dbd74ad9855031797a5750e031d0038802d82
MD5 c95dbfa8be6ed17a123c549e7bf80663
BLAKE2b-256 177e6aa3832fb8e07926dedc1916031bc3128b98ec1045853a7205bec1267c28

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6e0b76e5927dbc26ee6e807964d5d0816d53aab54d207674be808222c25a90a1
MD5 bbd258b88e8485f9fdde98a21ac12666
BLAKE2b-256 417001151934239e89112f48039d05244f03944d2df3a0c08ddd998f49673125

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5725d71790ab31cb6cbd4ca0a80fbcc9aedce5a9955c0097e71fcf059cf89a6e
MD5 5ae31afcc7c7636acef9d68f1dc2e8e9
BLAKE2b-256 0265a3cfe1b62dc92d160ee3e886c2d28e43da6e28bf05db57ce4df107afae26

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 aad1987596a06255e1dd2f03b81a65b0add76559d85c4664cbc1eb7e31f92175
MD5 48022b246495d88eba2c58953805e127
BLAKE2b-256 0b66e8290fce555677dbb159e75e886b722392c6c650418d5cd5066ae2d23b2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cpu_loader-0.1.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e398e36ea9b7e1b4aeec1db84c3a803854e8da03a6060fc68f392af103d3c498
MD5 04439f6efb8134597c58a870ba5e104a
BLAKE2b-256 0ba2db6e40fd2cf66630f43a201211498f00e35a5ef9e8509ff07be9e6795389

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.2-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: build-wheels.yml on the78mole/cpu-loader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page