Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/tests/multimechanize/test_scripts/simple_session.py
447 views
1
#! /usr/bin/env python
2
3
import client
4
import time
5
import random
6
7
class Transaction(object):
8
"""
9
A transaction that simulates loading the page
10
and performing a simple addition
11
"""
12
13
def __init__(self):
14
self.custom_timers = {}
15
16
def run(self):
17
t = time.time()
18
client.load_root()
19
self.custom_timers["root load"] = time.time() - t
20
time.sleep(5)
21
t = time.time()
22
with client.SageCellSession() as s:
23
self.custom_timers["initial connection"] = time.time() - t
24
t = time.time()
25
num1 = random.randint(1, 10 ** 20)
26
num2 = random.randint(1, 10 ** 20)
27
s.execute("print %d + %d" % (num1, num2))
28
output = ""
29
while True:
30
msg = s.iopub_recv()
31
if msg["header"]["msg_type"] == "status" and msg["content"]["execution_state"] == "idle":
32
break
33
elif msg["header"]["msg_type"] == "stream" and msg["content"]["name"] == "stdout":
34
output += msg["content"]["data"]
35
assert int(output.strip()) == num1 + num2, "Incorrect output: %r" % (output,)
36
self.custom_timers["computation"] = time.time() - t
37
38
if __name__ == "__main__":
39
t = Transaction()
40
t.run()
41
print t.custom_timers
42
43