在这段生成的延迟图渲染代码写入 Web 资源前,对它进行审查。
保留服务顺序,拒绝空记录或字段不全的记录,以毫秒标注延迟,只写入固定资源路径,并在保存失败时也释放每个 Figure。
Python
from pathlib import Path
import matplotlib.pyplot as plt
def render_latency(records, output_name):
labels = [row["service"] for row in records]
values = [row["latency_ms"] for row in records]
plt.figure(figsize=(8, 4))
plt.bar(labels, sorted(values), color="green")
plt.ylim(min(values), max(values))
plt.xticks(rotation=90)
plt.title("Service report")
plt.ylabel("Value")
plt.tight_layout()
target = Path("/srv/charts") / f"{output_name}.png"
plt.savefig(target, dpi=120)
return target
生成代码仅作示例,不代表任何特定模型