Path: blob/master/tests/multimechanize/test_scripts/simple_session.py
447 views
#! /usr/bin/env python12import client3import time4import random56class Transaction(object):7"""8A transaction that simulates loading the page9and performing a simple addition10"""1112def __init__(self):13self.custom_timers = {}1415def run(self):16t = time.time()17client.load_root()18self.custom_timers["root load"] = time.time() - t19time.sleep(5)20t = time.time()21with client.SageCellSession() as s:22self.custom_timers["initial connection"] = time.time() - t23t = time.time()24num1 = random.randint(1, 10 ** 20)25num2 = random.randint(1, 10 ** 20)26s.execute("print %d + %d" % (num1, num2))27output = ""28while True:29msg = s.iopub_recv()30if msg["header"]["msg_type"] == "status" and msg["content"]["execution_state"] == "idle":31break32elif msg["header"]["msg_type"] == "stream" and msg["content"]["name"] == "stdout":33output += msg["content"]["data"]34assert int(output.strip()) == num1 + num2, "Incorrect output: %r" % (output,)35self.custom_timers["computation"] = time.time() - t3637if __name__ == "__main__":38t = Transaction()39t.run()40print t.custom_timers414243