Skip to content

Instantly share code, notes, and snippets.

@fruch
Created March 10, 2011 05:27
Show Gist options
  • Save fruch/863616 to your computer and use it in GitHub Desktop.
Save fruch/863616 to your computer and use it in GitHub Desktop.
import rpyc
import time
import pushy
import unittest
import logging
BIG = SMALL = connection = bgsrv = None
TARGET_HOSTNAME = "localhost"
def setup_pushy():
''' connect and import the modules into globals'''
global BIG , SMALL , connection
connection = pushy.client.connect("daemon:localhost")
connection.execute("import big_module; reload(big_module)")
connection.execute("import small_module; reload(small_module)")
SYSTEMCONFIG = connection.modules.systemconfig
AVDS = connection.modules.srv_avds
MSL = connection.modules.msl_services
def teardown_pushy():
''' clean all services and stop connection '''
global BIG , SMALL , connection
del BIG
del SMALL
connection.close()
del connection
def setup_rpyc():
''' connect and import the modules into globals'''
global BIG , SMALL , connection, bgsrv
connection = rpyc.classic.connect(TARGET_HOSTNAME)
bgsrv = rpyc.BgServingThread(connection)
connection.execute("import big_module; reload(big_module)")
connection.execute("import small_module; reload(small_module)")
SYSTEMCONFIG = connection.modules.systemconfig
AVDS = connection.modules.srv_avds
MSL = connection.modules.msl_services
def teardown_rpyc():
''' clean all services and stop connection '''
global BIG , SMALL , connection, bgsrv
del BIG
del SMALL
bgsrv.stop()
connection.close()
del connection
del bgsrv
def decorator(fn, module):
def new(*args, **kargs):
ret = fn(*args, **kargs)
print "called: %s.%s returned: %s" % (module.__name__, fn.__name__, str(ret))
return ret
return new
def add_decorators_to_module(moudle):
t = time.time()
for c, v in moudle.__dict__.items():
if hasattr(v, '__call__'):
moudle.__dict__[c] = decorator(v, moudle)
t1 = time.time()
logging.debug("add_decorators_to_module [%s] : %0.6f" %
(moudle.__name__, (t1-t)) )
class Pushy_Tests(unittest.TestCase):
def setUp(self):
setup_pushy()
def tearDown(self):
teardown_pushy()
def test_import_locals_global_timing(self):
def import_to_global(module):
'''copy all public func from remote module into globals'''
t = time.time()
g = globals()
for key, value in module.__dict__.items():
if not key.startswith('_'):
g[key] = value
else: pass
t1 = time.time()
logging.debug("import_to_global took [%d] : %0.6f" %
(len(module.__dict__.items()), (t1 - t)) )
def import_to_local(module, loc):
'''copy all public func from remote module into locals'''
t = time.time()
for key , value in module.__dict__.items():
if not key.startswith('_'):
loc[key] = value
else: pass
t1 = time.time()
logging.debug("import_to_local took [%d] : %0.6f" %
(len(module.__dict__.items()), (t1 - t)) )
import_to_global(SMALL)
import_to_local(SMALL, locals())
import_to_global(BIG)
import_to_local(BIG, locals())
def test_monkey_patching_remote(self):
add_decorators_to_module(SMALL)
add_decorators_to_module(BIG)
class RPyC_Tests(unittest.TestCase):
def setUp(self):
setup_rpyc()
def tearDown(self):
teardown_rpyc()
def test_import_locals_global_timing(self):
def import_to_global(module):
'''copy all public func from remote module into globals'''
t = time.time()
g = globals()
for key, value in module.__dict__.items():
if not key.startswith('_'):
g[key] = value
else: pass
t1 = time.time()
logging.debug("import_to_global took [%d] : %0.6f" %
(len(module.__dict__.items()), (t1 - t)) )
def import_to_local(module, loc):
'''copy all public func from remote module into locals'''
t = time.time()
for key , value in module.__dict__.items():
if not key.startswith('_'):
loc[key] = value
else: pass
t1 = time.time()
logging.debug("import_to_local took [%d] : %0.6f" %
(len(module.__dict__.items()), (t1 - t)) )
import_to_global(SMALL)
import_to_local(SMALL, locals())
import_to_global(BIG)
import_to_local(BIG, locals())
def test_monkey_patching_remote(self):
add_decorators_to_module(SMALL)
add_decorators_to_module(BIG)
if __name__ == '__main__':
FORMAT = "%(levelname)s: %(asctime)s | %(message)s"
DATA_FORMAT = '%y/%m/%d %H:%M:%S'
logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATA_FORMAT)
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment