def _sync(self, operations, headers, name): with self.client.post(self.context.url + '/api/_action/sync', json=operations, headers=headers, name=name, catch_response=True) as response: if response.status_code in [200, 204]: response.success() return if response.status_code == 401: self.context.refresh_token() raise RescheduleTask() text = response.json() try: if ("INSERT INTO `product_translation`" in text['errors'][0]['detail']): raise RescheduleTask() if ("Deadlock found when trying to get lock" in text['errors'][0]['detail']): raise RescheduleTask() except (IndexError): pass raise ValueError('Sync error: ' + str(response.status_code) + ' ' + response.text)
def run(self, *args, **kwargs): self.args = args self.kwargs = kwargs try: self.on_start() except InterruptTaskSet as e: if e.reschedule: raise RescheduleTaskImmediately(e.reschedule).with_traceback( sys.exc_info()[2]) else: raise RescheduleTask(e.reschedule).with_traceback( sys.exc_info()[2]) while (True): try: if not self._task_queue: self.schedule_task(self.get_next_task()) try: self._check_stop_condition() self.execute_next_task() except RescheduleTaskImmediately: pass except RescheduleTask: self.wait() else: self.wait() except InterruptTaskSet as e: self.on_stop() if e.reschedule: raise RescheduleTaskImmediately(e.reschedule) from e else: raise RescheduleTask(e.reschedule) from e except (StopUser, GreenletExit): self.on_stop() raise except Exception as e: self.user.environment.events.user_error.fire( user_instance=self, exception=e, tb=sys.exc_info()[2]) if self.user.environment.catch_exceptions: logger.error("%s\n%s", e, traceback.format_exc()) self.wait() else: raise
def run(self, *args, **kwargs): self.args = args self.kwargs = kwargs try: if hasattr(self, "on_start"): self.on_start() except InterruptTaskSet as e: if e.reschedule: six.reraise(RescheduleTaskImmediately, RescheduleTaskImmediately(e.reschedule), sys.exc_info()[2]) else: six.reraise(RescheduleTask, RescheduleTask(e.reschedule), sys.exc_info()[2]) while (True): try: if self.locust.stop_timeout is not None and time() - self._time_start > self.locust.stop_timeout: return if not self._task_queue: self.schedule_task(self.get_next_task()) try: self.execute_next_task() except RescheduleTaskImmediately: pass except RescheduleTask: self.wait() else: self.wait() except InterruptTaskSet as e: if e.reschedule: six.reraise(RescheduleTaskImmediately, RescheduleTaskImmediately(e.reschedule), sys.exc_info()[2]) else: six.reraise(RescheduleTask, RescheduleTask(e.reschedule), sys.exc_info()[2]) except StopLocust: raise except GreenletExit: raise except Exception as e: events.locust_error.fire(locust_instance=self, exception=e, tb=sys.exc_info()[2]) if self.locust._catch_exceptions: sys.stderr.write("\n" + traceback.format_exc()) self.wait() else: raise
def run(self): try: self.on_start() except InterruptTaskSet as e: if e.reschedule: raise RescheduleTaskImmediately(e.reschedule).with_traceback( e.__traceback__) else: raise RescheduleTask(e.reschedule).with_traceback( e.__traceback__) while True: try: if not self._task_queue: self.schedule_task(self.get_next_task()) try: if self.user._state == LOCUST_STATE_STOPPING: raise StopUser() self.execute_next_task() except RescheduleTaskImmediately: pass except RescheduleTask: self.wait() else: self.wait() except InterruptTaskSet as e: self.on_stop() if e.reschedule: raise RescheduleTaskImmediately(e.reschedule) from e else: raise RescheduleTask(e.reschedule) from e except (StopUser, GreenletExit): self.on_stop() raise except Exception as e: self.user.environment.events.user_error.fire( user_instance=self, exception=e, tb=e.__traceback__) if self.user.environment.catch_exceptions: logger.error("%s\n%s", e, traceback.format_exc()) self.wait() else: raise
def wrapper(*args, **kwargs): start_time = time.time() try: result = func(*args, **kwargs) except xmlrpclib.Fault as e: total_time = int((time.time() - start_time) * 1000) events.request_failure.fire(request_type="xmlrpc", name=name, response_time=total_time, exception=e) raise RescheduleTask(e, name) else: total_time = int((time.time() - start_time) * 1000) events.request_success.fire(request_type="xmlrpc", name=name, response_time=total_time, response_length=0)
def _fire_post_listener_event(self, result, event, name, exec_time): if result: logger.debug("Listener resolved succesfully") LocustEventHandler.request_success.fire(**self._locust_event( event, name, exec_time, response_length=-1)) else: logger.debug("Listener resolved by timeout") exception = SocketIOTimeoutError('SocketIO listener timeout') LocustEventHandler.request_failure.fire(**self._locust_event( event, name, exec_time, exception=exception)) raise RescheduleTask(exception, name)
def _emit(self, msg, action_name): success = False try: logger.debug("Message sent:\n%s", msg) self._socket.emit(msg.type, msg.payload, path=self._namespace) except Exception as e: logger.debug("Message sent failure: %s", msg.type) LocustEventHandler.request_failure.fire( **self._locust_event('request', action_name, 0, exception=e)) raise RescheduleTask(e, action_name) else: success = True return success
def send(self, message): """Send message through socket""" message = [str(i) for i in message] logger.debug("Sending ZMQ message:\n%s", message) try: tracker = self._socket.send_multipart(message, copy=False, track=True) tracker.wait() LocustEventHandler.request_success.fire(**self._locust_event( response_length=-1)) except Exception as e: LocustEventHandler.request_failure.fire(**self._locust_event( exception=e)) raise RescheduleTask(e, self._name or self._endpoint)
def failure(self, exc): """ Report the response as a failure. exc can be either a python exception, or a string in which case it will be wrapped inside a CatchResponseError. Example:: with self.client.get("/", catch_response=True) as response: if response.content == "": response.failure("No data") """ if isinstance(exc, six.string_types): exc = CatchResponseError(exc) fire_failure(self.locust_request_meta, self.task, exc) self._is_reported = True name = "{} {}".format(self.locust_request_meta["method"], self.locust_request_meta["name"]) raise RescheduleTask(exc, name)
def _sync(self, operations, headers, name): with self.client.post('/api/_action/sync', json=operations, headers=headers, name=name, catch_response=True) as response: if response.status_code in [200, 204]: response.success() return content = response.json() if 'errors' not in content: return if 0 not in content['errors']: return if 'detail' not in content['errors'][0]: return message = content['errors'][0]['detail'] if 'Deadlock found when' in message: raise RescheduleTask()
def request_failure(self, request_type, name, response_time, response_length, exception, **_kwargs): raise RescheduleTask()
def request(self, exception, **_kwargs): if exception: raise RescheduleTask()
def about(self): with self.client.get("/does_not_exist/", catch_response=True) as response: if response.status_code != 404 : raise RescheduleTask()
def request(self, method, url, name=None, catch_response=False, **kwargs): """ Constructs and sends a :py:class:`requests.Request`. Returns :py:class:`requests.Response` object. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param name: (optional) An argument that can be specified to use as label in Locust's statistics instead of the URL path. This can be used to group different URL's that are requested into a single entry in Locust's statistics. :param catch_response: (optional) Boolean argument that, if set, can be used to make a request return a context manager to work as argument to a with statement. This will allow the request to be marked as a fail based on the content of the response, even if the response code is ok (2xx). The opposite also works, one can use catch_response to catch a request and then mark it as successful even if the response code was not (i.e 500 or 404). :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of ``'filename': file-like-objects`` for multipart encoding upload. :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth. :param timeout: (optional) How long to wait for the server to send data before giving up, as a float, or a (`connect timeout, read timeout <user/advanced.html#timeouts>`_) tuple. :type timeout: float or tuple :param allow_redirects: (optional) Set to True by default. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. :param stream: (optional) whether to immediately download the response content. Defaults to ``False``. :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. """ # prepend url with hostname unless it's already an absolute URL url = self._build_url(url) # store meta data that is used when reporting the request to locust's statistics request_meta = {} # set up pre_request hook for attaching meta data to the request object request_meta["method"] = method request_meta["start_time"] = time.time() response = self._send_request_safe_mode(method, url, **kwargs) # record the consumed time request_meta["response_time"] = int( (time.time() - request_meta["start_time"]) * 1000) request_name = (response.history and response.history[0] or response).request.path_url request_meta["name"] = name or request_name # get the length of the content, but if the argument stream is set to True, we take # the size from the content-length header, in order to not trigger fetching of the body if kwargs.get("stream", False): request_meta["content_size"] = int( response.headers.get("content-length") or 0) else: request_meta["content_size"] = len(response.content or "") if catch_response: response.locust_request_meta = request_meta return ResponseContextManager(response, self.binded_locust.current_task) else: try: response.raise_for_status() except HTTPError as exception: error = exception.message.split('for url:')[0] message = "{}for: {} {} ".format(error, method, request_meta["name"]) exception = exception.__class__( message, request=exception.request, response=exception.response, ) fire_failure(request_meta, self.binded_locust.current_task, exception) name = "{} {}".format(request_meta["method"], request_meta["name"]) raise RescheduleTask(exception, name) except RequestException as exception: fire_failure(request_meta, self.binded_locust.current_task, exception) name = "{} {}".format(request_meta["method"], request_meta["name"]) raise RescheduleTask(exception, name) else: fire_success(request_meta, self.binded_locust.current_task) return response