How to use add_worker method in locust

Best Python code snippet using locust

test_broker.py

Source:test_broker.py Github

copy

Full Screen

...44 assert w.is_heartbeat(now + 1.0) is True45def test_service_worker_added():46 svc = broker.Service()47 assert 0 == svc.available_workers48 svc.add_worker(broker.Worker(b'w1', b's1', time.time(), time.time()))49 assert 1 == svc.available_workers50 svc.add_worker(broker.Worker(b'w2', b's1', time.time(), time.time()))51 assert 2 == svc.available_workers52def test_service_worker_removed():53 svc = broker.Service()54 assert 0 == svc.available_workers55 svc.add_worker(broker.Worker(b'w1', b's1', time.time(), time.time()))56 svc.add_worker(broker.Worker(b'w2', b's1', time.time(), time.time()))57 assert 2 == svc.available_workers58 svc.remove_worker(b'w1')59 svc.remove_worker(b'w2')60 assert 0 == svc.available_workers61def test_service_request_queued():62 svc = broker.Service()63 assert 0 == svc.queued_requests64 svc.queue_request(b'c1', [b'f1', b'f2'])65 assert 1 == svc.queued_requests66 svc.queue_request(b'c1', [b'f1', b'f2'])67 assert 2 == svc.queued_requests68def test_service_request_dequeued():69 message = [b'f1', b'f2']70 worker = broker.Worker(b'w1', b's1', time.time(), time.time())71 client = b'c1'72 svc = broker.Service()73 svc.add_worker(worker)74 dequeued = list(svc.dequeue_pending())75 assert 0 == len(dequeued)76 svc.queue_request(client, message)77 dequeued = list(svc.dequeue_pending())78 assert 1 == len(dequeued)79 assert dequeued[0] == (message, worker, client)80def test_service_dequeue_no_workers():81 svc = broker.Service()82 svc.queue_request(b'c1', [b'f1', b'f2'])83 svc.queue_request(b'c1', [b'f1', b'f2'])84 svc.add_worker(broker.Worker(b'w1', b's1', time.time(), time.time()))85 assert 1 == svc.available_workers86 assert 2 == svc.queued_requests87 dequeued = list(svc.dequeue_pending())88 assert 1 == len(dequeued)89 assert 0 == svc.available_workers90 assert 1 == svc.queued_requests91 dequeued = list(svc.dequeue_pending())92 assert 0 == len(dequeued)93def test_service_dequeue_no_requests():94 svc = broker.Service()95 svc.queue_request(b'c1', [b'f1', b'f2'])96 svc.add_worker(broker.Worker(b'w1', b's1', time.time(), time.time()))97 svc.add_worker(broker.Worker(b'w2', b's1', time.time(), time.time()))98 assert 2 == svc.available_workers99 assert 1 == svc.queued_requests100 dequeued = list(svc.dequeue_pending())101 assert 1 == len(dequeued)102 assert 1 == svc.available_workers103 assert 0 == svc.queued_requests104 dequeued = list(svc.dequeue_pending())105 assert 0 == len(dequeued)106def test_service_container_yields_expired_workers():107 now = time.time()108 container = broker.ServicesContainer()109 container.add_worker(b'w1-good', b'service-1', now + 5, now)110 container.add_worker(b'w2-good', b'service-2', now + 1, now)111 container.add_worker(b'w3-exp', b'service-1', now - 0.5, now)112 container.add_worker(b'w4-exp', b'service-2', now - 1000, now)113 expired = list(container.expired_workers())114 assert 2 == len(expired)115 for w in expired:116 assert w.id in {b'w3-exp', b'w4-exp'}117def test_service_container_yields_expired_busy_workers():118 now = time.time()119 container = broker.ServicesContainer()120 container.add_worker(b'w1-good', b'service-1', now + 5, now)121 container.add_worker(b'w2-good', b'service-2', now + 1, now)122 container.add_worker(b'w3-exp', b'service-1', now - 0.5, now)123 container.add_worker(b'w4-exp', b'service-2', now - 1000, now)124 container.set_worker_busy(b'w2-good', now - 1)125 expired = list(container.expired_workers())126 assert 3 == len(expired)127def test_service_container_worker_removed():128 now = time.time()129 container = broker.ServicesContainer()130 container.add_worker(b'w1-good', b'service-1', now + 5, now)131 container.add_worker(b'w3-exp', b'service-1', now - 0.5, now)132 assert 1 == len(list(container.expired_workers()))133 for w in container.expired_workers():134 container.remove_worker(w.id)135 assert 0 == len(list(container.expired_workers()))136def test_service_container_busy_worker_removed():137 now = time.time()138 container = broker.ServicesContainer()139 container.add_worker(b'w1-good', b'service-1', now + 5, now)140 container.set_worker_busy(b'w1-good', now + 900.0)141 assert 0 == len(list(container.expired_workers()))142 container.remove_worker(b'w1-good')143 with raises(error.StateError):144 container.set_worker_available(b'w1-good', now + 5, now + 1)145def test_service_container_is_worker_busy():146 now = time.time()147 container = broker.ServicesContainer()148 container.add_worker(b'w1-good', b'service-1', now + 5, now)149 assert container.is_busy_worker(b'w1-good') is False150 container.set_worker_busy(b'w1-good', now + 900.0)151 assert container.is_busy_worker(b'w1-good') is True152 assert container.is_busy_worker(b'unknown-worker') is False153def test_service_container_non_existing_worker_removed():154 container = broker.ServicesContainer()155 with raises(error.StateError):156 container.remove_worker(b'w1-good')157def test_service_container_yields_heartbeat_workers():158 now = time.time()159 container = broker.ServicesContainer()160 container.add_worker(b'w1-good', b'service-1', now + 5, now + 1)161 container.add_worker(b'w2-good', b'service-2', now + 5, now + 2)162 container.add_worker(b'w3-time', b'service-1', now + 5, now)163 container.add_worker(b'w4-time', b'service-2', now + 5, now - 1)164 expired = list(container.heartbeat_workers())165 assert 2 == len(expired)166 for w in expired:167 assert w.id in {b'w3-time', b'w4-time'}168@mark.parametrize('id_,expected', [169 (b'\x01\x02\x03\x04', 16909060),170 ('\x01\x02\x03\x04', 16909060)171])172def test_id_to_int(id_, expected):...

Full Screen

Full Screen

workersupervisor.py

Source:workersupervisor.py Github

copy

Full Screen

...19 20 self.demandWorkQueue = Queue(maxsize=self.max_work_capacity * 2)21 def get_printer_actor(self):22 return self.directory.get_actor('printeractor')23 def add_worker(self):24 self.workers_cnt_id += 125 new_worker = Worker("worker%d" % self.workers_cnt_id, self.directory)26 self.get_printer_actor().inbox.put({"text":"ADD WORKER %d" % self.workers_cnt_id, "type":'warning'})27 new_worker.start()28 self.workers.put(new_worker)29 def add_named_worker(self, name):30 self.workers_cnt_id += 131 new_worker = Worker(name, self.directory)32 self.get_printer_actor().inbox.put({"text":"ADD NAMED WORKER %s" % name, "type":'warning'})33 new_worker.start()34 self.workers.put(new_worker)35 def remove_worker(self):36 worker = self.workers.get()37 self.get_printer_actor().inbox.put({"text":"REMOVE WORKER %s" % worker.get_name(), "type":'warning'})38 worker.stop()39 40 def get_directory(self):41 return self.directory42 def start(self):43 Actor.start(self) 44 def process_panic_message(self, current_worker):45 # print("PANIC")46 self.get_printer_actor().inbox.put({"text":"!!! PANIC !!!", "type":'warning-bold'})47 current_worker.inbox.put("PANIC")48 def process_worker_fail(self, current_worker):49 worker_to_be_restarted = self.worker_restart_policy.restart_worker(current_worker)50 name = current_worker.get_name()51 self.get_printer_actor().inbox.put({"text":"--killed worker %s" % name, "type":'warning'})52 self.workers.put(worker_to_be_restarted)53 self.get_printer_actor().inbox.put({"text":"--restarted worker %s" %worker_to_be_restarted.get_name(), "type":'warning'})54 55 def adapt_number_of_workers(self):56 # Cases when to add workers57 if(self.demandWorkQueue.qsize()>2 and (self.workers.qsize()<self.max_work_capacity)):58 self.add_worker()59 60 if(self.demandWorkQueue.qsize()>4 and (self.workers.qsize()+2<=self.max_work_capacity)):61 self.add_worker()62 self.add_worker()63 if(self.demandWorkQueue.qsize()>6 and (self.workers.qsize()+3<=self.max_work_capacity)):64 self.add_worker()65 self.add_worker()66 self.add_worker()67 if(self.demandWorkQueue.qsize()>8):68 for i in range(1, self.demandWorkQueue.qsize()/1.5):69 if self.workers.qsize()<self.max_work_capacity:70 self.add_worker()71 72 # Cases when to remove workers73 if (self.workers.qsize()> (self.demandWorkQueue.qsize()+ 6)):74 for i in range(self.demandWorkQueue.qsize()-self.workers.qsize()):75 if(not self.workers.empty()):76 self.remove_worker()77 else:78 if(self.workers.qsize()>( self.demandWorkQueue.qsize()+ 4)):79 if(not self.workers.empty()):80 self.remove_worker()81 if(not self.workers.empty()):82 self.remove_worker()83 84 def get_directory(self):85 return self.directory86 def add_first_actors(self):87 self.add_worker() 88 self.add_worker() 89 self.add_worker() 90 self.add_worker() 91 self.add_worker() 92 def get_worker_name(self, name):93 size = self.workers.qsize()94 cnt = 095 96 while (not self.workers.empty()) and cnt < size:97 worker = self.workers.get()98 # print(worker.get_name())99 if(worker.get_name() == name):100 self.workers.put(worker)101 return worker102 self.workers.put(worker)103 cnt +=1104 return ''105 def receive(self, message):106 if(message=='start'):107 self.add_first_actors()108 return109 110 self.get_printer_actor().inbox.put({"text":'Receives work', "type":'normal'}) 111 self.demandWorkQueue.put(message)112 self.get_printer_actor().inbox.put({"text": str("Demand work: %d" %self.demandWorkQueue.qsize()), "type":'green'})113 if -1 == self.workers.qsize() - 1 or self.workers.empty():114 self.get_printer_actor().inbox.put({"text":"Supervisor received work but no workers to give it to!",115 "type":"error"})116 if self.workers.qsize() < self.max_work_capacity:117 self.get_printer_actor().inbox.put({"text":"Adding new worker", "type":"warning"})118 self.add_worker()119 else:120 self.get_printer_actor().inbox.put({"text":"Max work Capacity exceeded!!! waiting for free worker", "type":"error"})121 return122 123 if self.workers.empty():124 self.get_printer_actor().inbox.put({"text":"No active worker. Adding new worker", "type":"warning"})125 self.add_worker()126 message = self.demandWorkQueue.get()127 # IF MESSAGE==PANIC128 if(message=='{"message": panic}' or message=="panic" or message=="PANIC"):129 current_worker = self.workers.get()130 self.process_panic_message(current_worker)131 self.workers.put(current_worker)132 # when worker sends notification of exception, after panic message for example133 elif "EXCEPTION WORKER" in message:134 start = message.find('EXCEPTION WORKER ') + len("EXCEPTION WORKER ")135 worker_name = message[start:]136 # for debug137 # print("WORKER_NAME" + worker_name)138 current_worker = self.get_worker_name(worker_name)139 self.process_worker_fail(current_worker)...

Full Screen

Full Screen

zadanie_2.py

Source:zadanie_2.py Github

copy

Full Screen

...6# Uruchomić tą samą 30klientową kolejkę na 2 różnych biurach7office_1 = Office()8office_2 = Office()9for i in range(3):10 office_1.add_worker(Worker('A'))11 office_1.add_worker(Worker('B'))12 office_1.add_worker(Worker('C'))13for i in range(2):14 office_2.add_worker(Worker('A'))15 office_2.add_worker(Worker('B'))16 office_2.add_worker(Worker('C'))17 office_2.add_worker(Worker('E'))18queue_1 = SinglyLinkedList()19queue_2 = SinglyLinkedList()20for i in range(30):21 queue_1.add(Client())22 queue_2.add(Client())23simulation.run_simulation(office_1, queue_1)24simulation.run_simulation(office_2, queue_2)25office_1.print_summary()26office_2.print_summary()27del office_128del office_229del queue_130del queue_231# Część 2ga32# Narysować histogram dla 100 kolejek33office_1_times = []34office_2_times = []35for i in range(100):36 office_1 = Office()37 office_2 = Office()38 for i in range(3):39 office_1.add_worker(Worker('A'))40 office_1.add_worker(Worker('B'))41 office_1.add_worker(Worker('C'))42 for i in range(2):43 office_2.add_worker(Worker('A'))44 office_2.add_worker(Worker('B'))45 office_2.add_worker(Worker('C'))46 office_2.add_worker(Worker('E'))47 queue_1 = SinglyLinkedList()48 queue_2 = SinglyLinkedList()49 for i in range(30):50 queue_1.add(Client())51 queue_2.add(Client())52 simulation.run_simulation(office_1, queue_1)53 simulation.run_simulation(office_2, queue_2)54 office_1_times.append(office_1.worked_time)55 office_2_times.append(office_2.worked_time)56office_1_avg = sum(office_1_times)/len(office_1_times)57office_2_avg = sum(office_2_times)/len(office_2_times)58print(f"""59Średni czas pracy:609 okienkowy urząd: {office_1_avg}...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run locust automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful