Skip to content

Stress test tool with statistical TPS reports based on Worker Dispatcher in Python

License

Notifications You must be signed in to change notification settings

yidas/python-stress-tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Stress Tool


Stress test tool with statistical TPS reports based on Worker Dispatcher in Python

PyPI

Features

  • Based on Worker Dispatcher to managed workers

  • Statistical TPS Report in Excel sheets

  • Customized Config for the report


OUTLINE


DEMONSTRATION

Just write your own callback functions based on the Worker Dispatcher library, then run it and generate the report file:

import stress_test

def each_task(id: int, config, task, log):
    response = requests.get('https://your.name/reserve-api/')
    return response

def main():
    results = stress_test.start({
        'task': {
            'list': 1000,
            'callback': each_task,
        }
    })
    # Generate the TPS report if the stress test completes successfully.
    if results != False:
        file_path = stress_test.generate_report(file_path='./tps-report.xlsx')
        print("Report has been successfully generated at {}".format(file_path))

if __name__ == '__main__':
    main()


INTRODUCTION

This tool generates professional TPS report based on the execution result from the Worker Dispatcher library.

Dependencies:


INSTALLATION

To install the current release:

$ pip install stress-tool

Import it in your Pythone code:

import stress_test

USAGE

By calling the start() method with the configuration parameters, the package will invoke Worker Dispatcher to dispatch tasks, managing threading or processing based on the provided settings. Once the tasks are completed, generate_report() can be called to produce a TPS report based on the result of Worker Dispatcher.

generate_report()

An example configuration setting with all options is as follows:

def generate_report(config: dict={}, worker_dispatcher: object=None, file_path: str='./tps-report.xlsx', display_intervals: bool=True, interval: float=1):

config

Option Type Deafult Description
raw_logs.fields dict None Customized field setting for the Raw Logs sheet.
Key is field name, the value can be two types:
- String: Use the provided value as the log key name from Worker Dispatcher to retrieve the value.
- lambda function: Retrieve the return value from the lambda function.

Sample config

import stress_tool
import requests

# task.callback function
def task(id: int, config, task, log):
    try:
        response = log['response'] = requests.get('https://your.name/path/')
        try:
            api_return_code = log['api_return_code'] = response.json().get('returnCode')
            return True if api_return_code == "0000" else False
        except Exception as e:
            return False
    except requests.exceptions.ConnectionError:
        log['error'] = 'ConnectionError'
    return False

# Start stress test
results = stress_tool.start({
    # 'debug': True,
    'task': {
        'list': 60,
        'callback': task,
    },
})

# Generate the report
file_path = stress_test.generate_report(config={
    'raw_logs': {
        'fields': {
            'Customized Field - HTTP code': lambda log: log.get('response').status_code,
            'Customized Field - API Return code': 'api_return_code',
            'Customized Field - Response Body': lambda log: log.get('response').text,
        }
    },
})

display_intervals

Indicates whether to generate Intervals sheet.

interval

Based on Intervals sheet, specifies the number of seconds for each split.

About

Stress test tool with statistical TPS reports based on Worker Dispatcher in Python

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages