def test_4_authkey(self): logging.debug('') logging.debug('test_authkey') factory = self.start_factory() # Start server in non-public-key mode. # Connections must have matching authkey, # but data is sent in the clear!? # This is standard multiprocessing behaviour. authkey = 'password' server_dir = 'Factory_authkey' if os.path.exists(server_dir): shutil.rmtree(server_dir) os.mkdir(server_dir) os.chdir(server_dir) self.server_dirs.append(server_dir) try: logging.debug('starting server (authkey %s)...', authkey) allowed_types = ['openmdao.main.test.test_distsim.Box'] server = start_server(authkey=authkey, allowed_types=allowed_types, timeout=30) address, port, key = read_server_config('server.cfg') logging.debug('server address: %s', address) logging.debug('server port: %s', port) logging.debug('server key: %s', key) finally: os.chdir('..') factory = None try: assert_raises(self, 'connect(address, port, pubkey=key)', globals(), locals(), AuthenticationError, 'digest sent was rejected') factory = connect(address, port, authkey=authkey) logging.debug('factory: %r', factory) # Create model and run it. box = factory.create(_MODULE+'.Box') model = set_as_top(Model(box)) model.run() # Check results. for width in range(1, 2): for height in range(1, 3): for depth in range(1, 4): case = model.driver.recorder.cases.pop(0) self.assertEqual(case.outputs[0][2], width*height*depth) finally: if factory is not None: factory.cleanup() logging.debug('terminating server (authkey %s) pid %s', authkey, server.pid) server.terminate(timeout=10) server = None
def start_factory(self, port=None, allowed_users=None): """ Start each factory process in a unique directory. """ global _SERVER_ID _SERVER_ID += 1 server_dir = 'Factory_%d' % _SERVER_ID if os.path.exists(server_dir): shutil.rmtree(server_dir) os.mkdir(server_dir) os.chdir(server_dir) self.server_dirs.append(server_dir) try: logging.debug('') logging.debug('tester pid: %s', os.getpid()) logging.debug('starting server...') if port is None: # Exercise both AF_INET and AF_UNIX/AF_PIPE. port = -1 if _SERVER_ID & 1 else 0 if allowed_users is None: credentials = get_credentials() allowed_users = {credentials.user: credentials.public_key} allowed_types = [ 'openmdao.main.test.test_distsim.HollowSphere', 'openmdao.main.test.test_distsim.Box', 'openmdao.main.test.test_distsim.ProtectedBox' ] server, server_cfg = start_server(port=port, allowed_users=allowed_users, allowed_types=allowed_types, log_prefix=server_dir) self.servers.append(server) cfg = read_server_config(server_cfg) self.address = cfg['address'] self.port = cfg['port'] self.tunnel = cfg['tunnel'] self.key = cfg['key'] logging.debug('server pid: %s', server.pid) logging.debug('server address: %s', self.address) logging.debug('server port: %s', self.port) logging.debug('server key: %s', self.key) finally: os.chdir('..') factory = connect(self.address, self.port, self.tunnel, pubkey=self.key) self.factories.append(factory) logging.debug('factory: %r', factory) return factory
def start_factory(self, port=None, allowed_users=None): """ Start each factory process in a unique directory. """ global _SERVER_ID _SERVER_ID += 1 server_dir = 'Factory_%d' % _SERVER_ID if os.path.exists(server_dir): shutil.rmtree(server_dir) os.mkdir(server_dir) os.chdir(server_dir) self.server_dirs.append(server_dir) try: logging.debug('') logging.debug('tester pid: %s', os.getpid()) logging.debug('starting server...') if port is None: # Exercise both AF_INET and AF_UNIX/AF_PIPE. port = -1 if _SERVER_ID & 1 else 0 if allowed_users is None: credentials = get_credentials() allowed_users = {credentials.user: credentials.public_key} allowed_types = ['openmdao.main.test.test_distsim.HollowSphere', 'openmdao.main.test.test_distsim.Box', 'openmdao.main.test.test_distsim.ProtectedBox'] server, server_cfg = start_server(port=port, allowed_users=allowed_users, allowed_types=allowed_types, log_prefix=server_dir) self.servers.append(server) cfg = read_server_config(server_cfg) self.address = cfg['address'] self.port = cfg['port'] self.tunnel = cfg['tunnel'] self.key = cfg['key'] logging.debug('server pid: %s', server.pid) logging.debug('server address: %s', self.address) logging.debug('server port: %s', self.port) logging.debug('server key: %s', self.key) finally: os.chdir('..') factory = connect(self.address, self.port, self.tunnel, pubkey=self.key) self.factories.append(factory) logging.debug('factory: %r', factory) return factory
def start_factory(self, port=None, allowed_users=None): """ Start each factory process in a unique directory. """ global _SERVER_ID _SERVER_ID += 1 server_dir = "Factory_%d" % _SERVER_ID if os.path.exists(server_dir): shutil.rmtree(server_dir) os.mkdir(server_dir) os.chdir(server_dir) self.server_dirs.append(server_dir) try: logging.debug("") logging.debug("tester pid: %s", os.getpid()) logging.debug("starting server...") if port is None: # Exercise both AF_INET and AF_UNIX/AF_PIPE. port = -1 if _SERVER_ID & 1 else 0 if allowed_users is None: credentials = get_credentials() allowed_users = {credentials.user: credentials.public_key} allowed_types = [ "openmdao.main.test.test_distsim.HollowSphere", "openmdao.main.test.test_distsim.Box", "openmdao.main.test.test_distsim.ProtectedBox", ] server, server_cfg = start_server(port=port, allowed_users=allowed_users, allowed_types=allowed_types) self.servers.append(server) cfg = read_server_config(server_cfg) self.address = cfg["address"] self.port = cfg["port"] self.tunnel = cfg["tunnel"] self.key = cfg["key"] logging.debug("server pid: %s", server.pid) logging.debug("server address: %s", self.address) logging.debug("server port: %s", self.port) logging.debug("server key: %s", self.key) finally: os.chdir("..") factory = connect(self.address, self.port, self.tunnel, pubkey=self.key) self.factories.append(factory) logging.debug("factory: %r", factory) return factory
def test_4_authkey(self): logging.debug('') logging.debug('test_authkey') factory = self.start_factory() # Start server in non-public-key mode. # Connections must have matching authkey, # but data is sent in the clear!? # This is standard multiprocessing behaviour. authkey = 'password' server_dir = 'Factory_authkey' if os.path.exists(server_dir): shutil.rmtree(server_dir) os.mkdir(server_dir) os.chdir(server_dir) self.server_dirs.append(server_dir) try: logging.debug('starting server (authkey %s)...', authkey) allowed_types = ['openmdao.main.test.test_distsim.Box'] server, server_cfg = start_server(authkey=authkey, allowed_types=allowed_types, timeout=30) cfg = read_server_config(server_cfg) address = cfg['address'] port = cfg['port'] key = cfg['key'] logging.debug('server address: %s', address) logging.debug('server port: %s', port) logging.debug('server tunnel: %s', cfg['tunnel']) logging.debug('server key: %s', key) finally: os.chdir('..') factory = None try: assert_raises(self, 'connect(address, port, pubkey=key)', globals(), locals(), AuthenticationError, 'digest sent was rejected') factory = connect(address, port, authkey=authkey) logging.debug('factory: %r', factory) # Create model and run it. box = factory.create(_MODULE + '.Box') model = set_as_top(Model(box)) model.run() # Check results. for width in range(1, 2): for height in range(1, 3): for depth in range(1, 4): case = model.driver.recorders[0].cases.pop(0) self.assertEqual(case.outputs[0][2], width * height * depth) finally: if factory is not None: factory.cleanup() logging.debug('terminating server (authkey %s) pid %s', authkey, server.pid) server.terminate(timeout=10) server = None
def test_4_authkey(self): logging.debug("") logging.debug("test_authkey") factory = self.start_factory() # Start server in non-public-key mode. # Connections must have matching authkey, # but data is sent in the clear!? # This is standard multiprocessing behaviour. authkey = "password" server_dir = "Factory_authkey" if os.path.exists(server_dir): shutil.rmtree(server_dir) os.mkdir(server_dir) os.chdir(server_dir) self.server_dirs.append(server_dir) try: logging.debug("starting server (authkey %s)...", authkey) allowed_types = ["openmdao.main.test.test_distsim.Box"] server, server_cfg = start_server(authkey=authkey, allowed_types=allowed_types, timeout=30) cfg = read_server_config(server_cfg) address = cfg["address"] port = cfg["port"] key = cfg["key"] logging.debug("server address: %s", address) logging.debug("server port: %s", port) logging.debug("server tunnel: %s", cfg["tunnel"]) logging.debug("server key: %s", key) finally: os.chdir("..") factory = None try: assert_raises( self, "connect(address, port, pubkey=key)", globals(), locals(), AuthenticationError, "digest sent was rejected", ) factory = connect(address, port, authkey=authkey) logging.debug("factory: %r", factory) # Create model and run it. box = factory.create(_MODULE + ".Box") model = set_as_top(Model(box)) model.run() # Check results. for width in range(1, 2): for height in range(1, 3): for depth in range(1, 4): case = model.driver.recorders[0].cases.pop(0) self.assertEqual(case.outputs[0][2], width * height * depth) finally: if factory is not None: factory.cleanup() logging.debug("terminating server (authkey %s) pid %s", authkey, server.pid) server.terminate(timeout=10) server = None
def main(): """ Run latency & thruput tests on various server configurations. """ init_messages() latency_results = {} thruput_results = {} # For each configuration... count = 0 for authkey in ('PublicKey', 'UnEncrypted'): for ip_port in (0, -1): for hops in (1, 2): # Start factory in unique directory. count += 1 name = 'Echo_%d' % count if os.path.exists(name): shutil.rmtree(name, onerror=onerror) os.mkdir(name) os.chdir(name) try: server_proc, server_cfg = \ start_server(authkey=authkey, port=ip_port) cfg = read_server_config(server_cfg) finally: os.chdir('..') # Connect to factory. address = cfg['address'] port = cfg['port'] key = cfg['key'] print print '%s, %s %d, hops: %d' % (authkey, address, port, hops) factory = connect(address, port, authkey=authkey, pubkey=key) if hops == 1: server = factory else: # Create a server. server = factory.create('') # Run test. results = run_test(name, server) # Shutdown. if server is not factory: factory.release(server) factory.cleanup() server_proc.terminate(timeout=10) # Add results. for size, latency, thruput in results: if size not in latency_results: latency_results[size] = [] latency_results[size].append(latency) if size not in thruput_results: thruput_results[size] = [] thruput_results[size].append(thruput) # Write out results in X, Y1, Y2, ... format. header = 'Bytes,En-S-1,En-S-2,En-P-1,En-P-2,Un-S-1,Un-S-2,Un-P-1,Un-P-2\n' with open('latency.csv', 'w') as out: out.write(header) for size in sorted(latency_results.keys()): out.write('%d' % size) for value in latency_results[size]: out.write(', %g' % value) out.write('\n') with open('thruput.csv', 'w') as out: out.write(header) for size in sorted(thruput_results.keys()): out.write('%d' % size) for value in thruput_results[size]: out.write(', %g' % value) out.write('\n') for path in glob.glob('Echo_*'): shutil.rmtree(path, onerror=onerror)
def test_factory(self): logging.debug('') logging.debug('test_factory') testdir = 'test_factory' if os.path.exists(testdir): shutil.rmtree(testdir, onerror=onerror) os.mkdir(testdir) os.chdir(testdir) factory = None try: # Create a factory. factory = ObjServerFactory() # Echo some arguments. args = factory.echo('Hello', 'world!') self.assertEqual(args[0], 'Hello') self.assertEqual(args[1], 'world!') # List types. types = factory.get_available_types() names = [name for name, version in types] self.assertTrue('openmdao.test.execcomp.ExecComp' in names) # Create a component. exec_comp = factory.create('openmdao.test.execcomp.ExecComp') exec_comp.run() directory = 'Server_1' + os.sep if sys.platform == 'win32': directory = directory.lower() self.assertTrue(exec_comp.get_abs_directory().endswith(directory)) # Create another in specified directory. exec_comp = factory.create('openmdao.test.execcomp.ExecComp', res_desc={'working_directory': 'floyd'}) exec_comp.run() directory = 'floyd' + os.sep if sys.platform == 'win32': directory = directory.lower() self.assertTrue(exec_comp.get_abs_directory().endswith(directory)) # Start server, connect, stop. server, cfg = start_server() proxy = connect_to_server(cfg) shutil.copy(cfg, 'saved_cfg') stop_server(server, cfg) _PROXIES.clear() assert_raises(self, "connect_to_server('saved_cfg')", globals(), locals(), RuntimeError, "Can't connect to server at ") # Force failed factory server startup via invalid port. address = socket.gethostbyname(socket.gethostname()) code = "start_server(address=address, port='xyzzy'," \ " allow_shell=True, tunnel=True, resources='')" assert_raises(self, code, globals(), locals(), RuntimeError, 'Server startup failed') # Try to release a server that doesn't exist (release takes an # OpenMDAO_Proxy as argument, string here is easier). assert_raises(self, "factory.release('xyzzy')", globals(), locals(), ValueError, "can't identify server at 'not-a-proxy'") # get_ram() is used by RAM.add_remotes(). ram = factory.get_ram() self.assertTrue(ram is RAM._get_instance()) finally: if factory is not None: factory.cleanup() SimulationRoot.chroot('..') if sys.platform == 'win32': time.sleep(2) # Wait for process shutdown. keep_dirs = int(os.environ.get('OPENMDAO_KEEPDIRS', '0')) if not keep_dirs: shutil.rmtree(testdir, onerror=onerror)
def test_remote(self): logging.debug('') logging.debug('test_remote') # Start remote server. server_dir = 'Factory' if os.path.exists(server_dir): shutil.rmtree(server_dir, onerror=onerror) os.mkdir(server_dir) os.chdir(server_dir) try: server, server_cfg = start_server() cfg = read_server_config(server_cfg) factory = None try: factory = connect(cfg['address'], cfg['port'], pubkey=cfg['key']) prefix = RAM._make_prefix(factory.host) remote = '%s_LocalHost' % prefix # Show no remotes currently in RAM. allocator_names = \ [allocator.name for allocator in RAM.list_allocators()] logging.debug('%s', allocator_names) self.assertFalse(remote in allocator_names) # Add remote server's allocator. RAM.add_remotes(factory) allocator_names = \ [allocator.name for allocator in RAM.list_allocators()] logging.debug('%s', allocator_names) self.assertTrue(remote in allocator_names) self.assertFalse(RAM.get_allocator(remote) is RAM.list_allocators()[0]) self.assertTrue(RAM.get_allocator(remote) is RAM.list_allocators()[1]) # Max servers. max_servers = RAM.max_servers(dict(allocator=remote)) self.assertTrue(max_servers >= 0) # Avoid host load issues. remote_alloc = RAM.get_allocator(remote) max_servers, info = \ remote_alloc.max_servers(dict(localhost=True)) self.assertEqual(max_servers, 0) self.assertEqual(info, dict(localhost='requested local host')) max_servers, info = \ remote_alloc.max_servers(dict(allocator='LocalHost')) self.assertEqual(max_servers, 0) self.assertEqual(info, dict(allocator='wrong allocator')) estimate, info = \ remote_alloc.time_estimate(dict(allocator='LocalHost')) self.assertEqual(estimate, -2) self.assertEqual(info, dict(allocator='wrong allocator')) # Allocate, release. remote_server, info = RAM.allocate(dict(allocator=remote)) RAM.release(remote_server) # Remove remote allocators. allocator_names = \ [allocator.name for allocator in RAM.list_allocators()] for name in allocator_names: if name.startswith(prefix): RAM.remove_allocator(name) allocator_names = \ [allocator.name for allocator in RAM.list_allocators()] logging.debug('%s', allocator_names) self.assertFalse(remote in allocator_names) finally: if factory is not None: factory.cleanup() server.terminate(timeout=10) finally: os.chdir('..') shutil.rmtree(server_dir, onerror=onerror) # Access local RAM in manner it would be accessed in the server. self.assertEqual(RAM._get_instance().get_total_allocators(), 1) self.assertTrue(RAM._get_instance().get_allocator_proxy(0) is RAM.list_allocators()[0])
def test_remote(self): logging.debug('') logging.debug('test_remote') # Start remote server. server_dir = 'Factory' if os.path.exists(server_dir): shutil.rmtree(server_dir, onerror=onerror) os.mkdir(server_dir) os.chdir(server_dir) try: server, server_cfg = start_server() cfg = read_server_config(server_cfg) factory = None try: factory = connect(cfg['address'], cfg['port'], pubkey=cfg['key']) prefix = RAM._make_prefix(factory.host) remote = '%s_LocalHost' % prefix # Show no remotes currently in RAM. allocator_names = \ [allocator.name for allocator in RAM.list_allocators()] logging.debug('%s', allocator_names) self.assertFalse(remote in allocator_names) # Add remote server's allocator. RAM.add_remotes(factory) allocator_names = \ [allocator.name for allocator in RAM.list_allocators()] logging.debug('%s', allocator_names) self.assertTrue(remote in allocator_names) self.assertFalse( RAM.get_allocator(remote) is RAM.list_allocators()[0]) self.assertTrue( RAM.get_allocator(remote) is RAM.list_allocators()[1]) # Max servers. max_servers = RAM.max_servers(dict(allocator=remote)) self.assertTrue(max_servers >= 0) # Avoid host load issues. remote_alloc = RAM.get_allocator(remote) max_servers, info = \ remote_alloc.max_servers(dict(localhost=True)) self.assertEqual(max_servers, 0) self.assertEqual(info, dict(localhost='requested local host')) max_servers, info = \ remote_alloc.max_servers(dict(allocator='LocalHost')) self.assertEqual(max_servers, 0) self.assertEqual(info, dict(allocator='wrong allocator')) estimate, info = \ remote_alloc.time_estimate(dict(allocator='LocalHost')) self.assertEqual(estimate, -2) self.assertEqual(info, dict(allocator='wrong allocator')) # Allocate, release. remote_server, info = RAM.allocate(dict(allocator=remote)) RAM.release(remote_server) # Remove remote allocators. allocator_names = \ [allocator.name for allocator in RAM.list_allocators()] for name in allocator_names: if name.startswith(prefix): RAM.remove_allocator(name) allocator_names = \ [allocator.name for allocator in RAM.list_allocators()] logging.debug('%s', allocator_names) self.assertFalse(remote in allocator_names) finally: if factory is not None: factory.cleanup() server.terminate(timeout=10) finally: os.chdir('..') shutil.rmtree(server_dir, onerror=onerror) # Access local RAM in manner it would be accessed in the server. self.assertEqual(RAM._get_instance().get_total_allocators(), 1) self.assertTrue(RAM._get_instance().get_allocator_proxy(0) is RAM.list_allocators()[0])
def main(): """ Run latency & thruput tests on various server configurations. """ init_messages() latency_results = {} thruput_results = {} # For each configuration... count = 0 for authkey in ('PublicKey', 'UnEncrypted'): for ip_port in (0, -1): for hops in (1, 2): # Start factory in unique directory. count += 1 name = 'Echo_%d' % count if os.path.exists(name): shutil.rmtree(name) os.mkdir(name) os.chdir(name) try: server_proc, server_cfg = \ start_server(authkey=authkey, port=ip_port) cfg = read_server_config(server_cfg) finally: os.chdir('..') # Connect to factory. address = cfg['address'] port = cfg['port'] key = cfg['key'] print print '%s, %s %d, hops: %d' % (authkey, address, port, hops) factory = connect(address, port, authkey=authkey, pubkey=key) if hops == 1: server = factory else: # Create a server. server = factory.create('') # Run test. results = run_test(name, server) # Shutdown. if server is not factory: factory.release(server) factory.cleanup() server_proc.terminate(timeout=10) # Add results. for size, latency, thruput in results: if size not in latency_results: latency_results[size] = [] latency_results[size].append(latency) if size not in thruput_results: thruput_results[size] = [] thruput_results[size].append(thruput) # Write out results in X, Y1, Y2, ... format. header = 'Bytes,En-S-1,En-S-2,En-P-1,En-P-2,Un-S-1,Un-S-2,Un-P-1,Un-P-2\n' with open('latency.csv', 'w') as out: out.write(header) for size in sorted(latency_results.keys()): out.write('%d' % size) for value in latency_results[size]: out.write(', %g' % value) out.write('\n') with open('thruput.csv', 'w') as out: out.write(header) for size in sorted(thruput_results.keys()): out.write('%d' % size) for value in thruput_results[size]: out.write(', %g' % value) out.write('\n') for path in glob.glob('Echo_*'): shutil.rmtree(path)
def test_factory(self): logging.debug('') logging.debug('test_factory') testdir = 'test_factory' if os.path.exists(testdir): shutil.rmtree(testdir, onerror=onerror) os.mkdir(testdir) os.chdir(testdir) factory = None try: # Create a factory. factory = ObjServerFactory() # Echo some arguments. args = factory.echo('Hello', 'world!') self.assertEqual(args[0], 'Hello') self.assertEqual(args[1], 'world!') # List types. types = factory.get_available_types() names = [name for name, version in types] self.assertTrue('openmdao.test.execcomp.ExecComp' in names) # Create a component. exec_comp = factory.create('openmdao.test.execcomp.ExecComp') exec_comp.run() # Start server, connect, stop. server, cfg = start_server() proxy = connect_to_server(cfg) shutil.copy(cfg, 'saved_cfg') stop_server(server, cfg) _PROXIES.clear() assert_raises(self, "connect_to_server('saved_cfg')", globals(), locals(), RuntimeError, "Can't connect to server at ") # Force failed factory server startup via invalid port. address = socket.gethostbyname(socket.gethostname()) code = "start_server(address=address, port='xyzzy'," \ " allow_shell=True, tunnel=True, resources='')" assert_raises(self, code, globals(), locals(), RuntimeError, 'Server startup failed') # Try to release a server that doesn't exist (release takes an # OpenMDAO_Proxy as argument, string here is easier). assert_raises(self, "factory.release('xyzzy')", globals(), locals(), ValueError, "can't identify server at 'not-a-proxy'") # get_ram() is used by RAM.add_remotes(). ram = factory.get_ram() self.assertTrue(ram is RAM._get_instance()) finally: if factory is not None: factory.cleanup() SimulationRoot.chroot('..') if sys.platform == 'win32': time.sleep(2) # Wait for process shutdown. keep_dirs = int(os.environ.get('OPENMDAO_KEEPDIRS', '0')) if not keep_dirs: shutil.rmtree(testdir, onerror=onerror)