Created
July 8, 2020 14:41
-
-
Save sirosen/73f2e525c3eebd5fc3dfc6d60da0a160 to your computer and use it in GitHub Desktop.
ASCII art server timing bar graph from response object
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
""" | |
Parse and display Server-Timing header info | |
""" | |
import shutil | |
def timing_string_to_dict(server_timing_string): | |
""" | |
Given a Server Timing value as a string, parse it into a dict of the format | |
nice_name: value | |
For example | |
'a=1, "alpha"; b=2' | |
will parse as | |
{"alpha": 1, "b": 2} | |
""" | |
def parse_item(item): | |
item = [x.strip() for x in item.split(";")] | |
assert len(item) <= 2, "Too many semicolons in timing item, cannot parse" | |
nice_name = None | |
if len(item) == 2: | |
nice_name = item[1].strip('"') | |
item = item[0] | |
item = item.split("=") | |
assert len(item) == 2, "Wrong number of '=' delimited values" | |
if not nice_name: | |
nice_name = item[0] | |
return (nice_name, float(item[1])) | |
items = [x.strip() for x in server_timing_string.split(",")] | |
return {key: value for (key, value) in [parse_item(x) for x in items]} | |
def render_dict_onscreen(timing_dict): | |
term_width = shutil.get_terminal_size((80, 20)).columns | |
use_width = term_width - 4 | |
items = sorted(list(timing_dict.items()), key=lambda x: x[1]) | |
items = [("{}={}".format(*item), item[1]) for item in items] | |
last = items[-1] | |
factor = last[1] | |
desc_width = (max(len(x[0]) for x in items) if items else 0) + 1 | |
print("+" + "-" * (term_width - 2) + "+") | |
for desc, size in items: | |
desc = desc.ljust(desc_width, ".") | |
bar_width = max(int((use_width - desc_width) * size / factor), 1) | |
msg = (desc + "#" * bar_width).ljust(use_width, " ") | |
print("| {} |".format(msg)) | |
print("+" + "-" * (term_width - 2) + "+") | |
def render_timing_from_response(response): | |
timing_str = response._data.headers.get("Server-Timing") | |
if timing_str: | |
render_dict_onscreen(timing_string_to_dict(timing_str)) |
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
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | |
| Query Parsing Time=0.14.......# | | |
| query_exec_build_query=0.34...# | | |
| query_exec_render_result=5.58.## | | |
| raw_query_exec=15.89..........###### | | |
| query_exec_invoke=17.83.......###### | | |
| Query Execution Time=23.99....######### | | |
| overall=128.38................################################################# | | |
| groupauth=178.64..............#################################################################### | | |
| auth=249.05...................############################################################################################### | | |
| total=382.72..................################################################################################################################################################### | | |
+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment