捆绑式npm模块';干杯';在K6测试中

捆绑式npm模块';干杯';在K6测试中,npm,k6,Npm,K6,我正试图从LoadImpact使用K6框架创建一些测试,但我对按照他们文档站点上的说明包含外部NPM模块感到困惑 在loadImpacts documentations站点上,它们包括一个关于我所追求的内容的详细示例,这些模块使我能够从soap服务响应解析xml。但是,我无法让它工作!现在,我完全是一个javascript新手,但我已经编写了很多年了,我真的很想解决这个问题。 可在以下位置找到: 有人能让这个工作吗?我需要在与Internet隔离的服务器上运行此功能,因此我完全依赖于创建包和传输

我正试图从LoadImpact使用K6框架创建一些测试,但我对按照他们文档站点上的说明包含外部NPM模块感到困惑

在loadImpacts documentations站点上,它们包括一个关于我所追求的内容的详细示例,这些模块使我能够从soap服务响应解析xml。但是,我无法让它工作!现在,我完全是一个javascript新手,但我已经编写了很多年了,我真的很想解决这个问题。 可在以下位置找到: 有人能让这个工作吗?我需要在与Internet隔离的服务器上运行此功能,因此我完全依赖于创建包和传输所需的文件

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

-- 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”文件夹。 我的“root”目录中的cheerio.js仅包含以下内容:

+ 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示例:

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中第一行的投诉,该行定义了一个它找不到的“parse”变量:
var parse=require(“./parse”),
如果我把这个改成
var parse=require(“./parse.js”)
它继续抱怨缺少“htmlparser2”,我也可以在这个结构中找到它,但似乎整个依赖结构都不工作


有谁能给我一些指导,告诉我如何为cheerio创建一个包含依赖项的browserify包,以及我需要如何/什么复制到我的k6项目中,以使其像loadImpact站点一样工作。

这方面的k6文档肯定需要一些澄清,我稍后会做。当前提到的
vendor
文件夹没有什么特别之处,文档只是缺少了将browserify生成的
cheerio.js
xml2js.js
文件复制到k6项目中新的
vendor
文件夹的步骤

现在,我将尝试提供一个简单的解释,说明如何以更简单的方式实现同样的目标:

  • 创建一个新的空文件夹并在终端中转到它
  • 在那里运行
    npm install browserify cheerio
    (忽略关于缺少
    package.json
    或说明的npm警告)
  • 在该文件夹中运行
    /node\u modules/.bin/browserify./node\u modules/cheerio/-s cheerio>cheerio.js
  • 文件夹根目录中生成的
    cheerio.js
    文件应该是从k6脚本导入的文件:
  • 对于单个npm库来说应该就是这样

    如果您需要使用多个npm软件包,最好花些时间将它们捆绑到一个单独的browserified
    .js
    文件中。例如,如果您需要k6文档中提到的
    cheerio
    xml2js
    库,您可以执行以下操作:

    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));
        });
    }
    
  • 创建一个新的空文件夹
  • 在其中添加类似以下
    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"
    }
    
    
    当然,如果您需要不同于
    cheerio
    xml2js
    的库,则需要调整
    devdependency
    选项

  • 添加一个
    npm main.js
    文件,如下所示(再次调整所需的库):

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

    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/http”导入http;
    从“/vendored libs.js”导入{cheerio,xml2js}”;
    导出默认函数(){
    const res=http.get(“https://loadimpact.com/");
    const$=cheerio.load(res.body);
    console.log($('headtitle').text()
    var xmlString=''+
    '' +
    “福”+
    “酒吧”+
    ''
    parseString(xmlString,函数(err,result){
    log(JSON.stringify(result));
    });
    }
    

  • 或者,对于节点模块,您可以执行
    npx browserify-r cheerio-s cheerio>cheerio.js
    。这将使用节点模块加载器获取模块的入口点,而无需猜测文件路径。
    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));
        });
    }