如何在我的网站上创建一个包括多个页面并模拟真实用户行为的用户场景



我们在Load Impact中经常遇到这个问题,所以我想把它添加到Stack Overflow社区中,这样更容易找到:

我希望我的负载测试是现实的。我如何创建一个模拟真实用户行为的负载影响用户场景,访问不同的页面,并像真实用户一样更频繁地访问某些页面(例如主页)?

如果你的网站上有3个页面,用户可以访问,并且你知道每个页面被用户访问了多少次,你可以计算每个页面的"权重",并创建一个用户场景,模拟真实用户在网站上展示的同一种访问者模式。这是一个如何做到这一点的例子。

首先,我们必须了解这三页中的每一页都有多受欢迎。这可以通过查看Google Analytics的统计数据来完成,例如查看上个月左右每个页面的访问次数。假设我们有以下数据:

==== Page ====    ==== Visits/day ====
/                 8453
/news.php         1843
/contacts.php     277

页面访问总数为10573(8453+1843+277)。如果我们将每个单独的数字除以总数,我们就会得到特定页面的"权重"(百分比),即网站上的随机页面加载碰巧加载该特定页面的可能性有多大:

==== Page ====    ==== Visits/day ====    =========== Weight ===========
/                 8453                    0.799 (79.9% of all page loads)
/news.php         1843                    0.174 (17.4% of all page loads)
/contacts.php     277                     0.026 (2.6% of all page loads)

现在,我们可以创建我们的用户场景,复制我们网站上的真实流量,也就是说,这将像真实用户一样锻炼我们的网络服务器

-- We create functions for each of the three pages. Calling one of these functions 
-- will result in the simulated client loading all the resources necessary for rendering
-- the page. I.e. the client will perform one page load of that particular page.
--
-- Main/start page
local page1 = function()
    -- First load HTML code
    http.request_batch({
        "http://test.loadimpact.com/"
    })
    -- When HTML code is done loading, start loading other resources that are
    -- referred to in the HTML code, emulating the load order a real browser uses
    http.request_batch({
        "http://test.loadimpact.com/style.css",
        "http://test.loadimpact.com/images/logo.png"
    })
end
--
-- /news.php page
local page2 = function()
    -- This example page consist of only one resource - the main HTML code for the page
    http.request_batch({
        "http://test.loadimpact.com/news.php"
    })
end
--
-- /contacts.php page
local page3 = function()
    -- This example page consist of only one resource - the main HTML code for the page
    http.request_batch({
      "http://test.loadimpact.com/contacts.php"
    })
end
--
--
-- Get a random page to load, using our page weights that we found out earlier
--
-- Generate a value in the range 0-1
local randval = math.random()
-- Find out which page to load
if randval <= 0.799 then
    -- 79.9% chance that we load page1
    page1()
elseif randval <= (0.799 + 0.174) then  
    -- 17.4% chance that page2 gets loaded
    page2()
else
    -- ...and the rest of the time (2.7%), page3 gets loaded
    page3()  
end

我建议使用自动web测试工具。

一个选项是JMeter。有关如何创建基本网站测试的测试计划的说明,请参阅Web测试计划手册,包括:;用户操作、用户数量、执行速度和频率以及数据收集。

基本web脚本的另一个选项是SeleniumIDE。

或者,如果您有编程经验,我会考虑使用SeleniumWebDriver。这为您提供了最大的灵活性,并且可以集成到现有的Java、C#、Python等测试项目中。这也可以很好地扩展,并可以与Sauce Labs 等CI服务集成

还有记录用户行为的选项,可以为Load Impact创建脚本。

参考和说明在这里模拟真实负载

一旦记录并可能调整了用户场景,就应该进入测试配置,包括如何将用户分布在不同的位置和场景中,以创建尽可能接近真实世界使用情况的模拟。

找出你的测试中应该有多少用户是一个稍微不同的问题,我会推迟到真正需要的时候再进一步讨论。

最新更新