Path: blob/master/timing/test_scripts/simple_upload_modify_download.py
447 views
1from urllib2 import urlopen2from urllib import urlencode3import json4from random import random5from time import sleep, time6import sys7import numpy8from multiprocessing import Pool9import contextlib10import traceback1112from timing_util import timing, json, json_request1314from sagecell import Session1516code="""17print('beginning...')18with open('test.txt','r+') as f:19s = f.read()20f.seek(0)21f.write(s.replace('test','finished'))22print('ending...')23"""2425FILE_CONTENTS = 'This is a test file'26FILE_RESULT_CONTENTS = FILE_CONTENTS.replace('test','finished')2728class Transaction(object):29def __init__(self, **kwargs):30self.custom_timers={}31self.BASE_URL=kwargs.get('base_url', 'http://localhost:8080/')32self.POLL_INTERVAL=kwargs.get('poll_interval', 0.1)33with open('test.txt', 'w') as f:34f.write(FILE_CONTENTS)3536def run(self):37"""38Upload a file, change it, and then download it again39"""40computation_times=[]41response_times=[]4243s=Session(self.BASE_URL)44request=s.prepare_execution_request(code,files=['test.txt'])45sequence=046with timing(computation_times):47with timing(response_times):48s.send_execution_request(request)4950done=False51while not done:52sleep(self.POLL_INTERVAL)53with timing(response_times):54r=s.output_poll(sequence)55if len(r)==0 or 'content' not in r:56continue57for m in r['content']:58sequence+=159if (m['msg_type']=="extension"60and m['content']['msg_type']=="files"):61returned_file=m['content']['content']['files'][0]62if returned_file!='test.txt':63print("RETURNED FILENAME NOT CORRECT")64raise ValueError("Returned filename not correct: %s"%returned_file)65with timing(response_times):66f=s.get_file(returned_file)67if f!=FILE_RESULT_CONTENTS:68print("RETURNED FILE CONTENTS NOT CORRECT")69raise ValueError("Returned file contents not correct: %s"%f)70# if we've made it this far, we're done71done=True72break7374self.custom_timers['Computation']=computation_times75self.custom_timers['Response']=response_times7677__all__=['Transaction']7879if __name__ == '__main__':80trans = Transaction()81trans.run()82print(trans.custom_timers)838485