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

Scripting 如何创建一个在我的网站上包含多个页面并模拟真实用户行为的用户场景?,scripting,performance-testing,load-testing,stress-testing,Scripting,Performance Testing,Load Testing,Stress Testing,我们在Load Impact中经常遇到这个问题,所以我想我应该将它添加到Stack Overflow社区中,以便更容易找到: 我希望我的负载测试是现实的。我如何创建一个负载影响用户场景,模拟真实的用户行为,访问不同的页面,同时更频繁地访问某些页面(例如主页),就像真实用户一样?如果您的站点上有3个页面,用户可以访问,并且您知道每个页面被用户访问了多少次,您可以计算每个页面的“权重”,并创建一个用户场景,模拟真实用户在站点上展示的相同类型的访问者模式。这是一个如何做到这一点的例子 首先,我们必须了

我们在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)
现在,我们可以创建一个用户场景来复制我们网站上的真实流量——也就是说,它将以真实用户的方式运行我们的web服务器。代码如下:

-- 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
——我们为这三个页面中的每一页创建函数。调用其中一个函数
--将导致模拟客户端加载渲染所需的所有资源
--这一页。即,客户端将对该特定页面执行一次页面加载。
--
--主页/起始页
本地页面1=函数()
--首先加载HTML代码
http.request\u批处理({
"http://test.loadimpact.com/"
})
--HTML代码加载完成后,开始加载其他已加载的资源
--在HTML代码中引用,模拟真实浏览器使用的加载顺序
http.request\u批处理({
"http://test.loadimpact.com/style.css",
"http://test.loadimpact.com/images/logo.png"
})
终止
--
--/news.php页面
本地页面2=函数()
--这个示例页面只包含一个资源—页面的主HTML代码
http.request\u批处理({
"http://test.loadimpact.com/news.php"
})
终止
--
--/contacts.php页面
本地页面3=函数()
--这个示例页面只包含一个资源—页面的主HTML代码
http.request\u批处理({
"http://test.loadimpact.com/contacts.php"
})
终止
--
--
--使用我们之前发现的页面权重,获取要加载的随机页面
--
--生成范围为0-1的值
local randval=math.random()
--找出要加载的页面

如果randval我建议使用一个自动化的web测试工具

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

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


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

还可以选择记录用户行为,以创建脚本来影响负载

参考和说明在这里

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

找出测试中应该有多少用户是一个稍微不同的问题,我将推迟到实际需要时再进一步讨论