https://github.com/thevickypedia/stress-injector
Python script, to inject memory, CPU and URL stress
https://github.com/thevickypedia/stress-injector
cpu-stress memory-management memory-stress multiprocessing numpy-arrays stress-test
Last synced: 2 months ago
JSON representation
Python script, to inject memory, CPU and URL stress
- Host: GitHub
- URL: https://github.com/thevickypedia/stress-injector
- Owner: thevickypedia
- License: mit
- Created: 2020-10-08T04:17:37.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-05-18T05:46:20.000Z (about 1 year ago)
- Last Synced: 2025-02-28T10:35:58.607Z (3 months ago)
- Topics: cpu-stress, memory-management, memory-stress, multiprocessing, numpy-arrays, stress-test
- Language: Python
- Homepage: https://thevickypedia.github.io/stress_injector/
- Size: 389 KB
- Stars: 2
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pypi.org/project/stress-injector)
[](https://pypi.org/project/stress-injector)[](https://github.com/thevickypedia/stress-injector/actions/workflows/pages/pages-build-deployment)
[](https://github.com/thevickypedia/stress-injector/actions/workflows/python-publish.yml)[](https://pypi.org/project/stress-injector/#files)
[](https://pypi.org/project/stress-injector)
[](https://api.github.com/repos/thevickypedia/stress-injector)
[](https://api.github.com/repos/thevickypedia/stress-injector)
[](https://api.github.com/repos/thevickypedia/stress-injector)# Stress Injector
Python module, to inject memory, CPU and URL stress.Insights about CPU Stress
* To achieve CPU stress, I have used multiprocess, looped for the number of logical cores, triggering an infinite loop on
each core.
* The infinite loop will run for a given number of seconds (provided by the user)
* Mean-while the `cpu_percent` from `psutil` runs (in a dedicated thread) in an infinite loop calculating the current CPU
utilization on each CPU core.
* The dedicated thread runs for 3 seconds in addition to the number of seconds provided by the user.
* Once the given number of seconds have passed, the `processes` and `threads` initiated to monitor CPU usage are stopped.
Insights about Memory Stress
* In this script, I have used `numpy.random.bytes` which are sampled from uniform distribution.
* These random bytes are collected from the machine's physical memory increasing the program's usage.
* I have then used `getrusage` (get resource usage) for `SELF` to get the memory consumed only by the current script.
* The `size_converter` converts the bytes from resource usage to a human understandable format.
Insights about URL Stress
* In this script, I have used threadpools to make concurrent requests.
* The script uses `requests` module to make calls.
* Takes arguments
* **rate**: Number of calls to make. _Defaults to 100K_
* **timeout**: Timeout for each request. _Defaults to 0.5_
* **retry_limit**: Retry limit if the system is unable to spinup more threads. _Defaults to 5_
* **circuit_break**: Wait time in seconds between retries. _Defaults to 5_
* **request_type**: Function from `requests` module.### Usage
`pip install stress-injector`[CPU Stress](https://github.com/thevickypedia/stress-injector/blob/main/stressinjector/cpu.py)
```python
import stressinjector as injectorif __name__ == '__main__':
injector.CPUStress(seconds=300)
```[Memory Stress](https://github.com/thevickypedia/stress-injector/blob/main/stressinjector/memory.py)
```python
import stressinjector as injectorif __name__ == '__main__':
injector.MemoryStress(gigabytes=2_000)
```[URL Stress](https://github.com/thevickypedia/stress-injector/blob/main/stressinjector/url.py)
```python
import os
import stressinjector as injectorif __name__ == '__main__':
injector.URLStress(url='http://0.0.0.0:5002/') # Stress test GET calls# Stress test POST calls, also supports PUT, and DELETE
sample_data = {'headers': {'Authorization': 'Bearer %s' % os.environ.get('TOKEN')}}
injector.URLStress(
url='http://0.0.0.0:5002/',
request_type=injector.RequestType.post,
**sample_data
)
```
> This module can only induce stress on a given URL by making N number of calls. Suitable for APIs running on localhost.
>
> To perform a real-time load test, refer [locust.io](https://locust.io/)#### Coding Standards
Docstring format: [`Google`](https://google.github.io/styleguide/pyguide.html#38-comments-and-docstrings)
Styling conventions: [`PEP 8`](https://www.python.org/dev/peps/pep-0008/)
Clean code with pre-commit hooks: [`flake8`](https://flake8.pycqa.org/en/latest/) and
[`isort`](https://pycqa.github.io/isort/)#### [Release Notes](https://github.com/thevickypedia/stress-injector/blob/main/release_notes.rst)
**Requirement**
```shell
python -m pip install gitverse
```**Usage**
```shell
gitverse-release reverse -f release_notes.rst -t 'Release Notes'
```#### Linting
`PreCommit` will ensure linting, and the doc creation are run on every commit.**Requirement**
```bash
pip install --no-cache sphinx==5.1.1 pre-commit recommonmark
```**Usage**
```bash
pre-commit run --all-files
```### Pypi Package
[](https://packaging.python.org/tutorials/packaging-projects/)[https://pypi.org/project/stress-injector/](https://pypi.org/project/stress-injector/)
### Runbook
[](https://www.sphinx-doc.org/en/master/man/sphinx-autogen.html)[https://thevickypedia.github.io/stress-injector/](https://thevickypedia.github.io/stress-injector/)
## License & copyright
© Vignesh Sivanandha Rao
Licensed under the [MIT License](https://github.com/thevickypedia/stress-injector/blob/main/LICENSE)