Beispiel #1
0
    def send_message(self):
        ws = create_connection("wss://echo.websocket.org",
                               sslopt={"cert_reqs": ssl.CERT_NONE})

        user_id = six.text_type(uuid.uuid4())
        start_at = time.time()
        json_val = {
            'id': user_id,
            'salutation': "Hello",
            'name': "James",
            'start_at': start_at
        }
        msg = json.dumps(json_val)
        ws.send(msg)
        print(f"Sent {msg}")
        res = ws.recv()
        data = json.loads(res)
        end_at = time.time()
        response_time = int((end_at - data['start_at']) * 1000)
        print(f"Received {res}")
        print(f"Got back id {data['id']}")

        myrandom = random.randint(1, 10)

        if myrandom == 1:
            request_failure.fire(request_type='WebSocket Response',
                                 name='ws',
                                 response_time=response_time,
                                 exception=Exception("My Exception"))
        else:
            request_success.fire(request_type='WebSocket Response',
                                 name='ws',
                                 response_time=response_time,
                                 response_length=len(res))
Beispiel #2
0
 def run(self):
     log.info("Starting MobileStreamingUpdateProcessor connecting to uri: " + self._uri)
     self._running = True
     while self._running:
         try:
             init_start = time.time()
             messages = self._connect()
             for msg in messages:
                 if not self._running:
                     break
                 message_ok = self.process_message(self._store, self._requester, msg)
                 if message_ok is True and self._ready.is_set() is False:
                     log.info("MobileStreamingUpdateProcessor initialized ok.")
                     init_duration = int((time.time() - init_start) * 1000)
                     request_success.fire(request_type="ld:init", name="mobile", response_time=init_duration, response_length=0)
                     self._ready.set()
         except UnsuccessfulResponseException as e:
             log.error(http_error_message(e.status, "stream connection"))
             init_duration = int((time.time() - init_start) * 1000)
             request_failure.fire(request_type="ld:init", name="mobile", response_time=init_duration, response_length=0, exception=e)
             if not is_http_error_recoverable(e.status):
                 self._ready.set()  # if client is initializing, make it stop waiting; has no effect if already inited
                 self.stop()
                 break
         except Exception as e:
             init_duration = int((time.time() - init_start) * 1000)
             request_failure.fire(request_type="ld:init", name="mobile", response_time=init_duration, response_length=0, exception=e)
             log.warning("Caught exception. Restarting stream connection after one second. %s" % e)
             # no stacktrace here because, for a typical connection error, it'll just be a lengthy tour of urllib3 internals
         time.sleep(1)
        def _receive():
            sub_mapping = {}
            while True:
                start_time = time.time()
                try:
                    res = ws.recv()
                except:
                    log.exception("Error receiving message")
                    break
                message = json.loads(res)
                try:
                    sub_id = message["id"]
                except KeyError:
                    pass
                else:
                    try:
                        sub_mapping[message["result"]] = sub_id
                        continue
                    except KeyError:
                        pass
                    # except TypeError:
                    #     print(sub_mapping)
                    #     print(message)
                    #     print(sub_id)

                end_time = time.time()
                # if output:
                # print(message)
                try:
                    message["params"]["subscription"] = sub_mapping[
                        message["params"]["subscription"]]
                except KeyError:
                    log.warning("Mapping missing for %s",
                                message["params"]["subscription"])
                    end_at = time.time()
                    response_time = int(round((end_time - start_time) * 1000))
                    num = len(self.sends) // 10
                    request_failure.fire(request_type='WebSocket Recv',
                                         name=f'gotsomething{num}',
                                         response_time=1,
                                         response_length=len(res),
                                         exception="Mapping missing for %s" %
                                         message["params"]["subscription"])

                end_at = time.time()
                num = len(self.sends) // 10
                # print("recv")
                # print(message)
                # if num == 0:
                # print(len(self.sends))
                # print(self.sends)
                response_time = int(round((end_time - start_time) * 1000))
                request_success.fire(
                    request_type='WebSocket Recv',
                    name=f'gotsomething{num}',
                    response_time=response_time,
                    response_length=len(res),
                )
 def test_any(self):
     test_dict = random.choice(self.locust.tests)
     try:
         self.test_runner.run_test(test_dict)
     except (AssertionError, MyBaseError, MyBaseFailure) as ex:
         request_failure.fire(
             request_type=self.test_runner.exception_request_type,
             name=self.test_runner.exception_name,
             response_time=0,
             exception=ex)
Beispiel #5
0
    def with_requests(self, url):
        """Get a streaming response for the given event feed using requests."""
        import requests

        try:
            r = requests.get(url, stream=True)
            return r
        except requests.exceptions.RequestException as e:
            print(e)
            request_failure.fire()
Beispiel #6
0
 def run(self, path):
     try:
         self.runner.run(path)
     except exceptions.MyBaseError as ex:
         # TODO: refactor
         from locust.events import request_failure
         request_failure.fire(request_type=test.testcase_dict.get(
             "request", {}).get("method"),
                              name=test.testcase_dict.get("request",
                                                          {}).get("url"),
                              response_time=0,
                              exception=ex)
Beispiel #7
0
    def urlopen(self, method, url, redirect=True, **kw):
        is_stream = not kw.get('preload_content', True)
        start_time = time.time()
        x = urlparse(url)
        name = url
        req_type = method
        if method == 'GET' and (x.path.find('/meval/') == 0
                                or x.path.find('/users/') > -1
                                or x.path.find('/eval/') == 0):
            parts = x.path.split('/')
            parts.pop()
            parts.append('[user]')
            newpath = '/'.join(parts)
            x = x._replace(path=newpath)
            name = urlunparse(x)

        if is_stream and kw.get('headers', {}).get(
                'Accept', 'lol') == 'text/event-stream':
            req_type = 'sse:connect'

        resp = None
        content_len = 0

        try:
            resp = super(LocustPoolManager,
                         self).urlopen(method, url, redirect, **kw)
            if is_stream:
                content_len = int(resp.headers.get("content-length") or 0)
            else:
                content_len = len(resp.data or b"")
            throw_if_unsuccessful_response(resp)
        except UnsuccessfulResponseException as e:
            request_failure.fire(request_type=req_type,
                                 name=name,
                                 exception=e,
                                 response_length=content_len,
                                 response_time=int(
                                     (time.time() - start_time) * 1000))
            return resp
        except Exception as e:
            request_failure.fire(request_type=req_type,
                                 name=name,
                                 exception=e,
                                 response_length=content_len,
                                 response_time=int(
                                     (time.time() - start_time) * 1000))
            raise e
        request_success.fire(request_type=req_type,
                             name=name,
                             response_length=content_len,
                             response_time=int(
                                 (time.time() - start_time) * 1000))
        return resp
Beispiel #8
0
 def test_any(self):
     teststeps = random.choice(self.locust.tests)
     for teststep in teststeps:
         try:
             test_runner.run_test(teststep)
         except (MyBaseError, MyBaseFailure) as ex:
             request_failure.fire(request_type=teststep.get(
                 "request", {}).get("method"),
                                  name=teststep.get("name"),
                                  response_time=0,
                                  exception=ex)
             break
         gevent.sleep(1)
Beispiel #9
0
 def run(self):
     for suite in self.task_suite:
         for test in suite:
             try:
                 test.runTest()
             except exception.MyBaseError as ex:
                 from locust.events import request_failure
                 request_failure.fire(
                     request_type=test.testcase_dict.get("request", {}).get("method"),
                     name=test.testcase_dict.get("request", {}).get("url"),
                     response_time=0,
                     exception=ex
                 )
Beispiel #10
0
 def run(self):
     for suite in self.task_suite:
         for test in suite:
             try:
                 test.runTest()
             except exception.MyBaseError as ex:
                 from locust.events import request_failure
                 request_failure.fire(
                     request_type=test.testcase_dict.get("request",
                                                         {}).get("method"),
                     name=test.testcase_dict.get("request", {}).get("url"),
                     response_time=0,
                     exception=ex)
Beispiel #11
0
    def run(self):
        for suite in self.task_suite.tasks:
            for test in suite.tests:
                try:
                    test.runTest()
                except p_exception.MyBaseError as e:
                    from locust.events import request_failure
                    request_step = test.testcase_dict["steps"][0]

                    request_failure.fire(request_type=request_step.get(
                        "request", {}).get("method"),
                                         name=request_step.get("request",
                                                               {}).get("url"),
                                         response_time=0,
                                         exception=e)
Beispiel #12
0
 def run(self):
     for suite in self.task_suite:
         for test in suite:
             try:
                 test.runTest()
             except exception.MyBaseError as ex:
                 try:
                     from locust.events import request_failure
                     request_failure.fire(
                         request_type=test.testcase_dict.get(
                             "request", {}).get("method"),
                         name=test.testcase_dict.get("request",
                                                     {}).get("url"),
                         response_time=0,
                         exception=ex)
                 except ImportError:
                     logging.exception(
                         "Exception occured in testcase: {}".format(
                             test.testcase_dict.get("name")))
Beispiel #13
0
    def urlopen(self, method, url, redirect=True, **kw):
        is_stream = not kw.get('preload_content', True)
        start_time = time.time()
        name = clean_name(method, url)
        req_type = method

        if is_stream and kw.get('headers', {}).get(
                'Accept', 'lol') == 'text/event-stream':
            req_type = 'sse:connect'

        resp = None
        content_len = 0

        try:
            resp = super(LocustProxyPoolManager,
                         self).urlopen(method, url, redirect, **kw)
            if is_stream:
                content_len = int(resp.headers.get("content-length") or 0)
            else:
                content_len = len(resp.data or b"")
            throw_if_unsuccessful_response(resp)
        except UnsuccessfulResponseException as e:
            request_failure.fire(request_type=req_type,
                                 name=name,
                                 exception=e,
                                 response_length=content_len,
                                 response_time=int(
                                     (time.time() - start_time) * 1000))
            return resp
        except Exception as e:
            request_failure.fire(request_type=req_type,
                                 name=name,
                                 exception=e,
                                 response_length=content_len,
                                 response_time=int(
                                     (time.time() - start_time) * 1000))
            raise e
        request_success.fire(request_type=req_type,
                             name=name,
                             response_length=content_len,
                             response_time=int(
                                 (time.time() - start_time) * 1000))
        return resp
Beispiel #14
0
    def __next__(self):
        while not self._event_complete():
            try:
                nextline = next(self.resp_file)
                # There are some bad cases where we don't always get a line: https://github.com/requests/requests/pull/2431
                if not nextline:
                    raise EOFError()
                self.buf += nextline.decode("utf-8")
            except (StopIteration, EOFError) as e:
                request_failure.fire(
                    request_type='sse:disconnect',
                    name="/meval",
                    response_time=int(
                        (time.time() - self._connect_start) * 1000),
                    response_length=0,
                    exception=e)
                time.sleep(self.retry / 1000.0)

                # The SSE spec only supports resuming from a whole message, so
                # if we have half a message we should throw it out.
                head, sep, tail = self.buf.rpartition('\n')
                self.buf = head + sep
                continue

        split = re.split(end_of_field, self.buf)
        head = split[0]
        tail = "".join(split[1:])

        self.buf = tail
        msg = Event.parse(head, instance=self)

        # If the server requests a specific retry delay, we need to honor it.
        if msg.retry:
            self.retry = msg.retry

        # last_id should only be set if included in the message.  It's not
        # forgotten if a message omits it.
        if msg.id:
            self.last_id = msg.id

        return msg
Beispiel #15
0
 def put_record(self):
     start_at = time.time()
     print(start_at)
     try:
         res = self.conn.put_record(stream_name="test-stream",
                                    data="test",
                                    partition_key="partition_key1")
         print(res)
         request_success.fire(
             request_type='Kinesis put record',
             name='test-stream',
             response_time=int((time.time() - start_at) * 1000),
             response_length=len(json.dumps(res)),
         )
     except Exception as e:
         print(e)
         request_failure.fire(request_type='Kinesis put record',
                              name='test-stream',
                              response_time=int(
                                  (time.time() - start_at) * 1000),
                              exception=e)
Beispiel #16
0
    def handle_message(self, msg, **kwargs):
        init_time = kwargs.get('init_time', time.time())

        if "CONNECTED" in msg:
            logging.info("%s: Connection initiated", self.instanceId)
            request_success.fire(request_type='WebSocket Init',
                                 name='Init websocket connection',
                                 response_time=(time.time() - init_time) *
                                 1000,
                                 response_length=len(msg))
        elif "ERROR" in msg:
            logging.warning("%s: Received error: \n %s", self.instanceId,
                            str(msg))
            request_failure.fire(request_type='WebSocket Error',
                                 name='Websocket server Error',
                                 response_time=(time.time() - init_time) *
                                 1000,
                                 response_length=len(msg),
                                 exception=InterruptTaskSet())
            raise StopLocust()
        elif "MESSAGE" in msg:
            logging.info("%s: Message received: %s", self.instanceId, str(msg))
            message = str(msg)
            idx = message.find("{")
            content = message[idx:-1]

            json_msg = json.loads(content)
            res = stomper.send(
                "/app/device",
                '{ "time": "' + datetime.datetime.now().isoformat() + '",' +
                '"status": "OK",' + '"id": "' + json_msg["id"] + '",' +
                '"data": ' + str([1, 0, -1, 0]) + '}',
                content_type="application/json")
            self.ws.send(res)
            request_success.fire(request_type='Msg Receive',
                                 name='Msg received',
                                 response_time=(time.time() - init_time) *
                                 1000,
                                 response_length=len(msg))
Beispiel #17
0
 def do_work(self, testfile):
     work = get_work(testfile, self.locust.client)
     for suite in work:
         for test in suite:
             test.testcase_dict['variables'].update(self.variables)
             test.testcase_dict['request']['group'] = test.testcase_dict[
                 'name']
             test.testcase_dict['request']['verify'] = False
             test.testcase_dict['request']['timeout'] = 30
             try:
                 test.runTest()
             except (MyBaseError, MyBaseFailure) as ex:
                 from locust.events import request_failure
                 request_failure.fire(
                     request_type=test.testcase_dict.get("request",
                                                         {}).get("method"),
                     name=test.testcase_dict.get("request",
                                                 {}).get("group"),
                     response_time=0,
                     exception=ex)
                 break
             gevent.sleep(1)