在 K6 测试中捆绑 npm 模块“cheerio" [英] Bundle npm module 'cheerio' in K6 test

查看:47
本文介绍了在 K6 测试中捆绑 npm 模块“cheerio"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 LoadImpact 的 K6 框架创建一些测试,但我正在努力按照其文档站点上的说明包含外部 NPM 模块.

I am trying to create some tests using K6 framework from LoadImpact, but I am struggelig with including external NPM module following the instructions on their documentation site.

在 loadImpacts 文档站点上,它们包含一个关于我所追求的内容的详细示例,这些模块使我能够从肥皂服务响应中解析 xml.但是,我无法让它工作!现在,我是一个完全的 javascript 新手,但我已经编码多年并且真的很想解决这个问题.可以在这里找到:https://docs.k6.io/docs/模块#section-npm-modules任何人都可以得到这个工作吗?我需要在与 Internet 隔离的服务器上运行它,所以我完全依赖于创建包和传输所需的文件.

On loadImpacts documentations site they include a detailed example on just what I am after, modules that enable me to parse xml from a soap service response. But, I am unable to get this working! Now, I am a total javascript newbie, but I have been coding for many years and would really like to solve this. The can be found here: https://docs.k6.io/docs/modules#section-npm-modules can anyone get this working? I need to run this on servers isolated from the Internet, so I am totaly dependent on creating the packages and transfer the required files.

根据文档,一个包是这样创建的

According to the documentation a package is created like this

-- bundle `cheerio` npm module
git clone git@github.com:cheeriojs/cheerio.git
npm install browserify index.js -s cheerio > cheerio.js

我的第一个问题:在我运行此命令时所在的文件夹中,会创建一个cheerio.js"文件以及一个cheerio"文件夹和一个node_modules"文件夹.我的根"目录中的cheerio.js 只包含以下内容:

My first question: In the folder I am residing when running this command a 'cheerio.js' file is created along with a a 'cheerio' folder and a 'node_modules' folder. the cheerio.js in my "root" directory only contains the following:

+ cheerio@0.22.0
+ index.js@0.0.3
+ browserify@16.2.3
updated 3 packages and audited 2829 packages in 2.221s
found 0 vulnerabilities

回到有关如何在 k6 javascript 中引用此包的 LoadImpacts 示例:

Back to LoadImpacts example on how to reference this package in a k6 javascript:

import cheerio from "./vendor/cheerio.js";
export default function() 
{
  const res = http.get("https://loadimpact.com/");
  const $ = cheerio.load(res.body);

这是什么文件,在browserify生成的结构中哪里可以找到?我试图将其更改为指向cheerio"文件夹中的index.js"或cheerio/lib"中的cheerio.js.然后我会收到关于cheerio.js 中第一行的投诉,该行定义了一个找不到的解析"变量:var parse = require("./parse'),如果我将其更改为var parse = require("./parse.js')它继续抱怨缺少'htmlparser2',我也可以在这个结构中找到它,但似乎整个依赖结构都不起作用.

What file is this, and where in the structure generated by browserify can I find it? I have tried to change this to point to 'index.js' in the 'cheerio' folder or cheerio.js found in 'cheerio/lib'. I will then receive a complaint about the first line in cheerio.js which defines a "parse" variable it cannot find: var parse = require("./parse'), if I change this to var parse = require("./parse.js') it goes on to complain about missing 'htmlparser2' which I can also find in this structure, but it seems like the entire dependency structure is not working.

任何人都可以给我一些指导,告诉我如何为cheerio 创建一个带有依赖项的browserify 包,以及我需要如何/需要复制到我的k6 项目中以使其像在loadImpact 站点上一样工作.

Can anybody give me some guidance on how to create a browserify package with dependencies for cheerio and how/what I need to copy to my k6 project to make this work like on the loadImpact site.

推荐答案

k6 文档对此肯定需要一些澄清,我稍后会做.当前提到的 vendor 文件夹没有什么特别的,文档只是缺少复制 cheerio.jsxml2js.js 的步骤由 browserify 生成的文件到 k6 项目中的新 vendor 文件夹.

The k6 docs for this definitely need some clarification, which I'll later do. The vendor folder currently mentioned there isn't something special, the docs are just missing a step to copy the cheerio.js and xml2js.js files that were generated by browserify to a new vendor folder in your k6 project.

现在,我将尝试提供关于如何以更简单的方式实现相同目标的简化解释:

For now, I'll try to offer a simplified explanation on how to achieve the same thing in a simpler way:

  1. 创建一个新的空文件夹并在终端中转到它
  2. 在那里运行 npm install browserifycheerio(忽略关于缺少 package.json 或描述的 npm 警告)
  3. 运行 ./node_modules/.bin/browserify ./node_modules/cheerio/-scheerio >该文件夹中的cheerio.js
  4. 在文件夹根目录中生成的 cheerio.js 文件应该是您从 k6 脚本导入的文件:
  1. Create a new empty folder and go to it in a terminal
  2. Run npm install browserify cheerio there (ignore the npm warnings about missing package.json or description)
  3. Run ./node_modules/.bin/browserify ./node_modules/cheerio/ -s cheerio > cheerio.js in that folder
  4. The resulting cheerio.js file in the folder root should be the file you import from the k6 script:

import http from "k6/http";
import cheerio from "./cheerio.js";

export default function () {
    const res = http.get("https://loadimpact.com/");

    const $ = cheerio.load(res.body);

    console.log($('head title').text())
}

对于单个 npm 库来说应该是这样.

That should be it for a single npm library.

如果您需要使用多个 npm 包,最好花一些时间将它们捆绑在一个浏览器化的 .js 文件中.例如,如果您同时需要 k6 文档中提到的 cheerioxml2js 库,您可以这样做:

And if you need to use multiple npm packages, it might be better to invest some time into bundling them in a single browserified .js file. For example, if you need both the cheerio and the xml2js libraries mentioned in the k6 docs, you can do something like this:

  1. 新建一个空文件夹
  2. 在其中添加类似于以下 package.json 文件的内容:

{
  "name": "k6-npm-libs-demo",
  "version": "0.0.1",
  "description": "just a simple demo of how to use multiple npm libs in k6",
  "main": "npm-main.js",
  "dependencies": {},
  "devDependencies": {
    "browserify": "*",
    "cheerio": "*",
    "xml2js": "*"
  },
  "scripts": {
    "install": "./node_modules/.bin/browserify npm-main.js -s npmlibs  > vendored-libs.js"
  },
  "author": "",
  "license": "ISC"
}

当然,如果您需要与cheerioxml2js 不同的库,则需要调整devDependencies 选项.

Of course, if you need different libraries than cheerio and xml2js, you need to adjust the devDependencies options.

像这样添加一个 npm-main.js 文件(再次根据你想要的库进行调整):

Add an npm-main.js file like this (again, adjusting for the libraries you want):

exports.xml2js = require('xml2js');
exports.cheerio = require('cheerio');

  • 在终端中打开该文件夹并运行 npm install.这应该会在文件夹的根目录中创建一个 vendored-libs.js 文件,您可以像这样在 k6 中使用它:

  • Open that folder in a terminal and run npm install. That should result in the creation of a vendored-libs.js file in the root of the folder, which you can use in k6 like this:

    import http from "k6/http";
    import { cheerio, xml2js } from "./vendored-libs.js";
    
    export default function () {
        const res = http.get("https://loadimpact.com/");
    
        const $ = cheerio.load(res.body);
        console.log($('head title').text())
    
        var xmlString = '<?xml version="1.0" ?>' +
            '<items xmlns="http://foo.com">' +
            ' <item>Foo</item>' +
            ' <item color="green">Bar</item>' +
            '</items>'
    
        xml2js.parseString(xmlString, function (err, result) {
            console.log(JSON.stringify(result));
        });
    }
    

  • 这篇关于在 K6 测试中捆绑 npm 模块“cheerio"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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