1

我希望 locust 能够登录到我的 Web 应用程序并开始单击 Web 应用程序内的链接。

使用此代码,我只是通过登录获得首页的活动,并且我没有从应用程序内部收到任何通知。

代码:

import random
from locust import HttpLocust, TaskSet, task
from pyquery import PyQuery


class WalkPages(TaskSet):
    def on_start(self):
        self.client.post("/", {
            "UserName": "my@email.com",
            "Password": "2Password!",
            "submit": "Sign In"
        })
       self.index_page()

    @task(10)
    def index_page(self):
        r = self.client.get("/Dashboard.mvc")
        pq = PyQuery(r.content)
        link_elements = pq("a")
        self.urls_on_current_page = []
        for l in link_elements:
          if "href" in l.attrib:
            self.urls_on_current_page.append(l.attrib["href"])

    @task(30)
    def load_page(self):
        url = random.choice(self.urls_on_current_page)
        r = self.client.get(url)


class AwesomeUser(HttpLocust):
    task_set = WalkPages
    host = "https://myenv.beta.webapp.com"
    min_wait = 20  * 1000
    max_wait = 60  * 1000

第一轮结束后,我在终端收到了后续消息。

[2015-02-13 12:08:43,740] webapp-qa/ERROR/stderr: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 267, in run
    self.execute_next_task()
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 293, in execute_next_task
    self.execute_task(task["callable"], *task["args"], **task["kwargs"])
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 305, in execute_task
    task(self, *args, **kwargs)
  File "/home/webapp/LoadTest/locustfile.py", line 31, in load_page
    url = random.choice(self.urls_on_current_page)
  File "/usr/lib/python2.7/random.py", line 273, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range
[2015-02-13 12:08:43,752] webapp-qa/ERROR/stderr: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 267, in run
    self.execute_next_task()
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 293, in execute_next_task
    self.execute_task(task["callable"], *task["args"], **task["kwargs"])
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 305, in execute_task
    task(self, *args, **kwargs)
  File "/home/webapp/LoadTest/locustfile.py", line 31, in load_page
    url = random.choice(self.urls_on_current_page)
  File "/usr/lib/python2.7/random.py", line 273, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range
[2015-02-13 12:08:43,775] webapp-qa/ERROR/stderr: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 267, in run
    self.execute_next_task()
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 293, in execute_next_task
    self.execute_task(task["callable"], *task["args"], **task["kwargs"])
  File "/usr/local/lib/python2.7/dist-packages/locust/core.py", line 305, in execute_task
    task(self, *args, **kwargs)
  File "/home/webapp/LoadTest/locustfile.py", line 31, in load_page
    url = random.choice(self.urls_on_current_page)
  File "/usr/lib/python2.7/random.py", line 273, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty
IndexError: list index out of range
4

2 回答 2

0

您的列表可能为空。

@task(30)
def load_page(self):
    if self.urls_on_current_page:
        url = random.choice(self.urls_on_current_page)
        r = self.client.get(url)
于 2015-05-25T02:39:18.943 回答
0

我需要时间,但有人可能需要这个。我在您的代码中的发现:登录请求似乎不正确(如果正确,请检查我的),您无法从另一个函数访问函数内部定义的变量,给定task(10)不适合数据设置函数。将 urls_on_current_page 设置为类变量以服务于其他类成员。查看我的代码和评论:

import random
from locust import HttpLocust, TaskSet, task
from pyquery import PyQuery


class WalkPages(TaskSet):
    # define variable here to access them from inside the functions
    urls_on_current_page = []

    def login(self):
        self.client.post("/login", data = {"UserName": "mesutgunes@email.com", "Password": "password"})

    def get_urls(self):
        r = self.client.get("/Dashboard.mvc")
        pq = PyQuery(r.content)
        link_elements = pq("a")
        for link in link_elements:
            if key in link.attrib and "http" not in link.attrib[key]: 
            # there maybe external link on the page
                self.urls_on_current_page.append(link.attrib[key])


    def on_start(self):
        self.login()
        self.get_urls()


    @task(30)
    def load_page(self):
        url = random.choice(self.urls_on_current_page)
        r = self.client.get(url)


class AwesomeUser(HttpLocust):
    task_set = WalkPages
    host = "https://myenv.beta.webapp.com"
    min_wait = 20  * 1000 
    max_wait = 60  * 1000
于 2015-10-13T05:24:28.803 回答