|
| 1 | +#!/usr/bin/env python |
| 2 | +import argparse |
| 3 | +import csv |
| 4 | +import matplotlib.pyplot as plt |
| 5 | +import numpy as np |
| 6 | +import sys |
| 7 | + |
| 8 | +parser = argparse.ArgumentParser(description='Display results of performance tests') |
| 9 | +parser.add_argument('infile', default='-', type=str, |
| 10 | + help='Input file (use \'-\' for standard input, default: standard input)') |
| 11 | +parser.add_argument('--charttype', default='avgrestime', type=str, |
| 12 | + help='Chart type to display the results (default: %(default)s, use \'help\' for a list)') |
| 13 | + |
| 14 | +args = parser.parse_args() |
| 15 | + |
| 16 | +def log(x, end='\n'): |
| 17 | + print(x, end=end, file=sys.stderr) |
| 18 | + |
| 19 | +def avgrestime_selector(row): |
| 20 | + """ Create a row for each request based on (pool, connection, reqtime) """ |
| 21 | + return [ |
| 22 | + int(row['pool']), |
| 23 | + int(row['connection']), |
| 24 | + int(row['time_data']) + 0 if int(row['time_connect'])<0 else int(row['time_connect']) |
| 25 | + ] |
| 26 | + |
| 27 | +def avgrestime_visualizer(data): |
| 28 | + """ Use (pool, connection, reqtime) to display a chart """ |
| 29 | + # pool, connection, reqtime |
| 30 | + pool_labels = ["Pool %d" % p for p in sorted(set(d[0] for d in data))] |
| 31 | + con_labels = ["Conn. %d" % c for c in sorted(set(d[1] for d in data))] |
| 32 | + concount = len(con_labels) |
| 33 | + ind = np.arange(concount) |
| 34 | + width = 0.35 |
| 35 | + fig, axs = plt.subplots(len(pool_labels), 1, squeeze=False, constrained_layout=True) |
| 36 | + fig.suptitle("Average Time per Request") |
| 37 | + for pool in range(len(pool_labels)): |
| 38 | + plotmeans = [] |
| 39 | + plotstd = [] |
| 40 | + for con in range(concount): |
| 41 | + condata = [x[2]/1000.0 for x in data if x[0]==pool and x[1]==con] |
| 42 | + plotmeans.append(np.mean(condata)) |
| 43 | + plotstd.append(np.std(condata)) |
| 44 | + axs[pool,0].bar(ind, plotmeans, width, yerr=plotstd) |
| 45 | + axs[pool,0].set_title(pool_labels[pool]) |
| 46 | + axs[pool,0].set_ylabel('Time (ms)') |
| 47 | + axs[pool,0].set_xticks(ind, minor=False) |
| 48 | + axs[pool,0].set_xticklabels(con_labels, fontdict=None, minor=False) |
| 49 | + |
| 50 | +charttypes = { |
| 51 | + "avgrestime": { |
| 52 | + "description": "Average Time per Request", |
| 53 | + "selector": avgrestime_selector, |
| 54 | + "visualizer": avgrestime_visualizer |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +if not args.charttype in charttypes: |
| 59 | + if args.charttype.lower()!='help': |
| 60 | + log("Invalid chart type. ", end="") |
| 61 | + log("The following options are available for --charttype:") |
| 62 | + for charttype in charttypes.keys(): |
| 63 | + print(" %s - %s" % (charttype, charttypes[charttype]['description'])) |
| 64 | +else: |
| 65 | + charttype = charttypes[args.charttype] |
| 66 | + |
| 67 | + data = [] |
| 68 | + with sys.stdin if args.infile=='-' else open(args.infile, 'r') as infile: |
| 69 | + csvdata = csv.DictReader(infile, delimiter=',', quotechar='"') |
| 70 | + for row in csvdata: |
| 71 | + data.append(charttype['selector'](row)) |
| 72 | + |
| 73 | + charttype['visualizer'](data) |
| 74 | + plt.show() |
0 commit comments