| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
import matplotlib.pyplot as plt |
| 4 |
import xdg |
| 5 |
import re |
| 6 |
import pycurl |
| 7 |
import certifi |
| 8 |
import os |
| 9 |
import sys |
| 10 |
from bs4 import BeautifulSoup |
| 11 |
from io import BytesIO |
| 12 |
|
| 13 |
# where we get the bench list from |
| 14 |
# sadly message edits don't appear in the public archive so we have to |
| 15 |
# download the gitlab raw logs too |
| 16 |
# (maybe we should be getting the messages from the zulip API instead??) |
| 17 |
archive = 'https://coq.gitlab.io/zulip-archive/stream/240008-GitHub-notifications/topic/Bench.20Notifications.html' |
| 18 |
|
| 19 |
print ('Getting bench list.') |
| 20 |
buffer = BytesIO() |
| 21 |
c = pycurl.Curl() |
| 22 |
c.setopt(c.URL, archive) |
| 23 |
c.setopt(c.WRITEFUNCTION, buffer.write) |
| 24 |
c.setopt(c.CAINFO, certifi.where()) |
| 25 |
c.perform() |
| 26 |
c.close() |
| 27 |
str = buffer.getvalue().decode('utf8') |
| 28 |
|
| 29 |
print ('Parsing bench list.') |
| 30 |
|
| 31 |
# parse HTML and remove tags |
| 32 |
# NB since coq was made default language on zulip the tables are full of <span> tags |
| 33 |
# so we really don't want to keep them |
| 34 |
str = BeautifulSoup(str, "html.parser").text |
| 35 |
|
| 36 |
# NB: the line with the date starts with a space |
| 37 |
# (in the original there is the avatar broken image) |
| 38 |
datere = re.compile(' Bench bot \((.*)\):') |
| 39 |
|
| 40 |
jobre = re.compile('Bench at https://gitlab.com/coq/coq/-/jobs/(.*)$') |
| 41 |
|
| 42 |
benches=[] |
| 43 |
curdate=None |
| 44 |
|
| 45 |
for line in iter(str.splitlines()): |
| 46 |
match = re.match(datere, line) |
| 47 |
if match: |
| 48 |
# Next post |
| 49 |
curdate = match[1] |
| 50 |
else: |
| 51 |
match = re.match(jobre, line) |
| 52 |
if match: |
| 53 |
job = match[1] |
| 54 |
benches += [(curdate, match[1])] |
| 55 |
|
| 56 |
cachedir = xdg.xdg_cache_home() / "coq-bench-plotter" |
| 57 |
os.makedirs(cachedir, exist_ok=True) |
| 58 |
|
| 59 |
## download job logs |
| 60 |
print ('Downloading job logs.') |
| 61 |
|
| 62 |
m = pycurl.CurlMulti() |
| 63 |
m.setopt(pycurl.M_MAX_HOST_CONNECTIONS, 8) |
| 64 |
|
| 65 |
files=[] |
| 66 |
|
| 67 |
for _, job in benches: |
| 68 |
fname = cachedir / (job + '.log') |
| 69 |
if not(os.path.exists(fname)): |
| 70 |
f = open(fname, 'xb') # x: will error if already exists |
| 71 |
files += [f] |
| 72 |
c = pycurl.Curl() |
| 73 |
c.setopt(c.URL, 'https://gitlab.com/coq/coq/-/jobs/' + job + '/raw') |
| 74 |
c.setopt(c.WRITEFUNCTION, f.write) |
| 75 |
c.setopt(c.CAINFO, certifi.where()) |
| 76 |
c.setopt(c.FOLLOWLOCATION, True) |
| 77 |
m.add_handle(c) |
| 78 |
|
| 79 |
num_handles = len(files) |
| 80 |
while num_handles: |
| 81 |
print (f'Downloading {num_handles} log files.') |
| 82 |
ret = m.select(5.0) |
| 83 |
if ret == -1: |
| 84 |
continue |
| 85 |
while 1: |
| 86 |
ret, num_handles = m.perform() |
| 87 |
if ret != pycurl.E_CALL_MULTI_PERFORM: |
| 88 |
break |
| 89 |
|
| 90 |
for f in files: |
| 91 |
f.flush() |
| 92 |
f.close() |
| 93 |
|
| 94 |
## parse job logs |
| 95 |
print ('Parsing job logs.') |
| 96 |
|
| 97 |
# captures package name and OLD time |
| 98 |
# the numerals are to avoid matching the table header |
| 99 |
packre = re.compile('│[ ]+([^ ]+) │[ ]+[^ ]+[ ]+([0-9][^ ]*)[ ]+([^ ]+)') |
| 100 |
|
| 101 |
parsed=[] |
| 102 |
for date, job in benches: |
| 103 |
cur={} |
| 104 |
fname = cachedir / (job + '.log') |
| 105 |
with open(fname) as f: |
| 106 |
for l in f: |
| 107 |
match = re.match(packre, l) |
| 108 |
if match: |
| 109 |
# the table appears multiple times in the log so this may be overriding the value |
| 110 |
# that is OK (it's not worth bothering to find the last table) |
| 111 |
cur[match[1]] = (match[2], match[3]) |
| 112 |
if cur: |
| 113 |
parsed += [(date, job, cur)] |
| 114 |
|
| 115 |
def filter_to(packname): |
| 116 |
dates=[] |
| 117 |
jobs=[] |
| 118 |
times=[] |
| 119 |
changes=[] |
| 120 |
for date, job, packs in parsed: |
| 121 |
if packname in packs: |
| 122 |
dates += [date] |
| 123 |
jobs += [job] |
| 124 |
time, change = packs[packname] |
| 125 |
times += [float(time)] |
| 126 |
changes += [float(change)] |
| 127 |
return dates, jobs, times, changes |
| 128 |
|
| 129 |
def plot(packname): |
| 130 |
dates, jobs, times, changes = filter_to(packname) |
| 131 |
# alternatively you can use dates for x axis, times for y axis |
| 132 |
# with x = jobs, if you find an interesting point, hover the mouse on it |
| 133 |
# pyplot will say somewhere in the window "x=..., y=..." |
| 134 |
# now if you don't want to read and type the whole 10 digit number |
| 135 |
# export as svg and search for the last 3 digits followed by a space |
| 136 |
# it should be easy to find a xml comment with the job number eg "<!-- 2100592270 -->" |
| 137 |
plt.plot(jobs, changes) |
| 138 |
plt.ylabel(packname) |
| 139 |
plt.show() |
| 140 |
|
| 141 |
def list_packs(): |
| 142 |
known={} |
| 143 |
for _, _, packs in parsed: |
| 144 |
for pack in packs.keys(): |
| 145 |
if pack in known.keys(): |
| 146 |
known[pack] += 1 |
| 147 |
else: |
| 148 |
known[pack] = 1 |
| 149 |
return known |
| 150 |
|
| 151 |
plot(sys.argv[1]) |