OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 |
| 3 import argparse # for ArgumentParser |
| 4 import subprocess # for Popen |
| 5 import tempfile # for NamedTemporaryFile |
| 6 import os # for remove |
| 7 |
| 8 class gnuplot(object): |
| 9 |
| 10 output = "result.png" |
| 11 |
| 12 script = """ |
| 13 set terminal png size 1024, 768 |
| 14 set output "{}.png" |
| 15 set title "re2 benchlog" |
| 16 set datafile separator ";" |
| 17 set grid x y |
| 18 set ylabel "MB/s" |
| 19 set autoscale |
| 20 plot """ |
| 21 |
| 22 template = """'{}' using 1:5:xticlabels(2) with linespoints linewidth 3 titl
e "{}",\\\n""" |
| 23 |
| 24 benchdata = dict() |
| 25 tempfiles = [] |
| 26 |
| 27 def __enter__(self): |
| 28 return self |
| 29 |
| 30 def __exit__(self, type, value, traceback): |
| 31 """ |
| 32 remove all temporary files |
| 33 """ |
| 34 |
| 35 for filename in self.tempfiles: |
| 36 os.remove(filename) |
| 37 |
| 38 def parse_re2_benchlog(self, filename): |
| 39 """ |
| 40 parse the input benchlog and return a dictionary contain bench data |
| 41 """ |
| 42 |
| 43 benchdata = self.benchdata |
| 44 |
| 45 with open(filename) as f: |
| 46 |
| 47 for raw in f.readlines(): |
| 48 |
| 49 data = raw.split('\t') |
| 50 |
| 51 if len(data) == 4: |
| 52 |
| 53 data = data[0].split('/') + data[1:] |
| 54 data = list(map(str.strip, data)) |
| 55 |
| 56 if not benchdata.get(data[0]): |
| 57 benchdata[data[0]] = [ data[1:] ] |
| 58 else: |
| 59 benchdata[data[0]].append(data[1:]) |
| 60 |
| 61 def gen_csv(self): |
| 62 """ |
| 63 generate temporary csv files |
| 64 """ |
| 65 |
| 66 for name, data in self.benchdata.items(): |
| 67 |
| 68 with tempfile.NamedTemporaryFile(delete=False) as f: |
| 69 |
| 70 for index, line in enumerate(data): |
| 71 f.write('{};{}\n'.format(index, ';'.join(line)).encode()) |
| 72 |
| 73 self.tempfiles.append(f.name) |
| 74 self.script = self.script + self.template.format(f.name, name) |
| 75 |
| 76 def run(self): |
| 77 self.gen_csv() |
| 78 script = self.script[:-3].format(self.output) |
| 79 command = subprocess.Popen(['gnuplot'], stdin=subprocess.PIPE) |
| 80 command.communicate(script.encode()) |
| 81 |
| 82 |
| 83 if __name__ == '__main__': |
| 84 |
| 85 parser = argparse.ArgumentParser(description='generate plots for benchlog') |
| 86 parser.add_argument('benchlog', type=str, help='benchlog generated by re2') |
| 87 args = parser.parse_args() |
| 88 |
| 89 try: |
| 90 subprocess.Popen(['gnuplot'], stdin=subprocess.PIPE) |
| 91 except FileNotFoundError: |
| 92 print('you can install "gnuplot" to generate plots automatically') |
| 93 exit(1) |
| 94 |
| 95 with gnuplot() as plot: |
| 96 plot.output = args.benchlog |
| 97 plot.parse_re2_benchlog(args.benchlog) |
| 98 plot.run() |
OLD | NEW |