K6: Ship common utility libraries

Created on 22 Mar 2017  Â·  13Comments  Â·  Source: loadimpact/k6

Off the top of my head:

What all else is there?

(ping @caalle)

Most helpful comment

I'm working on imports by URL, so what if we instead did something like provide an adapter for CDNJS?

Let's say you want to import Faker; you'd just do:

import faker from "cdnjs.com/libraries/Faker";

All 13 comments

I think I mentioned this in some other issue but something like https://github.com/marak/Faker.js would be nice to have in tool like k6.

The three mentioned would help a lot.

Maybe something like https://www.npmjs.com/package/validator could be useful as well?

@uppfinnarn @robingustafsson @caalle

The list could be infinite. What about finding out a convention to import libraries in some predefined module standard? We don't need to make importing external modules perfect; drop the library build file in this folder would be more than enough for me.

Yeah, I agree that the list could go on forever. Dropping libraries into a predefined folder that attach to the window object might be sufficient.

... another suggestion could be to introduce something similar to jsfiddles' external resources letting the user define resources in a list:

export let options = {
    external_resources: [
        "https://<url-to-moment>.org/moment.min.js",
        "https://raw.githubusercontent.com/chriso/validator.js/master/validator.min.js"
    ]
}

I guess the second one is a bit more complex but would be nice to have when running a distributed test.

I'm working on imports by URL, so what if we instead did something like provide an adapter for CDNJS?

Let's say you want to import Faker; you'd just do:

import faker from "cdnjs.com/libraries/Faker";

@technololigy That sounds good to me.

Closing this in favor of #152

@liclac #152 does not replace this feature. Should we re-open or create a new issue?

IMO, being able to use "browser or not browser" libraries could bring big advantages in many situations, this is the reason I insist on this issue to avoid being forgotten.

@robingustafsson I would like to know if this one fits in any of the current https://github.com/loadimpact/k6/milestones

Could you elaborate?

Most scripts these days will register to whatever is available out of module.exports and whatever other module systems they support; we expose a node-compatible require() function as well as a module.exports (+ alias to module), and allow loading arbitrary scripts from Github or CDNJS (support for other services can trivially be added) or plain files hosted anywhere.

@liclac

Most scripts these days will register to whatever is available out of module.exports and whatever other module systems they support; we expose a node-compatible require() function as well as a module.exports (+ alias to module), and allow loading arbitrary scripts from Github or CDNJS (support for other services can trivially be added) or plain files hosted anywhere.

Sorry, I didn't know; I could import non-ES6 modules, this looks solved to me. I will test some libraries and come back if any question comes up.

This was not obvious to me; we should describe precisely it in the documentation.

@liclac

Running the latest master (k6 version 0.12.2 7a757c52a0b26521e342201405c42b9f06ece254), I don't make it work.

1) Trying different ways the URL syntax:

import {faker} from "cdnjs.com/libraries/Faker/3.1.0/faker.js";
import {faker} from "cdnjs.com/libraries/Faker/3.1.0";
import {moment} from "cdnjs.com/ajax/libs/moment.js/2.18.1/moment.js";

_Error: open /Users/ppcano/dev/go/src/github.com/loadimpact/k6/js/lib/cdnjs.com/libraries/Faker/3.1.0/faker.js.js: no such file or directory_

2) Trying different ways to import a local package:
import { moment } from "./vendor/moment";
import moment from "./vendor/moment";
import {faker} from "./vendor/faker";
import faker from "./vendor/faker";

_Error: TypeError: Cannot access member 'faker' of undefined_
_Error: TypeError: Cannot access member 'moment' of undefined_

You need to use k6 run -t js2 to use loaders until js2 becomes the default (soonâ„¢).

Thanks, working now:

import moment from "cdnjs.com/libraries/moment.js/2.18.1";
import faker from "cdnjs.com/libraries/Faker";

export default function() {
  http.get("http://test.loadimpact.com/");
  console.log(moment().format());
  console.log(faker.name.findName());
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

na-- picture na--  Â·  3Comments

gushengyuan picture gushengyuan  Â·  3Comments

euclid1990 picture euclid1990  Â·  3Comments

if-kenn picture if-kenn  Â·  4Comments

msznek picture msznek  Â·  3Comments