1#!/usr/bin/env python 2 3from __future__ import print_function 4 5 6class TimingScriptGenerator: 7 """Used to generate a bash script which will invoke the toy and time it""" 8 9 def __init__(self, scriptname, outputname): 10 self.shfile = open(scriptname, "w") 11 self.timeFile = outputname 12 self.shfile.write('echo "" > %s\n' % self.timeFile) 13 14 def writeTimingCall(self, irname, callname): 15 """Echo some comments and invoke both versions of toy""" 16 rootname = irname 17 if "." in irname: 18 rootname = irname[: irname.rfind(".")] 19 self.shfile.write( 20 'echo "%s: Calls %s" >> %s\n' % (callname, irname, self.timeFile) 21 ) 22 self.shfile.write('echo "" >> %s\n' % self.timeFile) 23 self.shfile.write('echo "With MCJIT" >> %s\n' % self.timeFile) 24 self.shfile.write( 25 '/usr/bin/time -f "Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb"' 26 ) 27 self.shfile.write(" -o %s -a " % self.timeFile) 28 self.shfile.write( 29 "./toy -suppress-prompts -use-mcjit=true -enable-lazy-compilation=true -use-object-cache -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" 30 % (irname, callname, rootname, rootname) 31 ) 32 self.shfile.write('echo "" >> %s\n' % self.timeFile) 33 self.shfile.write('echo "With MCJIT again" >> %s\n' % self.timeFile) 34 self.shfile.write( 35 '/usr/bin/time -f "Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb"' 36 ) 37 self.shfile.write(" -o %s -a " % self.timeFile) 38 self.shfile.write( 39 "./toy -suppress-prompts -use-mcjit=true -enable-lazy-compilation=true -use-object-cache -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" 40 % (irname, callname, rootname, rootname) 41 ) 42 self.shfile.write('echo "" >> %s\n' % self.timeFile) 43 self.shfile.write('echo "With JIT" >> %s\n' % self.timeFile) 44 self.shfile.write( 45 '/usr/bin/time -f "Command %C\\n\\tuser time: %U s\\n\\tsytem time: %S s\\n\\tmax set: %M kb"' 46 ) 47 self.shfile.write(" -o %s -a " % self.timeFile) 48 self.shfile.write( 49 "./toy -suppress-prompts -use-mcjit=false -input-IR=%s < %s > %s-mcjit.out 2> %s-mcjit.err\n" 50 % (irname, callname, rootname, rootname) 51 ) 52 self.shfile.write('echo "" >> %s\n' % self.timeFile) 53 self.shfile.write('echo "" >> %s\n' % self.timeFile) 54 55 56class LibScriptGenerator: 57 """Used to generate a bash script which will invoke the toy and time it""" 58 59 def __init__(self, filename): 60 self.shfile = open(filename, "w") 61 62 def writeLibGenCall(self, libname, irname): 63 self.shfile.write( 64 "./toy -suppress-prompts -use-mcjit=false -dump-modules < %s 2> %s\n" 65 % (libname, irname) 66 ) 67 68 69def splitScript(inputname, libGenScript, timingScript): 70 rootname = inputname[:-2] 71 libname = rootname + "-lib.k" 72 irname = rootname + "-lib.ir" 73 callname = rootname + "-call.k" 74 infile = open(inputname, "r") 75 libfile = open(libname, "w") 76 callfile = open(callname, "w") 77 print("Splitting %s into %s and %s" % (inputname, callname, libname)) 78 for line in infile: 79 if not line.startswith("#"): 80 if line.startswith("print"): 81 callfile.write(line) 82 else: 83 libfile.write(line) 84 libGenScript.writeLibGenCall(libname, irname) 85 timingScript.writeTimingCall(irname, callname) 86 87 88# Execution begins here 89libGenScript = LibScriptGenerator("make-libs.sh") 90timingScript = TimingScriptGenerator("time-lib.sh", "lib-timing.txt") 91 92script_list = [ 93 "test-5000-3-50-50.k", 94 "test-5000-10-100-10.k", 95 "test-5000-10-5-10.k", 96 "test-5000-10-1-0.k", 97 "test-1000-3-10-50.k", 98 "test-1000-10-100-10.k", 99 "test-1000-10-5-10.k", 100 "test-1000-10-1-0.k", 101 "test-200-3-2-50.k", 102 "test-200-10-40-10.k", 103 "test-200-10-2-10.k", 104 "test-200-10-1-0.k", 105] 106 107for script in script_list: 108 splitScript(script, libGenScript, timingScript) 109print("All done!") 110