import plotly.graph_objects as go
from plotly.subplots import make_subplots
import hashlib
def get_color(name):
hash_object = hashlib.md5(name.encode())
color_index = int(hash_object.hexdigest(), 16) % 360
return f'hsl({color_index}, 100%, 50%)'
def plot_results(results, filename):
fig = make_subplots(rows=3, cols=1, shared_xaxes=True)
for i, (coro_name, x_values) in enumerate(results.items(), start=1):
x_list = []
y_starve_list = []
y_throughput_list = []
y_time_in_coro_list = []
starve_hover_text = []
throughput_hover_text = []
for speedup, experiments in x_values.items():
for experiment in experiments:
starve = experiment["time_starving"]
processed = experiment["processed"]
time_in_coro = experiment["time_in_coro"]
real_duration = experiment["real_duration"]
x_list.append(speedup)
# handle starve graph
starve_time = sum(starve.values())
y_starve_list.append(starve_time)
breakdown = "
".join([f" {key[0]} ({key[1]}): {round(value, 4)}" for key, value in starve.items()])
starve_hover_text.append(f"({speedup}, {round(starve_time, 4)})
{coro_name}
Breakdown:
{breakdown}")
# handle throughput graph
throughput = (speedup * processed) / real_duration
y_throughput_list.append(throughput)
throughput_hover_text.append(f"({speedup}, {round(throughput, 4 )})
{coro_name}
True experiment duration: {round(real_duration, 4)}
Total Processed: {processed}")
# handle time in coro graph
y_time_in_coro_list.append(time_in_coro / speedup)
fig.add_trace(go.Scatter(
x=x_list,
y=y_starve_list,
mode='markers',
name=coro_name,
hoverinfo='text',
hovertext=starve_hover_text,
marker=dict(color=get_color(coro_name)),
showlegend=True,
), row=1, col=1)
fig.add_trace(go.Scatter(
x=x_list,
y=y_throughput_list,
mode='markers',
name=coro_name,
hovertext=throughput_hover_text,
marker=dict(color=get_color(coro_name)),
showlegend=False,
), row=2, col=1)
fig.add_trace(go.Scatter(
x=x_list,
y=y_time_in_coro_list,
mode='markers',
name=coro_name,
marker=dict(color=get_color(coro_name)),
showlegend=False,
), row=3, col=1)
fig.update_xaxes(title_text="Speedup (times faster)", row=1, col=1)
fig.update_xaxes(title_text="Speedup (times faster)", row=2, col=1)
fig.update_xaxes(title_text="Speedup (times faster)", row=3, col=1)
fig.update_yaxes(title_text="Total Starvation Time (seconds)", row=1, col=1)
fig.update_yaxes(title_text="Total Throughput (handles per second)", row=2, col=1)
fig.update_yaxes(title_text="Synchronous Time in Coroutine (seconds)", row=3, col=1)
fig.write_html(filename)