def test_syscall_client_init(): """Tests SysCallClient.__init__""" from apyfal.client.syscall import SysCallClient from apyfal import Accelerator import apyfal.configuration as cfg import apyfal.exceptions as exc # Test: accelerator_executable_available, checks return type assert type(cfg.accelerator_executable_available()) is bool # Mocks some functions accelerator_available = True class DummySysCallClient(SysCallClient): """Dummy SysCallClient""" @staticmethod def _stop(*_, **__): """Do Nothing to skip object deletion""" def dummy_accelerator_executable_available(): """Return fake result""" return accelerator_available cfg_accelerator_executable_available = cfg.accelerator_executable_available cfg.accelerator_executable_available = ( dummy_accelerator_executable_available) # Tests try: # Accelerator not available DummySysCallClient() # Default for Accelerator if no host specified config = cfg.Configuration() try: del config._sections['host'] except KeyError: pass client = Accelerator(config=config).client client._stop = DummySysCallClient._stop # Disable __del__ assert isinstance(client, SysCallClient) # Accelerator not available accelerator_available = False with pytest.raises(exc.HostConfigurationException): SysCallClient() # Restores functions finally: cfg.accelerator_executable_available = ( cfg_accelerator_executable_available)
def __init__(self, accelerator=None, config=None, accelize_client_id=None, accelize_secret_id=None, host_type=None, stop_mode='term', workers_count=4, **host_kwargs): # Uses a common configuration file config = create_configuration(config) # Needs to lazy import to avoid importing issues from apyfal import Accelerator # Initializes Accelerators workers self._accelerator = accelerator self._workers_count = workers_count self._workers = [ Accelerator(accelerator=accelerator, config=config, accelize_client_id=accelize_client_id, accelize_secret_id=accelize_secret_id, host_type=host_type, stop_mode=stop_mode, **host_kwargs) for _ in range(workers_count) ]
def _get_accelerator(name, action='load', parameters=None, accelerator=None): """ Instantiate apyfal.Accelerator. Args: name (str): --name argument value action (str): 'load', 'create' or 'update". parameters (dict): apyfal.Accelerator parameters. accelerator (apyfal.Accelerator): Accelerator. Returns: apyfal.Accelerator: Accelerator instance. """ # Loads cached accelerator if action in ('load', 'update'): parameters = _cached_accelerator(name, 'load') # Instantiates accelerator if accelerator is None: from apyfal import Accelerator, get_logger, exceptions # Shows logger get_logger(True) # Tries to instantiates accelerator try: accelerator = Accelerator(**parameters) # Raises special exception if accelerator not created previously except exceptions.AcceleratorException: if action != 'create': raise _CommandLineException( ('No accelerator found for "--name %s"' 'Please run "apyfal create" command before' ' use other commands.') % name) raise # Caches accelerator if action in ('create', 'update'): for attribute, key in (('host_type', 'host_type'), ('url', 'host_ip'), ('instance_id', 'instance_id')): try: parameters[key] = getattr(accelerator.host, attribute) except AttributeError: continue _cached_accelerator(name, 'dump', parameters) # Returns accelerator return accelerator
def _get_accelerator_object(self, force_real_one=False): """ Lazy instantiates accelerator. Args: force_real_one (bool): Forces to instantiate real accelerator if not already instantiated. Returns: apyfal.Accelerator """ if self._accelerator_object is None and force_real_one: # Can't import it at top level from apyfal import Accelerator # Instantiates accelerator self._accelerator_object = Accelerator(**self._accelerator_kwargs) return self._accelerator_object
def test_accelerator(): """Tests Accelerator""" from apyfal import Accelerator from apyfal.exceptions import HostException, ClientException import apyfal # Mocks variables raises_on_get_url = False raises_on_client_stop = False raises_on_host_stop = False dummy_url = 'http://accelize.com' dummy_start_result = 'dummy_start_result' dummy_stop_result = 'dummy_stop_result' dummy_process_result = 'dummy_process_result' dummy_stop_mode = 'dummy_stop_mode' dummy_accelerator = 'dummy_accelerator' dummy_host_type = 'dummy_host_type' dummy_src = 'dummy_src' dummy_accelerator_parameters = {'dummy_accelerator_parameters': None} dummy_src = 'dummy_src' dummy_dst = 'dummy_dst' process_duration = 0.0 # Mocks client accelerator_client_class = apyfal.client.AcceleratorClient class DummyClient(accelerator_client_class): """Dummy apyfal.client.AcceleratorClient""" url = None running = True def __new__(cls, *args, **kwargs): return object.__new__(cls) def __init__(self, accelerator, host_ip=None, **kwargs): """Checks arguments""" self.url = host_ip accelerator_client_class.__init__(self, accelerator, **kwargs) assert accelerator == dummy_accelerator def __del__(self): """Don nothing""" def _start(self, *_): """Do Nothing""" def _stop(self, *_): """Do Nothing""" def _process(self, *_): """Do Nothing""" def start(self, src=None, host_env=None, info_dict=None, reset=False, reload=False, **parameters): """Checks arguments and returns fake result""" assert src == dummy_src assert parameters == {'parameters': dummy_accelerator_parameters} return dummy_start_result def stop(self, **_): """Returns fake result""" DummyClient.running = False if raises_on_client_stop: # Raises exception raise ClientException return dummy_stop_result def process(self, src=None, dst=None, info_dict=None, **parameters): """Checks arguments and returns fake result""" assert parameters == {'parameters': dummy_accelerator_parameters} assert src == dummy_src assert dst == dummy_dst sleep(process_duration) return dummy_process_result apyfal.client.AcceleratorClient = DummyClient # Mocks Host class DummyHost: """Dummy apyfal.host.Host""" _url = dummy_url running = True def __init__(self, **kwargs): """Checks arguments""" assert dummy_host_type in kwargs.values() def __del__(self): """Don nothing""" @property def url(self): """Raises or returns result normally""" if raises_on_get_url: # Raises exception raise HostException return self._url @staticmethod def start(accelerator, stop_mode): """Checks arguments""" assert accelerator == dummy_accelerator assert stop_mode == dummy_stop_mode if raises_on_host_stop: # Raises exception raise HostException @staticmethod def stop(stop_mode): """Checks arguments""" if DummyHost.running: assert stop_mode == dummy_stop_mode DummyHost.running = False if raises_on_host_stop: raise HostException def get_configuration_env(*_, **__): """Do nothing""" host_class = apyfal.host.Host apyfal.host.Host = DummyHost # Tests try: # Creating New host accel = Accelerator(dummy_accelerator, host_type=dummy_host_type) assert isinstance(accel.host, DummyHost) assert isinstance(accel.client, DummyClient) assert DummyClient.running assert DummyHost.running # Start assert accel.start( src=dummy_src, stop_mode=dummy_stop_mode, parameters=dummy_accelerator_parameters) == dummy_start_result assert accel.client.url == dummy_url # Process assert accel.process( src=dummy_src, dst=dummy_dst, parameters=dummy_accelerator_parameters) == dummy_process_result # Async Process process_duration = 0.05 future = accel.process_submit(src=dummy_src, dst=dummy_dst, parameters=dummy_accelerator_parameters) assert accel.process_running_count == 1 assert future.result() == dummy_process_result sleep(0.05) # Avoid some Python 2 timing issues assert accel.process_running_count == 0 # Stop assert accel.stop(stop_mode=dummy_stop_mode) == dummy_stop_result assert not DummyClient.running assert not DummyHost.running # Repr assert repr(accel) == str(accel) # Using existing IP accel = Accelerator(dummy_accelerator, host_type=dummy_host_type, host_ip=dummy_url) assert accel.client.url == dummy_url # Using existing IP that not exists raises_on_get_url = True accel = Accelerator(dummy_accelerator, host_type=dummy_host_type, host_ip=dummy_url) assert accel.client.url is None raises_on_get_url = False # Auto-stops with context manager dummy_stop_mode = None DummyClient.running = True DummyHost.running = True with Accelerator(dummy_accelerator, host_type=dummy_host_type) as accel: assert isinstance(accel, Accelerator) assert not DummyClient.running assert not DummyHost.running # Auto-stops on garbage collection DummyClient.running = True DummyHost.running = True Accelerator(dummy_accelerator, host_type=dummy_host_type) gc.collect() assert not DummyClient.running assert not DummyHost.running # Clean stop even if error in client or host accel = Accelerator(dummy_accelerator, host_type=dummy_host_type) raises_on_client_stop = True accel.stop() raises_on_client_stop = False accel = Accelerator(dummy_accelerator, host_type=dummy_host_type) raises_on_host_stop = True accel.stop() raises_on_host_stop = False accel = Accelerator(dummy_accelerator, host_type='localhost') assert accel.host is None assert accel.start( src=dummy_src, stop_mode=dummy_stop_mode, parameters=dummy_accelerator_parameters) == dummy_start_result assert accel.client.url is None # Restore classes finally: apyfal.client.AcceleratorClient = accelerator_client_class apyfal.host.Host = host_class
#! /usr/bin/env python # coding=utf-8 """This shows the use of ${accelerator_name} accelerator. ${example_description} Please, set up your configuration file before running following example. Read Apyfal documentation for more information: https://apyfal.readthedocs.io""" if __name__ == "__main__": from apyfal import Accelerator, get_logger # Enable extra information for this demo by logging (Disabled by default) get_logger(True) # Run example print("1- Creating Accelerator...") with Accelerator(accelerator='${accelerator_id}') as myaccel: print("2- Creating and Initializing Instance...") ${example_script_start} print("3- Processing") ${example_script_process} print(" Processing completed with success") print("4- Stopping Accelerator...") print(" Accelerator stopped.")
# coding=utf-8 """This shows the use of Hyper FiRe (Find/Replace) accelerator. For this example, we will process the complete shakespeare work (`samples/shakespeare.txt`) with a corpus of 2500 words (`samples/corpus.csv`). Please, set up your configuration file before running following example. Read Apyfal documentation for more information: https://apyfal.readthedocs.io""" if __name__ == "__main__": from apyfal import Accelerator, get_logger # Enable extra information for this demo by logging (Disabled by default) get_logger(True) # Run example print("1- Creating Accelerator...") with Accelerator(accelerator='axonerve_hyperfire') as myaccel: print("2- Creating and Initializing Instance...") myaccel.start(datafile="samples/corpus.csv") print("3- Processing") myaccel.process(file_in="samples/shakespeare.txt", file_out="results/shakespeare_out.txt") print(" Processing completed with success") print("4- Stopping Accelerator...") print(" Accelerator stopped.")
# coding=utf-8 """This shows the use of JPEG Encoder Accelerator accelerator. Convert an sample BMP image to JPEG (*[Image source](https://pixabay.com/en/switzerland-zermatt-mountains-snow-862870)*). Please, set up your configuration file before running following example. Read Apyfal documentation for more information: https://apyfal.readthedocs.io""" if __name__ == "__main__": from apyfal import Accelerator, get_logger # Enable extra information for this demo by logging (Disabled by default) get_logger(True) # Run example print("1- Creating Accelerator...") with Accelerator(accelerator='alse_jpegenc') as myaccel: print("2- Creating and Initializing Instance...") myaccel.start() print("3- Processing") myaccel.process(file_in="samples/image.bmp", file_out="result/image.jpg") print(" Processing completed with success") print("4- Stopping Accelerator...") print(" Accelerator stopped.")
#! /usr/bin/env python # coding=utf-8 """This shows the use of TRNG Accelerator accelerator. This example show the generation of 1MB of random bytes. Please, set up your configuration file before running following example. Read Apyfal documentation for more information: https://apyfal.readthedocs.io""" if __name__ == "__main__": from apyfal import Accelerator, get_logger # Enable extra information for this demo by logging (Disabled by default) get_logger(True) # Run example print("1- Creating Accelerator...") with Accelerator(accelerator='secureic_trng') as myaccel: print("2- Creating and Initializing Instance...") myaccel.start() print("3- Processing") myaccel.process(file_out="results/output.bin", nbBytes=1048576) print(" Processing completed with success") print("4- Stopping Accelerator...") print(" Accelerator stopped.")
This example show how make a TAR.GZ file containing 3 files. Please, set up your configuration file before running following example. Read Apyfal documentation for more information: https://apyfal.readthedocs.io""" if __name__ == "__main__": from apyfal import Accelerator, get_logger # Enable extra information for this demo by logging (Disabled by default) get_logger(True) # Run example print("1- Creating Accelerator...") with Accelerator(accelerator='aclz_tgz') as myaccel: print("2- Creating and Initializing Instance...") myaccel.start(mode=3) print("3- Processing") myaccel.process(file_in="samples/sample_1_1MB.txt", startOfTx=1, endOfTx=0) myaccel.process(file_in="samples/sample_1_1MB.txt", startOfTx=0, endOfTx=0) myaccel.process(file_in="samples/sample_1_1MB.txt", file_out="results/out.tar.gz", startOfTx=0, endOfTx=1)
#! /usr/bin/env python # coding=utf-8 """This shows the use of GUNZIP Accelerator accelerator. This example decompress a file of 1 MB. Please, set up your configuration file before running following example. Read Apyfal documentation for more information: https://apyfal.readthedocs.io""" if __name__ == "__main__": from apyfal import Accelerator, get_logger # Enable extra information for this demo by logging (Disabled by default) get_logger(True) # Run example print("1- Creating Accelerator...") with Accelerator(accelerator='cast_gunzip') as myaccel: print("2- Creating and Initializing Instance...") myaccel.start() print("3- Processing") myaccel.process(file_in="samples/sample_1_1MB.txt.gz", file_out="results/sample_1_1MB.txt") print(" Processing completed with success") print("4- Stopping Accelerator...") print(" Accelerator stopped.")
# coding=utf-8 """This shows the use of SHA3 and SHAKE Application accelerator. This example computes the sha3-224 for the run_example.py file. Make sure the accelerator.conf file has been completed before running the script. Please, set up your configuration file before running following example. Read Apyfal documentation for more information: https://apyfal.readthedocs.io""" if __name__ == "__main__": from apyfal import Accelerator, get_logger # Enable extra information for this demo by logging (Disabled by default) get_logger(True) # Run example print("1- Creating Accelerator...") with Accelerator(accelerator='silex_sha3') as myaccel: print("2- Creating and Initializing Instance...") myaccel.start() print("3- Processing") digest = myaccel.process(file_in="run_example.py", type="sha3-224") print(" Result is %s" % digest) print(" Processing completed with success") print("4- Stopping Accelerator...") print(" Accelerator stopped.")