summaryrefslogtreecommitdiff
path: root/actions/perf
blob: d3ff8a6706a02750fc95c4679cc796e66dd1ccc5 (plain)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 
#!/usr/bin/env python import signal import subprocess import os import json import re from tempfile import NamedTemporaryFile from distutils.spawn import find_executable try: from charms.benchmark import Benchmark except ImportError: subprocess.check_call(['apt-get', 'install', '-y', 'python-pip']) subprocess.check_call(['pip', 'install', '-U', 'charms.benchmark']) from charms.benchmark import Benchmark def handler(signum, frame): raise IOError('Timeout') def action_set(key, val): action_cmd = ['action-set'] if isinstance(val, dict): for k, v in val.iteritems(): action_set('%s.%s' % (key, k), v) return action_cmd.append('%s=%s' % (key, val)) subprocess.check_call(action_cmd) def action_get(key): if find_executable('action-get'): return subprocess.check_output(['action-get', key]).strip() return None def main(): Benchmark.start() """ mongoperf runs until interupted so we have to use a signal handler to stop it and gather the results """ signal.signal(signal.SIGALRM, handler) runtime = int(action_get('runtime') or 180) signal.alarm(runtime) js = {} js['nThreads'] = int(action_get('nthreads')) js['fileSizeMB'] = int(action_get('fileSizeMB')) js['sleepMicros'] = int(action_get('sleepMicros')) js['mmf'] = action_get('mmf') js['r'] = action_get('r') js['w'] = action_get('w') js['recSizeKB'] = int(action_get('recSizeKB')) js['syncDelay'] = int(action_get('syncDelay')) config = NamedTemporaryFile(delete=False) config.write(json.dumps(js)) config.close() config = open(config.name, 'r') output = NamedTemporaryFile(delete=False) p = None try: p = subprocess.Popen( 'mongoperf', stdin=config, stdout=output, ) os.waitpid(p.pid, 0) except subprocess.CalledProcessError as e: rc = e.returncode print "Exit with error code %d" % rc except IOError as e: signal.alarm(0) os.kill(p.pid, signal.SIGKILL) finally: os.unlink(config.name) output.close() output = open(output.name, 'r') scores = [] regex = re.compile(r'(\d+)\sops\/sec') for line in output: m = regex.match(line) if m: scores.append(int(m.group(1))) action_set( "results.total", {'value': sum(scores), 'units': 'ops'} ) action_set( "results.iterations", {'value': len(scores), 'units': 'iterations'} ) action_set( "results.average", {'value': sum(scores) / float(len(scores)), 'units': 'ops/sec'} ) action_set( "results.max", {'value': max(scores), 'units': 'ops/sec'} ) action_set( "results.min", {'value': min(scores), 'units': 'ops/sec'} ) Benchmark.set_composite_score( sum(scores) / float(len(scores)), 'ops/sec', 'desc' ) Benchmark.finish() if __name__ == "__main__": main()