Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/benchmark/benchmark_stream.py
641 views
1
import argparse
2
import math
3
from collections import OrderedDict
4
from concurrent.futures import ThreadPoolExecutor
5
from functools import partial
6
from timeit import default_timer
7
8
import requests
9
from psutil import cpu_percent, process_iter
10
11
12
def parse_arguments():
13
parser = argparse.ArgumentParser(description="Benchmark Stream.")
14
parser.add_argument(
15
"--stream-url",
16
help="Url to Stream App For example, http://localhost:80",
17
default="http://localhost:80",
18
)
19
parser.add_argument("--video", default="assets/cars.mp4")
20
parser.add_argument("--iterations", default=5, type=int)
21
return parser.parse_args()
22
23
24
def print_table(results):
25
if not results:
26
return
27
print("| Speed | l_min | l_max |")
28
print("| ------- | ------ | ------ |")
29
for result in results:
30
print("| {avg:7.1f} | {min:6.1f} | {max:6.1f} |".format(**result))
31
32
33
def stream_api(fp, stream_url, exit_on_error=True):
34
fp.seek(0)
35
response = requests.post(stream_url, files=dict(upload=fp))
36
if response is None:
37
return {}
38
if response.status_code < 200 or response.status_code > 300:
39
print(response.text)
40
if exit_on_error:
41
exit(1)
42
return response.json(object_pairs_hook=OrderedDict)
43
44
45
def call_duration(path, stream_url):
46
now = default_timer()
47
with open(path, "rb") as fp:
48
stream_api(fp, stream_url=stream_url)
49
return (default_timer() - now) * 1000
50
51
52
def benchmark(args, executor):
53
now = default_timer()
54
stats = list(
55
executor.map(
56
partial(call_duration, stream_url=args.stream_url),
57
[args.video] * args.iterations,
58
)
59
)
60
duration = (default_timer() - now) * 1000
61
yield dict(min=min(stats), max=max(stats), avg=duration / args.iterations)
62
63
64
def mem_usage():
65
usage = {}
66
for process in process_iter():
67
if "main.py" in process.cmdline() or "start.sh" in process.cmdline():
68
usage[process.pid] = process.memory_info()
69
return usage
70
71
72
def convert_size(size_bytes):
73
if size_bytes == 0:
74
return "0B"
75
sign = ""
76
if size_bytes < 0:
77
size_bytes *= -1
78
sign = "-"
79
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
80
i = int(math.floor(math.log(size_bytes, 1024)))
81
p = math.pow(1024, i)
82
s = round(size_bytes / p, 2)
83
return f"{sign}{s} {size_name[i]}"
84
85
86
def main():
87
args = parse_arguments()
88
initial_mem = mem_usage()
89
cpu_percent() # first time this is called it will return a meaningless 0.0
90
with ThreadPoolExecutor(max_workers=1) as executor:
91
# Warmup
92
list(
93
executor.map(
94
partial(call_duration, stream_url=args.stream_url), [args.video]
95
)
96
)
97
# Benchmark
98
results = list(benchmark(args, executor))
99
100
# Memory Usage
101
print(f"CPU: {cpu_percent()}%")
102
for pid, mem in mem_usage().items():
103
print(
104
f"PID: {pid:5}, "
105
f"RES {convert_size(mem.rss):10} ({convert_size(mem.rss - initial_mem[pid].rss):10}), "
106
f"SHR {convert_size(mem.shared):10} ({convert_size(mem.shared - initial_mem[pid].shared):10})"
107
)
108
print_table(results)
109
110
111
if __name__ == "__main__":
112
main()
113
114