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.3.tar.gz (19.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.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.0 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.3 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

cpu_loader-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.9 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

cpu_loader-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.9 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ ARM64

cpu_loader-0.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (30.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.2 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ ARM64

cpu_loader-0.1.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (31.2 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

cpu_loader-0.1.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (31.7 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ ARM64

File details

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

File metadata

  • Download URL: cpu_loader-0.1.3.tar.gz
  • Upload date:
  • Size: 19.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.3.tar.gz
Algorithm Hash digest
SHA256 f680631c4f080446e5c35aceb20ebae256a9a028a46eaf466ec5814ae88d8120
MD5 25ca3e866eade100c9b07b817781c53e
BLAKE2b-256 f6804869976b1b4b10d42a988316ad95410b2563f7050509e9f0292ff863de2e

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3.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.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a2019defc043988156bc5c49ad96a4c1b6535774e9bb8e07668ba7c973d0bb00
MD5 939d704514d9d4fffda5cab1cc1fcb95
BLAKE2b-256 795b1699a4612984a6884c9e30edfb24c3245cc5df3472ed307b32404208b3fe

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 6ab1eb03eebaeba1df8176ad9512694d96cddf1ee6bcbc20c11a7a8289283f0c
MD5 231b53f31bf27d3aed3825d9117f77a9
BLAKE2b-256 df9f3bfd1db409dd9baaaa62307517f2c7e628531241bd79dbbaadb948960de0

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 043e8e1c7bb9b44c46f088af83c7a04b9a8417e3cc919923af95cc3d6c11a604
MD5 12f608e34280f50461cef16efc25c814
BLAKE2b-256 a6407e0ad71f079e3032fa7ab2cf3b8834f55cebe44f1e299be2004bf1d33c3f

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 651e0bbf76dc9091a057210ae814db1a4c7564b7016ae3ce820c8c2b00876264
MD5 3fc300de5ef2a90b63abd49f0fb4bd0d
BLAKE2b-256 fa161274d4a3e833a7857f990f0e09f5c8392c515f6eace1be1a39b2f6177d99

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fa32eda08e9a427f728a58522ee74a80d43728a5c9afea3361908e1adbb6ff50
MD5 f235309008568b712035f5d5692c9255
BLAKE2b-256 ee6ba82d9637fc015acb2389c21d91884fa46369815800e5c53ac7f261488412

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e67edcf152fab12cb84544d9bef32ded6cffacfb26de6242d6616b7d2077c7a6
MD5 63eab5343aa0903fdfe9d3f8cad7aecd
BLAKE2b-256 463b023f0179a10f0018bb8caddfa8f43768ce90ec586561dcb5f39694293fa8

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 985bd2ab79455ea6b64d8c8cd3c9358ee69b77a44cd7a95300fe7a10bd63adf6
MD5 8b4ea21f423eeb2bcafb675acc11065f
BLAKE2b-256 fafbe0b7813254fedb29962d4a6be2ef83fbdee9c0a16bf7e7bd6a6fdb360ec6

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 22e7655f38348ba891ae95aef67afd65a6550656a267e49f52c43d14177e0fdd
MD5 418b62cf9079e5c8f3d7ddf8aace2556
BLAKE2b-256 3f2b0f31db71d9a15dce2f605a72ccab02e035464fe5210cf499c65033fce64c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ffc9699fabd1ff9af78be847c74ac999f40c800cf4df2a07b169dc3667050e4d
MD5 e72d35169d9aebcb066e5926733f99c8
BLAKE2b-256 045d8cf2393dd86008a0e124b4b560031023ffa2b6d8eccb0dd6e44dfe631f42

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cpu_loader-0.1.3-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 be85a6c4a770967df0c8c6b9279dbf8009d9b37c763bc7a652797ae765a3d2ac
MD5 03126d43059d39df2537485f21bcfcaa
BLAKE2b-256 c8d4ad529aaeb2480f2c2ff0c4e0bc3dbd4415e031a4fba43ac6b82e25077276

See more details on using hashes here.

Provenance

The following attestation bundles were made for cpu_loader-0.1.3-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