silverstream/tools/plot_stats.py
Casper V. Kristensen 49eb78d5c0
Publish.
2019-04-19 02:33:31 +02:00

48 lines
1.4 KiB
Python

import json
import sys
from datetime import datetime
from pathlib import Path
import matplotlib.pyplot as plt
paths = [Path(f) for f in sys.argv[1:]]
if not paths:
exit(f"Usage: {__file__} <stats.json> <stats.json> ..")
def parse_stats(path: Path):
timestamps = []
data = []
with path.open() as file:
for line in file.readlines():
stat = json.loads(line)
timestamps.append(datetime.utcfromtimestamp(stat["utc"]))
data.append(stat["data"])
return max(d["crawler"]["num_nodes"] for d in data), timestamps, data
fig, (ax1, ax2) = plt.subplots(1, 2)
ax1.set_ylabel("Torrent Hashes")
ax2.set_ylabel("Peers")
ax2.yaxis.tick_right()
ax2.yaxis.set_label_position("right")
for ax in (ax1, ax2):
ax.set_xlabel("Hours")
ax.tick_params("y")
ax.yaxis.set_major_formatter(plt.FuncFormatter(lambda x, loc: "{:,}".format(int(x))))
ax.grid(linewidth=.2)
for num_nodes, timestamps, data in sorted((parse_stats(p) for p in paths), reverse=True):
timestamps = [(t - timestamps[0]).total_seconds()/3600 for t in timestamps] # convert to hours relative to start
ax1.plot(timestamps, [d["torrent_database"]["num_hashes"] for d in data], label=f"{num_nodes} nodes")
ax2.plot(timestamps, [d["crawler"]["num_peers"] for d in data])
ax1.legend(loc="upper left")
#fig.subplots_adjust(wspace=0)
plt.show()