-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathsimple_computation.py
81 lines (70 loc) · 2.77 KB
/
simple_computation.py
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
from urllib2 import urlopen
from urllib import urlencode
import json
from random import random
from time import sleep, time
import sys
from multiprocessing import Pool
import contextlib
import traceback
from timing_util import timing, json, json_request
from time import time
from sagecell import Session
class Transaction(object):
def __init__(self, **kwargs):
self.custom_timers={}
self.MAXRAND=kwargs.get('maxrand', 2**30)
self.BASE_URL=kwargs.get('base_url', 'http://localhost:8080/')
self.POLL_INTERVAL=kwargs.get('poll_interval', 0.25)
self.TIMEOUT=kwargs.get('timeout', 30)
def run(self):
"""
Ask for the sum of two random numbers and check the result
"""
computation_times=[]
response_times=[]
a=int(random()*self.MAXRAND)
b=int(random()*self.MAXRAND)
code=json.dumps('print(%d+%d)' % (a, b))
s=Session(self.BASE_URL)
request=s.prepare_execution_request(code)
sequence=0
with timing(computation_times):
with timing(response_times):
s.send_execution_request(request)
start_time = time()
done=False
while not done:
if time()-start_time>self.TIMEOUT:
raise Exception("TIMEOUT")
sleep(self.POLL_INTERVAL)
with timing(response_times):
r=s.output_poll(sequence)
if len(r)==0 or 'content' not in r:
continue
for m in r['content']:
sequence+=1
if (m['msg_type']=="stream"
and m['content']['name']=="stdout"):
ans=int(m['content']['data'])
if ans!=a+b:
print("COMPUTATION NOT CORRECT")
raise ValueError("Computation not correct: %s+%s!=%s, off by %s "%(a,b,ans, ans-a-b))
else:
done=True
break
self.custom_timers['Computation']=computation_times
self.custom_timers['Response']=response_times
__all__=['Transaction']
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Run simple additionc computation.')
parser.add_argument('--base_url', default='http://localhost:8080',
help='the base url for the sage server')
parser.add_argument('-q','--quiet', dest='quiet', action='store_true')
parser.add_argument('--timeout', dest='timeout', default=30, type=float)
args = parser.parse_args()
trans = Transaction(base_url=args.base_url, timeout=args.timeout)
trans.run()
if not args.quiet:
print(trans.custom_timers)