libentry 1.18__py3-none-any.whl → 1.19__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
libentry/test_api.py ADDED
@@ -0,0 +1,120 @@
1
+ #!/usr/bin/env python3
2
+
3
+ __author__ = "xi"
4
+
5
+ import json
6
+ from argparse import ArgumentParser
7
+ from concurrent.futures import ThreadPoolExecutor
8
+ from threading import Lock
9
+ from time import time
10
+ from typing import Literal, Optional
11
+
12
+ from pydantic import BaseModel
13
+
14
+ from libentry.api import APIClient
15
+
16
+
17
+ class TestRequest(BaseModel):
18
+ url: str
19
+ method: Literal["GET", "POST"] = "GET"
20
+ data: Optional[dict] = None
21
+ timeout: float = 15
22
+ num_threads: int = 1
23
+ num_calls: int = 1
24
+ quiet: bool = False
25
+ max_resp_len: int = 100
26
+
27
+
28
+ class TestResponse(BaseModel):
29
+ num_success: int = 0
30
+ num_failed: int = 0
31
+ avg_time: float = 0
32
+ max_time: float = 0
33
+ min_time: float = 0
34
+
35
+
36
+ def test(request: TestRequest):
37
+ time_list = []
38
+ lock = Lock()
39
+
40
+ def _worker(tid):
41
+ for cid in range(request.num_calls):
42
+ try:
43
+ kwargs = dict(
44
+ on_error=lambda err: print(f"[{tid}:{cid}:RETRY] {err}"),
45
+ timeout=request.timeout
46
+ )
47
+ t = time()
48
+ if request.method == "GET":
49
+ response = APIClient().get(
50
+ request.url,
51
+ **kwargs
52
+ )
53
+ else:
54
+ response = APIClient().post(
55
+ request.url,
56
+ request.data,
57
+ **kwargs
58
+ )
59
+ t = time() - t
60
+ with lock:
61
+ time_list.append(t)
62
+ if not request.quiet:
63
+ content = str(response).replace("\n", " ")
64
+ print(f"[{tid}:{cid}:SUCCESS] Time={t:.04f} Response={content:.{request.max_resp_len}}")
65
+ except Exception as e:
66
+ print(f"[{tid}:{cid}:FAILED] {e}")
67
+
68
+ with ThreadPoolExecutor(request.num_threads) as pool:
69
+ futures = [pool.submit(_worker, i) for i in range(request.num_threads)]
70
+ for future in futures:
71
+ future.result()
72
+
73
+ if len(time_list) > 0:
74
+ return TestResponse(
75
+ num_success=len(time_list),
76
+ num_failed=request.num_threads * request.num_calls - len(time_list),
77
+ max_time=max(time_list),
78
+ min_time=min(time_list),
79
+ avg_time=sum(time_list) / len(time_list)
80
+ )
81
+ else:
82
+ return TestResponse(
83
+ num_success=0,
84
+ num_failed=request.num_threads * request.num_calls,
85
+ )
86
+
87
+
88
+ def main():
89
+ parser = ArgumentParser()
90
+ parser.add_argument("url", nargs="?")
91
+ parser.add_argument("--method", "-m", default="GET", choices=["GET", "POST"])
92
+ parser.add_argument("--data", "-d")
93
+ parser.add_argument("--timeout", type=float, default=15)
94
+ parser.add_argument("--num_threads", "-t", type=int, default=1)
95
+ parser.add_argument("--num_calls", "-c", type=int, default=1)
96
+ parser.add_argument("--quiet", "-q", action="store_true")
97
+ args = parser.parse_args()
98
+
99
+ if args.url is None:
100
+ print("URL should be given.")
101
+ return 1
102
+
103
+ data = args.data
104
+ if data is not None:
105
+ data = json.loads(data)
106
+ response = test(TestRequest(
107
+ url=args.url,
108
+ method=args.method,
109
+ data=data,
110
+ timeout=args.timeout,
111
+ num_threads=args.num_threads,
112
+ num_calls=args.num_calls,
113
+ quiet=args.quiet
114
+ ))
115
+ print(response.model_dump_json(indent=4))
116
+ return 0
117
+
118
+
119
+ if __name__ == "__main__":
120
+ raise SystemExit(main())
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: libentry
3
- Version: 1.18
3
+ Version: 1.19
4
4
  Summary: Entries for experimental utilities.
5
5
  Home-page: https://github.com/XoriieInpottn/libentry
6
6
  Author: xi
@@ -13,6 +13,7 @@ Requires-Dist: Flask
13
13
  Requires-Dist: PyYAML
14
14
  Requires-Dist: gunicorn
15
15
  Requires-Dist: json5
16
+ Requires-Dist: numpy
16
17
  Requires-Dist: pydantic
17
18
  Requires-Dist: requests
18
19
 
@@ -8,6 +8,7 @@ libentry/json.py,sha256=1-Kv5ZRb5dBrOTU84n6sZtYZV3xE-O6wEt_--ynbSaU,1209
8
8
  libentry/logging.py,sha256=IiYoCUzm8XTK1fduA-NA0FI2Qz_m81NEPV3d3tEfgdI,1349
9
9
  libentry/schema.py,sha256=BO7EE7i43Cb4xn_OLsBM-HDwWOT6V5fPs91Am3QqnNQ,8178
10
10
  libentry/server.py,sha256=gYPoZXd0umlDYZf-6ZV0_vJadg3YQvnLDc6JFDJh9jc,1503
11
+ libentry/test_api.py,sha256=uiHo9rVli9ENkzIyyFDDK6PdteBdetFTA8ikCDNykRQ,3488
11
12
  libentry/service/__init__.py,sha256=1oLL20yLB1GL9IbFiZD8OReDqiCpFr-yetIR6x1cNkI,23
12
13
  libentry/service/common.py,sha256=OVaW2afgKA6YqstJmtnprBCqQEUZEWotZ6tHavmJJeU,42
13
14
  libentry/service/flask.py,sha256=Alpsix01ROqI28k-dabD8wJlWAWs-ObNc1nJ-LxOXn4,13349
@@ -15,9 +16,10 @@ libentry/service/list.py,sha256=ElHWhTgShGOhaxMUEwVbMXos0NQKjHsODboiQ-3AMwE,1397
15
16
  libentry/service/running.py,sha256=FrPJoJX6wYxcHIysoatAxhW3LajCCm0Gx6l7__6sULQ,5105
16
17
  libentry/service/start.py,sha256=mZT7b9rVULvzy9GTZwxWnciCHgv9dbGN2JbxM60OMn4,1270
17
18
  libentry/service/stop.py,sha256=wOpwZgrEJ7QirntfvibGq-XsTC6b3ELhzRW2zezh-0s,1187
18
- libentry-1.18.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
19
- libentry-1.18.dist-info/METADATA,sha256=3LKED9qfL_-_CvMdtfNW2YnoK1LBDdnQxN7Bf2_ZsKE,791
20
- libentry-1.18.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
21
- libentry-1.18.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
22
- libentry-1.18.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
23
- libentry-1.18.dist-info/RECORD,,
19
+ libentry-1.19.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
20
+ libentry-1.19.dist-info/METADATA,sha256=L3cYIpAJrvAMvlHXoqrDqlKe3HeBf2_6BI8EfvqCGuo,812
21
+ libentry-1.19.dist-info/WHEEL,sha256=tZoeGjtWxWRfdplE7E3d45VPlLNQnvbKiYnx7gwAy8A,92
22
+ libentry-1.19.dist-info/entry_points.txt,sha256=vgHmJZhM-kqM7U9S179UwDD3pM232tpzJ5NntncXi_8,62
23
+ libentry-1.19.dist-info/top_level.txt,sha256=u2uF6-X5fn2Erf9PYXOg_6tntPqTpyT-yzUZrltEd6I,9
24
+ libentry-1.19.dist-info/zip-safe,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
25
+ libentry-1.19.dist-info/RECORD,,
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ libentry_test_api = libentry.test_api:main
3
+