from twisted.internet import reactor, task from threading import Thread from app import BasicApp, BasicAppCommand from ztimer import ZTimer import time, copy, random, os def twists(delay,commands): app = BasicApp() task.LoopingCall(app.checkuserinput).start(0.0001) delaycount = 0 for command in commands: delaycount += delay reactor.callLater(delaycount,app.adduserinput,command) reactor.run() return delay,commands def threads(delay,commands): def simulate(app,delay,commands): for command in commands: time.sleep(delay) app.adduserinput_threaded(command) def process(app): while not app.exiting: app.checkuserinput_threaded() time.sleep(0.0001) app = BasicApp() processthread = Thread(target=process,args=(app,)) processthread.start() simulatethread = Thread(target=simulate,args=(app,delay,commands)) simulatethread.start() simulatethread.join() processthread.join() return delay,commands def printtimes(delay,commands): print '-'*72 print 'delay:%f'%(delay) print '-'*72 totalA = totalB = 0 for command in commands: totalA += command.timerA.millis totalB += command.timerB.millis print '%6s %d : %8.2f : %8.2f' % ( command.action, command.index, command.timerA.millis, command.timerB.millis ) print '-'*72 print ' %8.2f : %8.2f' % ( totalA, totalB ) def savecsv(runs,filename): f = file(filename,'w') f.write('"user delay","",') for command in runs[0][1]: f.write('%s %d,'%(command.action,command.index)) f.write(' ,') for command in runs[0][1]: f.write('%s %d,'%(command.action,command.index)) f.write('\n') for userspeed,userspace in runs: f.write('%f, ,'%(userspeed)) for command in userspace: f.write('%8.2f,'%(command.timerA.millis)) f.write(' ,') for command in userspace: f.write('%8.2f,'%(command.timerB.millis)) f.write('\n') f.close() if __name__=='__main__': if not os.path.exists('output'): os.mkdir('output') script = [ BasicAppCommand('load', 0), BasicAppCommand('heavy',0), BasicAppCommand('load', 1), BasicAppCommand('medium',1), BasicAppCommand('save', 1), BasicAppCommand('resize',1), BasicAppCommand('save', 1), BasicAppCommand('resize',0), BasicAppCommand('save', 0), ] threadscript = copy.deepcopy(script) twistscript = copy.deepcopy(script) threadscript.append( BasicAppCommand('exit_threaded', 0) ) twistscript.append( BasicAppCommand('exit_twisted', 0) ) delay = 0.1 # warmup run print '( With Threads )' printtimes( *threads(delay,threadscript) ) printtimes( *threads(0.2,threadscript) ) print '( W/O Threads )' printtimes( *twists(delay,twistscript) ) printtimes( *twists(0.2,twistscript) ) # big test and csv save follows threadruns = [] twistruns = [] for delay in [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0,1.2,1.4,1.6,1.8,2.0,2.33,2.66,3,3.5,4.0,5.0]: print '[delay=%f]' % (delay) if random.randint(0,1): threadruns.append( copy.deepcopy(threads(delay,threadscript)) ) twistruns.append( copy.deepcopy(twists(delay,twistscript)) ) else: twistruns.append( copy.deepcopy(twists(delay,twistscript)) ) threadruns.append( copy.deepcopy(threads(delay,threadscript)) ) #for delay,commands in twistruns: # printtimes(delay,commands) savecsv(threadruns,'thread-%f.csv'%(time.time())) savecsv(twistruns,'twist-%f.csv'%(time.time()))