Skip to the content.

Stats

start

it should be call first to start stats task, so all stats info could be sent to realtime monitor and recorded into stats files for later use.

set_response_time_ranges

It is to set response time ranges, so in the statistics we could see how many reponses in what response time range.

If not called, default ranges are: 0-100ms , 100-500ms, 500-1000ms , >1000ms


It should be called before calling the method start, and the paramter is a list of int to specify the ranges.

The following code will set the ranges to 0-100ms , 100-500ms, 500-1000ms , >1000ms

Stats.set_response_time_ranges([100, 500, 1000])

After testing done, we will get stats like this

0-100ms      : 100
100-500ms    : 130
500-1000ms   : 23
>1000ms      : 3

if not set, [100, 500, 1000,3000] will be the default one.

HttpClient

One HttpClient object represents a http client,it is defined as this

def __init__(self, timeout:int=10, proxy: None|str=None)


Parameters:

send

send method is used to send HTTP request to server and receive response from server.

def send(self,
    method: str,
    url:    str,
    params: Dict[str, str] | None=None,
    headers:Dict[str, str] | None=None, 
    data:   Dict[str, str] | str | bytes | None=None, 
    json:   Dict[str, str] | None=None,
    request_body_encoding:str | None=None,
    debug:bool=False,
    debug_print_request_body_encoding:  str | None=None,
    debug_print_response_body_encoding: str | None=None,    
    debug_print_body_max_len:           int=4096,  
):

Parameters:


send method returns a HttpResponse object.

get/post/put/delete/patch/head

They are shotcut methods of send(method=xxx, ...)

HttpResponse

send method returns a HttpResponse object through which we could get information like response status code/headers/body, response time, etc.

error_type

If server did reponse, the value of the attribute error_type of HttpResponse is None .

Otherwise, it is a integer value indicate no reponse, and something wrong happend.

response_time

We could get response time by the attribute response_time of HttpResponse.

It is in milliseconds.

Like

res = client.get('/api/path1' )
print(f"response time is {res.response_time} ms") 

status_code

We could get status code by the attribute status_code of HttpResponse.

Like

res = client.get('/api/path1' )
print(f"status code is {res.status_code} ") 

headers

We could get status code by the attribute headers of HttpResponse.

We can view the server’s response headers like this:

client = HttpClient() 
res = client.get('/api/path1' )

print(res.headers)

The output is like this

Content-Type: application/json; charset=utf-8
Date: Tue, 06 Feb 2024 03:41:17 GMT
Content-Length: 141



HTTP Header names are case-insensitive.

So, we can access the headers using any capitalization we want:

>>> res.headers['Content-Type']
'application/json'

>>> res.headers.get('content-type')
'application/json'

text

If response body is text, We can read the content of the server’s response as string by attribute text, like this

client = HttpClient() 
res = client.get('https://api.github.com/events' )
print(f"response body is:\n {res.text} ") 

hyload will automatically decode content from the server.

hyload makes guesses about the encoding of the response based on the HTTP headers. The text encoding guessed by hyload is used when you access r.text. You can find out what encoding hyload is using, and change it, using the encoding attribute:

比如

client = HttpClient() 
res = client.get('https://www.163.com/' )

r.encoding = 'gbk'
print(f"response body is:\n{res.text} ") 

json()

If response body is text in JSON format, you could just use method json() of reponse object to parse it and get corresponding Python object.

Like

client = HttpClient() 
res = client.get('https://httpbin.org/json' )
print(f"response body is:\n{res.json()} ") 

content

You can also access the response body as bytes, usually for non-text requests, by attribute content,:

client = HttpClient() 
res = client.get('https://httpbin.org/image' )

body = res.content

run_task

By calling run_task with passing callable object as parameter target , usually a function or method, hyload will run target code in hyload task, which is a greenlet (a coroutine, a light-weight thread) under the hood.

All tasks run in parallel.

wait_for_tasks_done

Calling wait_for_tasks_done will wait until all hyload tasks are over.