cpu-loader 0.0.3__tar.gz → 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. {cpu_loader-0.0.3/src/cpu_loader.egg-info → cpu_loader-0.1.3}/PKG-INFO +106 -2
  2. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/README.md +104 -1
  3. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/pyproject.toml +4 -3
  4. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/setup.py +2 -2
  5. cpu_loader-0.1.3/src/cpu_loader/__init__.py +5 -0
  6. cpu_loader-0.1.3/src/cpu_loader/__main__.py +6 -0
  7. {cpu_loader-0.0.3/src → cpu_loader-0.1.3/src/cpu_loader}/cpu_loader.py +1 -1
  8. cpu_loader-0.1.3/src/cpu_loader/main.py +328 -0
  9. cpu_loader-0.1.3/src/cpu_loader/mqtt_publisher.py +189 -0
  10. {cpu_loader-0.0.3 → cpu_loader-0.1.3/src/cpu_loader.egg-info}/PKG-INFO +106 -2
  11. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/src/cpu_loader.egg-info/SOURCES.txt +7 -4
  12. cpu_loader-0.1.3/src/cpu_loader.egg-info/entry_points.txt +2 -0
  13. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/src/cpu_loader.egg-info/requires.txt +1 -0
  14. cpu_loader-0.1.3/src/cpu_loader.egg-info/top_level.txt +1 -0
  15. cpu_loader-0.0.3/src/cpu_loader.egg-info/entry_points.txt +0 -2
  16. cpu_loader-0.0.3/src/cpu_loader.egg-info/top_level.txt +0 -4
  17. cpu_loader-0.0.3/src/main.py +0 -658
  18. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/LICENSE +0 -0
  19. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/MANIFEST.in +0 -0
  20. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/setup.cfg +0 -0
  21. {cpu_loader-0.0.3/src → cpu_loader-0.1.3/src/cpu_loader}/cpu_loader_core.c +0 -0
  22. {cpu_loader-0.0.3/src → cpu_loader-0.1.3/src/cpu_loader}/example.py +0 -0
  23. {cpu_loader-0.0.3 → cpu_loader-0.1.3}/src/cpu_loader.egg-info/dependency_links.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: cpu-loader
3
- Version: 0.0.3
3
+ Version: 0.1.3
4
4
  Summary: A tool to generate CPU load on a system with runtime configuration through a WebUI and REST API
5
5
  Author-email: the78mole <the78mole@users.noreply.github.com>
6
6
  License: MIT
@@ -33,6 +33,7 @@ Requires-Dist: pydantic==2.10.0
33
33
  Requires-Dist: requests==2.32.3
34
34
  Requires-Dist: psutil>=5.9.0
35
35
  Requires-Dist: websockets>=12.0
36
+ Requires-Dist: paho-mqtt>=2.1.0
36
37
  Dynamic: license-file
37
38
 
38
39
  # CPU Loader
@@ -42,7 +43,7 @@ Dynamic: license-file
42
43
  [![Python Version](https://img.shields.io/pypi/pyversions/cpu-loader)](https://pypi.org/project/cpu-loader/)
43
44
  [![License](https://img.shields.io/github/license/the78mole/cpu-loader)](https://github.com/the78mole/cpu-loader/blob/main/LICENSE)
44
45
 
45
- A high-performance CPU load generator with **per-thread control** through an intuitive WebUI and REST API. Built with a native C extension using pthreads for accurate, nanosecond-precision load generation.
46
+ A tool to generate CPU load on a system with runtime configuration through a WebUI and REST API.
46
47
 
47
48
  ## ✨ Key Features
48
49
 
@@ -53,6 +54,7 @@ A high-performance CPU load generator with **per-thread control** through an int
53
54
  - **🚀 REST API**: Complete programmatic control for automation and testing
54
55
  - **📱 Responsive Design**: Works seamlessly on desktop and mobile devices
55
56
  - **🔄 Instant Updates**: Changes take effect immediately with sub-second response time
57
+ - 📡 **MQTT Integration**: Publish metrics and settings to MQTT broker for IoT/monitoring systems
56
58
 
57
59
  ## 🖼️ Screenshots
58
60
 
@@ -133,6 +135,22 @@ uv run src/main.py
133
135
 
134
136
  The server will start on `http://localhost:8000`
135
137
 
138
+ #### Command-Line Options
139
+
140
+ ```bash
141
+ uv run src/main.py --help
142
+ ```
143
+
144
+ **Available Options:**
145
+ - `--host HOST`: Host to bind the server to (default: 0.0.0.0)
146
+ - `--port PORT`: Port to bind the server to (default: 8000)
147
+ - `--mqtt-broker-host HOST`: MQTT broker hostname
148
+ - `--mqtt-broker-port PORT`: MQTT broker port (default: 1883)
149
+ - `--mqtt-username USER`: MQTT username
150
+ - `--mqtt-password PASS`: MQTT password
151
+ - `--mqtt-topic-prefix PREFIX`: MQTT topic prefix (default: cpu-loader)
152
+ - `--mqtt-client-id ID`: MQTT client ID (default: cpu-loader)
153
+
136
154
  ### WebUI
137
155
 
138
156
  Open your browser and navigate to `http://localhost:8000`
@@ -188,6 +206,88 @@ curl -X POST http://localhost:8000/api/threads \
188
206
 
189
207
  Once the server is running, visit `http://localhost:8000/docs` for interactive API documentation powered by Swagger UI.
190
208
 
209
+ ## MQTT Publishing
210
+
211
+ 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.
212
+
213
+ ### MQTT Topics
214
+
215
+ The application publishes to two topics:
216
+
217
+ 1. **`{prefix}/cpu_metrics`**: Published every second with current CPU utilization
218
+ ```json
219
+ {
220
+ "total_cpu_percent": 25.5,
221
+ "per_cpu_percent": [25.0, 26.0, 25.3, 25.7]
222
+ }
223
+ ```
224
+
225
+ 2. **`{prefix}/load_settings`**: Published when load settings change (retained message)
226
+ ```json
227
+ {
228
+ "num_threads": 4,
229
+ "loads": {"0": 25.0, "1": 25.0, "2": 25.0, "3": 25.0},
230
+ "average_load": 25.0
231
+ }
232
+ ```
233
+
234
+ ### Configuration
235
+
236
+ MQTT can be configured using environment variables or command-line arguments. Command-line arguments take precedence over environment variables.
237
+
238
+ #### Using Environment Variables
239
+
240
+ ```bash
241
+ export MQTT_BROKER_HOST=mqtt.example.com
242
+ export MQTT_BROKER_PORT=1883
243
+ export MQTT_USERNAME=myuser
244
+ export MQTT_PASSWORD=mypassword
245
+ export MQTT_TOPIC_PREFIX=cpu-loader
246
+ export MQTT_CLIENT_ID=cpu-loader-001
247
+
248
+ uv run src/main.py
249
+ ```
250
+
251
+ #### Using Command-Line Arguments
252
+
253
+ ```bash
254
+ uv run src/main.py \
255
+ --mqtt-broker-host mqtt.example.com \
256
+ --mqtt-broker-port 1883 \
257
+ --mqtt-username myuser \
258
+ --mqtt-password mypassword \
259
+ --mqtt-topic-prefix cpu-loader \
260
+ --mqtt-client-id cpu-loader-001
261
+ ```
262
+
263
+ #### Testing with Mosquitto
264
+
265
+ To test MQTT publishing locally:
266
+
267
+ ```bash
268
+ # Install mosquitto
269
+ sudo apt-get install mosquitto mosquitto-clients
270
+
271
+ # Start CPU Loader with MQTT
272
+ uv run src/main.py --mqtt-broker-host localhost
273
+
274
+ # Subscribe to all topics (in another terminal)
275
+ mosquitto_sub -h localhost -t "cpu-loader/#" -v
276
+ ```
277
+
278
+ ### MQTT Settings
279
+
280
+ | Setting | Environment Variable | CLI Argument | Default | Description |
281
+ |---------|---------------------|--------------|---------|-------------|
282
+ | Broker Host | `MQTT_BROKER_HOST` | `--mqtt-broker-host` | None | MQTT broker hostname or IP |
283
+ | Broker Port | `MQTT_BROKER_PORT` | `--mqtt-broker-port` | 1883 | MQTT broker port |
284
+ | Username | `MQTT_USERNAME` | `--mqtt-username` | None | MQTT authentication username |
285
+ | Password | `MQTT_PASSWORD` | `--mqtt-password` | None | MQTT authentication password |
286
+ | Topic Prefix | `MQTT_TOPIC_PREFIX` | `--mqtt-topic-prefix` | cpu-loader | Prefix for all MQTT topics |
287
+ | Client ID | `MQTT_CLIENT_ID` | `--mqtt-client-id` | cpu-loader | MQTT client identifier |
288
+
289
+ **Note:** If no MQTT broker host is configured, MQTT publishing will be disabled and the application will function normally without it.
290
+
191
291
  ## Example Script
192
292
 
193
293
  An example script (`src/example.py`) is provided to demonstrate programmatic control:
@@ -212,6 +312,7 @@ The example demonstrates:
212
312
  - **src/cpu_loader_core.c**: High-performance C implementation using pthreads for CPU load generation
213
313
  - **src/cpu_loader.py**: Python wrapper providing a clean API to the C extension
214
314
  - **src/main.py**: FastAPI application with REST API and embedded WebUI
315
+ - **src/mqtt_publisher.py**: MQTT client for publishing metrics and settings
215
316
  - **Threading Model**: Native pthreads for maximum efficiency and precise timing
216
317
  - **Load Algorithm**: High-resolution busy-wait loops with nanosecond precision
217
318
 
@@ -221,6 +322,7 @@ The example demonstrates:
221
322
  - FastAPI 0.115.0+
222
323
  - Uvicorn 0.32.0+
223
324
  - Pydantic 2.10.0+
325
+ - paho-mqtt 2.1.0+ (for MQTT publishing)
224
326
  - C compiler (gcc or clang) for building the extension
225
327
 
226
328
  ## Use Cases
@@ -230,6 +332,8 @@ The example demonstrates:
230
332
  - **Thermal Testing**: Check cooling system effectiveness
231
333
  - **Power Consumption Analysis**: Measure power usage at different load levels
232
334
  - **Benchmarking**: Create reproducible load scenarios
335
+ - **IoT Integration**: Integrate with Home Assistant, Node-RED, or other MQTT-based systems
336
+ - **Monitoring**: Feed CPU metrics into monitoring dashboards via MQTT
233
337
 
234
338
  ## Development
235
339
 
@@ -5,7 +5,7 @@
5
5
  [![Python Version](https://img.shields.io/pypi/pyversions/cpu-loader)](https://pypi.org/project/cpu-loader/)
6
6
  [![License](https://img.shields.io/github/license/the78mole/cpu-loader)](https://github.com/the78mole/cpu-loader/blob/main/LICENSE)
7
7
 
8
- A high-performance CPU load generator with **per-thread control** through an intuitive WebUI and REST API. Built with a native C extension using pthreads for accurate, nanosecond-precision load generation.
8
+ A tool to generate CPU load on a system with runtime configuration through a WebUI and REST API.
9
9
 
10
10
  ## ✨ Key Features
11
11
 
@@ -16,6 +16,7 @@ A high-performance CPU load generator with **per-thread control** through an int
16
16
  - **🚀 REST API**: Complete programmatic control for automation and testing
17
17
  - **📱 Responsive Design**: Works seamlessly on desktop and mobile devices
18
18
  - **🔄 Instant Updates**: Changes take effect immediately with sub-second response time
19
+ - 📡 **MQTT Integration**: Publish metrics and settings to MQTT broker for IoT/monitoring systems
19
20
 
20
21
  ## 🖼️ Screenshots
21
22
 
@@ -96,6 +97,22 @@ uv run src/main.py
96
97
 
97
98
  The server will start on `http://localhost:8000`
98
99
 
100
+ #### Command-Line Options
101
+
102
+ ```bash
103
+ uv run src/main.py --help
104
+ ```
105
+
106
+ **Available Options:**
107
+ - `--host HOST`: Host to bind the server to (default: 0.0.0.0)
108
+ - `--port PORT`: Port to bind the server to (default: 8000)
109
+ - `--mqtt-broker-host HOST`: MQTT broker hostname
110
+ - `--mqtt-broker-port PORT`: MQTT broker port (default: 1883)
111
+ - `--mqtt-username USER`: MQTT username
112
+ - `--mqtt-password PASS`: MQTT password
113
+ - `--mqtt-topic-prefix PREFIX`: MQTT topic prefix (default: cpu-loader)
114
+ - `--mqtt-client-id ID`: MQTT client ID (default: cpu-loader)
115
+
99
116
  ### WebUI
100
117
 
101
118
  Open your browser and navigate to `http://localhost:8000`
@@ -151,6 +168,88 @@ curl -X POST http://localhost:8000/api/threads \
151
168
 
152
169
  Once the server is running, visit `http://localhost:8000/docs` for interactive API documentation powered by Swagger UI.
153
170
 
171
+ ## MQTT Publishing
172
+
173
+ 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.
174
+
175
+ ### MQTT Topics
176
+
177
+ The application publishes to two topics:
178
+
179
+ 1. **`{prefix}/cpu_metrics`**: Published every second with current CPU utilization
180
+ ```json
181
+ {
182
+ "total_cpu_percent": 25.5,
183
+ "per_cpu_percent": [25.0, 26.0, 25.3, 25.7]
184
+ }
185
+ ```
186
+
187
+ 2. **`{prefix}/load_settings`**: Published when load settings change (retained message)
188
+ ```json
189
+ {
190
+ "num_threads": 4,
191
+ "loads": {"0": 25.0, "1": 25.0, "2": 25.0, "3": 25.0},
192
+ "average_load": 25.0
193
+ }
194
+ ```
195
+
196
+ ### Configuration
197
+
198
+ MQTT can be configured using environment variables or command-line arguments. Command-line arguments take precedence over environment variables.
199
+
200
+ #### Using Environment Variables
201
+
202
+ ```bash
203
+ export MQTT_BROKER_HOST=mqtt.example.com
204
+ export MQTT_BROKER_PORT=1883
205
+ export MQTT_USERNAME=myuser
206
+ export MQTT_PASSWORD=mypassword
207
+ export MQTT_TOPIC_PREFIX=cpu-loader
208
+ export MQTT_CLIENT_ID=cpu-loader-001
209
+
210
+ uv run src/main.py
211
+ ```
212
+
213
+ #### Using Command-Line Arguments
214
+
215
+ ```bash
216
+ uv run src/main.py \
217
+ --mqtt-broker-host mqtt.example.com \
218
+ --mqtt-broker-port 1883 \
219
+ --mqtt-username myuser \
220
+ --mqtt-password mypassword \
221
+ --mqtt-topic-prefix cpu-loader \
222
+ --mqtt-client-id cpu-loader-001
223
+ ```
224
+
225
+ #### Testing with Mosquitto
226
+
227
+ To test MQTT publishing locally:
228
+
229
+ ```bash
230
+ # Install mosquitto
231
+ sudo apt-get install mosquitto mosquitto-clients
232
+
233
+ # Start CPU Loader with MQTT
234
+ uv run src/main.py --mqtt-broker-host localhost
235
+
236
+ # Subscribe to all topics (in another terminal)
237
+ mosquitto_sub -h localhost -t "cpu-loader/#" -v
238
+ ```
239
+
240
+ ### MQTT Settings
241
+
242
+ | Setting | Environment Variable | CLI Argument | Default | Description |
243
+ |---------|---------------------|--------------|---------|-------------|
244
+ | Broker Host | `MQTT_BROKER_HOST` | `--mqtt-broker-host` | None | MQTT broker hostname or IP |
245
+ | Broker Port | `MQTT_BROKER_PORT` | `--mqtt-broker-port` | 1883 | MQTT broker port |
246
+ | Username | `MQTT_USERNAME` | `--mqtt-username` | None | MQTT authentication username |
247
+ | Password | `MQTT_PASSWORD` | `--mqtt-password` | None | MQTT authentication password |
248
+ | Topic Prefix | `MQTT_TOPIC_PREFIX` | `--mqtt-topic-prefix` | cpu-loader | Prefix for all MQTT topics |
249
+ | Client ID | `MQTT_CLIENT_ID` | `--mqtt-client-id` | cpu-loader | MQTT client identifier |
250
+
251
+ **Note:** If no MQTT broker host is configured, MQTT publishing will be disabled and the application will function normally without it.
252
+
154
253
  ## Example Script
155
254
 
156
255
  An example script (`src/example.py`) is provided to demonstrate programmatic control:
@@ -175,6 +274,7 @@ The example demonstrates:
175
274
  - **src/cpu_loader_core.c**: High-performance C implementation using pthreads for CPU load generation
176
275
  - **src/cpu_loader.py**: Python wrapper providing a clean API to the C extension
177
276
  - **src/main.py**: FastAPI application with REST API and embedded WebUI
277
+ - **src/mqtt_publisher.py**: MQTT client for publishing metrics and settings
178
278
  - **Threading Model**: Native pthreads for maximum efficiency and precise timing
179
279
  - **Load Algorithm**: High-resolution busy-wait loops with nanosecond precision
180
280
 
@@ -184,6 +284,7 @@ The example demonstrates:
184
284
  - FastAPI 0.115.0+
185
285
  - Uvicorn 0.32.0+
186
286
  - Pydantic 2.10.0+
287
+ - paho-mqtt 2.1.0+ (for MQTT publishing)
187
288
  - C compiler (gcc or clang) for building the extension
188
289
 
189
290
  ## Use Cases
@@ -193,6 +294,8 @@ The example demonstrates:
193
294
  - **Thermal Testing**: Check cooling system effectiveness
194
295
  - **Power Consumption Analysis**: Measure power usage at different load levels
195
296
  - **Benchmarking**: Create reproducible load scenarios
297
+ - **IoT Integration**: Integrate with Home Assistant, Node-RED, or other MQTT-based systems
298
+ - **Monitoring**: Feed CPU metrics into monitoring dashboards via MQTT
196
299
 
197
300
  ## Development
198
301
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "cpu-loader"
3
- version = "0.0.3"
3
+ version = "0.1.3"
4
4
  description = "A tool to generate CPU load on a system with runtime configuration through a WebUI and REST API"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.8"
@@ -33,10 +33,11 @@ dependencies = [
33
33
  "requests==2.32.3",
34
34
  "psutil>=5.9.0",
35
35
  "websockets>=12.0",
36
+ "paho-mqtt>=2.1.0",
36
37
  ]
37
38
 
38
39
  [project.scripts]
39
- cpu-loader = "main:run"
40
+ cpu-loader = "cpu_loader.__main__:run"
40
41
 
41
42
  [project.urls]
42
43
  Homepage = "https://github.com/the78mole/cpu-loader"
@@ -50,4 +51,4 @@ build-backend = "setuptools.build_meta"
50
51
 
51
52
  [tool.setuptools]
52
53
  package-dir = {"" = "src"}
53
- py-modules = ["cpu_loader", "main", "example"]
54
+ packages = ["cpu_loader"]
@@ -9,8 +9,8 @@ if platform.system() != 'Windows':
9
9
  extra_link_args = ['-pthread']
10
10
 
11
11
  module = Extension(
12
- 'cpu_loader_core',
13
- sources=['src/cpu_loader_core.c'],
12
+ 'cpu_loader.cpu_loader_core',
13
+ sources=['src/cpu_loader/cpu_loader_core.c'],
14
14
  extra_compile_args=extra_compile_args,
15
15
  extra_link_args=extra_link_args,
16
16
  )
@@ -0,0 +1,5 @@
1
+ """CPU Loader - Generate configurable CPU load with WebUI and REST API."""
2
+
3
+ from .cpu_loader import CPULoader
4
+
5
+ __all__ = ["CPULoader"]
@@ -0,0 +1,6 @@
1
+ """Main entry point for CPU Loader application."""
2
+
3
+ from cpu_loader.main import run
4
+
5
+ if __name__ == "__main__":
6
+ run()
@@ -7,7 +7,7 @@ import multiprocessing
7
7
  from typing import Dict
8
8
 
9
9
  try:
10
- import cpu_loader_core
10
+ from cpu_loader import cpu_loader_core # type: ignore[attr-defined]
11
11
  except ImportError:
12
12
  raise ImportError(
13
13
  "cpu_loader_core module not found. Please build the C extension with: "