github.com/clysto/awgo@v0.15.0/bench.py (about) 1 #!/usr/bin/env python 2 # encoding: utf-8 3 # 4 # Copyright (c) 2018 Dean Jackson <deanishe@deanishe.net> 5 # 6 # MIT Licence. See http://opensource.org/licenses/MIT 7 # 8 # Created on 2018-02-13 9 # 10 11 """Benchmark calling Alfred via JXA. 12 13 Test the necessity of bundling calls to Alfred. 14 """ 15 16 from __future__ import print_function, absolute_import 17 18 from contextlib import contextmanager 19 import subprocess 20 import sys 21 from time import time 22 23 # How many times to repeat each benchmark 24 REPS = 5 25 # How many values to set in Alfred 26 VALUES = 5 27 28 # Which techniques to benchmark 29 SINGLE = True 30 MULTI = True 31 MULTI_ALT = True 32 33 BUNDLE_ID = 'net.deanishe.awgo' 34 35 TPL_ONE = """\ 36 Application('Alfred 3').setConfiguration('{key}', {{ 37 toValue: '{value}', 38 inWorkflow: '{bid}' 39 }}); 40 """ 41 42 TPL_MANY = """\ 43 var alfred = Application('Alfred 3'); 44 """ 45 46 TPL_LINE = """\ 47 alfred.setConfiguration('{key}', {{ 48 toValue: '{value}', 49 inWorkflow: '{bid}' 50 }}); 51 """ 52 53 54 def log(s, *args): 55 """Simple STDERR logger.""" 56 if args: 57 s = s % args 58 print(s, file=sys.stderr) 59 60 61 @contextmanager 62 def timed(title): 63 """Time a section of code.""" 64 start = time() 65 yield 66 log('%s took %0.3fs', title, time() - start) 67 68 69 def run_script(script): 70 """Execute JXA.""" 71 args = ['/usr/bin/osascript', '-l', 'JavaScript', '-e', script] 72 subprocess.check_output(args) 73 74 75 def single(): 76 """Set variables one at a time.""" 77 with timed('single (%d values)' % VALUES): 78 79 for i in range(VALUES): 80 81 key = 'BENCH_{}'.format(i) 82 value = 'VAL_SINGLE_{}'.format(i) 83 84 script = TPL_ONE.format(key=key, value=value, bid=BUNDLE_ID) 85 run_script(script) 86 87 88 def multiple(): 89 """Set variables all at once.""" 90 with timed('multiple (%d values)' % VALUES): 91 92 script = [TPL_MANY] 93 94 for i in range(VALUES): 95 96 key = 'BENCH_{}'.format(i) 97 value = 'VAL_SINGLE_{}'.format(i) 98 99 script.append(TPL_LINE.format(key=key, value=value, bid=BUNDLE_ID)) 100 101 script = '\n'.join(script) 102 103 run_script(script) 104 105 106 def multiple_alt(): 107 """Set variables all at once.""" 108 with timed('multi-alt (%d values)' % VALUES): 109 110 script = [] 111 112 for i in range(VALUES): 113 114 key = 'BENCH_{}'.format(i) 115 value = 'VAL_SINGLE_{}'.format(i) 116 117 script.append(TPL_ONE.format(key=key, value=value, bid=BUNDLE_ID)) 118 119 script = '\n'.join(script) 120 121 run_script(script) 122 123 124 def main(): 125 """Run benchmarks.""" 126 cumone = cumall = cumalt = 0 127 logs = [] 128 129 if SINGLE: 130 for i in range(REPS): 131 start = time() 132 single() 133 cumone += time() - start 134 135 logs.append('single: %0.1fs (%0.3fs/rep)' % (cumone, cumone / REPS)) 136 137 if MULTI: 138 for i in range(REPS): 139 start = time() 140 multiple() 141 cumall += time() - start 142 143 logs.append('multi: %0.1fs (%0.3fs/rep)' % (cumall, cumall / REPS)) 144 145 if MULTI_ALT: 146 for i in range(REPS): 147 start = time() 148 multiple_alt() 149 cumalt += time() - start 150 151 logs.append('multi-alt: %0.1fs (%0.3fs/rep)' % (cumalt, cumalt / REPS)) 152 153 log(', '.join(logs)) 154 155 156 if __name__ == '__main__': 157 main()