用k6下载整个网站 [英] Downloading whole websites with k6

查看:134
本文介绍了用k6下载整个网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在评估k6是否符合我们的负载测试需求.我们有一个相当传统的网站架构,该网站架构将Apache Web服务器与PHP和MySQL数据库一起使用.用k6发送简单的HTTP请求看起来很简单,我认为我们可以用它测试所有主要功能,因为我们对JavaScript的依赖程度不是很高,而且大多数页面都是静态的.

I'm currently evaluating whether k6 fits our load testing needs. We have a fairly traditional website architecture that uses Apache webservers with PHP und a MySQL database. Sending simple HTTP requests with k6 looks simple enough and I think we will be able to test all major functionality with it, as we don't rely on JavaScript that much and most pages are static.

但是,我不确定如何处理请求中返回的HTML中引用的资源(样式表,图像等).我们还需要加载它们,因为这有时会导致数据库请求,这必须是负载测试的一部分.

However, I'm unsure how to deal with resources (stylesheets, images, etc.) that are referenced in the HTML that is returned in the requests. We need to load them as well, as this sometimes leads to database requests, which must be part of the load test.

k6中是否有一些现成的功能,可让您像浏览器一样加载所有资源?我知道k6不会渲染页面,我也不需要这样做.我只需要请求HTML内的所有资源.

Is there some out-of-the-box functionality in k6 that allows you to load all the resources like a browser would? I'm aware that k6 does NOT render the page and I don't need it to. I only need to request all the resources inside the HTML.

推荐答案

您基本上有两个选择,都带有警告:

You basically have two options, both with their caveats:

  1. 记录会话-您可以如此处所示直接从浏览器导出har,或使用扩展 firefox chromes .两者都可以在没有k6云帐户的情况下使用,您只需将它们设置为下载har,当您按一下stop时,它将自动(以某种程度的静默方式)下载它们.然后使用in k6 har转换器(已弃用,但仍可以使用)或新的 har-to-k6 其中一个.

  1. Record your session - you can either export har directly from the browser as shown there or use an extension made for your browser here is firefox and chromes. Both should be usable without a k6 cloud account you just need to set them to download the har and it will automatically (and somewhat silently) download them when you hit stop. And then either use the in k6 har converter (which is deprecated, but still works) or the new har-to-k6 one which.

如果您具有大量页面和/或资源,并且如果您具有单个页面样式的应用程序,则此方法特别好,因为它只是获得浏览器以

This method is particularly good if you have a lot of pages and/or resources and even works if you have a single page style of application as it just gets what the browser requested as a HAR and then transforms it into a script. And if there were no dynamic things that need to be inputed (username/password) the final script can be used as is most of the time.

这种方法的最大问题是,如果添加一个css文件,则需要重做整个练习.如果您每次更改css/js文件名或类似的名称,这甚至会更成问题.哪种方法最适合以下哪一种?

The biggest problem with this approach is that if you add a css file you need to redo this whole exercise. This is even more problematic if you css/js file name change on each change or something like that. Which is what the next method is good for:

import http from "k6/http";
import {parseHTML} from "k6/html";

export default function() {
    const res = http.get("https://stackoverflow.com");
    const doc = parseHTML(res.body);
    doc.find("link").toArray().forEach(function (item) {
        console.log(item.attr("href"));
        // make http gets for it
        // or added them to an array and make one batch request
     });
}

会产生

NFO[0001] https://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d
INFO[0001] https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a
INFO[0001] https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a
INFO[0001] /opensearch.xml
INFO[0001] https://cdn.sstatic.net/Shared/stacks.css?v=53507c7c6e93
INFO[0001] https://cdn.sstatic.net/Sites/stackoverflow/primary.css?v=d3fa9a72fd53
INFO[0001] https://cdn.sstatic.net/Shared/Product/product.css?v=c9b2e1772562
INFO[0001] /feeds
INFO[0001] https://cdn.sstatic.net/Shared/Channels/channels.css?v=f9809e9ffa90

如您所见,某些url是相对的,而不是绝对的,因此您需要进行处理.在此示例中,只有一些是css,因此可能需要更多的过滤.这里的问题是您需要编写代码,并且如果添加相对链接或其他内容,则需要处理它.幸运的是,k6是可编写脚本的,因此您可以重复使用代码:D.

As you can see some of the urls are relative and not absolute so you will need to handle this. And in this example only some are css, so probably more filtering is needed. The problem here is that you need to write the code and if you add a relative link or something else you need to handle it. Luckily k6 is scriptable so you can reuse the code :D.

这篇关于用k6下载整个网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆