Skip to main content

ThreadRegulator

Python class that allows to control thread execution in time (requests per second) for both constant rate mode, or burst mode. With a notify option that is called after a specific number of executions or a time delta

Sample code

from thread_regulator import ThreadRegulator, safe_sleep, create_regular, create_burst
from thread_regulator.graphs import PerformanceGraphs
from random import choice


def demo_constant_rate():
    def my_notifier(stats_dict, arg1, **kwargs):
        print(arg1, stats_dict)

    def my_thread_call(*args, **kwargs):
        safe_sleep(choice((0.1, 0.2, 0.3)))
        return True

    tr = create_regular(users=4, rps=10.0, duration_sec=1.0, executions=15)
    print(tr)
    print("="*100)
    tr.set_notifier(notify_method=my_notifier, every_sec=1, every_exec=8, notify_method_args=("notify_example_arg_1", )).\
        start(my_thread_call, "arg1", "arg2", arg3="my_name", arg4="my_demo")

    return tr


def demo_burst_mode():
    def my_notifier(arg1, stats_dict):
        print(arg1, stats_dict)

    def my_thread_call(*args, **kwargs):
        safe_sleep(choice((0.1, 0.2, 0.3)))
        return True

    tr = create_burst(users=4, rps=10.0, duration_sec=2.0, req=10, dt_sec=0.5, executions=20)
    print(tr)
    print("="*100)

    tr.set_notifier(notify_method=my_notifier, every_sec=1, every_exec=8, notify_method_args=("notify_example_arg_1", )). \
        start(my_thread_call, "arg1", "arg2", arg3="my_name", arg4="my_demo")

    return tr


def show_statistics(tr):
    print("="*100)
    print("Statistics:", tr.get_statistics())
    print(f"Requests start_time jitter:\n{tr.get_execution_dataframe().start_ts.diff().describe()}")
    print(f"Requests call period: {tr.get_executions_call_period()}")
    print(f"Should be executed {tr.get_max_executions()} requests, and {tr.get_executions_started()} were executed, and {tr.get_executions_completed()} completed, and {tr.get_executions_missing()} missing.", )
    print("How many successes over how many requests executed:", tr.get_success_ratio())
    print("How many successes over how many requests should be executed:", tr.get_success_ratio_overall())


def save_results(tr, filename):
    pg = PerformanceGraphs()
    # this will save the results on an Excel file (that can be used to plot graphs as explained on last bullet)
    pg.collect_data(tr).save_data(filename)


if __name__ == "__main__":
    print("RegularMode")
    show_statistics(demo_constant_rate())

    print("\n\nBurstMode")
    tr = demo_burst_mode()
    show_statistics(tr)

    save_results(tr, "burst_mode_results.xls")

To see the graphical results:

  • Run python -m thread_regulator
  • Open the browser http://127.0.0.1:8050/
  • Drag/drop to the browser the .xls file (saved before)

Counters

Durations

Resample

Metadata

Release files for thread-regulator-pjn2work 0.2.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thread-regulator-pjn2work 0.2.5
File Size Uploaded Provenance Actions
thread_regulator-pjn2work-0.2.5.tar.gz 15.9 kB Without provenance Details for thread_regulator-pjn2work-0.2.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for thread-regulator-pjn2work 0.2.5
File Interpreter ABI Platform Actions
thread_regulator_pjn2work-0.2.5-py3-none-any.whl Python 3 none any Details for thread_regulator_pjn2work-0.2.5-py3-none-any.whl

Total release size: 33.1 kB

Release files / thread_regulator-pjn2work-0.2.5.tar.gz

Download URL thread_regulator-pjn2work-0.2.5.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
10731672a310d9799e9a8259a15c8c98220c051d17b6adc309bcf8c0c623bfed
BLAKE2b-256 checksum
How to use checksums
e211ff465be3c773e35a1571fa8f9681b36232a428aeed4d1d822524ee55255e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/3.10.0 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.2

Release files / thread_regulator_pjn2work-0.2.5-py3-none-any.whl

Download URL thread_regulator_pjn2work-0.2.5-py3-none-any.whl
Size 17.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
762e125252996a96af44e10b2871225e6dcc6931fa2fa27ef2eddaf2f2259ed3
BLAKE2b-256 checksum
How to use checksums
219d16a426ea761edaf54c1483475059b26dd55d3cae77c80bf3de1e974d5c8b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.6.0 importlib_metadata/3.10.0 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.2

Security for thread-regulator-pjn2work 0.2.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page