File size: 2,663 Bytes
46f5320
 
 
 
 
 
 
 
 
 
 
 
 
c10d55e
46f5320
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""https://zetcode.com/python/concurrent-http-requests/"""

import asyncio
import random
import time
import pandas as pd
import httpx
from os.path import exists

NUMBER_OF_CALLS = 1

headers = {"Content-Type": "application/json; charset=utf-8"}

# base_url = "https://tangibleai-mathtext-fastapi.hf.space/{endpoint}"
base_url = "http://localhost:7860/run/{endpoint}"

data_list_1 = {
    "endpoint": "text2int",
    "test_data": [
        "one hundred forty five",
        "twenty thousand nine hundred fifty",
        "one hundred forty five",
        "nine hundred eighty three",
        "five million",
    ]
}

data_list_2 = {
    "endpoint": "text2int-preprocessed",
    "test_data": [
        "one hundred forty five",
        "twenty thousand nine hundred fifty",
        "one hundred forty five",
        "nine hundred eighty three",
        "five million",
    ]
}
data_list_3 = {
    "endpoint": "sentiment-analysis",
    "test_data": [
        "Totally agree",
        "I like it",
        "No more",
        "I am not sure",
        "Never",
    ]
}


# async call to endpoint
async def call_api(url, data, call_number, number_of_calls):
    json = {"data": [data]}
    async with httpx.AsyncClient() as client:
        start = time.perf_counter()  # Used perf_counter for more precise result.
        response = await client.post(url=url, headers=headers, json=json, timeout=30)
        end = time.perf_counter()
        return {
            "endpoint": url.split("/")[-1],
            "test data": data,
            "status code": response.status_code,
            "response": response.json().get("data"),
            "call number": call_number,
            "number of calls": number_of_calls,
            "start": start.__round__(4),
            "end": end.__round__(4),
            "delay": (end - start).__round__(4)
        }


data_lists = [data_list_1, data_list_2, data_list_3]

results = []


async def main(number_of_calls):
    for data_list in data_lists:
        calls = []
        for call_number in range(1, number_of_calls + 1):
            url = base_url.format(endpoint=data_list["endpoint"])
            data = random.choice(data_list["test_data"])
            calls.append(call_api(url, data, call_number, number_of_calls))
        r = await asyncio.gather(*calls)
        results.extend(r)



start = time.perf_counter()
asyncio.run(main(NUMBER_OF_CALLS))
end = time.perf_counter()
print(end-start)
df = pd.DataFrame(results)

if exists("call_history.csv"):
    df.to_csv(path_or_buf="call_history.csv", mode="a", header=False, index=False)
else:
    df.to_csv(path_or_buf="call_history.csv", mode="w", header=True, index=False)