示例#1
0
    def on_start(self):
        """ This function is called before any of the defined tasks is scheduled
        It should create a new user with a random amount of credit
        and save the userid for future requests """

        userResponse = self.client.post("/user/create")
        if userResponse.status_code != 200:
            raise StopLocust()
        self.userid = json.loads(userResponse.text)['id']
        self.client.post("/user/" + self.userid + "/credit/add/" +
                         str(random.randint(1, 10)))

        # Create a list of orders for this user
        self.orders = []

        # Create an initial order
        response = self.client.post("/order/create/" + self.userid)
        if response.status_code != 200:
            raise StopLocust()
        self.orders.append(Order(self.userid, response.text))

        # Make sure that at least one item exists
        response = self.client.post("/stock/item/create")
        if response.status_code != 200:
            raise StopLocust()
        self.orders[0].addItem(json.loads(response.text)['id'])
示例#2
0
    def __check_limits(self):
        if self.locust_start_time is None:
            self.locust_start_time = time.time()

        if time.time() - self.locust_start_time >= self.locust_duration:
            raise StopLocust('Duration limit reached')

        if self.num_requests <= 0:
            raise StopLocust('Request limit reached')
示例#3
0
    def __check_limits(self):
        if self.locust_start_time is None:
            self.locust_start_time = time.time()

        # Only raise an exception if the actual test is running
        if self.locust_stop_time is None:
            if time.time() - self.locust_start_time >= self.locust_duration:
                raise StopLocust('Duration limit reached')

            if self.num_requests <= 0:
                raise StopLocust('Request limit reached')
示例#4
0
        def list(self):
            """ This locust task verifies if producers are terminated
      and, if they are, checks number of tasks left in queue.
      When queue is empty it triggers sys.exit(0)
      """
            try:
                producer_process = psutil.Process(PRODUCER_PID)
                if './producer_locust.py' in producer_process.cmdline():
                    return
            except psutil.NoSuchProcess:
                pass

            with self.client.rest('GET',
                                  path_suffix=f'/{PULL_QUEUE}/tasks') as resp:
                tasks = resp.json().get('items', [])
                active_tasks = [
                    # It would have to be ` < RETRY_LIMIT`, but out TQ works in wrong way
                    task for task in tasks
                    if task['retry_count'] <= RETRY_LIMIT + 1
                ]
                if active_tasks:
                    return

                if Worker.LAST_LEASE_TIME + LEASE_SECONDS + 150 < time.time():
                    logging.info(
                        'producer_locust is already terminated and we couldn\'t find '
                        'any active tasks (which did not exhausted retries) in queue. '
                        f'No tasks were leased during last {LEASE_SECONDS + 150} seconds.'
                    )
                    raise StopLocust('All work seems to be done')
示例#5
0
    def create_purchase(self):
        session = HttpSession(self.base_url)
        # self.client.post('/api/v0.9/reservation/', reservation_body)
        reservation_response = session.post('/api/v0.9/reservation/',
                                            data=reservation_body)

        if reservation_response.status_code != 201:
            return

        if reservation_response.status_code == 409:
            raise StopLocust('No available tickets.')

        checkout_response = session.post('/api/v0.9/reservation/_checkout',
                                         data=checkout_body)

        if checkout_response.status_code != 200:
            return

        purchase = checkout_response.json()['purchase']
        completion_response = session.post(
            '/api/v0.9/purchases/_complete',
            json.dumps(
                dict(purchase_id=purchase['purchase_id'],
                     hash=purchase['completion_params']['hash'],
                     processor=purchase['completion_params']['processor'],
                     payment_ref=purchase['completion_params']['order_ref'])))
    def on_disconnect(self):
        '''
    A subtlety to be concerned here. 
    '''
        if (True == self.client._should_stop_waiting()):
            # If the execution reaches here by actively calling `self.client.disconnect()`, then one finds "True == self.client._should_stop_waiting() == self.client._wants_to_close".
            print(
                '[ACTIVELY DISCONNECTED] SimplestConnectionEstablishmentPlayer for player.id == %s to room_id == %s.'
                % (self.player_id, self.room_id))
            raise StopLocust()  # This is within the "main locust".
        else:
            # If the execution reaches here passively within `self.client.wait()`, then one finds "False == self.client._should_stop_waiting() == self.client._wants_to_close", and should help terminate the loop in the spawned `self.client.wait()`. See https://github.com/invisibleroads/socketIO-client/blob/master/socketIO_client/__init__.py for details (socketIO_client v0.7.2).
            print(
                '[PASSIVELY DISCONNECTED] SimplestConnectionEstablishmentPlayer for player.id == %s to room_id == %s.'
                % (self.player_id, self.room_id))
            self.client._close()
            gevent.getcurrent().spawning_greenlet().kill()
            '''
      Killing the current `gevent.Greenlet` instance silently. 

      Quoted from http://www.gevent.org/api/gevent.greenlet.html#gevent.GreenletExit for "gevent v1.3.7.dev0".  

      "
      A special exception that kills the greenlet silently.

      When a greenlet raises GreenletExit or a subclass, the traceback is not printed and the greenlet is considered successful. The exception instance is available under value property as if it was returned by the greenlet, not raised.
      "
      '''
            raise GreenletExit(
            )  # This is within the "spawned greenlet from main locust".
示例#7
0
    def send_message(self):
        """
        this is an actual task executed by locust framework, you can have mutiple tasks within
        one task set, in that case if you want to have some form of controlling execution
        ration of those tasks, that can done via weight argument

            :Example
            @task(1)
            def func1(self):
                .....

            @task(2)
            def func2(self):
                .....

        func2 will be executed twice more often than func1

        To programmatically stop execution of taskset can be achieved by raising StopLocust exception
        :return: None
        """

        if self.is_task_ready():
            headers = Headers.get_auth_json_header(self.user.access_token)
            data = {
                'roomId': self.room['id'],
                'text': 'This is message from Locust task'
            }

            logging.debug('Headers user are: %s', headers)

            self.post(path='messages', data=data, headers=headers)
        else:
            raise StopLocust('Task is not ready')
示例#8
0
def wrap_for_locust(request_type, name, func, *args, **kwargs):
    """
    Wrap Selenium activity function with Locust's event fail/success
    method

    :param request_type: the type of request
    :param name: name to be reported to events.request_*.fire
    :param func: callable to be timed and logged
    :return result: Result of the provided function if doesn't raise exception
    """
    try:
        start_time = time.time()
        result = func(*args, **kwargs)
    except Exception as event_exception:
        total_time = int((time.time() - start_time) * 1000)
        events.request_failure.fire(request_type=request_type,
                                    name=name,
                                    response_time=total_time,
                                    exception=event_exception)
        raise StopLocust()
    else:
        total_time = int((time.time() - start_time) * 1000)
        events.request_success.fire(request_type=request_type,
                                    name=name,
                                    response_time=total_time,
                                    response_length=0)
        return result
示例#9
0
 def waitUntilDeletingIsDone(self):
   self._reset_timer()
   delay = 10
   failcount = 0
   clusterSubstring = (self.cluster.split("."))[1]
   getPodsResponse = self.client.get(
       "https://console." + clusterSubstring + ".openshift.com/api/v1/namespaces/" + self.locust.taskUserName + "-che/pods",
       headers={"Authorization": "Bearer " + self.openshiftToken},
       name="getPods-"+self.cluster, catch_response=True)
   podsJson = getPodsResponse.json()
   while "rm-" in str(podsJson):
     rmpods = str(podsJson).count("rm-") / 7
     self.log("[" + str(failcount) + "] There are still removing pods running. Trying again after " + str(delay) + " seconds.")
     self.log("Number of removing pods running: " + str(rmpods))
     time.sleep(delay)
     getPodsResponse = self.client.get(
         "https://console." + clusterSubstring + ".openshift.com/api/v1/namespaces/" + self.locust.taskUserName + "-che/pods",
         headers={"Authorization": "Bearer " + self.openshiftToken},
         name="getPods_"+self.clusterName, catch_response=True)
     podsJson = getPodsResponse.json()
     failcount += 1
     # After waiting for a minute, stop the locust test with generating the results
     if (failcount >= 6):
       raise StopLocust("The remove pod failed to finish execution within a minute. Stopping locust thread.")
   events.request_success.fire(request_type="REPEATED_GET",
                               name="deleteWorkspace_"+self.clusterName,
                               response_time=self._tick_timer(),
                               response_length=0)
   self.log("All removing pods finished.")
示例#10
0
    def query_image(self):
        auth = 'Bearer ' + config.token
        header = {
            'Authorization': auth,
            'Ocp-Apim-Subscription-Key': config.apim_key,
            'Content-Type': 'application/json'
        }
        param = {
            #'sizeTypeCode': '200x133'
        }
        print('Querying ' + photoId + ' ...')
        with self.client.get(url=config.query_url + '/' + photoId,
                             params=param,
                             headers=header,
                             name=config.query_url) as response:
            self.check_response(response)
            downloaded_size = int(response.headers['content-length'])
            downloaded_format = response.headers['content-type']
            file_size = os.path.getsize(config.image_file)
            assert (file_size == downloaded_size
                    ), "Got %d, expected %d" % (file_size, downloaded_size)
            assert (
                downloaded_format == config.image_format
            ), "Got %s, expected %s" % (downloaded_format, config.image_format)

            raise StopLocust()
示例#11
0
 def zapocni_test(self):
     body = f'{{"test": {self.test_id} }}'.encode('ascii')
     r = self.client.post('/api/pocetak/', body, headers=self.headers)
     if r.status_code != 201:
         raise StopLocust()
     self.test_ucenika_id = r.json()['test_ucenika']
     self.odgovor_count = 0
示例#12
0
 def zapocni_test(self):
     r = self.client.post('/api/pocetak/',
                          json={'test': self.test_id},
                          headers=self.headers)
     if r.status_code != 201:
         raise StopLocust()
     self.test_ucenika_id = r.json()['test_ucenika']
     self.odgovor_count = 0
示例#13
0
    def __init__(self, *args, **kwargs):
        super(UserBehaviourLocust, self).__init__(*args, **kwargs)

        self.user_token = os.environ['EXT_TOKEN']
        if self.user_token is None:
            raise StopLocust("Invalid token. Seppuku!!!")

        logger.info("Locust [%s] is joining the swarm..." % self.user_id)
示例#14
0
        def executeTestTwo(self):

            result1 = self.queryLocalMethodExample("some-value")
            if result1 is not None:
                logger.debug("result1: %s" % result1)
            else:
                logger.warn("Unrecoverable error occurred!")
                raise StopLocust("RIP...")
示例#15
0
 def login(self):
     r = self.client.post('/api/token-auth/', {
         'username': '******',
         'password': '******'
     })
     if r.status_code != 200:
         raise StopLocust()
     self.token = r.json()['token']
     self.headers = {
         'Authorization': f'JWT {self.token}',
         'Content-Type': 'application/json'
     }
示例#16
0
    def process_pages(self):
        global ids, limit

        with threadLock:
            if limit < 0:
                raise StopLocust()
            else:
                response = self.client.delete(ids[limit - 1],
                                              name="(delete) Patient delete")
                if response.status_code != 204:
                    print("Unexpected response code on patient delete: {}".
                          format(response.status_code))
                limit = limit - 1
示例#17
0
    def process_pages(self):
        response = self.client.get(
            self.next_link,
            name="(pagination) Iterate thru /Careplan response (10 items per)"
        ).json()
        next_links = ([
            link for link in response['link'] if link['relation'] == 'next'
        ])

        if len(next_links) >= 1:
            self.next_link = next_links[0]['url']
        else:
            raise StopLocust()
示例#18
0
    def addItemToOrder(self):
        unpaidOrders = self.findOrdersWithStatus(False)

        if len(unpaidOrders) > 0:
            randomOrder = unpaidOrders[random.randint(0,
                                                      len(unpaidOrders) - 1)]
            randomID = randomOrder.orderid

            response = self.client.post("/stock/item/create")
            if response.status_code != 200:
                raise StopLocust()
            itemID = json.loads(response.text)['id']

            self.client.post("/order/addItem/" + randomID + "/" + itemID)
            randomOrder.addItem(itemID)
示例#19
0
def process_requests(self):
    i = self.locust.request_number
    timestamps = self.locust.request_timestamps
    if i < timestamps.size:
        delta = (timestamps.iloc[i] - timestamps.iloc[i - 1]) / np.timedelta64(1, 's')
        print("client %s waits or %s" % (self.locust.client_id, delta))
        gevent.sleep(delta)
        self.locust.request_number += 1
    else:
        try:
            idx, timestamps = self.locust.client_queue.get(timeout=1)
            self.client_id = idx
            self.request_timestamps = timestamps
            self.request_number = 1
        except Empty:
            raise StopLocust("stop this instance")
示例#20
0
    def upload_resource(self):
        global files

        if len(files) <= 0:
            raise StopLocust()

        with threadLock:
            filename = files[-1]
            del files[-1]

        filepath = "%s/%s/%s" % (str(Path.cwd()), exampleslocation,
                                 filename)

        bundle = self.process_bundle(filepath)

        print("Uploading %s, %s bundles to go" % (filename, len(files)))
        self.client.post("/", name="(upload) Synthea bundle", headers={'Content-Type': 'application/json'}, json=bundle)
示例#21
0
 def createStartDeleteWorkspace(self):
   print("\n["+self.clusterName+"] Running workspace start test "+str(self.cycles + 1)+" of "+str(self.cyclesMax)+"\n")
   self.log("Checking if there are some removing pods before creating and running new workspace.")
   self.waitUntilDeletingIsDone()
   self.id = self.createWorkspace()
   self.wait()
   self._reset_timer()
   self.startWorkspace()
   self.wait()
   self.waitForWorkspaceToStart()
   self._reset_timer()
   self.stopWorkspaceSelf()
   self.waitForWorkspaceToStopSelf()
   self.wait()
   self.deleteWorkspaceSelf()
   if (self.cycles == (self.cyclesMax - 1)):
     raise StopLocust("Tests finished, unable to set Locust to run set number of times (https://github.com/locustio/locust/pull/656), issuing hard-stop.")
   self.cycles += 1
示例#22
0
 def register(self):
     data = json.dumps({
         "username": self.username,
         "password": self.password,
         "email": self.email,
         "deviceId": self.deviceId,
         "city": self.city,
         "makePublic": self.makePublic
     })
     headers = {"Content-Type": "application/json"}
     with self.client.post("http://localhost:4000/api/v1/register",
                           headers=headers,
                           data=data,
                           name="Register",
                           catch_response=True) as response:
         if response.status_code != 200:
             response.failure(f"Registration failed with data {data}")
             raise StopLocust()
示例#23
0
def wrapSeleniumEvent(request_type, name, func, *args, **kwargs):
    try:
        start_time = time.time()
        result = func(*args, **kwargs)
    except Exception as event_exception:
        total_time = int((time.time() - start_time) * 1000)
        events.request_failure.fire(request_type=request_type,
                                    name=name,
                                    response_time=total_time,
                                    exception=event_exception)
        raise StopLocust()
    else:
        total_time = int((time.time() - start_time) * 1000)
        events.request_success.fire(request_type=request_type,
                                    name=name,
                                    response_time=total_time,
                                    response_length=0)
        return result
示例#24
0
    def feedback(self):
        shuffle(self.current_visible)

        n_liked = randint(0, len(self.current_visible))
        n_disliked = randint(0, len(self.current_visible) - n_liked)

        liked = self.current_visible[:n_liked]
        disliked = self.current_visible[n_liked:n_liked + n_disliked]
        unknown = self.current_visible[n_liked + n_disliked:len(self.current_visible)]

        result = self.client.post("/feedback", json={'liked': liked,
                                                     'disliked': disliked,
                                                     'unknown': unknown}, headers=self.headers).json()

        if 'prediction' not in result or not result['prediction']:
            self._set_current_visible(result)
        else:
            raise StopLocust()
示例#25
0
    def pull_meta(self):

        auth = 'Bearer ' + config.token
        header = {
            'Authorization': auth,
            'Ocp-Apim-Subscription-Key': config.apim_key,
            'Content-Type': 'application/json'
        }
        print('Querying EXIF data from ' + photoId + ' ...')

        with self.client.get(url=config.photoinfo_url + '/' + photoId,
                             headers=header) as response:
            self.check_response(response)

        print(response.json())
        getphotoinfo = dict(response.json())
        self.compare_exif(self.get_exif(config.image_file), getphotoinfo, True)

        raise StopLocust()
def process_requests(self):
    # print "Enter:" + str(getframeinfo(currentframe()).filename + ":" + getframeinfo(currentframe()).function) + "-LINE:" + str(getframeinfo(currentframe()).lineno) + str(traceback.format_stack())
    i = self.locust.request_number
    timestamps = self.locust.request_timestamps
    if i < timestamps.size:
        delta = (timestamps.iloc[i] - timestamps.iloc[i - 1]) / np.timedelta64(
            1, 's')
        print "LINE:" + str(getframeinfo(currentframe()).filename) + ":" + str(
            getframeinfo(currentframe()).lineno) + "," + str(
                "client %s waits or %s" % (self.locust.client_id, delta))
        gevent.sleep(delta)
        self.locust.request_number += 1
    else:
        try:
            idx, timestamps = self.locust.client_queue.get(timeout=1)
            self.client_id = idx
            self.request_timestamps = timestamps
            self.request_number = 1
        except Empty:
            raise StopLocust("stop this instance")
示例#27
0
    def login(self):
        print("executing login")
        table_num = MyTaskSet.table_numbers[random.randint(
            0,
            len(MyTaskSet.table_numbers) - 1)]
        print("table_num " + str(table_num))
        self.client.headers['Content-Type'] = "application/json; charset=utf-8"
        response = self.client.post("service/validate_code_by_limit",
                                    json={
                                        u'event_id': 1,
                                        u'table_number': table_num
                                    })
        json_response_dict = response.json()
        print(json_response_dict)
        if json_response_dict['success'] == False:
            raise StopLocust()
        token = json_response_dict['token']
        print(token)

        return token
示例#28
0
    def __init__(self):
        super(SSOLocust, self).__init__()

        host_pattern = re.compile(r"(?P<scheme>https?)://"
                                  r"(?P<host>\w[\w.-]*)(?::(?P<port>\d+))?/?")

        if not self.host:
            user = User()
        else:
            match = host_pattern.match(self.host)

            if not match:
                raise StopLocust("Invalid host.")

            result = match.groupdict()

            user = User(schema=result["scheme"],
                        host=(result["host"],
                              int(result["port"] if result["port"] else 80)))

        self.client = LocustWrapper(user)
示例#29
0
文件: nge.py 项目: frozenpine/pyload
    def __init__(self, host=""):
        host_pattern = re.compile(r"(?P<scheme>https?)://"
                                  r"(?P<host>\w[\w.-]*)(?::(?P<port>\d+))?/?")

        self._locker = Lock()

        if host:
            match = host_pattern.match(host)
            if not match:
                raise StopLocust("Invalid host.")

            result = match.groupdict()

            self._sso_instance = self.User(
                schema=result["scheme"],
                host=(result["host"],
                      int(result["port"]) if result["port"] else 80))
        else:
            self._sso_instance = self.User()

        self._client = LocustWrapper(
            NGEClientPool(host=self._sso_instance.host(), size=10))
示例#30
0
    def reserve_tickets(self, tickets):
        # Make sure we have a clean session
        self.client.cookies.clear()

        self.client.get("/")

        resp = self.client.get("/tickets")

        html = lxml.html.fromstring(resp.content)
        form = html.get_element_by_id("choose_tickets")
        amounts = {
            i.label.text_content(): i.name
            for i in form.inputs if i.name.endswith("-amount")
        }

        data = dict(**form.fields)
        for display_name, count in tickets.items():
            data[amounts[display_name]] = count

        self.client.post("/tickets", data)

        raise StopLocust()