def __init__(self, service, remote_service_coord=None, address=None): """Create a communication channel to a remote service. service (Service): the local service. remote_service_coord (ServiceCoord): the description of the remote service to connect to. address (Address): alternatively, the address to connect to (used when accepting a connection). """ if address is None and remote_service_coord is None: raise ValueError("Please provide address or " "remote_service_coord") # service is the local service connecting to the remote one. self.service = service if address is None: self.remote_service_coord = remote_service_coord self.address = get_service_address(remote_service_coord) else: self.remote_service_coord = "" self.address = address self.connected = False
def __init__(self, shard=0): signal.signal(signal.SIGINT, lambda unused_x, unused_y: self.exit()) self.name = self.__class__.__name__ self.shard = shard cms.log.initialize_logging(self.name, self.shard) # Stores the function to call periodically. It is to be # managed with heapq. Format: (next_timeout, period, function, # plus) self._timeouts = [] # Whether we want to exit the main loop self._exit = False # Dictionaries of (to be) connected RemoteService, and # dictionaries of callback functions that are going to be # called every time the remote service becomes online. self.remote_services = {} self.on_remote_service_connected = {} # Event to signal that something happened and the sleeping in # run() must be interrupted self.event = gevent.event.Event() self.event.clear() self._my_coord = ServiceCoord(self.name, self.shard) # We setup the listening address for services which want to # connect with us. try: address = get_service_address(self._my_coord) except KeyError: address = None if address is not None: self.server = StreamServer(address, self._connection_handler) self.backdoor = None
def main(): parser = optparse.OptionParser(usage="usage: %prog [options]") parser.add_option("-c", "--contest", help="contest ID to export", dest="contest_id", action="store", type="int", default=None) parser.add_option("-n", "--actor-num", help="the number of actors to spawn", dest="actor_num", action="store", type="int", default=None) parser.add_option("-s", "--sort-actors", help="sort usernames alphabetically " "instead of randomizing before slicing them", action="store_true", default=False, dest="sort_actors") parser.add_option("-u", "--base-url", help="base URL for placing HTTP requests", action="store", default=None, dest="base_url") parser.add_option("-S", "--submissions-path", help="base path for submission to send", action="store", default=None, dest="submissions_path") options = parser.parse_args()[0] users, tasks = harvest_contest_data(options.contest_id) if options.actor_num is not None: user_items = users.items() if options.sort_actors: user_items.sort() else: random.shuffle(user_items) users = dict(user_items[:options.actor_num]) # If the base URL is not specified, we try to guess it; anyway, # the guess code isn't very smart... if options.base_url is not None: base_url = options.base_url else: base_url = "http://%s:%d/" % \ (get_service_address(ServiceCoord('ContestWebServer', 0))[0], config.contest_listen_port[0]) actors = [RandomActor(username, data['password'], DEFAULT_METRICS, tasks, log=RequestLog(log_dir=os.path.join('./test_logs', username)), base_url=base_url, submissions_path=options.submissions_path) for username, data in users.iteritems()] for actor in actors: actor.start() try: while True: time.sleep(1) except KeyboardInterrupt: print >> sys.stderr, "Taking down actors" for actor in actors: actor.die = True # Turn on some memory profiling #from meliae import scanner #print "Dumping" #scanner.dump_all_objects('objects.json') #print "Dump finished" finished = False while not finished: for actor in actors: actor.join() else: finished = True print >> sys.stderr, "Test finished" great_log = RequestLog() for actor in actors: great_log.merge(actor.log) great_log.print_stats()
def main(): parser = optparse.OptionParser(usage="usage: %prog [options]") parser.add_option("-c", "--contest", help="contest ID to export", dest="contest_id", action="store", type="int", default=None) parser.add_option("-n", "--actor-num", help="the number of actors to spawn", dest="actor_num", action="store", type="int", default=None) parser.add_option("-s", "--sort-actors", help="sort usernames alphabetically " "instead of randomizing before slicing them", action="store_true", default=False, dest="sort_actors") parser.add_option("-u", "--base-url", help="base URL for placing HTTP requests", action="store", default=None, dest="base_url") parser.add_option("-S", "--submissions-path", help="base path for submission to send", action="store", default=None, dest="submissions_path") options = parser.parse_args()[0] users, tasks = harvest_contest_data(options.contest_id) if options.actor_num is not None: user_items = users.items() if options.sort_actors: user_items.sort() else: random.shuffle(user_items) users = dict(user_items[:options.actor_num]) # If the base URL is not specified, we try to guess it; anyway, # the guess code isn't very smart... if options.base_url is not None: base_url = options.base_url else: base_url = "http://%s:%d/" % \ (get_service_address(ServiceCoord('ContestWebServer', 0))[0], config.contest_listen_port[0]) actors = [ RandomActor( username, data['password'], DEFAULT_METRICS, tasks, log=RequestLog(log_dir=os.path.join('./test_logs', username)), base_url=base_url, submissions_path=options.submissions_path) for username, data in users.iteritems() ] for actor in actors: actor.start() try: while True: time.sleep(1) except KeyboardInterrupt: print >> sys.stderr, "Taking down actors" for actor in actors: actor.die = True # Turn on some memory profiling #from meliae import scanner #print "Dumping" #scanner.dump_all_objects('objects.json') #print "Dump finished" finished = False while not finished: for actor in actors: actor.join() else: finished = True print >> sys.stderr, "Test finished" great_log = RequestLog() for actor in actors: great_log.merge(actor.log) great_log.print_stats()