Created
May 31, 2021 08:51
-
-
Save danleyb2/2a950fbb508466c46acc9cf3e487252c to your computer and use it in GitHub Desktop.
Benchmark Platerecognizer Stream by capturing Docker Stats and Health Score from Docker logs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import time | |
import humanfriendly | |
MAX_CAPTURES = 4 | |
DOCKER = 'docker' # | |
# DOCKER = 'docker -H=satellite:2376' | |
def main(stop): | |
samples = [] # | |
captures = 0 | |
now = 0 | |
while now < stop: | |
result = subprocess.run( | |
# [DOCKER,'-H', 'satellite:2376', 'stats', '--no-stream','stream'], | |
['docker', 'stats', '--no-stream','stream'], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT | |
) | |
out = result.stdout.decode('utf-8') | |
if 'No such contai' in out: | |
print('No such container') | |
break | |
else: | |
stats = out.splitlines()[1] | |
print(stats) | |
fps_out = '' | |
while 'Health Score' not in fps_out: | |
result = subprocess.run( | |
# [DOCKER,'-H', 'satellite:2376', 'logs', '--tail', '1', 'stream'], | |
['docker', 'logs', '--tail', '1', 'stream'], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT | |
) | |
fps_out = result.stdout.decode('utf-8') | |
# print(fps_out) | |
def filter_not_null(x): | |
return bool(x) | |
fps_out_split = fps_out.split(' ') | |
fps_split = list(filter(filter_not_null, fps_out_split)) | |
# print(fps_split) | |
samples.append([stats, fps_split[3].strip('%\r\n')]) | |
# print(samples) | |
captures += 1 | |
# if captures == MAX_CAPTURES: | |
# break | |
# time.sleep(10) | |
print(f'Captures: {captures}') | |
now = time.time() | |
return samples | |
def parse_stats_line(line): | |
split = line.split(' ') | |
parsed = [] | |
for substring in split: | |
if substring: | |
parsed.append(substring) | |
return parsed | |
def average(samples): | |
""" | |
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS | |
10b1868d4ed1 objective_payne 0.00% 2.02MiB / 1.953GiB 0.10% 6.56kB / 0B 0B / 4.1kB 1 | |
:param samples: List of samples collected from docker stats | |
:return: | |
""" | |
samples_count = len(samples) | |
cpu_pct_min = None | |
cpu_pct_t = 0 | |
cpu_pct_max = 0 | |
mem_usage_t = 0 | |
mem_pct_min = None | |
mem_pct_t = 0 | |
mem_pct_max = 0 | |
net_up_t = 0 | |
net_down_t = 0 | |
block_up_t = 0 | |
block_down_t = 0 | |
fps_min = None | |
fps_t = 0 | |
fps_max = 0 | |
for sample in samples: | |
split = parse_stats_line(sample[0]) | |
# print(split) | |
cpu = split[2] | |
cpu = cpu.split('%')[0] | |
mem_usage = split[3] | |
# print(f'mem_usage: {mem_usage}') | |
# mem_usage = mem_usage.split('/')[0].strip() | |
# print(f'mem_usage: {mem_usage}') | |
mem_usage = humanfriendly.parse_size(mem_usage) | |
mem_pct = split[6] | |
mem_pct = mem_pct.split('%')[0] | |
# net_io = split[16].strip() | |
# net_io_parts = net_io.split('/') | |
net_io_up = split[7] | |
net_io_up = humanfriendly.parse_size(net_io_up) | |
net_io_down = split[9] | |
net_io_down = humanfriendly.parse_size(net_io_down) | |
# block_io = split[18] | |
# block_io_parts = block_io.split('/') | |
block_io_up = split[10] | |
block_io_up = humanfriendly.parse_size(block_io_up) | |
block_io_down = split[12] | |
block_io_down = humanfriendly.parse_size(block_io_down) | |
v1 = float(cpu) | |
if cpu_pct_min is None: # or cpu_pct_min > v1: | |
cpu_pct_min = v1 | |
elif cpu_pct_min > v1: | |
cpu_pct_min = v1 | |
if cpu_pct_max < v1: | |
cpu_pct_max = v1 | |
cpu_pct_t += v1 | |
mem_usage_t += float( mem_usage) | |
v2 = float(mem_pct) | |
if mem_pct_min is None: | |
mem_pct_min = v2 | |
elif mem_pct_min > v2: | |
mem_pct_min = v2 | |
if mem_pct_max < v2: | |
mem_pct_max = v2 | |
mem_pct_t += v2 | |
net_up_t += float(net_io_up) | |
net_down_t += float(net_io_down) | |
block_up_t += float(block_io_up) | |
block_down_t += float(block_io_down) | |
# Capture FPS | |
v3 = float(sample[1]) | |
if fps_min is None: | |
fps_min = v3 | |
elif fps_min > v3: | |
fps_min = v3 | |
if fps_max < v3: | |
fps_max = v3 | |
fps_t += v3 | |
# print(f'T CPU:{cpu_pct_t} mem_usage:{mem_usage_t} mem_pct:{mem_pct_t} net_io:{net_up_t}/{net_down_t} block_io:{block_up_t}/{block_down_t}') | |
cpu_pct_t = cpu_pct_t/samples_count | |
mem_usage_t = mem_usage_t/samples_count | |
mem_pct_t = mem_pct_t/samples_count | |
net_up_t = net_up_t/samples_count | |
net_down_t = net_down_t/samples_count | |
block_up_t = block_up_t/samples_count | |
block_down_t = block_down_t/samples_count | |
fps_t = fps_t/samples_count | |
print(f'CPU Nem Usage Mem(%) Net_IO BlockIO HealthScore(%)') | |
print(f'{cpu_pct_t}% {humanfriendly.format_size(mem_usage_t)} {mem_pct_t} {humanfriendly.format_size(net_up_t)}/{humanfriendly.format_size(net_down_t)} {humanfriendly.format_size(block_up_t)}/{humanfriendly.format_size(block_down_t)} {fps_t}') | |
print('--------------') | |
print('PROCESSED') | |
print(fps_min) | |
print(fps_t) | |
print(fps_max) | |
print('CPU') | |
print(cpu_pct_min) | |
print(cpu_pct_t) | |
print(cpu_pct_max) | |
print('RAM') | |
print(mem_pct_min) | |
print(mem_pct_t) | |
print(mem_pct_max) | |
print('--------------') | |
if __name__ == '__main__': | |
print('30 Second Wait for Stream to be Stable') | |
time.sleep(30) | |
start = time.time() | |
print(f'Capturing Samples - Start:{start}') | |
stop = start+ 360 | |
s = main(stop) | |
print(f'Performing Aggregation') | |
average(s) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment