Skip to content

Instantly share code, notes, and snippets.

@pelletier
Last active January 3, 2022 23:53
Show Gist options
  • Save pelletier/1ae5dac452492fa6fdaa76c112dfa558 to your computer and use it in GitHub Desktop.
Save pelletier/1ae5dac452492fa6fdaa76c112dfa558 to your computer and use it in GitHub Desktop.
# Script to generate performance graph for utf8.Valid.
# Takes output of go test -bench as stdin.
#
# make && go test ./utf8 -run=None -bench=BenchmarkValid|tee out.txt
# python plot.py < out.txt
# open data.png
import fileinput
import re
import tempfile
import os
data = {}
for line in fileinput.input():
if "BenchmarkValid/small" not in line:
continue
x = re.search(r'BenchmarkValid/small([0-9]+)/([a-zA-Z]+)-[0-9]+.+?([0-9.]+) ns/op', line)
if x == None:
continue
size, name, time = x.groups()
size = int(size)
time = float(time)
name = name.lower()
if name not in data:
data[name] = []
data[name].append((size, time))
dataFile = 'plot.dat'
titles = []
with open(dataFile, 'w+') as f:
first = True
for name, points in data.items():
titles.append(name)
if not first:
print("\n", file=f)
points = sorted(points)
for x, y in points:
print(" {} {}".format(x, y), file=f)
first = False
scriptFile = 'plot.p'
output = 'data.png'
with open(scriptFile, 'w+') as f:
print(f"""
set terminal svg enhanced mouse size 1024,758
set terminal pngcairo size 1024,758
set output '{output}'
set ylabel 'ns/op'
set xlabel 'bytes'
set key inside bottom right
""", file=f)
first = True
idx = 0
for title in titles:
if not first:
print(", \\", file=f)
fileName = dataFile if first else ''
command = 'plot' if first else ' '
print(f"{command} '{fileName}' index {idx} with linespoints linestyle {idx+1} title \"{title}\"".format(), file=f, end='')
first = False
idx += 1
print("", file=f)
os.system(f"gnuplot {scriptFile}")
print(f"Result written to {output}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment